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

Function run_clip

quantization/autoclip.py:110–206  ·  view source on GitHub ↗
(
    model, enc,
    w_bit, q_config,
    n_samples=128, seqlen=1024,
    datasets="pile"
)

Source from the content-addressed store, hash-verified

108
109@torch.no_grad()
110def run_clip(
111 model, enc,
112 w_bit, q_config,
113 n_samples=128, seqlen=1024,
114 datasets="pile"
115):
116 print(f"Using {datasets} dataset to do calibation")
117 samples = get_calib_dataset(
118 datasets=datasets, tokenizer=enc, n_samples=n_samples, block_size=seqlen)
119 samples = torch.cat(samples, dim=0)
120
121 inps = []
122 layer_kwargs = {}
123
124 layers = get_blocks(model)
125
126 layers[0] = layers[0].cuda()
127 move_embed(model, "cuda")
128
129 # get input and kwargs to layer 0
130 # with_kwargs is only supported in PyTorch 2.0
131 # use this Catcher hack for now
132 class Catcher(nn.Module):
133 def __init__(self, module):
134 super().__init__()
135 self.module = module
136
137 def forward(self, inp, **kwargs):
138 inps.append(inp)
139 layer_kwargs.update(kwargs)
140 raise ValueError # early exit to break later inference
141
142 # patch layer 0 to catch input and kwargs
143 layers[0] = Catcher(layers[0])
144 try:
145 model(samples.to(next(model.parameters()).device))
146 except ValueError: # work with early exit
147 pass
148 del samples
149 layers[0] = layers[0].module # restore
150 inps = inps[0]
151
152 layers[0] = layers[0].cpu()
153 move_embed(model, "cpu")
154
155 gc.collect()
156 torch.cuda.empty_cache()
157
158 clip_results = {
159 "clip": [],
160 }
161
162 # solve layer by layer
163 for i in tqdm(range(len(layers)), desc="Running Asymmetric Clipping..."):
164 layer = layers[i]
165 layer = layer.cuda()
166 named_linears = get_named_linears(layer)
167

Callers 1

mainFunction · 0.85

Calls 9

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

Tested by

no test coverage detected