harbor run -t openthoughts/tasktrove-mix-h8-original-tests-v2-mix-h8-original-3694
harbor download openthoughts/tasktrove-mix-h8-original-tests-v2-mix-h8-original-3694
solution file
import math import torch from torch import nn, Tensor from .qlinear import DynamicQuantizeLinear, QEmbedding max_q_int8 = 2 ** (8 - 1) - 1 assert 127 == max_q_int8 @torch.no_grad() def quantize_int8(inputs: Tensor) -> tuple[torch.CharTensor, Tensor]: ''' inputs: for weight (out_dim, in_dim), for activation (...channels, features) ''' w_max, _ = torch.max(inputs.abs(), dim=1, keepdim=True) scale = torch.clamp(w_max / max_q_int8, min=1e-10) # safe for float16 inputs = torch.clamp(torch.round(inputs / scale), -max_q_int8, max_q_int8) return inputs.to(torch.int8), scale.squeeze(dim=-1) @torch.no_grad() def clamp_to_quantize_grid(x: Tensor, scale: Tensor) -> Tensor: q = torch.clamp(torch.round(x / scale), -max_q_int8, max_q_int8) return scale * q @torch.no_grad() def quantize_with_scale(x: Tensor, scale: Tensor) -> Tensor: return torch.clamp(torch.round(x / scale), -max_q_int8, max_q_int8).to(torch.int8) @torch.no_grad() def get_quant_int8_linear(layer: nn.Linear): assert isinstance(layer, nn.Linear) q_weight, scale = quantize_int8(layer.weight) qlinear = DynamicQuantizeLinear(layer.in_features, layer.out_features, layer.bias is not None) qlinear.apply_weights_(q_weight, scale, layer.bias) return qlinear @torch.no_grad() def get_quant_embedding(layer: nn.Embedding): assert isinstance(layer, nn.Embedding) q_weight, scale = quantize_int8(layer.weight.t()) qembedding = QEmbedding(layer.num_embeddings, layer.embedding_dim) qembedding.
Complete the code above, taking into account the cross-file context provided. Your solution must work correctly with the related files in the project.
Place your complete solution in /app/solution.py
/app/solution.py