Skip to content

Commit e5a81aa

Browse files
Fix conversion of values in libtorch agnostic tests (pytorch#155115)
Due to different byteorder, when copying data, it has to be put into last bytes to ensure that int32_t converted to int64_t keeps same value. Same has to be done when it's converted back. This change fixes test TestLibtorchAgnosticCPU::test_my_ones_like_cpu from cpp_extensions/libtorch_agnostic_extension/test/test_libtorch_agnostic.py on s390x. Pull Request resolved: pytorch#155115 Approved by: https://github.com/huydhn
1 parent 3e2aa4b commit e5a81aa

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

test/run_test.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,6 @@ def __contains__(self, item):
239239
# some false errors
240240
"doctests",
241241
# new failures to investigate and fix
242-
"cpp_extensions/libtorch_agnostic_extension/test/test_libtorch_agnostic",
243242
"test_tensorboard",
244243
# onnx + protobuf failure, see
245244
# https://github.com/protocolbuffers/protobuf/issues/22104

torch/csrc/stable/library.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,19 @@ struct FromImpl {
4444
// https://gcc.gnu.org/bugzilla/show_bug.cgi?id=107361) We have a
4545
// static_assert above that T is trivially copyable, which should be
4646
// enough.
47+
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
4748
std::memcpy(&result, reinterpret_cast<const void*>(&val), sizeof(val));
49+
#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
50+
// if value has size less than sizeof(StableIValue), then only lowest bytes
51+
// have to be updated
52+
std::memcpy(
53+
reinterpret_cast<unsigned char*>(&result) + sizeof(StableIValue) -
54+
sizeof(val),
55+
reinterpret_cast<const void*>(&val),
56+
sizeof(val));
57+
#else
58+
#error Unexpected or undefined __BYTE_ORDER__
59+
#endif
4860
return result;
4961
}
5062
};
@@ -127,7 +139,22 @@ struct ToImpl {
127139
};
128140
Result result;
129141
// See NOTE[ -Wclass-memaccess ] above.
142+
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
130143
std::memcpy(reinterpret_cast<void*>(&result.t), &val, sizeof(result));
144+
#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
145+
static_assert(
146+
sizeof(T) <= sizeof(StableIValue),
147+
"StableLibrary stack does not support parameter types larger than 64 bits.");
148+
// if value has size less than sizeof(StableIValue), then only lowest bytes
149+
// have to be updated
150+
std::memcpy(
151+
reinterpret_cast<void*>(&result.t),
152+
reinterpret_cast<unsigned char*>(&val) + sizeof(StableIValue) -
153+
sizeof(result),
154+
sizeof(result));
155+
#else
156+
#error Unexpected or undefined __BYTE_ORDER__
157+
#endif
131158
return result.t;
132159
}
133160
};

0 commit comments

Comments
 (0)