From 870f58ffa94d8dd44246351aec220c77399ed4f8 Mon Sep 17 00:00:00 2001 From: matthewcroughan Date: Tue, 2 Jan 2024 04:19:11 +0000 Subject: [PATCH 1/3] python3Packages.gpuctypes: init at unstable-2023-11-26 --- .../gpuctypes/0001-fix-dlopen-cuda.patch | 44 +++++++++ .../python-modules/gpuctypes/default.nix | 91 +++++++++++++++++++ .../gpuctypes/fix-dlopen-cuda.patch | 32 +++++++ pkgs/top-level/python-packages.nix | 2 + 4 files changed, 169 insertions(+) create mode 100644 pkgs/development/python-modules/gpuctypes/0001-fix-dlopen-cuda.patch create mode 100644 pkgs/development/python-modules/gpuctypes/default.nix create mode 100644 pkgs/development/python-modules/gpuctypes/fix-dlopen-cuda.patch diff --git a/pkgs/development/python-modules/gpuctypes/0001-fix-dlopen-cuda.patch b/pkgs/development/python-modules/gpuctypes/0001-fix-dlopen-cuda.patch new file mode 100644 index 000000000000..bc9f6c7ec64b --- /dev/null +++ b/pkgs/development/python-modules/gpuctypes/0001-fix-dlopen-cuda.patch @@ -0,0 +1,44 @@ +From d448321436e8314d3e2a6a09d4017c4bc10f612d Mon Sep 17 00:00:00 2001 +From: Gaetan Lepage +Date: Sat, 17 Feb 2024 17:37:22 +0100 +Subject: [PATCH] fix-dlopen-cuda + +--- + gpuctypes/cuda.py | 20 ++++++++++++++++++-- + 1 file changed, 18 insertions(+), 2 deletions(-) + +diff --git a/gpuctypes/cuda.py b/gpuctypes/cuda.py +index acba81c..091f7f7 100644 +--- a/gpuctypes/cuda.py ++++ b/gpuctypes/cuda.py +@@ -143,9 +143,25 @@ def char_pointer_cast(string, encoding='utf-8'): + + + ++NAME_TO_PATHS = { ++ "libcuda.so": ["@driverLink@/lib/libcuda.so"], ++ "libnvrtc.so": ["@libnvrtc@"], ++} ++def _try_dlopen(name): ++ try: ++ return ctypes.CDLL(name) ++ except OSError: ++ pass ++ for candidate in NAME_TO_PATHS.get(name, []): ++ try: ++ return ctypes.CDLL(candidate) ++ except OSError: ++ pass ++ raise RuntimeError(f"{name} not found") ++ + _libraries = {} +-_libraries['libcuda.so'] = ctypes.CDLL(ctypes.util.find_library('cuda')) +-_libraries['libnvrtc.so'] = ctypes.CDLL(ctypes.util.find_library('nvrtc')) ++_libraries['libcuda.so'] = _try_dlopen('libcuda.so') ++_libraries['libnvrtc.so'] = _try_dlopen('libnvrtc.so') + + + cuuint32_t = ctypes.c_uint32 +-- +2.43.0 + diff --git a/pkgs/development/python-modules/gpuctypes/default.nix b/pkgs/development/python-modules/gpuctypes/default.nix new file mode 100644 index 000000000000..4b1d8560fa4d --- /dev/null +++ b/pkgs/development/python-modules/gpuctypes/default.nix @@ -0,0 +1,91 @@ +{ lib +, config +, buildPythonPackage +, fetchFromGitHub +, substituteAll +, addDriverRunpath +, cudaSupport ? config.cudaSupport +, rocmSupport ? config.rocmSupport +, cudaPackages +, setuptools +, ocl-icd +, rocmPackages +, pytestCheckHook +}: +buildPythonPackage rec { + pname = "gpuctypes"; + version = "0.3.0"; + pyproject = true; + + src = fetchFromGitHub { + repo = "gpuctypes"; + owner = "tinygrad"; + rev = "refs/tags/${version}"; + hash = "sha256-xUMvMBK1UhZaMZfik0Ia6+siyZGpCkBV+LTnQvzt/rw="; + }; + + patches = [ + (substituteAll { + src = ./0001-fix-dlopen-cuda.patch; + inherit (addDriverRunpath) driverLink; + libnvrtc = + if cudaSupport + then "${lib.getLib cudaPackages.cuda_nvrtc}/lib/libnvrtc.so" + else "Please import nixpkgs with `config.cudaSupport = true`"; + }) + ]; + + nativeBuildInputs = [ + setuptools + ]; + + postPatch = '' + substituteInPlace gpuctypes/opencl.py \ + --replace "ctypes.util.find_library('OpenCL')" "'${ocl-icd}/lib/libOpenCL.so'" + '' + # hipGetDevicePropertiesR0600 is a symbol from rocm-6. We are currently at rocm-5. + # We are not sure that this works. Remove when rocm gets updated to version 6. + + lib.optionalString rocmSupport '' + substituteInPlace gpuctypes/hip.py \ + --replace "/opt/rocm/lib/libamdhip64.so" "${rocmPackages.clr}/lib/libamdhip64.so" \ + --replace "/opt/rocm/lib/libhiprtc.so" "${rocmPackages.clr}/lib/libhiprtc.so" \ + --replace "hipGetDevicePropertiesR0600" "hipGetDeviceProperties" + + substituteInPlace gpuctypes/comgr.py \ + --replace "/opt/rocm/lib/libamd_comgr.so" "${rocmPackages.rocm-comgr}/lib/libamd_comgr.so" + ''; + + pythonImportsCheck = [ "gpuctypes" ]; + + nativeCheckInputs = [ + pytestCheckHook + ]; + + disabledTestPaths = [ + "test/test_opencl.py" + ] ++ lib.optionals (!rocmSupport) [ + "test/test_hip.py" + ] ++ lib.optionals (!cudaSupport) [ + "test/test_cuda.py" + ]; + + # Require GPU access to run (not available in the sandbox) + pytestFlagsArray = [ + "-k" "'not TestCUDADevice'" + "-k" "'not TestHIPDevice'" + ]; + + preCheck = lib.optionalString cudaSupport '' + addToSearchPath LD_LIBRARY_PATH ${lib.getLib cudaPackages.cuda_cudart}/lib/stubs + ''; + + # If neither rocmSupport or cudaSupport is enabled, no tests are selected + dontUsePytestCheck = !(rocmSupport || cudaSupport); + + meta = with lib; { + description = "Ctypes wrappers for HIP, CUDA, and OpenCL"; + homepage = "https://github.com/tinygrad/gpuctypes"; + license = licenses.mit; + maintainers = with maintainers; [ GaetanLepage matthewcroughan wozeparrot ]; + }; +} diff --git a/pkgs/development/python-modules/gpuctypes/fix-dlopen-cuda.patch b/pkgs/development/python-modules/gpuctypes/fix-dlopen-cuda.patch new file mode 100644 index 000000000000..8d3b69e35e11 --- /dev/null +++ b/pkgs/development/python-modules/gpuctypes/fix-dlopen-cuda.patch @@ -0,0 +1,32 @@ +diff --git a/gpuctypes/cuda.py b/gpuctypes/cuda.py +index acba81c..aac5fc7 100644 +--- a/gpuctypes/cuda.py ++++ b/gpuctypes/cuda.py +@@ -143,9 +143,25 @@ def char_pointer_cast(string, encoding='utf-8'): + + + ++NAME_TO_PATHS = { ++ "libcuda.so": ["@driverLink@/lib/libcuda.so"], ++ "libnvrtc.so": ["@libnvrtc@"], ++} ++def _try_dlopen(name): ++ try: ++ return ctypes.CDLL(name) ++ except OSError: ++ pass ++ for candidate in NAME_TO_PATHS.get(name, []): ++ try: ++ return ctypes.CDLL(candidate) ++ except OSError: ++ pass ++ raise RuntimeError(f"{name} not found") ++ + _libraries = {} +-_libraries['libcuda.so'] = ctypes.CDLL(ctypes.util.find_library('cuda')) +-_libraries['libnvrtc.so'] = ctypes.CDLL(ctypes.util.find_library('nvrtc')) ++_libraries['libcuda.so'] = _try_dlopen('libcuda.so') ++_libraries['libnvrtc.so'] = _try_dlopen('libnvrtc.so') + + + cuuint32_t = ctypes.c_uint32 diff --git a/pkgs/top-level/python-packages.nix b/pkgs/top-level/python-packages.nix index b90813500e44..1da6f92cce85 100644 --- a/pkgs/top-level/python-packages.nix +++ b/pkgs/top-level/python-packages.nix @@ -4933,6 +4933,8 @@ self: super: with self; { gpsoauth = callPackage ../development/python-modules/gpsoauth { }; + gpuctypes = callPackage ../development/python-modules/gpuctypes { }; + gpustat = callPackage ../development/python-modules/gpustat { }; gpxpy = callPackage ../development/python-modules/gpxpy { }; From b7bcf94a708ce799a8586ce6255f80a27615731c Mon Sep 17 00:00:00 2001 From: Gaetan Lepage Date: Wed, 21 Feb 2024 00:15:11 +0100 Subject: [PATCH 2/3] python311Packages.gpuctypes: add gpu tests Co-authored-by: SomeoneSerge --- .../python-modules/gpuctypes/default.nix | 44 +++++++++++++++++-- 1 file changed, 40 insertions(+), 4 deletions(-) diff --git a/pkgs/development/python-modules/gpuctypes/default.nix b/pkgs/development/python-modules/gpuctypes/default.nix index 4b1d8560fa4d..e5987f78ee07 100644 --- a/pkgs/development/python-modules/gpuctypes/default.nix +++ b/pkgs/development/python-modules/gpuctypes/default.nix @@ -11,7 +11,14 @@ , ocl-icd , rocmPackages , pytestCheckHook +, gpuctypes +, testCudaRuntime ? false +, testOpenclRuntime ? false +, testRocmRuntime ? false }: +assert testCudaRuntime -> cudaSupport; +assert testRocmRuntime -> rocmSupport; + buildPythonPackage rec { pname = "gpuctypes"; version = "0.3.0"; @@ -61,7 +68,7 @@ buildPythonPackage rec { pytestCheckHook ]; - disabledTestPaths = [ + disabledTestPaths = lib.optionals (!testOpenclRuntime) [ "test/test_opencl.py" ] ++ lib.optionals (!rocmSupport) [ "test/test_hip.py" @@ -70,17 +77,46 @@ buildPythonPackage rec { ]; # Require GPU access to run (not available in the sandbox) - pytestFlagsArray = [ + pytestFlagsArray = lib.optionals (!testCudaRuntime) [ "-k" "'not TestCUDADevice'" + ] ++ lib.optionals (!testRocmRuntime) [ "-k" "'not TestHIPDevice'" + ] ++ lib.optionals (testCudaRuntime || testOpenclRuntime || testRocmRuntime) [ + "-v" ]; - preCheck = lib.optionalString cudaSupport '' + # Running these tests requires special configuration on the builder. + # e.g. https://github.com/NixOS/nixpkgs/pull/256230 implements a nix + # pre-build hook which exposes the devices and the drivers in the sandbox + # based on requiredSystemFeatures: + requiredSystemFeatures = lib.optionals testCudaRuntime [ + "cuda" + ] ++ lib.optionals testOpenclRuntime [ + "opencl" + ] ++ lib.optionals testRocmRuntime [ + "rocm" + ]; + + passthru.gpuChecks = { + cuda = gpuctypes.override { + cudaSupport = true; + testCudaRuntime = true; + }; + opencl = gpuctypes.override { + testOpenclRuntime = true; + }; + rocm = gpuctypes.override { + rocmSupport = true; + testRocmRuntime = true; + }; + }; + + preCheck = lib.optionalString (cudaSupport && !testCudaRuntime) '' addToSearchPath LD_LIBRARY_PATH ${lib.getLib cudaPackages.cuda_cudart}/lib/stubs ''; # If neither rocmSupport or cudaSupport is enabled, no tests are selected - dontUsePytestCheck = !(rocmSupport || cudaSupport); + dontUsePytestCheck = !(rocmSupport || cudaSupport) && (!testOpenclRuntime); meta = with lib; { description = "Ctypes wrappers for HIP, CUDA, and OpenCL"; From 24833cd162babe2c4d50b910d439d714fa8897d2 Mon Sep 17 00:00:00 2001 From: Gaetan Lepage Date: Sun, 11 Feb 2024 02:35:10 +0100 Subject: [PATCH 3/3] python3Packages.tinygrad: init at 0.8.0 Co-authored-by: matthewcroughan --- .../python-modules/tinygrad/default.nix | 111 ++++++++++++++++++ pkgs/top-level/python-packages.nix | 2 + 2 files changed, 113 insertions(+) create mode 100644 pkgs/development/python-modules/tinygrad/default.nix diff --git a/pkgs/development/python-modules/tinygrad/default.nix b/pkgs/development/python-modules/tinygrad/default.nix new file mode 100644 index 000000000000..25b1f049bf69 --- /dev/null +++ b/pkgs/development/python-modules/tinygrad/default.nix @@ -0,0 +1,111 @@ +{ lib +, buildPythonPackage +, fetchFromGitHub +, setuptools +, wheel +, gpuctypes +, numpy +, tqdm +, hypothesis +, librosa +, onnx +, pillow +, pytest-xdist +, pytestCheckHook +, safetensors +, sentencepiece +, tiktoken +, torch +, transformers +}: + +buildPythonPackage rec { + pname = "tinygrad"; + version = "0.8.0"; + pyproject = true; + + src = fetchFromGitHub { + owner = "tinygrad"; + repo = "tinygrad"; + rev = "refs/tags/v${version}"; + hash = "sha256-QAccZ79qUbe27yUykIf22WdkxYUlOffnMlShakKfp60="; + }; + + nativeBuildInputs = [ + setuptools + wheel + ]; + + propagatedBuildInputs = [ + gpuctypes + numpy + tqdm + ]; + + pythonImportsCheck = [ "tinygrad" ]; + + nativeCheckInputs = [ + hypothesis + librosa + onnx + pillow + pytest-xdist + pytestCheckHook + safetensors + sentencepiece + tiktoken + torch + transformers + ]; + + preCheck = '' + export HOME=$(mktemp -d) + ''; + + disabledTests = [ + # Require internet access + "test_benchmark_openpilot_model" + "test_bn_alone" + "test_bn_linear" + "test_bn_mnist" + "test_car" + "test_chicken" + "test_chicken_bigbatch" + "test_conv_mnist" + "testCopySHMtoDefault" + "test_data_parallel_resnet" + "test_e2e_big" + "test_fetch_small" + "test_huggingface_enet_safetensors" + "test_linear_mnist" + "test_load_convnext" + "test_load_enet" + "test_load_enet_alt" + "test_load_llama2bfloat" + "test_load_resnet" + "test_openpilot_model" + "test_resnet" + "test_shufflenet" + "test_transcribe_batch12" + "test_transcribe_batch21" + "test_transcribe_file1" + "test_transcribe_file2" + "test_transcribe_long" + "test_transcribe_long_no_batch" + "test_vgg7" + ]; + + disabledTestPaths = [ + "test/extra/test_lr_scheduler.py" + "test/models/test_mnist.py" + "test/models/test_real_world.py" + ]; + + meta = with lib; { + description = "A simple and powerful neural network framework"; + homepage = "https://github.com/tinygrad/tinygrad"; + changelog = "https://github.com/tinygrad/tinygrad/releases/tag/v${version}"; + license = licenses.mit; + maintainers = with maintainers; [ GaetanLepage ]; + }; +} diff --git a/pkgs/top-level/python-packages.nix b/pkgs/top-level/python-packages.nix index 1da6f92cce85..8bcdbe939dc9 100644 --- a/pkgs/top-level/python-packages.nix +++ b/pkgs/top-level/python-packages.nix @@ -14761,6 +14761,8 @@ self: super: with self; { tinydb = callPackage ../development/python-modules/tinydb { }; + tinygrad = callPackage ../development/python-modules/tinygrad { }; + tinyobjloader-py = callPackage ../development/python-modules/tinyobjloader-py { }; tinyrecord = callPackage ../development/python-modules/tinyrecord { };