Skip to content

Commit e12d200

Browse files
S390x big endian fixes (#8149)
Fixes for multiple tests on s390x
1 parent 526ec93 commit e12d200

File tree

3 files changed

+21
-9
lines changed

3 files changed

+21
-9
lines changed

test/test_transforms.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import os
33
import random
44
import re
5+
import sys
56
from functools import partial
67

78
import numpy as np
@@ -614,7 +615,7 @@ def _get_1_channel_tensor_various_types():
614615

615616
img_data_short = torch.ShortTensor(1, 4, 4).random_()
616617
expected_output = img_data_short.numpy()
617-
yield img_data_short, expected_output, "I;16"
618+
yield img_data_short, expected_output, "I;16" if sys.byteorder == "little" else "I;16B"
618619

619620
img_data_int = torch.IntTensor(1, 4, 4).random_()
620621
expected_output = img_data_int.numpy()
@@ -631,7 +632,7 @@ def _get_2d_tensor_various_types():
631632

632633
img_data_short = torch.ShortTensor(4, 4).random_()
633634
expected_output = img_data_short.numpy()
634-
yield img_data_short, expected_output, "I;16"
635+
yield img_data_short, expected_output, "I;16" if sys.byteorder == "little" else "I;16B"
635636

636637
img_data_int = torch.IntTensor(4, 4).random_()
637638
expected_output = img_data_int.numpy()
@@ -662,7 +663,7 @@ def test_1_channel_float_tensor_to_pil_image(self):
662663
[
663664
(torch.Tensor(4, 4, 1).uniform_().numpy(), "L"),
664665
(torch.ByteTensor(4, 4, 1).random_(0, 255).numpy(), "L"),
665-
(torch.ShortTensor(4, 4, 1).random_().numpy(), "I;16"),
666+
(torch.ShortTensor(4, 4, 1).random_().numpy(), "I;16" if sys.byteorder == "little" else "I;16B"),
666667
(torch.IntTensor(4, 4, 1).random_().numpy(), "I"),
667668
],
668669
)
@@ -744,7 +745,7 @@ def test_2d_tensor_to_pil_image(self, with_mode, img_data, expected_output, expe
744745
[
745746
(torch.Tensor(4, 4).uniform_().numpy(), "L"),
746747
(torch.ByteTensor(4, 4).random_(0, 255).numpy(), "L"),
747-
(torch.ShortTensor(4, 4).random_().numpy(), "I;16"),
748+
(torch.ShortTensor(4, 4).random_().numpy(), "I;16" if sys.byteorder == "little" else "I;16B"),
748749
(torch.IntTensor(4, 4).random_().numpy(), "I"),
749750
],
750751
)

torchvision/datasets/mnist.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -510,15 +510,25 @@ def read_sn3_pascalvincent_tensor(path: str, strict: bool = True) -> torch.Tenso
510510
# read
511511
with open(path, "rb") as f:
512512
data = f.read()
513+
513514
# parse
514-
magic = get_int(data[0:4])
515-
nd = magic % 256
516-
ty = magic // 256
515+
if sys.byteorder == "little":
516+
magic = get_int(data[0:4])
517+
nd = magic % 256
518+
ty = magic // 256
519+
else:
520+
nd = get_int(data[0:1])
521+
ty = get_int(data[1:2]) + get_int(data[2:3]) * 256 + get_int(data[3:4]) * 256 * 256
522+
517523
assert 1 <= nd <= 3
518524
assert 8 <= ty <= 14
519525
torch_type = SN3_PASCALVINCENT_TYPEMAP[ty]
520526
s = [get_int(data[4 * (i + 1) : 4 * (i + 2)]) for i in range(nd)]
521527

528+
if sys.byteorder == "big":
529+
for i in range(len(s)):
530+
s[i] = int.from_bytes(s[i].to_bytes(4, byteorder="little"), byteorder="big", signed=False)
531+
522532
parsed = torch.frombuffer(bytearray(data), dtype=torch_type, offset=(4 * (nd + 1)))
523533

524534
# The MNIST format uses the big endian byte order, while `torch.frombuffer` uses whatever the system uses. In case

torchvision/transforms/functional.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import math
22
import numbers
3+
import sys
34
import warnings
45
from enum import Enum
56
from typing import Any, List, Optional, Tuple, Union
@@ -162,7 +163,7 @@ def to_tensor(pic) -> Tensor:
162163
return torch.from_numpy(nppic).to(dtype=default_float_dtype)
163164

164165
# handle PIL Image
165-
mode_to_nptype = {"I": np.int32, "I;16": np.int16, "F": np.float32}
166+
mode_to_nptype = {"I": np.int32, "I;16" if sys.byteorder == "little" else "I;16B": np.int16, "F": np.float32}
166167
img = torch.from_numpy(np.array(pic, mode_to_nptype.get(pic.mode, np.uint8), copy=True))
167168

168169
if pic.mode == "1":
@@ -285,7 +286,7 @@ def to_pil_image(pic, mode=None):
285286
if npimg.dtype == np.uint8:
286287
expected_mode = "L"
287288
elif npimg.dtype == np.int16:
288-
expected_mode = "I;16"
289+
expected_mode = "I;16" if sys.byteorder == "little" else "I;16B"
289290
elif npimg.dtype == np.int32:
290291
expected_mode = "I"
291292
elif npimg.dtype == np.float32:

0 commit comments

Comments
 (0)