MCPcopy Create free account
hub / github.com/deepspeedai/DeepSpeed / concat_to_compat_param

Function concat_to_compat_param

deepspeed/inference/quantization/utils.py:186–216  ·  view source on GitHub ↗
(quantized_weight: Tensor,
                           quant_scale: Tensor,
                           quant_min: Tensor,
                           return_param: bool = True)

Source from the content-addressed store, hash-verified

184
185
186def concat_to_compat_param(quantized_weight: Tensor,
187 quant_scale: Tensor,
188 quant_min: Tensor,
189 return_param: bool = True) -> Union[nn.Parameter, Tensor]:
190 shape_wieght = quantized_weight.shape
191 shape_scale = quant_scale.shape
192 shape_min = quant_min.shape
193
194 quantized_weight = torch.flatten(quantized_weight)
195 quant_scale = torch.flatten(quant_scale)
196 quant_min = torch.flatten(quant_min)
197
198 def deconcat_individual_tensors(shape_wieght: torch.Size, shape_scale: torch.Size,
199 shape_min: torch.Size) -> Callable:
200
201 def fn(compat_tensor: nn.Parameter) -> Tuple[Tensor, Tensor, Tensor]:
202 weight = torch.narrow(compat_tensor, 0, 0, shape_wieght.numel()).view(shape_wieght)
203 scale = torch.narrow(compat_tensor, 0, shape_wieght.numel(), shape_scale.numel()).view(shape_scale)
204 min_val = torch.narrow(compat_tensor, 0,
205 shape_wieght.numel() + shape_scale.numel(), shape_min.numel()).view(shape_min)
206
207 return weight, scale, min_val
208
209 return fn
210
211 compat_tensor = torch.concat([quantized_weight, quant_scale, quant_min])
212 if return_param:
213 compat_tensor = nn.Parameter(compat_tensor, requires_grad=False)
214 compat_tensor.deconcat = deconcat_individual_tensors(shape_wieght, shape_scale, shape_min)
215
216 return compat_tensor
217
218
219def _quantize_param(param: nn.Parameter, quant_config: Dict):

Callers 3

_quantize_paramFunction · 0.85
wrapperFunction · 0.85

Calls 1

Tested by

no test coverage detected