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 for QCDQ to QOp conversion #75

Open
wants to merge 20 commits into
base: main
Choose a base branch
from
Open
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
48 changes: 48 additions & 0 deletions src/qonnx/custom_op/qop/averagepool_op.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
########################################################################
#
# Copyright (c) 2023 Advanced Micro Devices, Inc. All rights reserved.
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#
#########################################################################

import onnx

class AveragePool:

def __init__(self, node):

average_pool_node = node
a_name = average_pool_node.inputs[0].name

y_name = average_pool_node.outputs[0].name

new_average_pool_node = onnx.helper.make_node(name = average_pool_node.name, op_type = "AveragePool",
inputs = [a_name],
outputs = [y_name],
ceil_mode = average_pool_node.attrs["ceil_mode"],
kernel_shape = average_pool_node.attrs["kernel_shape"],
pads = average_pool_node.attrs["pads"],
strides = average_pool_node.attrs["strides"])

self.node = new_average_pool_node

def get_node(self):
return self.node
44 changes: 44 additions & 0 deletions src/qonnx/custom_op/qop/cast_op.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
########################################################################
#
# Copyright (c) 2023 Advanced Micro Devices, Inc. All rights reserved.
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#
#########################################################################

import onnx

class Cast:

def __init__(self, node):

cast_node = node

x_name = cast_node.inputs[0].name
y_name = cast_node.outputs[0].name

new_cast_node = onnx.helper.make_node(name = cast_node.name, op_type = "Cast",
inputs = [x_name],
outputs = [y_name],
to = cast_node.attrs["to"])
self.node = new_cast_node

def get_node(self):
return self.node
61 changes: 61 additions & 0 deletions src/qonnx/custom_op/qop/clip_op.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
########################################################################
#
# Copyright (c) 2023 Advanced Micro Devices, Inc. All rights reserved.
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#
#########################################################################

import onnx
from .helper import helper

class Clip:

def __init__(self, node):

clip_node = node

x_name = clip_node.inputs[0].name

x2_name = clip_node.inputs[1].name
x2_value = clip_node.inputs[1].values
x2_tensor = helper.create_initializer_tensor(x2_name,x2_value,onnx.TensorProto.INT8)

x3_name = clip_node.inputs[2].name
x3_value = clip_node.inputs[2].values
x3_tensor = helper.create_initializer_tensor(x3_name,x3_value,onnx.TensorProto.INT8)

new_clip_node = onnx.helper.make_node(name = clip_node.name, op_type = "Clip",
inputs= [x_name, x2_name, x3_name],
outputs = [clip_node.outputs[0].name])

self.node = new_clip_node

intializer_list = []
intializer_list.append(x2_tensor)
intializer_list.append(x3_tensor)
self.intializer_list = intializer_list

def get_node(self):
return self.node

def get_intializers(self):
return self.intializer_list

78 changes: 78 additions & 0 deletions src/qonnx/custom_op/qop/concat_op.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
########################################################################
#
# Copyright (c) 2023 Advanced Micro Devices, Inc. All rights reserved.
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#
#########################################################################

import onnx
from .helper import helper

class Concat:

def __init__(self, node):

concat_node = node

number_of_inputs = len(concat_node.inputs)

zp_value_list = []
zp_name_list = []
scale_values_list = []
scale_name_list = []
input_tensor_names = []

intializer_list = []
input_names = []

for i in range(number_of_inputs):
input_tensor_names.append(concat_node.inputs[i].name)
if len(concat_node.inputs[i].inputs) == 0:
c_input = helper.create_initializer_tensor(name=concat_node.inputs[i].name,
tensor_array=concat_node.inputs[i].values,
data_type=onnx.TensorProto.INT64)
intializer_list.append(c_input)
self.intializer_list = intializer_list

y_name = concat_node.outputs[0].name

for i in range(number_of_inputs):
input_names.append(input_tensor_names[i])
if len(scale_name_list)>0 and len(zp_name_list)>0:
input_names.append(scale_name_list[i])
input_names.append(zp_name_list[i])

kwargs = {}
kwargs["domain"] = 'com.microsoft'

new_concat_node = onnx.helper.make_node(name = concat_node.name,
op_type = "Concat",
inputs = input_names,
outputs = [y_name],
axis = concat_node.attrs["axis"])

self.node = new_concat_node

