Skip to content

Invoke is a leading creative engine that uses image and video generation models to empower professionals, artists, and enthusiasts to generate and create visual media using the latest AI-driven technologies. The solution offers an industry-leading application interface, and serves as the foundation for multiple commercial products.

fix(ideogram4): import bitsandbytes only where an nf4 build needs it

Alexander Eichhorn38f069792ff6
diff --git a/invokeai/backend/model_manager/load/model_loaders/ideogram4.py b/invokeai/backend/model_manager/load/model_loaders/ideogram4.py
index 2d665935fe..50c171c3ea 100644
--- a/invokeai/backend/model_manager/load/model_loaders/ideogram4.py
+++ b/invokeai/backend/model_manager/load/model_loaders/ideogram4.py
@@ -146,7 +146,6 @@ class Ideogram4DiffusersModel(ModelLoader):
reject_scale_spellings_this_path_drops,
swap_linears_to_fp8,
)
- from invokeai.backend.quantization.bnb_nf4 import quantize_model_nf4
target_device = TorchDevice.choose_torch_device()
compute_dtype = TorchDevice.choose_bfloat16_safe_dtype(target_device)
@@ -174,6 +173,10 @@ class Ideogram4DiffusersModel(ModelLoader):
self._ram_cache.make_room(sum(t.nelement() * t.element_size() for t in sd.values()))
if is_bnb4bit_state_dict(sd):
+ # Here rather than at the top: bitsandbytes is not installed on macOS, and importing it
+ # up front failed every load through this method there, fp8 and unquantized included.
+ from invokeai.backend.quantization.bnb_nf4 import quantize_model_nf4
+
# nf4: build the model with InvokeLinearNF4 layers (compress_statistics=False, matching
# the on-disk single-quant format), then load the prequantized state dict. The model
# stays on CPU/meta until the cache moves it to the GPU.
@@ -208,7 +211,6 @@ class Ideogram4DiffusersModel(ModelLoader):
reject_scale_spellings_this_path_drops,
swap_linears_to_fp8,
)
- from invokeai.backend.quantization.bnb_nf4 import quantize_model_nf4
encoder_path = model_path / "text_encoder"
target_device = TorchDevice.choose_torch_device()
@@ -262,6 +264,9 @@ class Ideogram4DiffusersModel(ModelLoader):
with accelerate.init_empty_weights():
model = AutoModel.from_config(cfg)
if is_bnb_nf4:
+ # Only this branch needs bitsandbytes, which macOS does not have; see the transformer.
+ from invokeai.backend.quantization.bnb_nf4 import quantize_model_nf4
+
model = quantize_model_nf4(model, modules_to_not_convert=set(), compute_dtype=compute_dtype)
_, unexpected = model.load_state_dict(sd, strict=False, assign=True)
diff --git a/tests/backend/model_manager/load/test_ideogram4_diffusers_loader.py b/tests/backend/model_manager/load/test_ideogram4_diffusers_loader.py
index a7116d2ad2..0760352852 100644
--- a/tests/backend/model_manager/load/test_ideogram4_diffusers_loader.py
+++ b/tests/backend/model_manager/load/test_ideogram4_diffusers_loader.py
@@ -273,7 +273,11 @@ def test_an_nf4_text_encoder_gets_past_the_guards(monkeypatch: pytest.MonkeyPatc
not the point. What must not happen is that it fails *here*: widening `is_scale_metadata_key` to
any bnb spelling would refuse a build that ships today, and nothing else in the tree would
notice.
+
+ Skipped where bitsandbytes is not installed (macOS): the branch imports it, and the broad
+ `except` below would otherwise swallow that `ModuleNotFoundError` and pass having checked nothing.
"""
+ pytest.importorskip("bitsandbytes")
state_dict = _tiny_encoder_state_dict()
weight = state_dict.pop(f"{ENCODER_LAYER}.weight")
state_dict[f"{ENCODER_LAYER}.weight"] = torch.zeros(weight.numel() // 2, 1, dtype=torch.uint8)