Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support matmul in QAT and loading quantized models in PTQ #47892

Merged
merged 5 commits into from
Dec 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
paddle.nn.quant.subtract,
paddle.nn.quant.multiply,
paddle.nn.quant.divide,
paddle.nn.quant.matmul,
]

fake_quant_leaf_layers = [
Expand Down
18 changes: 18 additions & 0 deletions python/paddle/fluid/contrib/slim/quantization/quantization_pass.py
Original file line number Diff line number Diff line change
Expand Up @@ -1939,6 +1939,15 @@ def apply(self, graph):
op_node.op()._set_attr("activation_bits", self._quant_bits)
op_node.op()._set_attr("with_quant_attr", True)
arg_names = utils._get_op_input_var_names(op_node)
# If already quanted, skip it.
skip_quant = False
for arg_name in arg_names:
if "quantized.dequantized" in arg_name:
skip_quant = True
break
if skip_quant:
continue

for arg_name in arg_names:
in_node = graph._find_node_by_name(
op_node.inputs, arg_name
Expand Down Expand Up @@ -2797,6 +2806,15 @@ def apply(self, graph):
continue

arg_names = utils._get_op_input_var_names(op_node)
# If already quanted, skip it.
skip_quant = False
for arg_name in arg_names:
if "quantized.dequantized" in arg_name:
yghstill marked this conversation as resolved.
Show resolved Hide resolved
skip_quant = True
break
if skip_quant:
continue

for arg_name in arg_names:
in_node = graph._find_node_by_name(
op_node.inputs, arg_name
Expand Down
1 change: 1 addition & 0 deletions python/paddle/nn/quant/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from .functional_layers import transpose # noqa: F401
from .functional_layers import concat # noqa: F401
from .functional_layers import flatten # noqa: F401
from .functional_layers import matmul # noqa: F401
from .quant_layers import QuantStub # noqa: F401

__all__ = []
10 changes: 9 additions & 1 deletion python/paddle/nn/quant/functional_layers.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from ...tensor import manipulation, math
from ...tensor import linalg, manipulation, math
from .. import Layer

__all__ = []
Expand Down Expand Up @@ -85,3 +85,11 @@ def __init__(self):

def forward(self, x, start_axis=0, stop_axis=-1, name=None):
return manipulation.flatten(x, start_axis, stop_axis, name)


class matmul(FloatFunctionalLayer):
def __init__(self):
super().__init__()

def forward(self, x, y, transpose_x=False, transpose_y=False, name=None):
return linalg.matmul(x, y, transpose_x, transpose_y, name)