Skip to content

Conversation

nehdiii
Copy link

@nehdiii nehdiii commented Aug 30, 2025

PCAM: lazy-import h5py via _get_h5py to keep dataset picklable

Fixes: #9195

Problem

  • PCAM previously stored the h5py module on the dataset instance, making it unpicklable.
  • DataLoader(num_workers>0) / spawn-based DDP pickle the dataset, causing TypeError: cannot pickle 'module' object.

Change

  • Introduce _get_h5py() helper that imports h5py on demand with a clear error message.
  • Use h5py = _get_h5py() in __len__ and __getitem__ (and any other call sites) instead of self.h5py.
  • No import at __init__; no module stored on self. Error text for missing h5py remains unchanged.

Tests

  1. test_update_PCAM.py

    • Adds PCAMTestCase (inherits ImageDatasetTestCase).
    • inject_fake_data(...) writes tiny HDF5 files for each split (train, val, test) with datasets x and y.
    • Requires h5py. Exercises core dataset behaviors on synthetic data (offline).
  2. test_update_PCAM_multiprocessing.py

    • Spawn-based DDP smoke test (torch.multiprocessing.spawn with world_size=2, backend gloo).
    • Uses DistributedSampler and DataLoader(num_workers=2, persistent_workers=True).
    • Iterates a few batches and performs an all_reduce sanity check to ensure workers progress without pickling errors.

…module' object) when using datasets in ddp mode which requier pickling
Copy link

pytorch-bot bot commented Aug 30, 2025

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/vision/9200

Note: Links to docs will display an error until the docs builds have been completed.

❗ 1 Active SEVs

There are 1 currently active SEVs. If your PR is affected, please view them below:

This comment was automatically generated by Dr. CI and updates every 15 minutes.

Copy link

meta-cla bot commented Aug 30, 2025

Hi @nehdiii!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at [email protected]. Thanks!

Copy link

meta-cla bot commented Aug 31, 2025

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

@deo-abhijit
Copy link

Hey @nehdiii , I think we can remove the test/test_update_pcam.py file as we are doing the same at

class PCAMTestCase(datasets_utils.ImageDatasetTestCase):

Also, since we don’t currently have multiprocessing tests in the suite, the dedicated multiprocessing test file could be removed as well.

Other than that, this PR looks very crisp.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

DataLoader multiprocessing fails when dataset stores a module object (TypeError: cannot pickle 'module' object)
2 participants