def _test_output_bf16(self, model, x): modelName = model.__class__.__name__ core.enable_auto_dnnl() core.enable_jit() core.disable_mix_bf16_fp32() model = model.to('dpcpp').eval() x = x.to('dpcpp') x2 = x.clone() fused_model = torch.jit.script(copy.deepcopy(model)) # bn folding, removing it after solve some issue, using mix_preci? to check core.disable_auto_dnnl() fused_model = wrap_cpp_module( torch._C._jit_pass_fold_convbn(fused_model._c)) core.enable_auto_dnnl() core.enable_mix_bf16_fp32() # prepack convolution weight, weight will be a bf16 tensor fused_model = wrap_cpp_module( core._jit_prepack_conv_weight(fused_model._c)) with torch.no_grad(): # bf16, native path result = model(x) # bf16, jit path fresult = fused_model(x2) #print(result) #print(fresult) self.assertEqual(fresult, result)
def _test_output_int8(self, model, x, kind_in_graph=None, kind_not_in_graph=None, prec=None): modelName = model.__class__.__name__ core.enable_auto_dnnl() core.enable_jit_opt() model = model.to(ipex.DEVICE).eval() x = x.to(ipex.DEVICE) x2 = x.clone() x3 = x.clone() script_fused_model = torch.jit.script(copy.deepcopy(model)) trace_fused_model = torch.jit.trace(copy.deepcopy(model), x3) with torch.no_grad(): # int8, native path int8_calibration(model, [x], "configure.json") int8_conf = ipex.AmpConf(torch.int8, "configure.json") with ipex.AutoMixPrecision(int8_conf): result = model(x) # int8, jit script path script_graph = script_fused_model.graph_for(x2) int8_calibration(script_fused_model, [x2], "configure.json") int8_conf = ipex.AmpConf(torch.int8, "configure.json") with ipex.AutoMixPrecision(int8_conf): fused_sresult = script_fused_model(x2) # int8, jit trace path trace_graph = trace_fused_model.graph_for(x3) int8_calibration(trace_fused_model, [x3], "configure.json") int8_conf = ipex.AmpConf(torch.int8, "configure.json") with ipex.AutoMixPrecision(int8_conf): fused_tresult = trace_fused_model(x3) os.remove('configure.json') self.assertEqual(fused_sresult, result, prec) self.assertEqual(fused_tresult, result, prec) # check if the fused node exists in the graph if kind_in_graph is not None: self.assertTrue( any(n.kind() == kind_in_graph for n in script_graph.nodes())) self.assertTrue( any(n.kind() == kind_in_graph for n in trace_graph.nodes())) # check if certain node does not exist in the graph if kind_not_in_graph is not None: self.assertTrue( all(n.kind() != kind_not_in_graph for n in script_graph.nodes())) self.assertTrue( all(n.kind() != kind_not_in_graph for n in trace_graph.nodes()))
def _test_output_bf16(self, model, x, kind_in_graph=None, kind_not_in_graph=None, prec=None): modelName = model.__class__.__name__ core.enable_auto_dnnl() core.enable_jit_opt() core.enable_mix_bf16_fp32() model = model.to(ipex.DEVICE).eval() x = x.to(ipex.DEVICE) x2 = x.clone() x3 = x.clone() script_fused_model = torch.jit.script(copy.deepcopy(model)) trace_fused_model = torch.jit.trace(copy.deepcopy(model), x3) with torch.no_grad(): # bf16, native path result = model(x) # bf16, jit script path script_graph = script_fused_model.graph_for(x2) fused_sresult = script_fused_model(x2) # bf 16, jit trace path trace_graph = trace_fused_model.graph_for(x3) fused_tresult = trace_fused_model(x3) # disable mix_bf16_fp32 when the calculation is done # to avoid affecting other scripts core.disable_mix_bf16_fp32() self.assertEqual(fused_sresult, result, prec=prec) self.assertEqual(fused_tresult, result, prec=prec) # check if the fused node exists in the graph if kind_in_graph is not None: self.assertTrue( any(n.kind() == kind_in_graph for n in script_graph.nodes())) self.assertTrue( any(n.kind() == kind_in_graph for n in trace_graph.nodes())) # check if certain node does not exist in the graph if kind_not_in_graph is not None: self.assertTrue( all(n.kind() != kind_not_in_graph for n in script_graph.nodes())) self.assertTrue( all(n.kind() != kind_not_in_graph for n in trace_graph.nodes()))
def _test_output(self, model, x): modelName = model.__class__.__name__ core.disable_jit() model = model.to('dpcpp').eval() x = x.to('dpcpp') with torch.no_grad(): result = model(x) script_model = torch.jit.script(model) script_model.eval() with torch.no_grad(): sresult = script_model(x) self.assertEqual(result, sresult) core.enable_jit() fused_model = torch.jit.script(model) # bn folding, removing it after solve some issue core.disable_auto_dnnl() fused_model = wrap_cpp_module( torch._C._jit_pass_fold_convbn(fused_model._c)) core.enable_auto_dnnl() # prepack convolution weight fused_model = wrap_cpp_module( core._jit_prepack_conv_weight(fused_model._c)) with torch.no_grad(): # conv relu fusion, conv sum fusion or conv sum relu fusion print(fused_model.graph_for(x)) fresult = fused_model(x) # print(result) # print(sresult) # print(fresult) self.assertEqual(result, fresult)
fused_model = wrap_cpp_module( core._jit_prepack_conv_weight(fused_model._c)) with torch.no_grad(): # conv relu fusion, conv sum fusion or conv sum relu fusion print(fused_model.graph_for(x)) fresult = fused_model(x) # print(result) # print(sresult) # print(fresult) self.assertEqual(result, fresult) def test_output_conv_relu(self): self._test_output(Conv2dRelu_Fixed(3, 32, kernel_size=3, stride=1), torch.randn(32, 3, 224, 224)) def test_output_cascaded_conv2d_bn_sum_relu(self): self._test_output( CascadedConv2dBnSumRelu(3, 64, 32, kernel_size=3, stride=1), torch.rand(32, 3, 224, 224)) def test_output_linear_relu(self): self._test_output(LinearRelu(3, 32, bias=True), torch.rand(32, 3)) self._test_output(LinearRelu(3, 32, bias=False), torch.rand(32, 3)) if __name__ == '__main__': core.enable_auto_dnnl() test = unittest.main()