Skip to content

tf.make_tensor_proto() does not respect byte order of numpy input array #55789

Description

@thias42
Click to expand!

Issue Type

Bug

Source

binary

Tensorflow Version

tf 2.8

Custom Code

No

OS Platform and Distribution

No response

Mobile device

No response

Python version

No response

Bazel version

No response

GCC/Compiler version

No response

CUDA/cuDNN version

No response

GPU model and memory

No response

Current Behaviour?

Byte order is not checked when creating a TensorProto with tf.make_tensor_proto() from a numpy.ndarray with non-native byte order. This leads to wrong tensor data.

I would expect that byte order is checked and swapped if necessary before assigning to tensor_proto.tensor_content in [python/framework/tensor_util.py](https://github.com/tensorflow/tensorflow/blob/v2.8.0/tensorflow/python/framework/tensor_util.py#L523)

Standalone code to reproduce the issue

x = np.ones(shape=(1, 2), dtype=np.float32)
x_bswap = x.astype('>f4') # assuming native byte order is little endian

y = tf.make_ndarray(tf.make_tensor_proto(x))
y_bswap = tf.make_ndarray(tf.make_tensor_proto(x_bswap))

print(y)
print(y_bswap)

assert np.array_equal(x, y)
assert np.array_equal(x, y_bswap)

Relevant log output

[[1. 1.]]
[[4.6006e-41 4.6006e-41]]
---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
in <cell line: 7>()
      5 print(y_bswap)
      6 assert np.array_equal(x, y)
----> 7 assert np.array_equal(x, y_bswap)

AssertionError:

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions