factory.py 3.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
# Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


import abc
import inspect
from functools import partial

from paddle.nn import Layer

from .base_quanter import BaseQuanter


class ClassWithArguments(metaclass=abc.ABCMeta):
    def __init__(self, *args, **kwargs):
        self._args = args
        self._kwargs = kwargs

    @property
    def args(self):
        return self._args

    @property
    def kwargs(self):
        return self._kwargs

    @abc.abstractmethod
    def _get_class(self):
        pass

    def __str__(self):
        args_str = ",".join(
            list(self.args) + [f"{k}={v}" for k, v in self.kwargs.items()]
        )
        return f"{self.__class__.__name__}({args_str})"

    def __repr__(self):
        return self.__str__()


class QuanterFactory(ClassWithArguments):
    r"""
    The factory holds the quanter's class information and
    the arguments used to create quanter instance.
    """

    def __init__(self, *args, **kwargs):
59
        super().__init__(*args, **kwargs)
60 61 62 63 64 65 66 67 68 69 70 71 72
        self.partial_class = None

    def _instance(self, layer: Layer) -> BaseQuanter:
        r"""
        Create an instance of quanter for target layer.
        """
        if self.partial_class is None:
            self.partial_class = partial(
                self._get_class(), *self.args, **self.kwargs
            )
        return self.partial_class(layer)


73 74 75
ObserverFactory = QuanterFactory


76 77 78 79 80 81 82 83 84 85
def quanter(class_name):
    r"""
    Annotation to declare a factory class for quanter.

    Args:
        class_name (str) - The name of factory class to be declared.

    Examples:
       .. code-block:: python

86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
            >>> # doctest: +SKIP
            >>> # Given codes in ./customized_quanter.py
            >>> from paddle.quantization import quanter
            >>> from paddle.quantization import BaseQuanter
            >>> @quanter("CustomizedQuanter")
            >>> class CustomizedQuanterLayer(BaseQuanter):
            ...     def __init__(self, arg1, kwarg1=None):
            ...         pass

            >>> # Used in ./test.py
            >>> # from .customized_quanter import CustomizedQuanter
            >>> from paddle.quantization import QuantConfig
            >>> arg1_value = "test"
            >>> kwarg1_value = 20
            >>> quanter = CustomizedQuanter(arg1_value, kwarg1=kwarg1_value)
            >>> q_config = QuantConfig(activation=quanter, weight=quanter)
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135

    """

    def wrapper(target_class):
        init_function_str = f"""
def init_function(self, *args, **kwargs):
    super(type(self), self).__init__(*args, **kwargs)
    import importlib
    module = importlib.import_module("{target_class.__module__}")
    my_class = getattr(module, "{target_class.__name__}")
    globals()["{target_class.__name__}"] = my_class
def get_class_function(self):
    return {target_class.__name__}
locals()["init_function"]=init_function
locals()["get_class_function"]=get_class_function
"""
        exec(init_function_str)
        frm = inspect.stack()[1]
        mod = inspect.getmodule(frm[0])
        new_class = type(
            class_name,
            (QuanterFactory,),
            {
                "__init__": locals()["init_function"],
                "_get_class": locals()["get_class_function"],
            },
        )
        setattr(mod, class_name, new_class)
        if "__all__" in mod.__dict__:
            mod.__all__.append(class_name)

        return target_class

    return wrapper