def get_node(self):
return self.node

def get_intializers(self):
return self.intializer_list
105 changes: 105 additions & 0 deletions src/qonnx/custom_op/qop/dequantizelinear_op.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
########################################################################
#
# Copyright (c) 2023 Advanced Micro Devices, Inc. All rights reserved.
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#
#########################################################################

import onnx
from .helper import helper
import numpy as np

class DequantizeLinear:

def __init__(self, node, remove_relu):

dql_node = node

x_name = dql_node.inputs[0].name

if helper.is_parent_exist(dql_node, 0, 0):
if dql_node.i().op == "QuantizeLinear":
ql_node = dql_node.i()
if helper.is_parent_exist(ql_node,0, 0):
if ql_node.i().op == "Relu":
relu_node = ql_node.i()
if remove_relu:
x_name = ql_node.outputs[0].name
else:
x_name = relu_node.outputs[0].name
else:
print("*************** WARNING *********************** Please check parent of QL node", ql_node.name, " ignore if pattern is correct")
else:
print("*************** WARNING *********************** Please check parent of DQL node", dql_node.name, " ignore if pattern is correct")
self.initializers = []

if len(dql_node.inputs[0].inputs) == 0:
if dql_node.inputs[0].dtype == np.uint8:
input_tensor = helper.create_initializer_tensor(name= dql_node.inputs[0].name,
tensor_array=dql_node.inputs[0].values,
data_type=onnx.TensorProto.UINT8)
elif dql_node.inputs[0].dtype == np.int8:
input_tensor = helper.create_initializer_tensor(name= dql_node.inputs[0].name,
tensor_array=dql_node.inputs[0].values,
data_type=onnx.TensorProto.INT8)
elif dql_node.inputs[0].dtype == np.int32:
input_tensor = helper.create_initializer_tensor(name= dql_node.inputs[0].name,
tensor_array=dql_node.inputs[0].values,
data_type=onnx.TensorProto.INT32)
self.initializers.append(input_tensor)

x_scale_name = dql_node.inputs[1].name
x_scale_value = dql_node.inputs[1].values
x_scale_tensor = helper.create_initializer_tensor(name=x_scale_name,tensor_array=x_scale_value,data_type=onnx.TensorProto.FLOAT)

x_zp_name = dql_node.inputs[2].name
x_zp_value = dql_node.inputs[2].values

if dql_node.inputs[2].dtype == np.uint8:
x_zp_tensor = helper.create_initializer_tensor(name=x_zp_name,
tensor_array=x_zp_value,
data_type=onnx.TensorProto.UINT8)
if dql_node.inputs[2].dtype == np.int32:
x_zp_tensor = helper.create_initializer_tensor(name=x_zp_name,
tensor_array=x_zp_value,
data_type=onnx.TensorProto.INT32)
elif dql_node.inputs[2].dtype == np.int8:
x_zp_tensor = helper.create_initializer_tensor(name=x_zp_name,
tensor_array=x_zp_value,
data_type=onnx.TensorProto.INT8)

y_name = dql_node.outputs[0].name

dequantizelinear_node = onnx.helper.make_node(name = dql_node.name,
op_type = "DequantizeLinear",
inputs = [x_name, x_scale_name, x_zp_name],
outputs = [y_name])

self.node = dequantizelinear_node

self.initializers.append(x_scale_tensor)
self.initializers.append(x_zp_tensor)

def get_node(self):
return self.node

def get_intializers(self):
return self.initializers
53 changes: 53 additions & 0 deletions src/qonnx/custom_op/qop/flatten_op.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
########################################################################
#
# Copyright (c) 2023 Advanced Micro Devices, Inc. All rights reserved.
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#
#########################################################################

import onnx

class Flatten:

def __init__(self, node):

flatten_node = node
x_name = flatten_node.inputs[0].name
y_name = flatten_node.outputs[0].name

if flatten_node.i().op == "DequantizeLinear":
node1 = flatten_node.i()
x_name = node1.inputs[0].name

if flatten_node.o().op == "QuantizeLinear":
node2 = flatten_node.o()
y_name = node2.outputs[0].name


new_flatten_node = onnx.helper.make_node(name = flatten_node.name, op_type = "Flatten",
inputs = [x_name],
outputs = [y_name])


self.node = new_flatten_node

def get_node(self):
return self.node
Loading