8 cudaSupport ? config.cudaSupport,
9 rocmSupport ? config.rocmSupport,
16 testCudaRuntime ? false,
17 testOpenclRuntime ? false,
18 testRocmRuntime ? false,
20 assert testCudaRuntime -> cudaSupport;
21 assert testRocmRuntime -> rocmSupport;
23 buildPythonPackage rec {
28 src = fetchFromGitHub {
31 rev = "refs/tags/${version}";
32 hash = "sha256-xUMvMBK1UhZaMZfik0Ia6+siyZGpCkBV+LTnQvzt/rw=";
37 src = ./0001-fix-dlopen-cuda.patch;
38 inherit (addDriverRunpath) driverLink;
41 "${lib.getLib cudaPackages.cuda_nvrtc}/lib/libnvrtc.so"
43 "Please import nixpkgs with `config.cudaSupport = true`";
47 nativeBuildInputs = [ setuptools ];
51 substituteInPlace gpuctypes/opencl.py \
52 --replace "ctypes.util.find_library('OpenCL')" "'${ocl-icd}/lib/libOpenCL.so'"
54 # hipGetDevicePropertiesR0600 is a symbol from rocm-6. We are currently at rocm-5.
55 # We are not sure that this works. Remove when rocm gets updated to version 6.
56 + lib.optionalString rocmSupport ''
57 substituteInPlace gpuctypes/hip.py \
58 --replace "/opt/rocm/lib/libamdhip64.so" "${rocmPackages.clr}/lib/libamdhip64.so" \
59 --replace "/opt/rocm/lib/libhiprtc.so" "${rocmPackages.clr}/lib/libhiprtc.so" \
60 --replace "hipGetDevicePropertiesR0600" "hipGetDeviceProperties"
62 substituteInPlace gpuctypes/comgr.py \
63 --replace "/opt/rocm/lib/libamd_comgr.so" "${rocmPackages.rocm-comgr}/lib/libamd_comgr.so"
66 pythonImportsCheck = [ "gpuctypes" ];
68 nativeCheckInputs = [ pytestCheckHook ];
71 lib.optionals (!testOpenclRuntime) [ "test/test_opencl.py" ]
72 ++ lib.optionals (!rocmSupport) [ "test/test_hip.py" ]
73 ++ lib.optionals (!cudaSupport) [ "test/test_cuda.py" ];
75 # Require GPU access to run (not available in the sandbox)
77 lib.optionals (!testCudaRuntime) [
79 "'not TestCUDADevice'"
81 ++ lib.optionals (!testRocmRuntime) [
85 ++ lib.optionals (testCudaRuntime || testOpenclRuntime || testRocmRuntime) [ "-v" ];
87 # Running these tests requires special configuration on the builder.
88 # e.g. https://github.com/NixOS/nixpkgs/pull/256230 implements a nix
89 # pre-build hook which exposes the devices and the drivers in the sandbox
90 # based on requiredSystemFeatures:
91 requiredSystemFeatures =
92 lib.optionals testCudaRuntime [ "cuda" ]
93 ++ lib.optionals testOpenclRuntime [ "opencl" ]
94 ++ lib.optionals testRocmRuntime [ "rocm" ];
96 passthru.gpuChecks = {
97 cuda = gpuctypes.override {
99 testCudaRuntime = true;
101 opencl = gpuctypes.override { testOpenclRuntime = true; };
102 rocm = gpuctypes.override {
104 testRocmRuntime = true;
108 preCheck = lib.optionalString (cudaSupport && !testCudaRuntime) ''
109 addToSearchPath LD_LIBRARY_PATH ${lib.getLib cudaPackages.cuda_cudart}/lib/stubs
112 # If neither rocmSupport or cudaSupport is enabled, no tests are selected
113 dontUsePytestCheck = !(rocmSupport || cudaSupport) && (!testOpenclRuntime);
116 description = "Ctypes wrappers for HIP, CUDA, and OpenCL";
117 homepage = "https://github.com/tinygrad/gpuctypes";
118 license = licenses.mit;
119 maintainers = with maintainers; [