Anonymous
Результат вывода onnx несовместим с результатом вывода numpy
Сообщение
Anonymous » 27 дек 2024, 04:54
Я хочу реализовать вывод модели onnx в своем собственном коде C, но на некоторых уровнях результат между C и ONNX имеет 1 ошибку, например C — 40, а onnx — 41.
Я хочу знать, почему результат numpy равен -87, а результат onnx - -88? ?
При выводе квантовой модели ошибка 1 является фатальной! Суммарная ошибка на многих уровнях может достигать 4-5 (в 8-битных целых числах).
Спасибо:>
тестовый код ниже⬇
onnxruntime==1.19.2 python==3.8
Код: Выделить всё
import onnx
from onnx import helper, TensorProto, numpy_helper
import numpy as np
import onnxruntime as ort
A = 'A'
B = 'B'
C = 'C'
A_scale = 0.008010663092136383
A_zero_point = 7
B_scale = 0.00622713053599
B_zero_point = -128
C_scale = 0.006873490754514933
C_zero_point = -128
input_A = helper.make_tensor_value_info(A, TensorProto.INT8, [1, 1, 1, 1])
input_B = helper.make_tensor_value_info(B, TensorProto.INT8, [1, 1, 1, 1])
output = helper.make_tensor_value_info(C, TensorProto.INT8, [1, 1, 1, 1])
initializer_A_scale = numpy_helper.from_array(np.array(A_scale, dtype=np.float32), name='A_scale')
initializer_A_zero_point = numpy_helper.from_array(np.array(A_zero_point, dtype=np.int8), name='A_zero_point')
initializer_B_scale = numpy_helper.from_array(np.array(B_scale, dtype=np.float32), name='B_scale')
initializer_B_zero_point = numpy_helper.from_array(np.array(B_zero_point, dtype=np.int8), name='B_zero_point')
initializer_C_scale = numpy_helper.from_array(np.array(C_scale, dtype=np.float32), name='C_scale')
initializer_C_zero_point = numpy_helper.from_array(np.array(C_zero_point, dtype=np.int8), name='C_zero_point')
qlinear_add_node = helper.make_node(
'QLinearAdd',
inputs=[A, 'A_scale', 'A_zero_point', B, 'B_scale', 'B_zero_point', 'C_scale', 'C_zero_point'],
outputs=[C],
name='QLinearAdd',
domain='com.microsoft'
)
opset_version_ai_onnx = 13
opset_version_com_microsoft = 1
graph = helper.make_graph(
nodes=[qlinear_add_node],
name='QLinearAdd_Graph',
inputs=[input_A, input_B],
outputs=[output],
initializer=[
initializer_A_scale,
initializer_A_zero_point,
initializer_B_scale,
initializer_B_zero_point,
initializer_C_scale,
initializer_C_zero_point
]
)
model = helper.make_model(graph, producer_name='onnx-qlinearadd-fixed-params',
opset_imports=[ helper.make_opsetid(domain='ai.onnx', version=opset_version_ai_onnx),
helper.make_opsetid(domain='com.microsoft', version=opset_version_com_microsoft)])
onnx.save(model, 'qlinearadd_fixed_params_model.onnx')
print("ONNX MODEL save 'qlinearadd_fixed_params_model.onnx'")
A_int8 = np.array([-8], dtype=np.int8)
B_int8 = np.array([-64], dtype=np.int8)
A_real = A_scale * (A_int8.astype(np.int32) - A_zero_point)
B_real = B_scale * (B_int8.astype(np.int32) - B_zero_point)
C_real = A_real + B_real
A1 = A_scale *(A_int8 - A_zero_point)
B1 = B_scale*(B_int8 - B_zero_point)
print((A1+B1) / C_scale + C_zero_point )
C_int32 = np.round(C_real / C_scale) + C_zero_point
C_int8 = C_int32.astype(np.int8)
print(C_int8)
session = ort.InferenceSession('qlinearadd_fixed_params_model.onnx')
output_name = session.get_outputs()[0].name
A_data = np.array([-8], dtype=np.int8).reshape([1, 1, 1, 1])
B_data = np.array([-64], dtype=np.int8).reshape([1, 1, 1, 1])
input_dict = {
'A': A_data,
'B': B_data
}
outputs = session.run([output_name], input_dict)
C_output = outputs[0]
print("output C:", C_output)
Результат тестового кода:
Код: Выделить всё
ONNX MODEL save 'qlinearadd_fixed_params_model.onnx'
[-87.49999529]
[-87]
output C: [[[[-88]]]]
но я думаю, что -87 — это то, что я хочу получить.
Подробнее здесь:
https://stackoverflow.com/questions/793 ... nce-result
1735264462
Anonymous
Я хочу реализовать вывод модели onnx в своем собственном коде C, но на некоторых уровнях результат между C и ONNX имеет 1 ошибку, например C — 40, а onnx — 41. Я хочу знать, почему результат numpy равен -87, а результат onnx - -88? ? При выводе квантовой модели ошибка 1 является фатальной! Суммарная ошибка на многих уровнях может достигать 4-5 (в 8-битных целых числах). Спасибо:> тестовый код ниже⬇ onnxruntime==1.19.2 python==3.8 [code]import onnx from onnx import helper, TensorProto, numpy_helper import numpy as np import onnxruntime as ort A = 'A' B = 'B' C = 'C' A_scale = 0.008010663092136383 A_zero_point = 7 B_scale = 0.00622713053599 B_zero_point = -128 C_scale = 0.006873490754514933 C_zero_point = -128 input_A = helper.make_tensor_value_info(A, TensorProto.INT8, [1, 1, 1, 1]) input_B = helper.make_tensor_value_info(B, TensorProto.INT8, [1, 1, 1, 1]) output = helper.make_tensor_value_info(C, TensorProto.INT8, [1, 1, 1, 1]) initializer_A_scale = numpy_helper.from_array(np.array(A_scale, dtype=np.float32), name='A_scale') initializer_A_zero_point = numpy_helper.from_array(np.array(A_zero_point, dtype=np.int8), name='A_zero_point') initializer_B_scale = numpy_helper.from_array(np.array(B_scale, dtype=np.float32), name='B_scale') initializer_B_zero_point = numpy_helper.from_array(np.array(B_zero_point, dtype=np.int8), name='B_zero_point') initializer_C_scale = numpy_helper.from_array(np.array(C_scale, dtype=np.float32), name='C_scale') initializer_C_zero_point = numpy_helper.from_array(np.array(C_zero_point, dtype=np.int8), name='C_zero_point') qlinear_add_node = helper.make_node( 'QLinearAdd', inputs=[A, 'A_scale', 'A_zero_point', B, 'B_scale', 'B_zero_point', 'C_scale', 'C_zero_point'], outputs=[C], name='QLinearAdd', domain='com.microsoft' ) opset_version_ai_onnx = 13 opset_version_com_microsoft = 1 graph = helper.make_graph( nodes=[qlinear_add_node], name='QLinearAdd_Graph', inputs=[input_A, input_B], outputs=[output], initializer=[ initializer_A_scale, initializer_A_zero_point, initializer_B_scale, initializer_B_zero_point, initializer_C_scale, initializer_C_zero_point ] ) model = helper.make_model(graph, producer_name='onnx-qlinearadd-fixed-params', opset_imports=[ helper.make_opsetid(domain='ai.onnx', version=opset_version_ai_onnx), helper.make_opsetid(domain='com.microsoft', version=opset_version_com_microsoft)]) onnx.save(model, 'qlinearadd_fixed_params_model.onnx') print("ONNX MODEL save 'qlinearadd_fixed_params_model.onnx'") A_int8 = np.array([-8], dtype=np.int8) B_int8 = np.array([-64], dtype=np.int8) A_real = A_scale * (A_int8.astype(np.int32) - A_zero_point) B_real = B_scale * (B_int8.astype(np.int32) - B_zero_point) C_real = A_real + B_real A1 = A_scale *(A_int8 - A_zero_point) B1 = B_scale*(B_int8 - B_zero_point) print((A1+B1) / C_scale + C_zero_point ) C_int32 = np.round(C_real / C_scale) + C_zero_point C_int8 = C_int32.astype(np.int8) print(C_int8) session = ort.InferenceSession('qlinearadd_fixed_params_model.onnx') output_name = session.get_outputs()[0].name A_data = np.array([-8], dtype=np.int8).reshape([1, 1, 1, 1]) B_data = np.array([-64], dtype=np.int8).reshape([1, 1, 1, 1]) input_dict = { 'A': A_data, 'B': B_data } outputs = session.run([output_name], input_dict) C_output = outputs[0] print("output C:", C_output) [/code] Результат тестового кода: [code]ONNX MODEL save 'qlinearadd_fixed_params_model.onnx' [-87.49999529] [-87] output C: [[[[-88]]]] [/code] но я думаю, что -87 — это то, что я хочу получить. Подробнее здесь: [url]https://stackoverflow.com/questions/79310751/the-onnx-inference-result-is-inconsistent-with-the-numpy-inference-result[/url]