Skip to content

BUG: casting from float32 to uint8 causes different results between s390x and x86_64 #23481

@kiszk

Description

@kiszk

Describe the issue:

The casting from float32 to uint8 causes incorrect different results for negative (relatively large abs) values (e.g. -2 and -1.5) on s390x. The correct expected behavior is the result generated on x86_64.

This issue is revealed by pytorch/pytorch#97794.

Reproduce the code example:

import numpy as np
vals = (0, -2, -1.5, -0.5, 0, 0.5, 1.5, 2, 0)
a = np.array(vals, dtype=np.float32).astype(np.uint8)
print(a)

Error message:

No error message

Runtime information:

s390x

% python -c "import sys, numpy; print(numpy.__version__); print(sys.version); print(numpy.show_runtime())"
1.24.2
3.10.6 (main, Nov 14 2022, 16:10:14) [GCC 11.3.0]
[{'simd_extensions': {'baseline': [],
                      'found': ['VX', 'VXE'],
                      'not_found': ['VXE2']}}]
None

x86_64

% python -c "import sys, numpy; print(numpy.__version__); print(sys.version); print(numpy.show_runtime())"
1.24.2
3.10.6 (main, Mar 10 2023, 10:55:28) [GCC 11.3.0]
[{'simd_extensions': {'baseline': ['SSE', 'SSE2', 'SSE3'],
                      'found': ['SSSE3',
                                'SSE41',
                                'POPCNT',
                                'SSE42',
                                'AVX',
                                'F16C',
                                'FMA3',
                                'AVX2'],
                      'not_found': ['AVX512F',
                                    'AVX512CD',
                                    'AVX512_KNL',
                                    'AVX512_KNM',
                                    'AVX512_SKX',
                                    'AVX512_CLX',
                                    'AVX512_CNL',
                                    'AVX512_ICL']}},
 {'architecture': 'Haswell',
  'filepath': '/home/ishizaki/PyTorch/.pytorch-master-build/lib/python3.10/site-packages/numpy.libs/libopenblas64_p-r0-15028c96.3.21.so',
  'internal_api': 'openblas',
  'num_threads': 32,
  'prefix': 'libopenblas',
  'threading_layer': 'pthreads',
  'user_api': 'blas',
  'version': '0.3.21'}]
None

Context for the issue:

When I exec the above program, I got different results on two platforms x86_64 and s390x. I believe that s390x platform generates incorrect result.

on x86_64

[  0 254 255   0   0   0   1   2   0]

on s390x

<string>:1: RuntimeWarning: invalid value encountered in cast
[0 0 0 0 0 0 1 2 0]

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions