unsloth/studio/backend/tests/test_diffusion_gguf_compile.py
Daniel Han 36df317293 Trim the comments across the diffusion backend
Comment-only pass over the Python this PR touches: drop what the code already
says, collapse multi-line explanations that still read on one line, and keep
the reasoning that is not recoverable from the code. No code, docstring
semantics or behaviour changes; verified with an AST comparison against the
previous revision, and the backend suite is unchanged (same 37 environment
failures as before: the API integration tests that need a live keyed server,
the flash-attn install hooks, and the GPU memory fields).
2026-07-26 20:31:19 +00:00

73 lines
2.7 KiB
Python

# SPDX-License-Identifier: AGPL-3.0-only
# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
"""Unit tests for the compiled GGUF dequant accelerator (``diffusion_gguf_compile.py``).
Covers install/uninstall idempotency + exact reversibility, the kill-switch, and the
on-by-default behaviour. Runs on CPU -- patching the module attribute is lazy
(torch.compile only traces on the first real call).
"""
from __future__ import annotations
import pytest
torch = pytest.importorskip("torch")
gguf_utils = pytest.importorskip("diffusers.quantizers.gguf.utils")
from core.inference import diffusion_gguf_compile as gc # noqa: E402
@pytest.fixture(autouse = True)
def _clean():
# Always start and end from a clean, unpatched state so tests do not leak the process-wide patch
# into each other.
gc.uninstall_all()
yield
gc.uninstall_all()
def test_compiled_dequant_install_uninstall_reversible():
orig = gguf_utils.dequantize_gguf_tensor
assert gc.is_compiled_dequant_installed() is False
assert gc.install_compiled_dequant() is True
assert gc.is_compiled_dequant_installed() is True
# The module attribute is now a different (compiled) callable...
assert gguf_utils.dequantize_gguf_tensor is not orig
# ...idempotent: a second install is a no-op, attribute unchanged.
patched = gguf_utils.dequantize_gguf_tensor
assert gc.install_compiled_dequant() is True
assert gguf_utils.dequantize_gguf_tensor is patched
gc.uninstall_compiled_dequant()
assert gc.is_compiled_dequant_installed() is False
# Exact original restored.
assert gguf_utils.dequantize_gguf_tensor is orig
# Uninstall is idempotent.
gc.uninstall_compiled_dequant()
assert gguf_utils.dequantize_gguf_tensor is orig
def test_compiled_dequant_kill_switch(monkeypatch):
monkeypatch.setenv("UNSLOTH_DIFFUSION_GGUF_COMPILE_DEQUANT", "0")
orig = gguf_utils.dequantize_gguf_tensor
assert gc.install_compiled_dequant() is False
assert gc.is_compiled_dequant_installed() is False
assert gguf_utils.dequantize_gguf_tensor is orig
def test_compiled_dequant_on_by_default(monkeypatch):
# The compiled dequant is the real win, so it is ON without any env opt-in.
monkeypatch.delenv("UNSLOTH_DIFFUSION_GGUF_COMPILE_DEQUANT", raising = False)
assert gc.install_compiled_dequant() is True
assert gc.is_compiled_dequant_installed() is True
def test_uninstall_all(monkeypatch):
orig = gguf_utils.dequantize_gguf_tensor
gc.install_compiled_dequant()
assert gc.is_installed() is True
gc.uninstall_all()
assert gc.is_installed() is False
assert gguf_utils.dequantize_gguf_tensor is orig