MCPcopy Create free account
hub / github.com/Pints-AI/1.5-Pints / quantization

Function quantization

lit_gpt/utils_old.py:32–88  ·  view source on GitHub ↗
(mode: Optional[str] = None)

Source from the content-addressed store, hash-verified

30
31@contextmanager
32def quantization(mode: Optional[str] = None):
33 if mode is None:
34 yield
35 return
36
37 if mode == "bnb.int8":
38 from quantize.bnb import InferenceLinear8bitLt
39
40 quantized_linear_cls = InferenceLinear8bitLt
41 elif mode == "bnb.fp4":
42 from quantize.bnb import Linear4bit
43
44 # Use a class instead `functools.partial` to respect `isinstance` checks and attribute accesses
45 class QuantizedLinear(Linear4bit):
46 def __init__(self, *args, **kwargs):
47 super().__init__(*args, quant_type="fp4", compress_statistics=False, **kwargs)
48
49 quantized_linear_cls = QuantizedLinear
50 elif mode == "bnb.fp4-dq":
51 from quantize.bnb import Linear4bit
52
53 class QuantizedLinear(Linear4bit):
54 def __init__(self, *args, **kwargs):
55 super().__init__(*args, quant_type="fp4", compress_statistics=True, **kwargs)
56
57 quantized_linear_cls = QuantizedLinear
58 elif mode == "bnb.nf4":
59 from quantize.bnb import Linear4bit
60
61 class QuantizedLinear(Linear4bit):
62 def __init__(self, *args, **kwargs):
63 super().__init__(*args, quant_type="nf4", compress_statistics=False, **kwargs)
64
65 quantized_linear_cls = QuantizedLinear
66 elif mode == "bnb.nf4-dq":
67 from quantize.bnb import Linear4bit
68
69 class QuantizedLinear(Linear4bit):
70 def __init__(self, *args, **kwargs):
71 super().__init__(*args, quant_type="nf4", compress_statistics=True, **kwargs)
72
73 quantized_linear_cls = QuantizedLinear
74 elif mode == "gptq.int4":
75 from quantize.gptq import ColBlockQuantizedLinear
76
77 class QuantizedLinear(ColBlockQuantizedLinear):
78 def __init__(self, *args, **kwargs):
79 super().__init__(*args, bits=4, tile_cols=-1, **kwargs)
80
81 quantized_linear_cls = QuantizedLinear
82 else:
83 raise ValueError(f"Unknown quantization mode: {mode}")
84
85 torch_linear_cls = torch.nn.Linear
86 torch.nn.Linear = quantized_linear_cls
87 yield
88 torch.nn.Linear = torch_linear_cls
89

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected