8000 BUG: `numpy.clip` doesn't maintain dtype · Issue #24253 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

BUG: numpy.clip doesn't maintain dtype #24253

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
vovaf709 opened this issue Jul 24, 2023 · 3 comments
Closed

BUG: numpy.clip doesn't maintain dtype #24253

vovaf709 opened this issue Jul 24, 2023 · 3 comments
Labels

Comments

@vovaf709
Copy link

Describe the issue:

For some min-max values numpy.clip changes input dtype like in the example below

Reproduce the code example:

import numpy as np

x = np.ones(1, dtype=np.float16)

print(np.clip(x, 0, 255).dtype)  # np.float16
print(np.clip(x, 0, 256).dtype)  # np.float32

Error message:

No response

Runtime information:

1.25.1
3.9.5 (default, Jun 4 2021, 12:28:51)
[GCC 7.5.0]
[{'numpy_version': '1.25.1',
'python': '3.9.5 (default, Jun 4 2021, 12:28:51) \n[GCC 7.5.0]',
'uname': uname_result(system='Linux', node='skynet', release='5.19.0-46-generic', version='#47~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Wed Jun 21 15:35:31 UTC 2', machine='x86_64')},
{'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': 'Zen',
'filepath': '/home/vovaf709/miniconda3/lib/python3.9/site-packages/numpy.libs/libopenblas64_p-r0-7a851222.3.23.so',
'internal_api': 'openblas',
'num_threads': 12,
'prefix': 'libopenblas',
'threading_layer': 'pthreads',
'user_api': 'blas',
'version': '0.3.23'}]
None

Context for the issue:

No response

@mattip
Copy link
Member
mattip commented Jul 25, 2023

This will be fixed by #23912, which uses NEP 50 semantics.

@ngoldbaum
Copy link
Member

As a workaround, you can explicitly specify an out array with the same shape as x:

In [3]: o = np.ones(x.shape, dtype=np.float16)

In [4]: np.clip(x, 0, 256, out=o)
Out[4]: array([1.], dtype=float16)

In [5]: print(o.dtype)
float16

You can also set NPY_PROMOTION_STATE=weak before you start python to opt into the NEP 50 promotion semantics.

Closing since this is a known issue that will be fixed by switching to weak promotion by default.

@vovaf709
Copy link
Author

Nice, thx!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants
0