Repository navigation
Commit 4e89184
Accept torch tensors as DiNTS arch_code for weights_only=True checkpoints (#9053)
Addresses the MONAI-side portion of #9025. This PR does not fully
resolve that issue
on its own — see "Follow-up required" below for the bundle-artifact
half.
### Description
`TopologyConstruction.__init__` converted the `arch_code` entries with
`torch.from_numpy(...)`, which raises `TypeError: expected np.ndarray
(got Tensor)`
when the architecture codes are already tensors. Checkpoints such as
`search_code_18590.pt` from the `pancreas_ct_dints_segmentation` and
`multi_organ_segmentation` bundles currently store numpy arrays, which
`torch.load(..., weights_only=True)` rejects; once those files are
re-saved with
tensors, the DiNTS network must accept tensor arch codes to load them.
This PR replaces the two `torch.from_numpy(...)` calls with
`torch.as_tensor(...)`
(`monai/networks/nets/dints.py`), which handles numpy arrays and tensors
identically:
- **numpy path unchanged**: `torch.as_tensor(ndarray)` is equivalent to
`torch.from_numpy` — zero-copy (same `data_ptr()`), dtype preserved,
followed by
the same `.to(self.device)`.
- **tensor path**: `torch.as_tensor(tensor)` returns the input as-is (no
copy,
no warning), unlike `torch.tensor(...)` which always copies and warns.
- `arch_code_c` still goes through `.to(torch.int64)` before
`F.one_hot`, so the
one-hot output dtype (`int64`) is identical for both input types; all
downstream
consumers (`== 1` gates, `arch_c > 0` in `MixedOp`, forward-path
activation
checks) already operated on tensors after the conversion site, so their
behavior
is unchanged.
Credit where due: another contributor (@Theerth1) independently traced
the same
root cause to `TopologyConstruction.__init__` in the issue discussion
before this
PR was prepared.
### Follow-up required (not resolved by this PR)
This change alone does not make the affected bundles loadable with
`weights_only=True`: the shipped `search_code_18590.pt` files contain
pickled
numpy arrays, which the `weights_only` unpickler rejects at `torch.load`
time
regardless of this fix. The checkpoints for
`pancreas_ct_dints_segmentation` and
`multi_organ_segmentation` (including their identical HuggingFace
mirrors) need to
be re-saved with tensors and re-uploaded in Project-MONAI/model-zoo.
This PR is
the prerequisite that lets those re-saved tensor checkpoints construct
the
network. Per the investigation in #9025, no other model-zoo bundles are
affected.
### Notes / disclosed risks
- `torch.as_tensor` also accepts Python lists/scalars, so the accepted
input is
slightly broader than the documented `numpy arrays or torch tensors`;
this is
benign.
- `arch_code_a`'s stored dtype now follows a tensor input's dtype
(previously
always the numpy dtype); all consumers are dtype-agnostic comparisons,
and no
functional difference was found.
- Verified locally (Windows, Python 3.11): full
`tests/networks/nets/test_dints_network.py` module passes via unittest
(including the TorchScript tests); the new `TestDintsArchCode` cases
pass;
`ruff check` clean on both changed files; `mypy
monai/networks/nets/dints.py`
clean. `black` was not available locally, so formatting is deferred to
CI.
### Types of changes
- [x] Non-breaking change (fix or new feature that would not break
existing functionality).
- [x] New tests added to cover the changes.
- [x] In-line docstrings updated.
---------
Signed-off-by: Minsu Kim <minsu.kim08@gmail.com>
Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
Co-authored-by: Eric Kerfoot <17726042+ericspod@users.noreply.github.com>1 parent b7f23cc commit 4e89184
2 files changed
Lines changed: 62 additions & 3 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
522 | 522 | | |
523 | 523 | | |
524 | 524 | | |
525 | | - | |
| 525 | + | |
| 526 | + | |
526 | 527 | | |
527 | 528 | | |
528 | 529 | | |
| |||
608 | 609 | | |
609 | 610 | | |
610 | 611 | | |
611 | | - | |
612 | | - | |
| 612 | + | |
| 613 | + | |
613 | 614 | | |
614 | 615 | | |
615 | 616 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
153 | 153 | | |
154 | 154 | | |
155 | 155 | | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
156 | 214 | | |
157 | 215 | | |
158 | 216 | | |
| |||
0 commit comments