You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Loading any bitsandbytes-quantized model (NF4/FP4) with a custom-OPS loader such as CheckpointLoaderNF4 (from comfyanonymous/ComfyUI_bitsandbytes_NF4) crashes at KSampler with:
RuntimeError: Creating a Parameter from an instance of type Params4bit requires that
detach() returns an instance of the same type, but return type Tensor was found
instead. To use the type as a Parameter, please correct the detach() semantics
defined by its __torch_dispatch__() implementation.
The model loads successfully; the crash fires the first time the sampler triggers model_management.load_models_gpu → partially_load → partially_unload.
set_attr_param (comfy/utils.py:1000) unconditionally wraps value in torch.nn.Parameter(). When value is already a Parameter subclass with a non-trivial detach semantic — notably bitsandbytes.nn.Params4bit / ForgeParams4bit — PyTorch's Parameter.__new__ strict check rejects the construction.
The bnb side cannot reasonably fix this from their end:
Params4bit.__new__ constructs via torch.Tensor._make_subclass(cls, data, ...), never round-trips through detach(), so detach() is not part of bnb's API contract.
Forge's ForgeParams4bit also does not override detach.
The natural fix is in ComfyUI: skip the rewrap when value is already a Parameter.
Proposed Fix
defset_attr_param(obj, attr, value):
if (nottorch.is_inference_mode_enabled()) andvalue.is_inference():
value=value.clone()
# Already a Parameter (incl. bnb Params4bit / ForgeParams4bit subclasses).# Re-wrapping triggers Parameter.__new__'s detach-roundtrip check which# fails for subclasses whose detach() returns a plain Tensor.ifisinstance(value, torch.nn.Parameter):
returnset_attr(obj, attr, value)
returnset_attr(obj, attr, torch.nn.Parameter(value, requires_grad=False))
Behavior is unchanged for plain tensors (the dominant path), and the inference-mode clone above still applies before the early-return because a Parameter wrapping an inference-mode tensor would already have hit is_inference() semantics during its own construction. The branch is a no-op rewrap elision.
I have applied this patch locally and confirmed the previously crashing partially_unload(...) step completes without the RuntimeError. Happy to follow with a PR (with the same patch and a brief test) if maintainers agree on the direction.
Downstream reports observed in the wild
These all show the same Params4bit × Parameter.__new__ traceback but were filed on consumer repos rather than upstream — they are surfacing the same root cause:
Summary
Loading any bitsandbytes-quantized model (NF4/FP4) with a custom-OPS loader such as
CheckpointLoaderNF4(fromcomfyanonymous/ComfyUI_bitsandbytes_NF4) crashes at KSampler with:The model loads successfully; the crash fires the first time the sampler triggers
model_management.load_models_gpu → partially_load → partially_unload.Reproduction
comfyanonymous/ComfyUI_bitsandbytes_NF4custom node.lllyasviel/flux1-dev-bnb-nf4'sflux1-dev-bnb-nf4-v2.safetensorsintomodels/checkpoints/.CheckpointLoaderNF4→CLIPTextEncode×2 →KSamplerAdvanced→VAEDecode→SaveImage.partially_unload) reproduces.Traceback (abridged, full chain)
Root Cause
set_attr_param(comfy/utils.py:1000) unconditionally wrapsvalueintorch.nn.Parameter(). Whenvalueis already a Parameter subclass with a non-trivialdetachsemantic — notablybitsandbytes.nn.Params4bit/ForgeParams4bit— PyTorch'sParameter.__new__strict check rejects the construction.The bnb side cannot reasonably fix this from their end:
Params4bit.__new__constructs viatorch.Tensor._make_subclass(cls, data, ...), never round-trips throughdetach(), sodetach()is not part of bnb's API contract.Params4bit.__torch_function__only interceptstorch.chunk/torch.split(added in Fix Params4bit tensor subclass handling bitsandbytes-foundation/bitsandbytes#1719 for FSDP2). Extending it todetach/clone/copy_is push-back-prone given upstream's preference to minimize__torch_function__fortorch.compilereasons.ForgeParams4bitalso does not overridedetach.The natural fix is in ComfyUI: skip the rewrap when
valueis already aParameter.Proposed Fix
Behavior is unchanged for plain tensors (the dominant path), and the inference-mode clone above still applies before the early-return because a Parameter wrapping an inference-mode tensor would already have hit
is_inference()semantics during its own construction. The branch is a no-op rewrap elision.I have applied this patch locally and confirmed the previously crashing
partially_unload(...)step completes without theRuntimeError. Happy to follow with a PR (with the same patch and a brief test) if maintainers agree on the direction.Downstream reports observed in the wild
These all show the same
Params4bit×Parameter.__new__traceback but were filed on consumer repos rather than upstream — they are surfacing the same root cause:Parameter.__new__× bnb subclass issue, not a ComfyUI-only bug)Related upstream activity
_apply()OOM fix for FP8/quantized models. Same family of small fixes around quantized weight handling.set_attr_paramsemantics on fp8_scaledQuantizedTensorwith LoRA. Different bug, same function; flagged for context.