def feedforward(x, model_dictionary): block_input = F.hardtanh_( F.conv2d(x, model_dictionary['conv1.weight'], model_dictionary['conv1.bias'], padding=1), 0, 2) for l in ['1', '2', '3', '4']: for s in ['0', '1']: name = 'layer' + l + '.' + s stride = 2 if ((l != '1') and (s == '0')) else 1 intermediate_activation = F.hardtanh_( F.conv2d(block_input, model_dictionary[name + '.conv1.weight'], model_dictionary[name + '.conv1.bias'], stride=stride, padding=1), 0, 2) block_output = F.conv2d(intermediate_activation, model_dictionary[name + '.conv2.weight'], model_dictionary[name + '.conv2.bias'], padding=1) shortcut = F.conv2d(block_input, model_dictionary[name + '.shortcut.weight'], model_dictionary[name + '.shortcut.bias'], stride=stride) if ((l != '1') and (s == '0')) else block_input block_input = F.hardtanh_(block_output + shortcut, 0, 2) linear_input = F.avg_pool2d(block_input, 4) linear_input = linear_input.view(linear_input.size(0), -1) y = torch.matmul(model_dictionary['linear.weight'], linear_input.transpose( 0, 1)) + model_dictionary['linear.bias'][:, None] #result is 10x BS return y
def feedforward(x,model_dictionary,B): BO = torch.from_numpy(np.loadtxt('precision_offsets/input.activation.txt')).type(torch.float).cuda() x = quantizeSigned(x,B+BO,4.0) BO = torch.from_numpy(np.loadtxt('precision_offsets/conv1.weight.txt')).type(torch.float).cuda() quantized_weight = quantizeSigned(model_dictionary['conv1.weight'],B+BO,torch.from_numpy(np.load('scalars/conv1.weight.npy')).cuda()) block_input = F.hardtanh_(F.conv2d(x,quantized_weight,model_dictionary['conv1.bias'],padding=1),0,2) BO = torch.from_numpy(np.loadtxt('precision_offsets/conv1.activation.txt')).type(torch.float).cuda() block_input = quantizeUnsigned(block_input,B+BO) for l in ['1','2','3','4']: for s in ['0','1']: name = 'layer'+l+'.'+s stride = 2 if ((l!='1') and (s=='0')) else 1 BO = torch.from_numpy(np.loadtxt('precision_offsets/'+name+'.conv1.weight.txt')).type(torch.float).cuda() quantized_weight = quantizeSigned(model_dictionary[name+'.conv1.weight'],B+BO,torch.from_numpy(np.load('scalars/'+name+'.conv1.weight.npy')).cuda()) intermediate_activation = F.hardtanh_(F.conv2d(block_input,quantized_weight,model_dictionary[name+'.conv1.bias'],stride=stride,padding=1) ,0,2) BO = torch.from_numpy(np.loadtxt('precision_offsets/'+name+'.inside.activation.txt')).type(torch.float).cuda() intermediate_activation = quantizeUnsigned(intermediate_activation,B+BO) BO = torch.from_numpy(np.loadtxt('precision_offsets/'+name+'.conv2.weight.txt')).type(torch.float).cuda() quantized_weight = quantizeSigned(model_dictionary[name+'.conv2.weight'],B+BO,torch.from_numpy(np.load('scalars/'+name+'.conv2.weight.npy')).cuda()) block_output = F.conv2d(intermediate_activation,quantized_weight,model_dictionary[name+'.conv2.bias'],padding=1) BO = torch.from_numpy(np.loadtxt('precision_offsets/'+name+'.shortcut.weight.txt')).type(torch.float).cuda() if ((l!='1') and (s=='0')) else BO quantized_weight = quantizeSigned(model_dictionary[name+'.shortcut.weight'],B+BO,torch.from_numpy(np.load('scalars/'+name+'.shortcut.weight.npy')).cuda()) if ((l!='1') and (s=='0')) else quantized_weight shortcut = F.conv2d(block_input,quantized_weight,model_dictionary[name+'.shortcut.bias'],stride=stride) if ((l!='1') and (s=='0')) else block_input block_input = F.hardtanh_(block_output+shortcut,0,2) BO = torch.from_numpy(np.loadtxt('precision_offsets/'+name+'.outside.activation.txt')).type(torch.float).cuda() block_input = quantizeUnsigned(block_input,B+BO) linear_input = F.avg_pool2d(block_input,4) linear_input = linear_input.view(linear_input.size(0),-1) BO = torch.from_numpy(np.loadtxt('precision_offsets/linear.weight.txt')).type(torch.float).cuda() quantized_weight = quantizeSigned(model_dictionary['linear.weight'],B+BO,torch.from_numpy(np.load('scalars/linear.weight.npy')).cuda()) y = torch.matmul(quantized_weight,linear_input.transpose(0,1))+model_dictionary['linear.bias'][:,None] #result is 10x BS return y
def forward(self, x): x = self.conv(x) x = self.relu6(x) self.relu6_(x) x = F.relu6(x) x = torch.clamp(x, -3, 3) x = x.clamp(-2.5, 2.5) # x = x.clamp_(-2, 2) # Enable when quantized `clamp_` is ready x = self.hardtanh(x) self.hardtanh_(x) x = F.hardtanh(x) F.hardtanh_(x) return x
def aten_hardtanh_(inputs, attributes, scope): inp, min_val, max_val = inputs[:3] ctx = current_context() net = current_context().network if ctx.is_tensorrt and has_trt_tensor(inputs): # use relu(x) - relu(x - 6) to implement relu6 (subset of hardtanh) assert min_val == 0, "only support relu6" layer = net.add_activation(inp, trt.ActivationType.RELU) output = layer.get_output(0) layer.name = scope + "/relu" tensor = np.full([1] * len(inp.shape), max_val, dtype=np.float32) trt_6 = ctx.network.add_constant([1] * len(inp.shape), tensor) layer = ctx.network.add_elementwise(output, trt_6.get_output(0), trt.ElementWiseOperation.MIN) output = layer.get_output(0) layer.name = scope + "/elem_min" output.name = scope + "/relu6" return [output] elif ctx.is_tvm and has_tvm_tensor(inputs): raise NotImplementedError return [F.hardtanh_(inp, min_val, max_val)]
def aten_hardtanh_(inputs, attributes, scope): inp, min_val, max_val = inputs[:3] ctx = current_context() net = current_context().network if ctx.is_tensorrt and has_trt_tensor(inputs): # use relu(x) - relu(x - 6) to implement relu6 (subset of hardtanh) assert min_val == 0, "only support relu6" layer = net.add_activation(inp, trt.ActivationType.RELU) output = layer.get_output(0) layer.name = scope + "/relu" inp_sub_6 = _scale_or_elementwise(net, inp, torch.tensor(max_val), "sub", scope + "/sub") layer = net.add_activation(inp_sub_6, trt.ActivationType.RELU) layer.name = scope + "/relu(x-6)" output_6 = layer.get_output(0) output = _scale_or_elementwise(net, output, output_6, "sub", scope + "/sub_relu") output.name = scope return [output] elif ctx.is_tvm and has_tvm_tensor(inputs): raise NotImplementedError return [F.hardtanh_(inp, min_val, max_val)]
def test_hardtanh_(self): inp = torch.randn(1, 3, 32, 32, device='cuda', dtype=self.dtype) output = F.hardtanh_(inp, min_val=-1., max_val=1.)
def __call__(self, a, b): a = self._to_vector(a) b = self._to_vector(b) sim = F.cosine_similarity(a, b, dim=1, eps=1e-8) sim = F.hardtanh_(sim, min_val=-1+1e-8, max_val=1-1e-8) return torch.acos(sim) * self._to_degrees
def __call__(self, g_pred_experts, g_true): g_pred_experts = self.pred_to_vector(g_pred_experts) g_true = self.true_to_vector(g_true) sim = F.cosine_similarity(g_pred_experts, g_true, dim=2, eps=1e-8) sim = F.hardtanh_(sim, min_val=-1 + 1e-8, max_val=1 - 1e-8) return torch.acos(sim) * self._to_degrees
def calculate_loss(self, a, b): a = pitchyaw_to_vector(a) b = pitchyaw_to_vector(b) sim = F.cosine_similarity(a, b, dim=1, eps=1e-8) sim = F.hardtanh_(sim, min_val=-1 + 1e-8, max_val=1 - 1e-8) return torch.acos(sim) * self._to_degrees
def forward(self, x): if self.inplace: return F.hardtanh_(x, min_val=0.0, max_val=1.0) return F.hardtanh(x, min_val=0.0, max_val=1.0)