Skip to content

Accept tensors and lists for DiNTS arch_code (fixes #9025) - #9143

Open
12yuuuu wants to merge 1 commit into
Project-MONAI:devfrom
12yuuuu:fix-9025-dints-arch-code-as-tensor
Open

12yuuuu wants to merge 1 commit into
Project-MONAI:devfrom
12yuuuu:fix-9025-dints-arch-code-as-tensor

Conversation

@12yuuuu

@12yuuuu 12yuuuu commented Sep 30, 2026

Copy link
Copy Markdown

Fixes #9025.

Description

TopologyConstruction.__init__ (base of TopologyInstance / TopologySearch) called
torch.from_numpy() on both entries of arch_code, so architecture codes that arrive as
torch.Tensor raised TypeError: expected np.ndarray (got Tensor).

This matters for #9025: the search_code_18590.pt files shipped with the
pancreas_ct_dints_segmentation / multi_organ_segmentation bundles store numpy.ndarray
objects (node_a, arch_code_a, arch_code_c, arch_code_a_max — the raw return values of
TopologySearch.decode()), which torch.load(weights_only=True) — the default since PyTorch 2.6 —
refuses (Unsupported global: GLOBAL numpy.core.multiarray._reconstruct). Re-saving those files as
tensors is the right fix on the bundle side, but MONAI must accept tensors first.

Changes:

  • torch.from_numpy(x) -> torch.as_tensor(x, device=self.device) for arch_code_a /
    arch_code_c. Numpy inputs keep the same zero-copy semantics; tensors and nested lists
    (e.g. from JSON/YAML configs) now work too. Fully backward compatible.
  • Docstrings updated (also fixes the DiNTS.node_a shape, which was written transposed).
  • Tests: test_dints_arch_code_types (numpy / tensor / list give identical arch_code_a,
    arch_code_c, cell_tree) and test_dints_search_code_weights_only_roundtrip
    (save tensors -> torch.load(weights_only=True) -> build TopologyInstance + DiNTS).

TopologySearch.decode() still returns numpy arrays; changing its return type is a public
API change and is left for a separate discussion. A companion model-zoo PR (save tensors in
search.py, load with numpy.asarray / torch.as_tensor and map_location='cpu' in the bundle
configs) follows and will be linked here.

Types of changes

  • Non-breaking change (fix or new feature that would not break existing functionality).
  • New tests added to cover the changes.
  • Quick tests passed locally: python -m unittest tests.networks.nets.test_dints_network (9 tests, CPU only — I don't have a CUDA machine).
  • In-line docstrings updated.

TopologyConstruction.__init__ called torch.from_numpy() on arch_code, so
architecture codes loaded as tensors (e.g. search checkpoints re-saved so
that they load with torch.load(weights_only=True)) raised
TypeError: expected np.ndarray (got Tensor).

Use torch.as_tensor(), which keeps the zero-copy numpy path and additionally
accepts tensors and nested lists. Add tests for the accepted input types and
for a tensor search checkpoint round-trip through weights_only=True.
Also fix the DiNTS.node_a docstring shape, which was written transposed.

Signed-off-by: 12yuuuu <yu1inge2@gmail.com>
@coderabbitai

coderabbitai Bot commented Sep 30, 2026 •

Copy link
Copy Markdown
Contributor

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository: Project-MONAI/MONAI/.coderabbit.yaml

Review profile: CHILL

Plan: Advanced

Run ID: 1a32d86f-cf31-4380-b029-c9a339bb2841

📥 Commits

Reviewing files that changed from the base of the PR and between 8605065 and dc35a68.

📒 Files selected for processing (2)
  • monai/networks/nets/dints.py
  • tests/networks/nets/test_dints_network.py

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.


📝 Walkthrough

Walkthrough

DiNTS now accepts NumPy arrays, tensors, and nested lists for architecture codes. Tests verify equivalent topology construction across these input forms and check construction from tensor values loaded with weights_only=True.

Priority: ➖ Normal

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

Severity of issue fixed: Medium

Merge Risk: ⚪ Minimal · up to dc35a

The change expands supported architecture-code inputs and tests tensor checkpoint loading. No actionable merge-blocking risk is identified; legacy NumPy checkpoint conversion remains a separate follow-up.

🚥 Pre-merge checks | ✅ 3 | ❌ 2

❌ Failed checks (2 warnings)

Check name Status Explanation Resolution
Linked Issues check ⚠️ Warning Issue [#9025] requires the affected stored DINTS weights to load with torch.load(weights_only=True). This PR updates DINTS to accept NumPy arrays, tensors, and nested lists, and adds a synthetic ten… Convert the affected stored DINTS weights to tensors and update the related bundle configurations. Add validation that loads the affected artifact with torch.load(weights_only=True) and constructs the model successfully.
Docstring Coverage ⚠️ Warning Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 4 functions across 2 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely describes the main change: DiNTS now accepts tensors and lists for arch_code.
Description check ✅ Passed The description identifies the issue, explains the implementation, lists the tests, and records the relevant change types. It also notes that CUDA, integration, and documentation-build checks were not…
Out of Scope Changes check ✅ Passed The changed DINTS documentation, input conversion, and tests directly support issue [#9025]. No unrelated change is demonstrated.
Full details: Linked Issues check

Explanation

Issue [#9025] requires the affected stored DINTS weights to load with torch.load(weights_only=True). This PR updates DINTS to accept NumPy arrays, tensors, and nested lists, and adds a synthetic tensor checkpoint round-trip test. It does not convert the affected model-zoo or Hugging Face weight files, such as search_code_18590.pt, to tensors. The actual stored-weight requirement remains unmet.

  • Fix all pre-merge checks with AI
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create a new PR

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.

@12yuuuu

12yuuuu commented Sep 30, 2026

Copy link
Copy Markdown
Author

Companion model-zoo PR: Project-MONAI/model-zoo#791 (bundle configs load the search code with numpy.asarray / torch.as_tensor and map_location='cpu'; search.py saves tensors).

This branch has not been deployed

No deployments
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.

Some Stored Weights for Bundles Not Compatible With weights_only=True

1 participant