Ejemplo n.º 1
0
    def get_quantizer_output(self) -> QuantizerOutput:
        """
        Quantization information can be stored both in q_output attribute
        and in meta attributes
        TODO: Merge approaches
        """
        if not self.is_quantized():
            raise ValueError("No quantization output found. Quantize this"
                             " XGraph object before retrieving the"
                             " quantization output")

        if (self.quantizer_output is not None
                and "is_quantized" in self.meta_attrs
                and self.meta_attrs["is_quantized"]):
            warnings.warn("Quantization info found both in XGraph meta"
                          " attributes and q_output attribute")

        if self.quantizer_output is not None:
            return self.quantizer_output

        # Retrieve quantization output from meta attributes
        q_output = QuantizerOutput(self.get_name())
        if "quant_keys" not in self.meta_attrs:
            raise ValueError("Expected `quant_keys` attribute in meta"
                             " attributes")

        for q_key in self.meta_attrs["quant_keys"]:
            q_output.add(q_key=q_key,
                         q_file=self.meta_attrs[q_key]['q_file'],
                         q_info=self.meta_attrs[q_key]['q_info'],
                         orig_pb=self.meta_attrs[q_key]['orig_pb'])
            logger.debug("QOutput q_info: {}".format(
                self.meta_attrs[q_key]['q_info']))

        return q_output
Ejemplo n.º 2
0
    def quantize(self, stop=None, subgraphs_only=True):
        # type: (str, boolean) -> None
        """
        Start quantization of the executable graph model

        Arguments
        ---------
        stop: str (optional, default = None)
            the name of the operation at which to stop quantization
        """

        self._quantize(stop, subgraphs_only)

        # quant_files = {}
        q_output = QuantizerOutput(self.xgraph.get_name())
        for qkey in self._quant_layers.keys():
            if qkey != 'None':
                quant_file = os.path.join(self.work_dir, qkey + '_quant.json')
                self._quant_param.save_to_dpu_v1_json(self._quant_layers[qkey],
                                                      quant_file)
                q_output.add(qkey, quant_file, None, None)

        self.xgraph.set_quantizer_output(q_output)

        logger.info("QUANTIZATION DONE")

        return self.xgraph
Ejemplo n.º 3
0
    def __init__(self,
                 xgraph,
                 inputs_func,
                 work_dir=os.path.join(os.getcwd(), 'work')):

        super(ExternalQuantizer, self).__init__(xgraph, inputs_func, work_dir)

        self.gen = TfGenerator()
        self.partition_graphs = {}
        self.res = {}
        self.q_output = QuantizerOutput(name=xgraph.get_name())
Ejemplo n.º 4
0
    def __init__(self,
                 xgraph,
                 inputs_func,
                 work_dir=os.path.join(os.getcwd(), 'work'),
                 quant_iter=1,
                 **kwargs):

        super(DECENTQuantizer, self).__init__(xgraph, inputs_func, work_dir)

        self.quant_iter = quant_iter
        self.gen = TfGenerator()
        self.partition_graphs = {}
        self.res = {}
        self.kwargs = kwargs

        self.q_output = QuantizerOutput(name=xgraph.get_name())
Ejemplo n.º 5
0
    def __init__(self,
                 xgraph,
                 inputs_func,
                 bitwidth=8,
                 work_dir=os.path.join(os.getcwd(), 'work'),
                 quant_iter=1,
                 mse_opt_num=50):
        super(XGraphMSEThresholdQuantizer, self).__init__(xgraph)

        self.inputs_func = inputs_func
        self.work_dir = work_dir
        self.bitwidth = bitwidth
        self.mse_opt_num = mse_opt_num

        self.quant_xgraph = None
        self.runtime = None

        self._quant_param = QuantParamFactory()
        self._quant_layers = {}

        self.q_output = QuantizerOutput(name=xgraph.get_name())