Skip to content

Add amp_mode support to experimental DiffusionUNet3D and UNetBlock3D - #1937

Open
pjaysaval wants to merge 2 commits into
NVIDIA:mainfrom
pjaysaval:fix/diffusion-unet3d-amp-mode
Open

Add amp_mode support to experimental DiffusionUNet3D and UNetBlock3D#1937
pjaysaval wants to merge 2 commits into
NVIDIA:mainfrom
pjaysaval:fix/diffusion-unet3d-amp-mode

Conversation

@pjaysaval

Copy link
Copy Markdown

Description

The experimental 3D diffusion U-Net could not run under torch.autocast: its internal physicsnemo.nn layers (Linear, PositionalEmbedding, FourierEmbedding) validate their amp_mode flag and raise when autocast is active, but neither DiffusionUNet3D nor UNetBlock3D exposed the flag, so mixed-precision training always failed with RuntimeError: amp_mode=False but torch autocast is enabled.

This PR threads amp_mode through both classes, mirroring SongUNet and the 2D UNetBlock:

  • DiffusionUNet3D gains amp_mode: bool = False and passes it to the noise/condition embeddings, the map linears, and every UNetBlock3D (via block_kwargs).
  • UNetBlock3D gains amp_mode: bool = False, passes it to its affine Linear, and applies the standard _validate_amp / conditional-cast pattern to the affine conditioning parameters (matching physicsnemo/nn/module/unet_layers.py).

Defaults preserve existing behavior (amp_mode=False), and previously saved checkpoints load unchanged (the existing .mdlus non-regression tests pass).

New tests (parametrized over all architecture configurations): autocast bf16 forward with amp_mode=True (shape/dtype/finiteness), error on the default amp_mode=False under autocast, and full-precision execution with amp_mode=True outside autocast; analogous tests for UNetBlock3D. Validated end-to-end by training an EDM-preconditioned DiffusionUNet3D under torch.autocast bf16.

closes #1935

Checklist

Dependencies

None.

@copy-pr-bot

copy-pr-bot Bot commented Aug 19, 2026

Copy link
Copy Markdown

This pull request requires additional validation before any workflows can run on NVIDIA's runners.

Pull request vetters can view their responsibilities here.

Contributors can view more details about this message here.

@github-actions

Copy link
Copy Markdown
Contributor

CODEOWNERS review map

Current for commit a2c27490b432. An approval covers every file listed for that owner; one owner is sufficient for shared files.

@CharlelieLrt — 2 file(s)
  • test/models/diffusion/test_diffusion_unet_3d.py
  • test/models/diffusion/test_layers_diffusion_unet_3d_blocks.py

No CODEOWNER

  • CHANGELOG.md
  • physicsnemo/experimental/models/diffusion_unets/diffusion_unet_3d.py
  • physicsnemo/experimental/nn/diffusion_unet_3d_blocks.py

Comment /codeowners-info to refresh.

@greptile-apps

greptile-apps Bot commented Aug 19, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

The PR adds mixed-precision support to the experimental 3D diffusion U-Net and its residual block by propagating amp_mode through conditioning layers and validating affine dtype handling.

  • Adds amp_mode to DiffusionUNet3D and UNetBlock3D.
  • Propagates the setting through embeddings, linear layers, and all 3D U-Net blocks.
  • Adds autocast, full-precision, and disabled-AMP behavior tests.
  • Updates the changelog.

Important Files Changed

Filename Overview
physicsnemo/experimental/models/diffusion_unets/diffusion_unet_3d.py Consistently propagates the new AMP setting through the model's validating embeddings, linear layers, and block constructors.
physicsnemo/experimental/nn/diffusion_unet_3d_blocks.py Adds AMP validation and conditional affine casting consistent with the existing 2D U-Net block behavior.
test/models/diffusion/test_diffusion_unet_3d.py Adds model-level AMP coverage, but its indexed CUDA device_type causes the tests to fail on CUDA-enabled hosts.
test/models/diffusion/test_layers_diffusion_unet_3d_blocks.py Adds block-level AMP coverage while repeating the invalid indexed CUDA autocast device type.
CHANGELOG.md Accurately documents the newly supported mixed-precision mode.

Reviews (1): Last reviewed commit: "Add amp_mode support to experimental Dif..." | Re-trigger Greptile

DiffusionUNet3D, seed=0, amp_mode=True, **arch_kwargs
).to(device)
data = _generate_batch_data(arch_kwargs, x_shape, GLOBAL_SEED, device)
with torch.autocast(device_type=device, dtype=torch.bfloat16):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P1 Indexed autocast device type

When the CUDA-enabled device fixture supplies cuda:0, this test passes that indexed string directly as torch.autocast's device_type, which requires a device category such as cuda; the autocast context therefore raises before exercising the model and fails CUDA test runs. The same pattern occurs in the default-mode test and both new block-level AMP tests.

@pjaysaval pjaysaval Aug 19, 2026

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Verified on an A100 with torch 2.11 (the version in ci-requirements.lock) and torch 2.13: torch.autocast(device_type="cuda:0", ...) behaves identically to "cuda" — autocast is enabled and ops produce bf16 — so these tests pass as written on CUDA runs. Normalized the device string anyway in 90db634 for robustness on stricter torch versions and consistency with the existing test-suite idiom.

@pjaysaval
pjaysaval force-pushed the fix/diffusion-unet3d-amp-mode branch from b68cf42 to 90db634 Compare August 19, 2026 15:50
The experimental 3D diffusion U-Net could not run under torch.autocast:
its internal physicsnemo.nn layers (Linear, PositionalEmbedding,
FourierEmbedding) validate their amp_mode flag and raise when autocast is
active, but neither DiffusionUNet3D nor UNetBlock3D exposed the flag, so
mixed-precision training always failed with
'RuntimeError: amp_mode=False but torch autocast is enabled'.

Thread amp_mode through both classes, mirroring SongUNet and the 2D
UNetBlock: DiffusionUNet3D passes it to the noise/condition embeddings,
the map linears, and every UNetBlock3D; UNetBlock3D passes it to its
affine Linear and applies the standard _validate_amp / conditional-cast
pattern to the affine conditioning parameters. Defaults preserve the
existing behavior (amp_mode=False), and saved checkpoints load unchanged.

Add tests covering autocast forward (bf16), the error on default
amp_mode under autocast, and full-precision execution with amp_mode=True
outside autocast, across all architecture configurations.

Signed-off-by: Piyoosh Jaysaval <piyoosh.jaysaval@pnnl.gov>
Use the bare device type rather than the possibly-indexed device string
from the test fixture when entering torch.autocast, matching the idiom
used elsewhere in the test suite.

Signed-off-by: Piyoosh Jaysaval <piyoosh.jaysaval@pnnl.gov>
@pjaysaval
pjaysaval force-pushed the fix/diffusion-unet3d-amp-mode branch from 90db634 to 39600aa Compare August 20, 2026 16:36
@pjaysaval

Copy link
Copy Markdown
Author

Gentle ping: the automated review findings were addressed (autocast device-type normalization pushed) and CI is green. Would appreciate a maintainer look when someone has a chance. The branch is behind main but has no conflicts; happy to rebase if preferred.

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.

DiffusionUNet3D cannot run under torch.autocast (amp_mode is not exposed)

1 participant