{"record":{"id":"4252767194cf609d","repo":"Lightning-AI/pytorch-lightning","slug":"you-are-using-the-bitsandbytes-precision-plugin-b","errorCode":null,"errorMessage":"You are using the bitsandbytes precision plugin, but your model has no Linear layers. This plugin won't work for your model.","messagePattern":"You are using the bitsandbytes precision plugin, but your model has no Linear layers\\. This plugin won't work for your model\\.","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"src/lightning/fabric/plugins/precision/bitsandbytes.py","lineNumber":107,"sourceCode":"\n        globals_ = globals()\n        mode_to_cls = {\n            \"nf4\": globals_[\"_NF4Linear\"],\n            \"nf4-dq\": globals_[\"_NF4DQLinear\"],\n            \"fp4\": globals_[\"_FP4Linear\"],\n            \"fp4-dq\": globals_[\"_FP4DQLinear\"],\n            \"int8-training\": globals_[\"_Linear8bitLt\"],\n            \"int8\": globals_[\"_Int8LinearInference\"],\n        }\n        self._linear_cls = mode_to_cls[mode]\n        self.dtype = dtype\n        self.ignore_modules = ignore_modules or set()\n\n    @override\n    def convert_module(self, module: torch.nn.Module) -> torch.nn.Module:\n        # avoid naive users thinking they quantized their model\n        if not any(isinstance(m, torch.nn.Linear) for m in module.modules()):\n            raise TypeError(\n                \"You are using the bitsandbytes precision plugin, but your model has no Linear layers. This plugin\"\n                \" won't work for your model.\"\n            )\n\n        # convert modules if they haven't been converted already\n        bnb = _import_bitsandbytes()\n        if not any(isinstance(m, (bnb.nn.Linear8bitLt, bnb.nn.Linear4bit)) for m in module.modules()):\n            # this will not quantize the model but only replace the layer classes\n            _convert_layers(module, self._linear_cls, self.ignore_modules)\n\n        # set the compute dtype if necessary\n        for m in module.modules():\n            if isinstance(m, bnb.nn.Linear4bit):\n                m.compute_dtype = self.dtype\n                m.compute_type_is_set = False\n        return module\n\n    @override","sourceCodeStart":89,"sourceCodeEnd":125,"githubUrl":"https://github.com/Lightning-AI/pytorch-lightning/blob/9fed5c27d2a62ff0efd6c3573599921d6ff67c14/src/lightning/fabric/plugins/precision/bitsandbytes.py#L89-L125","documentation":"BitsandbytesPrecision only quantizes torch.nn.Linear layers. convert_module checks the model contains at least one Linear; if not, it raises TypeError to make clear that the plugin would silently do nothing (no quantization would happen).","triggerScenarios":"Calling setup/fabric.setup(module) with BitsandbytesPrecision on a model with no nn.Linear layers (e.g. pure Conv/Transformer-with-conv/MLP built from conv1d, embeddings only).","commonSituations":"Applying the bnb plugin to CNNs, embedding-only models, or models whose linear ops are implemented via einsum/matmul on parameters instead of nn.Linear modules.","solutions":["Verify the model actually uses nn.Linear layers (sum(1 for m in model.modules() if isinstance(m, nn.Linear)))","Rewrite matmul-based layers as nn.Linear so they can be replaced by bnb Linear8bitLt/Linear4bit","Don't use the bitsandbytes plugin for models without Linear layers"],"exampleFix":"# before\n# model uses self.w @ x instead of nn.Linear\nfabric = Fabric(plugins=BitsandbytesPrecision(mode=\"nf4\"))\nmodel = fabric.setup(model)  # TypeError\n\n# after\nclass Block(nn.Module):\n    def __init__(self):\n        self.proj = nn.Linear(d, d)  # uses nn.Linear\n    def forward(self, x):\n        return self.proj(x)","handlingStrategy":"type-guard","validationCode":"import torch\n\ndef model_has_linear(module: torch.nn.Module) -> bool:\n    return any(isinstance(m, torch.nn.Linear) for m in module.modules())\n\nassert model_has_linear(model), \"bitsandbytes plugin requires nn.Linear layers\"","typeGuard":"def model_has_linear(module: torch.nn.Module) -> bool:\n    return any(isinstance(m, torch.nn.Linear) for m in module.modules())","tryCatchPattern":null,"preventionTips":["Check for nn.Linear before applying the bnb plugin","Build MLPs with nn.Linear rather than raw matmul on Parameters"],"tags":["bitsandbytes","quantization","model-structure","pytorch-lightning"],"backgroundTag":"unsupported-model-architecture","analyzedSha":"9fed5c27d2a62ff0efd6c3573599921d6ff67c14","analyzedAt":"2026-08-28T11:52:41.083Z","schemaVersion":2},"datasetVersion":"2026-08-28T16:17:29.566Z"}