From 1c0a219ee21aa179de174968e71a290b35ae0954 Mon Sep 17 00:00:00 2001 From: Randy Eckenrode Date: Tue, 18 Jul 2023 18:44:45 -0600 Subject: [PATCH 1/4] python3Packages.numpy: fix build with clang 16 * Fix an invalid function pointer conversion; and * Disable strict overflow hardening due to an unused argument warning that is promoted to -Werror. --- pkgs/development/python-modules/numpy/default.nix | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/pkgs/development/python-modules/numpy/default.nix b/pkgs/development/python-modules/numpy/default.nix index b52d127f6cd0..43e862e6a1b1 100644 --- a/pkgs/development/python-modules/numpy/default.nix +++ b/pkgs/development/python-modules/numpy/default.nix @@ -1,5 +1,7 @@ { lib +, stdenv , fetchPypi +, fetchpatch , python , buildPythonPackage , gfortran @@ -54,6 +56,14 @@ in buildPythonPackage rec { # Patching of numpy.distutils is needed to prevent it from undoing the # patch to distutils. ./numpy-distutils-C++.patch + ] + ++ lib.optionals stdenv.cc.isClang [ + # f2py.f90mod_rules generates code with invalid function pointer conversions, which are + # clang 16 makes an error by default. + (fetchpatch { + url = "https://github.com/numpy/numpy/commit/609fee4324f3521d81a3454f5fcc33abb0d3761e.patch"; + hash = "sha256-6Dbmf/RWvQJPTIjvchVaywHGcKCsgap/0wAp5WswuCo="; + }) ]; postPatch = '' @@ -67,6 +77,9 @@ in buildPythonPackage rec { nativeBuildInputs = [ gfortran cython ]; buildInputs = [ blas lapack ]; + # Causes `error: argument unused during compilation: '-fno-strict-overflow'` due to `-Werror`. + hardeningDisable = lib.optionals stdenv.cc.isClang [ "strictoverflow" ]; + # we default openblas to build with 64 threads # if a machine has more than 64 threads, it will segfault # see https://github.com/xianyi/OpenBLAS/issues/2993 From 8e12c817e9e2401006d05a1ad5fc7b2d49938d73 Mon Sep 17 00:00:00 2001 From: Randy Eckenrode Date: Tue, 18 Jul 2023 18:33:38 -0600 Subject: [PATCH 2/4] python3Packages.numpy: fix test failure on aarch64-darwin --- pkgs/development/python-modules/numpy/default.nix | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/pkgs/development/python-modules/numpy/default.nix b/pkgs/development/python-modules/numpy/default.nix index 43e862e6a1b1..c73a1855e223 100644 --- a/pkgs/development/python-modules/numpy/default.nix +++ b/pkgs/development/python-modules/numpy/default.nix @@ -64,6 +64,14 @@ in buildPythonPackage rec { url = "https://github.com/numpy/numpy/commit/609fee4324f3521d81a3454f5fcc33abb0d3761e.patch"; hash = "sha256-6Dbmf/RWvQJPTIjvchVaywHGcKCsgap/0wAp5WswuCo="; }) + ] + ++ lib.optionals (stdenv.isDarwin && stdenv.isAarch64) [ + # Backport from 1.25. `platform.machine` returns `arm64` on aarch64-darwin, which causes + # differing results between `_selected_real_kind_func` and Fortran’s `selected_real_kind`. + (fetchpatch { + url = "https://github.com/numpy/numpy/commit/afcedf4b63f4a94187e6995c2adea0da3bb18e83.patch"; + hash = "sha256-cxBoimX5a9wC2qUIGAo5o/M2E9+eV63bV2/wLmfDYKg="; + }) ]; postPatch = '' From 4f923aa46c86a74b9b10a6f000e566f36e711e70 Mon Sep 17 00:00:00 2001 From: Randy Eckenrode Date: Tue, 18 Jul 2023 18:34:38 -0600 Subject: [PATCH 3/4] python3Packages.numpy: fix test failure on x86_64-darwin under Rosetta 2 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The `atanhl` function is broken under Rosetta 2 with 80-bit long doubles, which numpy uses to implement long double complex numbers. This results in a test failure. Attempts were made to change the implementation of things, but that just changed the breakage. The following Swift program demonstrates the problem. import Foundation import Numerics let x = Float80(1.00000000e-20) let z = Complex(x) print("X: \(x), Z: \(z)") let x_atanh = Float80.atanh(x) let z_atanh = Complex.atanh(z) print("atanh:") print("X: \(x_atanh), Z: \(z_atanh)") let d = abs(x_atanh / z_atanh.real - 1) print("d: \(d)") On x86_64-darwin hardware, it prints the following: X: 1e-20, Z: (1e-20, 0.0) atanh: X: 1e-20, Z: (1e-20, 0.0) d: 0.0 On aarch64-darwin under Rosetta 2, it prints the following: X: 1e-20, Z: (1e-20, 0.0) atanh: X: 1e-20, Z: (-1.0237493319595677839e-40, 0.0) d: 9.7680161420558978584e+19 The latter is obviously incorrect. FB12656897 was submitted to Apple, but even if this is fixed eventually, this derivation needs to build for users (and Hydra) who aren’t on the latest version. --- .../python-modules/numpy/default.nix | 6 +++++ ...e-failing-long-double-test-Rosetta-2.patch | 23 +++++++++++++++++++ 2 files changed, 29 insertions(+) create mode 100644 pkgs/development/python-modules/numpy/disable-failing-long-double-test-Rosetta-2.patch diff --git a/pkgs/development/python-modules/numpy/default.nix b/pkgs/development/python-modules/numpy/default.nix index c73a1855e223..f8bb6c2b2e65 100644 --- a/pkgs/development/python-modules/numpy/default.nix +++ b/pkgs/development/python-modules/numpy/default.nix @@ -72,6 +72,12 @@ in buildPythonPackage rec { url = "https://github.com/numpy/numpy/commit/afcedf4b63f4a94187e6995c2adea0da3bb18e83.patch"; hash = "sha256-cxBoimX5a9wC2qUIGAo5o/M2E9+eV63bV2/wLmfDYKg="; }) + ] + ++ lib.optionals (stdenv.isDarwin && stdenv.isx86_64) [ + # Disable `numpy/core/tests/test_umath.py::TestComplexFunctions::test_loss_of_precision[complex256]` + # on x86_64-darwin because it fails under Rosetta 2 due to issues with trig functions and + # 80-bit long double complex numbers. + ./disable-failing-long-double-test-Rosetta-2.patch ]; postPatch = '' diff --git a/pkgs/development/python-modules/numpy/disable-failing-long-double-test-Rosetta-2.patch b/pkgs/development/python-modules/numpy/disable-failing-long-double-test-Rosetta-2.patch new file mode 100644 index 000000000000..3e446745f58c --- /dev/null +++ b/pkgs/development/python-modules/numpy/disable-failing-long-double-test-Rosetta-2.patch @@ -0,0 +1,23 @@ +diff --git a/numpy/core/tests/test_umath.py b/numpy/core/tests/test_umath.py +index 6951d41e4..eefe86ad4 100644 +--- a/numpy/core/tests/test_umath.py ++++ b/numpy/core/tests/test_umath.py +@@ -4180,7 +4180,17 @@ def test_against_cmath(self): + ) + @pytest.mark.xfail(IS_MUSL, reason="gh23049") + @pytest.mark.xfail(IS_WASM, reason="doesn't work") +- @pytest.mark.parametrize('dtype', [np.complex64, np.complex_, np.longcomplex]) ++ @pytest.mark.parametrize('dtype', [ ++ np.complex64, ++ np.complex_, ++ pytest.param( ++ np.longcomplex, ++ marks=pytest.mark.skipif( ++ sys.platform == "darwin" and platform.machine() == "x86_64", ++ reason="doesn’t work under Rosetta 2", ++ ), ++ ), ++ ]) + def test_loss_of_precision(self, dtype): + """Check loss of precision in complex arc* functions""" + From 7ea07d2c0c919cb3b49de10e9134d15a449a8ba4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20=C4=8Cun=C3=A1t?= Date: Thu, 20 Jul 2023 09:30:53 +0200 Subject: [PATCH 4/4] python3Packages.numpy: temporarily avoid rebuild on !isClang Ugly but fortunately I'll revert it immediately. --- pkgs/development/python-modules/numpy/default.nix | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/pkgs/development/python-modules/numpy/default.nix b/pkgs/development/python-modules/numpy/default.nix index f8bb6c2b2e65..65a077392ff9 100644 --- a/pkgs/development/python-modules/numpy/default.nix +++ b/pkgs/development/python-modules/numpy/default.nix @@ -39,7 +39,7 @@ let }; }; }; -in buildPythonPackage rec { +in buildPythonPackage (rec { pname = "numpy"; version = "1.24.2"; format = "setuptools"; @@ -91,9 +91,6 @@ in buildPythonPackage rec { nativeBuildInputs = [ gfortran cython ]; buildInputs = [ blas lapack ]; - # Causes `error: argument unused during compilation: '-fno-strict-overflow'` due to `-Werror`. - hardeningDisable = lib.optionals stdenv.cc.isClang [ "strictoverflow" ]; - # we default openblas to build with 64 threads # if a machine has more than 64 threads, it will segfault # see https://github.com/xianyi/OpenBLAS/issues/2993 @@ -140,4 +137,7 @@ in buildPythonPackage rec { license = lib.licenses.bsd3; maintainers = with lib.maintainers; [ fridh ]; }; -} +} // lib.optionalAttrs stdenv.cc.isClang { + # Causes `error: argument unused during compilation: '-fno-strict-overflow'` due to `-Werror`. + hardeningDisable = [ "strictoverflow" ]; +})