Skip to content

numpy_helper.to_array modifies TensorProto on big endian systems #5901

@tehbone

Description

@tehbone

Bug Report

Is the issue related to model conversion?

Yes, but debugged to an issue within ONNX

Describe the bug

Using tf2onnx to export a TF model to ONNX, some items were byteswapped incorrectly. Tracing of the code showed that the TensorProto holding the value had the correct contents, but at some point reexamination of the same TensorProto showed byteeswapped data. Further investigation showed that numpy_helper.to_array() was byteswapping the data within the TensorProto as opposed to returning a byteswapped copy of the data, yielding the inconsistent format. A local fix produced correct data.

System information

Linux RHEL 9.3 on s390x
ONNX 1.15.0
Python 3.10
gcc (compiling ONNX via pip install) 11.4.0
CMake 3.22.1
protobuf 3.20.3

Reproduction instructions

Simplified testcase:

from onnx import numpy_helper, TensorProto
import numpy as np

t = np.array(1, dtype=np.int32)
onnx_tensor = numpy_helper.from_array(t, "Tensor")
assert t == numpy_helper.to_array(onnx_tensor)
assert t == numpy_helper.to_array(onnx_tensor) # Currently blows up on big endian

Expected behavior

In the above testcase, the expectation is that onnx_tensor is treated as immutable during to_array()

Notes

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions