Fix hardcoded input dim in DiffusionModelEncoder by IamTingTing · Pull Request #8514 · Project-MONAI/MONAI · GitHub
Skip to content

Fix hardcoded input dim in DiffusionModelEncoder#8514

Merged
KumoLiu merged 4 commits into
Project-MONAI:devfrom
IamTingTing:fix-encoder-dim
Sep 16, 2025
Merged

Fix hardcoded input dim in DiffusionModelEncoder#8514
KumoLiu merged 4 commits into
Project-MONAI:devfrom
IamTingTing:fix-encoder-dim

Conversation

@IamTingTing

@IamTingTing IamTingTing commented Jul 18, 2025

Copy link
Copy Markdown
Contributor

Fixes #8496

Description

A few sentences describing the changes proposed in this pull request.

Types of changes

  • Non-breaking change (fix or new feature that would not break existing functionality).
  • Breaking change (fix or new feature that would cause existing functionality to change).
  • New tests added to cover the changes.
  • Integration tests passed locally by running ./runtests.sh -f -u --net --coverage.
  • Quick tests passed locally by running ./runtests.sh --quick --unittests --disttests.
  • In-line docstrings updated.
  • Documentation updated, tested make html command in the docs/ folder.

@coderabbitai

coderabbitai Bot commented Jul 18, 2025

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

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.

Actionable comments posted: 0

🧹 Nitpick comments (1)
monai/networks/nets/diffusion_model_unet.py (1)

2053-2056: Consider device placement and thread safety for lazy initialization.

The lazy initialization logic is correct, but consider these improvements:

  1. Device placement: The dynamically created module should be moved to the same device as the input tensor.
  2. Thread safety: In multi-threaded environments, this could create race conditions.

Apply this diff to ensure proper device placement:

-        if self.out is None:
-            self.out = nn.Sequential(
-                nn.Linear(h.shape[1], 512), nn.ReLU(), nn.Dropout(0.1), nn.Linear(512, self.out_channels)
-            )
+        if self.out is None:
+            self.out = nn.Sequential(
+                nn.Linear(h.shape[1], 512), nn.ReLU(), nn.Dropout(0.1), nn.Linear(512, self.out_channels)
+            ).to(h.device)

For thread safety, consider using a lock or moving the initialization to a separate method called during model setup.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between e499362 and 3e45145.

📒 Files selected for processing (1)
  • monai/networks/nets/diffusion_model_unet.py (2 hunks)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (18)
  • GitHub Check: min-dep-pytorch (2.6.0)
  • GitHub Check: min-dep-py3 (3.12)
  • GitHub Check: min-dep-pytorch (2.5.1)
  • GitHub Check: min-dep-py3 (3.11)
  • GitHub Check: min-dep-pytorch (2.4.1)
  • GitHub Check: min-dep-os (ubuntu-latest)
  • GitHub Check: min-dep-py3 (3.10)
  • GitHub Check: min-dep-py3 (3.9)
  • GitHub Check: min-dep-os (windows-latest)
  • GitHub Check: min-dep-os (macOS-latest)
  • GitHub Check: quick-py3 (macOS-latest)
  • GitHub Check: build-docs
  • GitHub Check: flake8-py3 (mypy)
  • GitHub Check: quick-py3 (ubuntu-latest)
  • GitHub Check: quick-py3 (windows-latest)
  • GitHub Check: packaging
  • GitHub Check: flake8-py3 (codeformat)
  • GitHub Check: flake8-py3 (pytype)
🔇 Additional comments (1)
monai/networks/nets/diffusion_model_unet.py (1)

2008-2008: LGTM: Proper lazy initialization setup.

Setting self.out = None correctly defers the output layer creation until runtime, enabling dynamic input dimension adaptation.

@KumoLiu KumoLiu requested a review from ericspod July 18, 2025 14:26
Signed-off-by: IamTingTing <6121smile@gmail.com>
@ericspod ericspod mentioned this pull request Sep 12, 2025
55 tasks
@KumoLiu

KumoLiu commented Sep 16, 2025

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

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.

Actionable comments posted: 1

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
monai/networks/nets/diffusion_model_unet.py (1)

1985-1996: Off‑by‑one: final downsample flag is wrong in Encoder.

is_final_block = i == len(channels) is never true; the last block still downsamples. This diverges from UNet’s logic and likely shrinks features one step too far.

Apply:

-            is_final_block = i == len(channels)  # - 1
+            is_final_block = i == len(channels) - 1
📜 Review details

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Knowledge base: Disabled due to Reviews -> Disable Knowledge Base setting

📥 Commits

Reviewing files that changed from the base of the PR and between 595ca84 and 99e9491.

📒 Files selected for processing (1)
  • monai/networks/nets/diffusion_model_unet.py (3 hunks)
🧰 Additional context used
📓 Path-based instructions (1)
**/*.py

⚙️ CodeRabbit configuration file

Review the Python code for quality and correctness. Ensure variable names adhere to PEP8 style guides, are sensible and informative in regards to their function, though permitting simple names for loop and comprehension variables. Ensure routine names are meaningful in regards to their function and use verbs, adjectives, and nouns in a semantically appropriate way. Docstrings should be present for all definition which describe each variable, return value, and raised exception in the appropriate section of the Google-style of docstrings. Examine code for logical error or inconsistencies, and suggest what may be changed to addressed these. Suggest any enhancements for code improving efficiency, maintainability, comprehensibility, and correctness. Ensure new or modified definitions will be covered by existing or new unit tests.

Files:

  • monai/networks/nets/diffusion_model_unet.py
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (19)
  • GitHub Check: build-docs
  • GitHub Check: flake8-py3 (mypy)
  • GitHub Check: flake8-py3 (codeformat)
  • GitHub Check: flake8-py3 (pytype)
  • GitHub Check: packaging
  • GitHub Check: quick-py3 (ubuntu-latest)
  • GitHub Check: quick-py3 (macOS-latest)
  • GitHub Check: quick-py3 (windows-latest)
  • GitHub Check: min-dep-pytorch (2.8.0)
  • GitHub Check: min-dep-pytorch (2.5.1)
  • GitHub Check: min-dep-py3 (3.10)
  • GitHub Check: min-dep-os (windows-latest)
  • GitHub Check: min-dep-pytorch (2.6.0)
  • GitHub Check: min-dep-py3 (3.12)
  • GitHub Check: min-dep-os (macOS-latest)
  • GitHub Check: min-dep-py3 (3.9)
  • GitHub Check: min-dep-pytorch (2.7.1)
  • GitHub Check: min-dep-os (ubuntu-latest)
  • GitHub Check: min-dep-py3 (3.11)
🔇 Additional comments (2)
monai/networks/nets/diffusion_model_unet.py (2)

36-36: Import Optional is appropriate for new lazy attr.

No issues.


2010-2010: ```shell
#!/bin/bash
set -euo pipefail
FILE="monai/networks/nets/diffusion_model_unet.py"
echo "PWD: $(pwd)"
if [ -f "$FILE" ]; then
echo "=== SNIPPET 1960..2040 ==="
sed -n '1960,2040p' "$FILE" || true
echo "=== LINES WITH self.out ==="
rg -n "self.out" "$FILE" || true
echo "=== LINES WITH 'self.out =' IN REPO ==="
rg -n "self.out\s*=" || true
echo "=== SEARCH LazyLinear ==="
rg -n "LazyLinear" || true
echo "=== def forward occurrences in file ==="
rg -n "def forward" "$FILE" || true
FORWARD_LINE=$(rg -n "def forward" "$FILE" | head -n1 | cut -d: -f1 || true)
if [ -n "$FORWARD_LINE" ]; then
END=$((FORWARD_LINE+400))
echo "=== FORWARD FUNCTION LINES $FORWARD_LINE..$END ==="
sed -n "${FORWARD_LINE},${END}p" "$FILE" || true
fi
else
echo "File not found at $FILE; searching repo for diffusion_model_unet.py"
rg -n "diffusion_model_unet.py" || true
fi
echo "=== SEARCH repo for torch usage / requirements ==="
rg -n --hidden --no-ignore "torch" || true
rg -n --hidden --no-ignore "torch.nn" || true
echo "=== CHECK common packaging files for torch version pins ==="
for f in requirements.txt pyproject.toml setup.cfg; do
if [ -f "$f" ]; then
echo "---- $f ----"
rg -n "torch" "$f" || true
fi
done
echo "=== DONE ==="


</blockquote></details>

</blockquote></details>

</details>

<!-- This is an auto-generated comment by CodeRabbit for review status -->

Comment thread monai/networks/nets/diffusion_model_unet.py
@KumoLiu KumoLiu merged commit 401ea4a into Project-MONAI:dev Sep 16, 2025
27 checks passed
arthurdjn pushed a commit to arthurdjn/MONAI that referenced this pull request Sep 29, 2025
Fixes Project-MONAI#8496 

### Description

A few sentences describing the changes proposed in this pull request.

### Types of changes
<!--- Put an `x` in all the boxes that apply, and remove the not
applicable items -->
- [x] Non-breaking change (fix or new feature that would not break
existing functionality).
- [ ] Breaking change (fix or new feature that would cause existing
functionality to change).
- [ ] New tests added to cover the changes.
- [ ] Integration tests passed locally by running `./runtests.sh -f -u
--net --coverage`.
- [ ] Quick tests passed locally by running `./runtests.sh --quick
--unittests --disttests`.
- [ ] In-line docstrings updated.
- [ ] Documentation updated, tested `make html` command in the `docs/`
folder.

Signed-off-by: IamTingTing <6121smile@gmail.com>
Co-authored-by: YunLiu <55491388+KumoLiu@users.noreply.github.com>
Co-authored-by: Eric Kerfoot <17726042+ericspod@users.noreply.github.com>
arthurdjn pushed a commit to arthurdjn/MONAI that referenced this pull request Sep 29, 2025
Fixes Project-MONAI#8496 

### Description

A few sentences describing the changes proposed in this pull request.

### Types of changes
<!--- Put an `x` in all the boxes that apply, and remove the not
applicable items -->
- [x] Non-breaking change (fix or new feature that would not break
existing functionality).
- [ ] Breaking change (fix or new feature that would cause existing
functionality to change).
- [ ] New tests added to cover the changes.
- [ ] Integration tests passed locally by running `./runtests.sh -f -u
--net --coverage`.
- [ ] Quick tests passed locally by running `./runtests.sh --quick
--unittests --disttests`.
- [ ] In-line docstrings updated.
- [ ] Documentation updated, tested `make html` command in the `docs/`
folder.

Signed-off-by: IamTingTing <6121smile@gmail.com>
Co-authored-by: YunLiu <55491388+KumoLiu@users.noreply.github.com>
Co-authored-by: Eric Kerfoot <17726042+ericspod@users.noreply.github.com>
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.

Hardcoded 4096 dimension in DiffusionModelEncoder prevents architectural flexibility

4 participants