Fixed nunchaku to Pytorch2.9.0
Affected Files and Functions
`python_embeded/Lib/site-packages/nunchaku/utils.py` — function `pad_tensor`
`python_embeded/Lib/site-packages/nunchaku/lora/flux/utils.py` — function `pad`
Both helpers allocate a padded tensor and copy the original tensor into the appropriate slice; they do not change any LoRA math.
Original Warning
UserWarning: Using a non-tuple sequence for multidimensional indexing is deprecated and will be changed in pytorch 2.9; use x[tuple(seq)] instead of x[seq]. In pytorch 2.9 this will be interpreted as tensor index, x[torch.tensor(seq)], which will result either in an error or a different result (Triggered internally at C:\actions-runner\_work\pytorch\pytorch\pytorch\torch\csrc\autograd\python_variable_indexing.cpp:351.)What Changes in PyTorch 2.9
Index interpretation flips: Up to 2.8, passing a Python list of slices (`[slice(...), ...]`) is treated exactly like a tuple of slices, so the copy works. In 2.9, that same list is converted into a tensor (`torch.tensor(seq)`) and handled as an integer index. Slices cannot be converted that way, so:
PyTorch will raise a `RuntimeError` when it encounters the list of `slice` objects.
Even if conversion succeeded (e.g., lists of integers), the semantics would switch from “slice copy” to “advanced indexing,” yielding different shapes and values.
Since these padding helpers run inside LoRA loading paths, any failure here would corrupt or abort the LoRA preparation workflow.
The warning is a deprecation notice: PyTorch emits the message now so users migrate to tuple-based indexing before 2.9 removes compatibility.
Pre-fix Code (Problem)
result[[slice(0, extent) for extent in tensor.shape]] = tensorA list comprehension produces a non-tuple sequence, triggering the warning and, in 2.9, either a hard error or incorrect indexing.
Updated Code
result = torch.empty(shape, dtype=tensor.dtype, device=tensor.device)
result.fill_(fill)
indices = tuple(slice(0, extent) for extent in tensor.shape)
result[indices] = tensor
return result result = torch.full(shape, fill_value, dtype=tensor.dtype, device=tensor.device)
indices = tuple(slice(0, extent) for extent in tensor.shape)
result[indices] = tensor
return resultEach slice is still `slice(0, extent)`, but the sequence is now a tuple, which PyTorch recognizes as regular multidimensional slicing. No other logic changes were made.
Effect of the Patch
Warning eliminated: All calls (including those triggered many times during LoRA processing) run cleanly without deprecation messages.
Future-proof: When PyTorch 2.9 ships, these functions continue to behave exactly as before—no runtime exceptions or unintended tensor reshaping.
LoRA functionality untouched: The helper still copies the original weights into the padded buffer; no modification to coefficients, arithmetic, or downstream results. Generated outputs, quality, and VRAM usage remain identical.
Backward compatibility preserved: Only the indexing style changed; the rest of the logic, parameters, and return values stay the same.
Optional Verification
The change is mechanical, but you can rerun any workflow that previously emitted the warning (e.g., loading Flux LoRAs) to confirm the messages no longer appear and everything operates normally.
