MCPcopy Create free account
hub / github.com/OpenBitSys/BitDistiller / run_awq

Function run_awq

quantization/pre_quant.py:77–194  ·  view source on GitHub ↗
(
    model, enc,
    w_bit, q_config,
    n_samples=512, seqlen=512,
    auto_scale=True, mse_range=True,
    # some configs for ablation study
    calib_data="pileval",
)

Source from the content-addressed store, hash-verified

75
76@torch.no_grad()
77def run_awq(
78 model, enc,
79 w_bit, q_config,
80 n_samples=512, seqlen=512,
81 auto_scale=True, mse_range=True,
82 # some configs for ablation study
83 calib_data="pileval",
84):
85 from ..utils.calib_data import get_calib_dataset
86 from ..utils.module import append_str_prefix, get_op_name
87
88 if "bigcode" in str(model.__class__).lower():
89 # otherwise attention_mask will always be on cpu.
90 model.transformer.bias = model.transformer.bias.to("cuda")
91 layers = get_blocks(model)
92
93 samples = get_calib_dataset(
94 data=calib_data, tokenizer=enc, n_samples=n_samples, block_size=seqlen)
95 samples = torch.cat(samples, dim=0)
96
97 inps = []
98 layer_kwargs = {}
99
100 layers[0] = layers[0].cuda()
101 move_embed(model, "cuda")
102
103 # get input and kwargs to layer 0
104 # with_kwargs is only supported in PyTorch 2.0
105 # use this Catcher hack for now
106 class Catcher(nn.Module):
107 def __init__(self, module):
108 super().__init__()
109 self.module = module
110
111 def forward(self, inp, **kwargs):
112 inps.append(inp)
113 layer_kwargs.update(kwargs)
114 raise ValueError # early exit to break later inference
115
116 # patch layer 0 to catch input and kwargs
117 layers[0] = Catcher(layers[0])
118 try:
119 model(samples.to(next(model.parameters()).device))
120 except ValueError: # work with early exit
121 pass
122 del samples
123 layers[0] = layers[0].module # restore
124 inps = inps[0]
125
126 layers[0] = layers[0].cpu()
127 move_embed(model, "cpu")
128
129 gc.collect()
130 torch.cuda.empty_cache()
131
132 awq_results = {
133 "scale": [],
134 "clip": [],

Callers

nothing calls this directly

Calls 9

get_calib_datasetFunction · 0.85
append_str_prefixFunction · 0.85
get_op_nameFunction · 0.85
auto_clip_blockFunction · 0.85
apply_clipFunction · 0.85
get_blocksFunction · 0.70
move_embedFunction · 0.70
CatcherClass · 0.70
get_named_linearsFunction · 0.70

Tested by

no test coverage detected