File size: 1,199 Bytes
fce7324
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
"""Owner-opt-in INT8 loading. Conversion is at startup, never inside a GPU job.

Based on the official MiniMax-H3 Diffusers memory recipe. Kept separate because
GPU/PEFT/ZeroGPU compatibility must be measured on the owner's actual runtime.
"""


def load_int8_transformer(repo):
    import torch
    try:
        from diffusers import MiniMaxH3Transformer3DModel, TorchAoConfig
        from torchao.quantization import Int8WeightOnlyConfig
    except ImportError as error:
        raise RuntimeError(
            "INT8 needs the optional requirements-int8.txt dependencies. "
            "Use H3_QUANTIZATION=bf16 for the existing supported path."
        ) from error
    model = MiniMaxH3Transformer3DModel.from_pretrained(
        repo, subfolder="transformer_ref", dtype=torch.bfloat16,
        quantization_config=TorchAoConfig(
            Int8WeightOnlyConfig(version=2),
            modules_to_not_convert=[
                "proj_in", "audio_proj_in", "context_embedder", "time_embedder", "time_proj",
                "token_refiner", "norm_out", "proj_out", "audio_proj_out",
            ],
        ),
        low_cpu_mem_usage=False,
    )
    model.requires_grad_(False)
    return model