Skip to content

Opt in to litdata pickle reads and lift the litdata<0.2.77 cap - #1797

Open
mysteryyoi wants to merge 2 commits into
sunlabuiuc:masterfrom
mysteryyoi:fix/litdata-allow-pickle
Open

mysteryyoi wants to merge 2 commits into
sunlabuiuc:masterfrom
mysteryyoi:fix/litdata-allow-pickle

Conversation

@mysteryyoi

@mysteryyoi mysteryyoi commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

Contributor: Rohit C Subhash (GitHub: @mysteryyoi), external contributor, no NetID

Why

litdata 0.2.77, released 2026-12-02, only unpickles values when the reader passes allow_pickle=True (Lightning-AI/litData#979). Without it, reading a record where litdata pickled a field raises ValueError: Pickle deserialization is disabled by default.... On master that broke these three tests:

  • tests/core/test_lime.py::TestLimeExplainerStageNet::test_lime_basic_attribution_stagenet
  • tests/core/test_lime.py::TestLimeExplainerStageNet::test_lime_stagenet_target_class
  • tests/core/test_lime.py::TestLimeExplainerStageNet::test_lime_stagenet_values_finite

#1796 caps litdata below 0.2.77 so CI passes again. This PR fixes the readers so PyHealth works on 0.2.77 and later, and removes the cap. While the cap is there, PyHealth installs can't get any newer litdata release, and CI never tests against one.

Only records in the older layout reach litdata's pickle serializer. These tests write SampleBuilder.transform output with litdata.optimize, so litdata pickles the None time of the stagenet_tensor field. The usual create_sample_dataset / set_task write path (STORAGE_FORMAT 2) stores only tensors and one bytes payload, so it never uses that serializer.

What changes

  • pyhealth/datasets/sample_dataset.py: a new _LITDATA_READER_KWARGS is {"allow_pickle": True} when the installed litdata.StreamingDataset accepts that argument (0.2.77 and later), and {} otherwise. SampleDataset passes it to litdata. If the caller passes allow_pickle themselves, their value wins.
  • pyhealth/datasets/base_dataset.py: the three litdata.StreamingDataset readers of the set_task task cache pass it too. That cache only holds bytes today, so they don't need it yet, but this way every reader PyHealth opens on its own caches gets the same setting.
  • pyproject.toml: litdata>=0.2.71,<0.2.77 goes back to litdata>=0.2.71, and the comment now says why 0.2.77 works. On 0.2.71 to 0.2.76 the kwargs are empty, so those calls behave exactly as before.

If you'd rather keep the cap for now, I can drop the pyproject.toml change. The rest works on both versions either way.

Why opting in is safe

These readers only open caches that PyHealth wrote, and reading them already unpickles data. SampleDataset loads schema.pkl with pickle.load and each record's _pyhealth_objects payload with pickle.loads. The task-cache readers call pickle.loads on every sample. Letting litdata unpickle too doesn't trust anything new.

Tests

The new TestSampleDatasetReadsLitdataPickledValues in tests/core/test_sample_dataset.py writes two StageNet-style records in-process with litdata's BinaryWriter and checks that litdata pickled a field. It then reads them back by index, by iteration and through subset(). It runs in about 0.05 s. On litdata 0.2.77 all four reads fail without this change and pass with it.

Local runs (Windows 11, Python 3.12), rebased onto dd02d72:

litdata 0.2.76 litdata 0.2.77
3 StageNet LIME tests pass pass (failed before this change)
new test pass pass (failed before this change)
test_sample_dataset, test_raw_storage, test_litdata_merge pass* pass*
full tests/core not run in full (the kwargs are empty there, so the calls are unchanged) no pickle errors; every other failure is Windows-only or a missing optional package**

* Except three failures that also happen on unmodified master: test_close_preserves_caller_owned_directory, test_merge_keeps_worker_order_beyond_ten_workers and the teardown of test_split_parts_round_trip. They raise WinError 32 because litdata still has the chunk file memory-mapped when the test deletes it, which only happens on Windows. CI runs on Linux.

** My Windows machine can't run the whole suite cleanly: there are file-lock errors, a pytest temp-dir permission error, and monai, wfdb and nibabel aren't installed. Those tests fail the same way on unmodified master with litdata 0.2.76. The only exceptions are 7 setup/teardown PermissionErrors, which pass on 0.2.77 with this change when run on their own. CI on Linux is the real check, and without the cap it installs 0.2.77.

tools/check_pr_rules.py's checks (ruff 0.17 on added lines, >>> docstring examples) pass on this diff.

This touches core dataset code and a dependency bound, so it will probably need a manual review.

litdata 0.2.77 (Lightning-AI/litData#979) unpickles values only when the
reader passes allow_pickle=True, so reading records with a pickled field
(the StageNet LIME tests) fails. Pass it wherever PyHealth opens its own
caches, when the installed litdata has the option; older litdata is
unchanged.
PyHealth's readers now pass allow_pickle=True on litdata 0.2.77 and
later, so the cap added in sunlabuiuc#1796 is no longer needed and CI can test
against the newest litdata again.
@mysteryyoi
mysteryyoi force-pushed the fix/litdata-allow-pickle branch from fed2ba6 to 8e5256b Compare December 2, 2026 18:25
@mysteryyoi mysteryyoi changed the title Opt in to litdata pickle reads for PyHealth's own caches (fixes CI with litdata 0.2.77) Opt in to litdata pickle reads and lift the litdata<0.2.77 cap Dec 2, 2026
@github-actions

github-actions Bot commented Dec 2, 2026

Copy link
Copy Markdown

Test Coverage

Total: 89.33% (65331 / 73132 lines)

Modified file Coverage Lines
pyhealth/datasets/base_dataset.py 93.19% 534 / 573
pyhealth/datasets/sample_dataset.py 95.08% 348 / 366

Workflow run · 8e5256b

@solarsys solarsys added the status: manual Pending maintainer's review label Dec 3, 2026
@solarsys

solarsys commented Dec 3, 2026

Copy link
Copy Markdown
Collaborator

Thanks for tracking down the litdata 0.2.77 change. Why this PR has status: manual:

  • It edits pyproject.toml (the litdata pin). Changes outside docs/ examples/ pyhealth/ test-resources/ tests/ go to manual review, and dependency changes are a maintainer decision.
  • It changes the shared BaseDataset and SampleDataset readers to pass allow_pickle=True. litdata turned pickle reads off by default for safety, so turning them back on for every PyHealth cache read is a security decision. A maintainer should decide whether to accept it, or to avoid pickled fields in the cache (for example, by storing those fields in a non-pickle format).

This is listed for the maintainers. Nothing more is needed from you right now.

solarsys commented Dec 4, 2026

Copy link
Copy Markdown
Collaborator

/review


Generated by Claude Code

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed commit 8e5256b

cache whose ``schema.pkl`` is unpickled anyway.
"""
super().__init__(path, **kwargs)
super().__init__(path, **{**_LITDATA_READER_KWARGS, **kwargs})

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[low] Caller override of allow_pickle is untested

The description promises that a caller-passed allow_pickle wins over the default, implemented here by the dict merge {**_LITDATA_READER_KWARGS, **kwargs}. No test covers this branch. Add a small test in tests/core/test_sample_dataset.py that passes allow_pickle explicitly and checks the value reaches litdata, for example by asserting on the reader's attribute or by expecting the pickled-read failure when allow_pickle=False on 0.2.77+. This is non-blocking because the default path is covered by the new test.

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AI Review

suggestion: approve
decision: human review

Thank you for this contribution, Rohit. It makes PyHealth's litdata readers work with litdata 0.2.77 and later by passing allow_pickle=True when litdata supports it. It also removes the <0.2.77 cap. The diff looks correct and complete. The description explains the reason and the trust model well: schema.pkl and the object payloads are already unpickled. The new test covers index, iteration and subset reads. Coverage meets the bar: 93.19% for base_dataset.py, 95.08% for sample_dataset.py, and 89.33% for the whole repository.

This looks ready to merge from a code-quality view. It can't be approved automatically, though, because it touches shared core code. It changes SampleDataset.__init__, which SampleBaseDataset and the kg_emb datasets build on, so their default reader arguments change on litdata 0.2.77 and later. It also changes the three litdata.StreamingDataset readers in the BaseDataset task-cache path. And it removes the litdata upper bound in pyproject.toml, which affects every install. A maintainer will make the final call.

Optional suggestions:

  • Add a small test for the caller-override branch ({**_LITDATA_READER_KWARGS, **kwargs}), for example passing allow_pickle=False explicitly.
  • Confirm that CI resolves litdata to 0.2.77 or later after the cap is removed. pixi.lock appears to pin an older litdata (0.2.59).
  • The new test only exercises the change on litdata 0.2.77 and later. On older versions it passes trivially. That's fine, but a short comment saying so would help.

Reviewed commit 8e5256b

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

status: manual Pending maintainer's review

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants