Skip to content

fix(transforms): preserve device and dtype in squarepulse#9005

Open
aymuos15 wants to merge 2 commits into
Project-MONAI:devfrom
aymuos15:fix/squarepulse-cpu-return
Open

fix(transforms): preserve device and dtype in squarepulse#9005
aymuos15 wants to merge 2 commits into
Project-MONAI:devfrom
aymuos15:fix/squarepulse-cpu-return

Conversation

@aymuos15

Copy link
Copy Markdown
Contributor

Description

squarepulse allocated its output via torch.zeros(t.shape), which defaults to a CPU float32 tensor regardless of the input tensor's device or dtype. This caused CUDA callers to silently receive a CPU result, breaking downstream transforms when GPU tensors were passed.

This fix allocates the output on the input tensor's device, and preserves the input's floating-point dtype. Integer inputs still promote to the default float dtype (matching torch.zeros behavior).

Diff

-    y = torch.zeros(t.shape)
+    y = torch.zeros(t.shape, device=t.device, dtype=t.dtype if t.is_floating_point() else None)

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.

Allocate the squarepulse output on the input tensor's device (and floating
dtype) so CUDA callers no longer silently get a CPU result. Integer inputs
still promote to the default float dtype.

Signed-off-by: Soumya Snigdha Kundu <soumya_snigdha.kundu@kcl.ac.uk>
@coderabbitai

coderabbitai Bot commented Jul 19, 2026

Copy link
Copy Markdown
Contributor

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 565c0329-e8ad-4308-8ff6-a65a3f365357

📥 Commits

Reviewing files that changed from the base of the PR and between 5da2472 and 2529047.

📒 Files selected for processing (2)
  • monai/transforms/utils.py
  • tests/transforms/utils/test_squarepulse.py

📝 Walkthrough

Walkthrough

Squarepulse now initializes its output tensor on the input tensor’s device and preserves floating-point input dtype while retaining default dtype promotion for integer inputs. New tests cover CPU and CUDA inputs, output shape, dtype, device placement, binary output values, and reference-value matching.

Estimated code review effort: 2 (Simple) | ~10 minutes

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 33.33% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly states the squarepulse device/dtype fix.
Description check ✅ Passed The description covers the fix, diff, and tests; only the issue reference is missing.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@aymuos15
aymuos15 changed the base branch from main to dev July 19, 2026 15:00
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