Пример #1
0
 def saveDQ(self, input_sample=None):
     if self.ts is None:
         self.export(input_sample)
     LOG.logI("Pytorch model dynamic quantize starting, will save model in {}".format(self.dq_output_file))
     quantized_model = quantize_dynamic_jit(self.ts, self.d_qconfig_dict)
     torch.jit.save(quantized_model, self.dq_output_file)
     LOG.logI("Pytorch model dynamic quantize succeeded, saved model in {}".format(self.dq_output_file))
    def checkGraphModeOp(self,
                         module,
                         data,
                         quantized_op,
                         tracing=False,
                         debug=False,
                         check=True,
                         eval_mode=True,
                         dynamic=False):
        if debug:
            print('Testing:', str(module))
        qconfig_dict = {
            '': get_default_qconfig(torch.backends.quantized.engine)
        }

        if eval_mode:
            module = module.eval()
        if dynamic:
            qconfig_dict = {'': default_dynamic_qconfig}
            inputs = data
        else:
            *inputs, target = data[0]
        model = get_script_module(module, tracing, inputs).eval()
        if debug:
            print('input graph:', model.graph)
        models = {}
        outputs = {}
        for d in [True, False]:
            # TODO: _test_only_eval_fn --> default_eval_fn
            if dynamic:
                models[d] = quantize_dynamic_jit(model, qconfig_dict, debug=d)
                # make sure it runs
                outputs[d] = models[d](inputs)
            else:
                # module under test can contain in-place ops, and we depend on
                # input data staying constant for comparisons
                data_copy = copy.deepcopy(data)
                models[d] = quantize_jit(model,
                                         qconfig_dict,
                                         test_only_eval_fn, [data_copy],
                                         inplace=False,
                                         debug=d)
                # make sure it runs
                outputs[d] = models[d](*inputs)

        if debug:
            print('debug graph:', models[True].graph)
            print('non debug graph:', models[False].graph)

        if check:
            # debug and non-debug option should have the same numerics
            self.assertEqual(outputs[True], outputs[False])

            # non debug graph should produce quantized op
            FileCheck().check(quantized_op) \
                       .run(models[False].graph)

        return models[False]
Пример #3
0
 def saveDQ(self):
     LOG.logI(
         "Pytorch model dynamic quantize starting, will save model in {}".
         format(self.dq_output_file))
     with torch.no_grad():
         quantized_model = quantize_dynamic_jit(self._jit(),
                                                self.d_qconfig_dict)
         torch.jit.save(quantized_model, self.dq_output_file)
     LOG.logI("Pytorch model dynamic quantize succeeded, saved model in {}".
              format(self.dq_output_file))