MCPcopy
hub / github.com/OpenPPL/ppq / forward

Method forward

ppq/quantization/qfunction/linear.py:113–123  ·  view source on GitHub ↗
(ctx, tensor: torch.Tensor, config: TensorQuantizationConfig)

Source from the content-addressed store, hash-verified

111 """
112 @ staticmethod
113 def forward(ctx, tensor: torch.Tensor, config: TensorQuantizationConfig) -> torch.Tensor:
114 from ppq.quantization.observer.range import minmax_to_scale_offset
115 # solve scale and offset at first.
116 scales, offsets = minmax_to_scale_offset(
117 tensor.min().item(), tensor.max().item(), config=config)
118 print(scales, offsets)
119 # quantization function, pytorch implmentation
120 tensor = ppq_tensor_round((tensor / scales), config.rounding) + offsets
121 tensor = torch.clamp(tensor, config.quant_min, config.quant_max)
122 tensor = (tensor - offsets) * scales
123 return tensor
124
125 @ staticmethod
126 def backward(ctx, dy: torch.Tensor):

Callers

nothing calls this directly

Calls 2

minmax_to_scale_offsetFunction · 0.90
ppq_tensor_roundFunction · 0.90

Tested by

no test coverage detected