Skip to content

fix: migrate data-parallel QML training from smdistributed to PyTorch native DDP (#907) - #992

Open
kavix wants to merge 1 commit into
amazon-braket:mainfrom
kavix:fix/issue-907-data-parallel
Open

kavix wants to merge 1 commit into
amazon-braket:mainfrom
kavix:fix/issue-907-data-parallel

Conversation

@kavix

@kavix kavix commented Nov 30, 2026

Copy link
Copy Markdown

Issue #907

Description of changes:

The Parallelize_training_for_QML example uses smdistributed.dataparallel which is no longer present in the current amazon-braket-pytorch-jobs:latest container, causing all jobs submitted with distribution="data_parallel" to fail with ModuleNotFoundError: No module named 'smdistributed'.

This PR migrates the data-parallel training script to use PyTorch's built-in torch.distributed and DistributedDataParallel (which is available in all modern PyTorch DLC images), and fixes several related bugs found during the migration.

Changes in qml_script/train_dp.py:

  • Replace smdistributed imports with torch.distributed as dist and torch.nn.parallel.DistributedDataParallel as DDP
  • Use dist.init_process_group(backend="nccl"/"gloo", init_method="env://") (populated by torchrun at job launch)
  • Read local_rank from int(os.environ.get("LOCAL_RANK", "0")) instead of the removed dist.get_local_rank()
  • Fix batch size scaling: batch_size //= world_size (the old // world_size // 8 caused ZeroDivisionError on instances with < 8 GPUs, e.g. ml.g6.12xlarge with 4 GPUs)
  • Properly set CUDA device before model construction: torch.cuda.set_device(local_rank) then DDP(model, device_ids=[local_rank]); fall back to CPU-only DDP(model) for non-GPU devices
  • Set pin_memory=torch.cuda.is_available() (avoid pinning on CPU-only runs)
  • Call train_sampler.set_epoch(epoch) for correct per-epoch shuffling
  • Add dist.barrier() after rank-0 logging to prevent rank desync between epochs
  • Add dist.destroy_process_group() at shutdown

Changes in qml_script/model.py:

  • Remove unused self.w1, self.w2, self.rot nn.Parameter attributes from DressedQNN; these were never used in forward() (weights are owned by TorchLayer), causing PyTorch native DDP to raise gradient-reduction errors for unused parameters

Changes in Parallelize_training_for_QML.ipynb:

  • Update all markdown cells to reference PyTorch DistributedDataParallel instead of smdistributed
  • Remove the >[!WARNING] deprecation notice (the fix removes the deprecated dependency)
  • Update !sed -n ...p qml_script/train_dp.py cell line ranges to match the updated script
  • Update Summary section links to point to PyTorch DDP docs

Testing done:

Verified locally with torchrun --nproc_per_node=2 using gloo backend (CPU), local:pennylane/lightning.qubit simulator, and Braket env vars set to match the container environment:

dp_info: {'world_size': 2, 'rank': 0, 'local_rank': 0}
dp_info: {'world_size': 2, 'rank': 1, 'local_rank': 1}
Train Epoch: 1 [0/16 (0%)]   Loss: 0.293408
Train Epoch: 1 [8/16 (50%)]  Loss: 0.074792
...
Train Epoch: 3 [8/16 (50%)]  Loss: 0.280639
Training Finished!!

Job results saved: {"last loss": 0.2806} ✅

Merge Checklist

General

Tests

  • I have verified my changes pass pytest test/ (see TESTING.md) — all 8 repo tests pass
  • If modifying a notebook, I have verified no broken outputs or missing imports

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

…uted to PyTorch native DDP

- Replace deprecated smdistributed imports with torch.distributed and
  torch.nn.parallel.DistributedDataParallel in train_dp.py
- Fix init_process_group to use env:// init_method with nccl/gloo backend
- Read LOCAL_RANK from env var (set by torchrun) instead of smdistributed API
- Fix batch_size scaling: divide by world_size (not world_size // 8)
- Properly set CUDA device before wrapping model in DDP with device_ids
- Add train_sampler.set_epoch(epoch) for correct shuffling across epochs
- Add dist.barrier() after rank-0 logging to prevent desync
- Add dist.destroy_process_group() at shutdown
- Fix pin_memory to only be True when CUDA is available
- Remove unused nn.Parameter attributes (w1, w2, rot) from DressedQNN
  that caused DDP gradient-reduction errors (parameters not in forward())
- Update notebook markdown to reference PyTorch DDP instead of smdistributed
- Update notebook sed line references to match updated train_dp.py
@kavix
kavix requested a review from a team as a code owner November 30, 2026 14:11
@review-notebook-app

Copy link
Copy Markdown

Review Notebooks

Notebooks changed:

Review visual diffs and comment on any notebook cell. All comments sync back to this PR.


Powered by ReviewNB

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant