Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

Guard VLM detection against config.architectures being None in the save paths (#7372)

Vineeth Sai Varikuntlad32b93c061d3
diff --git a/.github/workflows/consolidated-tests-ci.yml b/.github/workflows/consolidated-tests-ci.yml
index a016835caab..798a9dac10b 100644
--- a/.github/workflows/consolidated-tests-ci.yml
+++ b/.github/workflows/consolidated-tests-ci.yml
@@ -275,6 +275,7 @@ jobs:
tests/saving/test_imatrix_export.py \
tests/saving/test_gguf_single_pass_export.py \
tests/saving/test_offline_gguf_vlm_tokenizer_7481.py \
+ tests/saving/test_is_vlm_detection.py \
tests/utils/test_attention_masks.py \
tests/utils/test_packing.py \
tests/utils/test_trunc_normal_patch.py \
@@ -368,6 +369,7 @@ jobs:
tests/saving/test_imatrix_export.py \
tests/saving/test_gguf_single_pass_export.py \
tests/saving/test_offline_gguf_vlm_tokenizer_7481.py \
+ tests/saving/test_is_vlm_detection.py \
tests/utils/test_attention_masks.py \
tests/utils/test_packing.py \
tests/utils/test_trunc_normal_patch.py \
diff --git a/tests/saving/test_is_vlm_detection.py b/tests/saving/test_is_vlm_detection.py
new file mode 100644
index 00000000000..7ecad6a2319
--- /dev/null
+++ b/tests/saving/test_is_vlm_detection.py
@@ -0,0 +1,66 @@
+"""Regression test for `_is_vlm` in `unsloth/save.py`.
+
+The VLM check in `unsloth_save_pretrained_gguf` (and the torchao export path)
+used to guard on `hasattr(self.config, "architectures")` and then iterate
+`self.config.architectures` directly. That guard is a no-op: transformers'
+`PretrainedConfig` always sets `architectures` (defaulting to `None`), so a
+config with `architectures = None` passed the guard and hit `for x in None`,
+raising `TypeError: 'NoneType' object is not iterable` and aborting the export
+before any merge/convert work.
+
+`_is_vlm` centralizes the check and guards `architectures` with
+`getattr(config, "architectures", None) or ()`, matching the sibling
+`_is_gpt_oss` / `_is_qwen3_5_vlm` helpers. We ast-extract just that function so
+the test runs with no GPU and no `import unsloth` (which needs `unsloth_zoo`).
+"""
+
+import ast
+import os
+
+SAVE_PATH = os.path.join(os.path.dirname(__file__), os.pardir, os.pardir, "unsloth", "save.py")
+
+
+def _load_is_vlm():
+ tree = ast.parse(open(SAVE_PATH, encoding = "utf-8").read())
+ func = next(
+ node for node in tree.body if isinstance(node, ast.FunctionDef) and node.name == "_is_vlm"
+ )
+ namespace = {}
+ module = ast.Module(body = [func], type_ignores = [])
+ ast.fix_missing_locations(module)
+ exec(compile(module, SAVE_PATH, "exec"), namespace)
+ return namespace["_is_vlm"]
+
+
+class _Cfg:
+ def __init__(
+ self,
+ architectures,
+ vision_config = False,
+ ):
+ self.architectures = architectures
+ if vision_config:
+ self.vision_config = object()
+
+
+class _Model:
+ def __init__(self, config):
+ self.config = config
+
+
+def test_is_vlm_handles_none_architectures():
+ is_vlm = _load_is_vlm()
+ # architectures = None must not raise (it did before: `for x in None`).
+ assert is_vlm(_Model(_Cfg(None))) is False
+
+
+def test_is_vlm_detects_vision_architecture_and_config():
+ is_vlm = _load_is_vlm()
+ assert is_vlm(_Model(_Cfg(["Gemma3ForConditionalGeneration"]))) is True
+ assert is_vlm(_Model(_Cfg(None, vision_config = True))) is True
+
+
+def test_is_vlm_false_for_text_model_and_missing_config():
+ is_vlm = _load_is_vlm()
+ assert is_vlm(_Model(_Cfg(["LlamaForCausalLM"]))) is False
+ assert is_vlm(object()) is False
diff --git a/unsloth/save.py b/unsloth/save.py
index 17f294e93e9..dd0fceb2353 100644
--- a/unsloth/save.py
+++ b/unsloth/save.py
@@ -720,6 +720,16 @@ def _is_gpt_oss(model):
)
+def _is_vlm(model):
+ config = getattr(model, "config", None)
+ if config is None:
+ return False
+ architectures = getattr(config, "architectures", None) or ()
+ return hasattr(config, "vision_config") or any(
+ x.endswith(("ForConditionalGeneration", "ForVisionText2Text")) for x in architectures
+ )
+
+
def _qwen3_5_vlm_state_dict_for_save(state_dict):
remapped_state_dict = {}
for key, value in state_dict.items():
@@ -2948,13 +2958,7 @@ def unsloth_save_pretrained_gguf(
)
# Step 1: Check if this is a VLM (Vision-Language Model) and check if gpt-oss
- is_vlm = False
- if hasattr(self, "config") and hasattr(self.config, "architectures"):
- is_vlm = any(
- x.endswith(("ForConditionalGeneration", "ForVisionText2Text"))
- for x in self.config.architectures
- )
- is_vlm = is_vlm or hasattr(self.config, "vision_config")
+ is_vlm = _is_vlm(self)
is_processor = is_vlm and isinstance(tokenizer, ProcessorMixin)
@@ -4588,13 +4592,7 @@ def _unsloth_save_torchao_with_given_config(
quantization_config = TorchAoConfig(quant_type = torchao_config)
# Determine if this is a VLM
- is_vlm = False
- if hasattr(model, "config") and hasattr(model.config, "architectures"):
- is_vlm = any(
- x.endswith(("ForConditionalGeneration", "ForVisionText2Text"))
- for x in model.config.architectures
- )
- is_vlm = is_vlm or hasattr(model.config, "vision_config")
+ is_vlm = _is_vlm(model)
auto_model = AutoModelForImageTextToText if is_vlm else AutoModelForCausalLM
auto_processor = AutoProcessor if is_vlm else AutoTokenizer