Repository: hiyouga/LLaMA-Factory
File: tests_v1/plugins/model_plugins/test_quantization_plugin.py (Python)

Code before:
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import bitsandbytes
import pytest

from llamafactory.v1.config.model_args import ModelArguments
from llamafactory.v1.core.model_engine import ModelEngine


def check_quantization_status(model):
    quantized_info = {"bnb": []}

    for name, module in model.named_modules():
        # check BitsAndBytes quantization
        if isinstance(module, bitsandbytes.nn.modules.Linear8bitLt) or isinstance(
            module, bitsandbytes.nn.modules.Linear4bit
        ):
            quantized_info["bnb"].append(name)

    return quantized_info


@pytest.mark.parametrize("name, quantization_bit", [("bnb", 4), ("auto", 8)])
def test_quantization_plugin(name, quantization_bit):
    model_args = ModelArguments(
        model="llamafactory/tiny-random-qwen3",
        quant_config={
            "name": name,
            "quantization_bit": quantization_bit,
        },
    )

    model_engine = ModelEngine(model_args=model_args)
    quantized_info = check_quantization_status(model_engine.model)
    print(f"Quantized weights for method {name} with {quantization_bit} bit: {quantized_info}")
    assert any(v for v in quantized_info.values()), "model is not quantized properly."


Review comment (suggestion):
bnb can only be installed on gpu or npu machine. add `runs_on` marker here. we also need to install bnb during the test phase

Code after:
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import pytest

from llamafactory.v1.config.model_args import ModelArguments
from llamafactory.v1.core.model_engine import ModelEngine


bitsandbytes = pytest.importorskip("bitsandbytes")


def check_quantization_status(model):
    quantized_info = {"bnb": []}

    for name, module in model.named_modules():
        # check BitsAndBytes quantization
        if isinstance(module, bitsandbytes.nn.modules.Linear8bitLt) or isinstance(
            module, bitsandbytes.nn.modules.Linear4bit
        ):
            quantized_info["bnb"].append(name)

    return quantized_info


@pytest.mark.runs_on(["cuda"])
@pytest.mark.parametrize("name, quantization_bit", [("bnb", 4), ("auto", 4)])
def test_quantization_plugin(name, quantization_bit):
    model_args = ModelArguments(
        model="llamafactory/tiny-random-qwen3",
        quant_config={
            "name": name,
            "quantization_bit": quantization_bit,
        },
    )

    model_engine = ModelEngine(model_args=model_args)
    quantized_info = check_quantization_status(model_engine.model)
    print(f"Quantized weights for method {name} with {quantization_bit} bit: {quantized_info}")
    assert any(v for v in quantized_info.values()), "model is not quantized properly."