Skip to content

Commit

Permalink
Add fp16 ops for onnxrt adaptor (#915)
Browse files Browse the repository at this point in the history
Signed-off-by: Mengni Wang <mengni.wang@intel.com>
  • Loading branch information
mengniwang95 committed May 31, 2023
1 parent 9ecc9f5 commit 15d5518
Show file tree
Hide file tree
Showing 13 changed files with 356 additions and 11 deletions.
7 changes: 6 additions & 1 deletion neural_compressor/adaptor/onnxrt_cuda.yaml
Expand Up @@ -99,7 +99,12 @@
}
fp16: &common_fp16 ['Concat', 'Gather', 'Reshape', 'Squeeze', 'Transpose', 'Unsqueeze',
'EmbedLayerNormalization', 'Attention', 'Split', 'Sigmoid', 'Relu', 'Mul', 'Pad', 'MaxPool',
'MatMul', 'LeakyRelu', 'GlobalAveragePool', 'Gemm', 'Conv', 'AveragePool', 'Add', 'Clip']
'MatMul', 'LeakyRelu', 'GlobalAveragePool', 'Gemm', 'Conv', 'AveragePool', 'Add', 'Clip',
'BatchNormalization', 'Softmax', 'Sum', 'Abs', 'BiasGelu', 'Exp', 'FastGelu',
'Gelu', 'Log', 'Round', 'Sigmoid', 'Sqrt', 'Tanh', 'Sub', 'Mul', 'Div', 'Pow',
'ReduceMean', 'Equal', 'FusedMatMul', 'Greater', 'GreaterOrEqual', 'Less', 'LessOrEqual',
'ReduceL1', 'ReduceL2', 'ReduceLogSum', 'ReduceLogSumExp', 'ReduceMax', 'ReduceProd',
'ReduceSum', 'ReduceSumSquare', 'LayerNormalization', 'Concat']
bf16: &common_bf16 ['Concat', 'Gather', 'Reshape', 'Squeeze', 'Transpose', 'Unsqueeze',
'Split', 'Sigmoid', 'Relu', 'Mul', 'MatMul', 'Gemm', 'Add']
recipes: &default_optimization
Expand Down
10 changes: 9 additions & 1 deletion neural_compressor/adaptor/ox_utils/operators/activation.py
Expand Up @@ -138,4 +138,12 @@ def convert(self):
node.op_type.split('QLinear')[-1], inputs,
outputs, node.name + '_convert', **kwargs)
add_nodes.append(activation_node)
return True, add_nodes, inits
return True, add_nodes, inits

@op_registry(op_types="Softmax, BiasGelu, Elu, Exp, FastGelu, Gelu, Softplus, Tanh")
class Float16ActivationOperator(Operator):
"""Float16 Activation operator."""

def __init__(self, onnx_quantizer, onnx_node):
"""Initialization."""
super(Float16ActivationOperator, self).__init__(onnx_quantizer, onnx_node)
8 changes: 8 additions & 0 deletions neural_compressor/adaptor/ox_utils/operators/binary_op.py
Expand Up @@ -134,3 +134,11 @@ def convert(self):
outputs, node.name + '_convert', **kwargs)
add_nodes.append(binary_node)
return True, add_nodes, inits

@op_registry(op_types="Sum, Sub, Div, Pow, Equal, Greater, GreaterOrEqual, Less, LessOrEqual")
class Float16BinaryOperator(Operator):
"""Float16 Binary operator."""

def __init__(self, onnx_quantizer, onnx_node):
"""Initialization."""
super(Float16BinaryOperator, self).__init__(onnx_quantizer, onnx_node)
6 changes: 4 additions & 2 deletions neural_compressor/adaptor/ox_utils/operators/concat.py
Expand Up @@ -101,9 +101,11 @@ def convert(self, convert_format):
def cast(self): # pragma: no cover
"""Cast node."""
node = self.node
if node.input[0] not in [i.tensor_name for i in self.quantizer.new_value_info.values()]:
cast_tensor = [i.tensor_name for i in self.quantizer.new_value_info.values()]
if not all([i in cast_tensor for i in node.input]):
return
self.quantizer.dtype_cast(self.node, self.dtype)
self.quantizer.cast_inputs(self.node, self.dtype)
self.quantizer.cast_outputs(self.node, self.dtype)

@qop_registry(op_types="QLinearConcat")
class QConcatOperator(QOperator):
Expand Down
3 changes: 2 additions & 1 deletion neural_compressor/adaptor/ox_utils/operators/direct_q8.py
Expand Up @@ -79,7 +79,8 @@ def cast(self): # pragma: no cover
node = self.node
if node.input[0] not in [i.tensor_name for i in self.quantizer.new_value_info.values()]:
return
self.quantizer.dtype_cast(self.node, self.dtype)
self.quantizer.cast_inputs(self.node, self.dtype, [0])
self.quantizer.cast_outputs(self.node, self.dtype)

@qop_registry(op_types="Reshape, Transpose, Squeeze, Unsqueeze")
class QDirectOperator(QOperator):
Expand Down
8 changes: 8 additions & 0 deletions neural_compressor/adaptor/ox_utils/operators/matmul.py
Expand Up @@ -195,3 +195,11 @@ def convert(self):
outputs, node.name + '_convert', **kwargs)
add_nodes.append(matmul_node)
return True, add_nodes, inits

@op_registry(op_types="FusedMatMul")
class FusedMatMulOperator(Operator):
"""FusedMatMul Operator."""

def __init__(self, onnx_quantizer, onnx_node):
"""Initialization."""
super(FusedMatMulOperator, self).__init__(onnx_quantizer, onnx_node)
45 changes: 45 additions & 0 deletions neural_compressor/adaptor/ox_utils/operators/norm.py
@@ -0,0 +1,45 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright (c) 2021 Intel Corporation
#
# 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.
"""Normalization Operator."""

import onnx
from neural_compressor.adaptor.ox_utils.operators.ops import op_registry, Operator, QOperator, qop_registry
from neural_compressor.adaptor.ox_utils.util import attribute_to_kwarg, ms_domain

@op_registry(op_types="BatchNormalization")
class BatchNormalizationOperator(Operator):
"""BatchNormalization Operator."""

def __init__(self, onnx_quantizer, onnx_node):
"""Initialization."""
super(BatchNormalizationOperator, self).__init__(onnx_quantizer, onnx_node)

def cast(self):
"""Cast node."""
if self.dtype == 'bf16':
self.quantizer.cast_inputs(self.node, self.dtype, [0])
else:
self.quantizer.cast_inputs(self.node, self.dtype)
self.quantizer.cast_outputs(self.node, self.dtype)

@op_registry(op_types="LayerNormalization")
class NormalizationOperator(Operator):
"""Normalization Operator."""

def __init__(self, onnx_quantizer, onnx_node):
"""Initialization."""
super(NormalizationOperator, self).__init__(onnx_quantizer, onnx_node)
3 changes: 2 additions & 1 deletion neural_compressor/adaptor/ox_utils/operators/ops.py
Expand Up @@ -107,7 +107,8 @@ def convert(self, convert_format):

def cast(self): # pragma: no cover
"""Cast node."""
self.quantizer.dtype_cast(self.node, self.dtype)
self.quantizer.cast_inputs(self.node, self.dtype)
self.quantizer.cast_outputs(self.node, self.dtype)

class QOperator(object):
"""Base QOperator."""
Expand Down
28 changes: 28 additions & 0 deletions neural_compressor/adaptor/ox_utils/operators/reduce.py
@@ -0,0 +1,28 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright (c) 2021 Intel Corporation
#
# 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.
"""Reduce Operator."""

from neural_compressor.adaptor.ox_utils.operators.ops import op_registry, Operator

@op_registry(op_types="ReduceMean, ReduceLogSum, ReduceLogSumExp, ReduceMax, " \
"ReduceL1, ReduceL2, ReduceProd, ReduceSum, ReduceSumSquare")
class ReduceOperator(Operator):
"""Reduce Operator."""

def __init__(self, onnx_quantizer, onnx_node):
"""Initialization."""
super(ReduceOperator, self).__init__(onnx_quantizer, onnx_node)
3 changes: 2 additions & 1 deletion neural_compressor/adaptor/ox_utils/operators/split.py
Expand Up @@ -87,7 +87,8 @@ def cast(self): # pragma: no cover
node = self.node
if node.input[0] not in [i.tensor_name for i in self.quantizer.new_value_info.values()]:
return
self.quantizer.dtype_cast(self.node, self.dtype)
self.quantizer.cast_inputs(self.node, self.dtype)
self.quantizer.cast_outputs(self.node, self.dtype)

@qop_registry(op_types="Split")
class QSplitOperator(QOperator):
Expand Down
27 changes: 27 additions & 0 deletions neural_compressor/adaptor/ox_utils/operators/unary_op.py
@@ -0,0 +1,27 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright (c) 2021 Intel Corporation
#
# 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.
"""Unary operator."""

from neural_compressor.adaptor.ox_utils.operators.ops import op_registry, Operator

@op_registry(op_types="Abs, Exp, Log, Round, Sqrt")
class UnaryOperator(Operator):
"""Unary operator."""

def __init__(self, onnx_quantizer, onnx_node):
"""Initialization."""
super(UnaryOperator, self).__init__(onnx_quantizer, onnx_node)
12 changes: 8 additions & 4 deletions neural_compressor/adaptor/ox_utils/quantizer.py
Expand Up @@ -372,9 +372,11 @@ def dfs(match_nodes, node, pattern):
self.model.replace_node_input(node, old_input_name, new_input_name)
self.model.update()

def dtype_cast(self, node, cfg, keep_io_types=True): # pragma: no cover
"""Cast node dtype."""
def cast_inputs(self, node, cfg, indices=None):
"""Cast node input dtype."""
for idx, tensor_name in enumerate(node.input):
if indices and idx not in indices:
continue
initializer = find_by_name(tensor_name, self.model.initializer())
if initializer is not None:
if initializer.data_type != onnx_proto.TensorProto.FLOAT:
Expand All @@ -394,10 +396,12 @@ def dtype_cast(self, node, cfg, keep_io_types=True): # pragma: no cover
node.input[idx] = name
self.new_value_info[name] = ValueInfo(tensor_name,
TensorProto.FLOAT, dtype_mapping[cfg])
if all([i not in self.new_value_info for i in node.input]):
return

def cast_outputs(self, node, cfg, indices=None):
"""Cast node output dtype."""
for idx, tensor_name in enumerate(node.output):
if indices and idx not in indices:
continue
if tensor_name in self.value_infos and \
self.value_infos[tensor_name].type.HasField('tensor_type') and \
self.value_infos[tensor_name].type.tensor_type.elem_type != TensorProto.FLOAT:
Expand Down

0 comments on commit 15d5518

Please sign in to comment.