summaryrefslogtreecommitdiff
path: root/pkgs/development/python-modules/tinygrad/default.nix
blob: 56c29e669db57a9696d79fd8ec779bfc45080c36 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
{
  lib,
  stdenv,
  config,
  buildPythonPackage,
  fetchFromGitHub,

  # patches
  replaceVars,
  addDriverRunpath,
  cudaPackages,
  llvmPackages,
  ocl-icd,
  rocmPackages,

  # build-system
  setuptools,

  # optional-dependencies
  # arm
  unicorn,
  # triton
  triton,
  # testing_minimal
  hypothesis,
  numpy,
  pytest-xdist,
  torch,
  z3-solver,
  # testing_unit
  ggml-python,
  openai,
  safetensors,
  tabulate,
  tqdm,
  # testing
  blobfile,
  boto3,
  bottle,
  capstone,
  librosa,
  networkx,
  nibabel,
  onnx,
  onnxruntime,
  opencv4,
  pandas,
  pillow,
  pycocotools,
  sentencepiece,
  tiktoken,
  transformers,

  # tests
  pytestCheckHook,
  writableTmpDirAsHomeHook,

  # passthru
  tinygrad,

  cudaSupport ? config.cudaSupport,
  rocmSupport ? config.rocmSupport,
}:

buildPythonPackage (finalAttrs: {
  pname = "tinygrad";
  version = "0.12.0";
  pyproject = true;

  src = fetchFromGitHub {
    owner = "tinygrad";
    repo = "tinygrad";
    tag = "v${finalAttrs.version}";
    hash = "sha256-Lied19C1sAbislr2WznnCZEmOn5PA0OzMg2KOdWOYkA=";
  };

  patches =
    let
      libExtension = stdenv.hostPlatform.extensions.sharedLibrary;
    in
    [
      (replaceVars ./patch-deps-paths.patch {
        libllvm = "${lib.getLib llvmPackages.llvm}/lib/libLLVM${libExtension}";
        libclang = "${lib.getLib llvmPackages.libclang}/lib/libclang${libExtension}";

        # Use the unwrapped variant to enable the "native" features currently unavailable in the sandbox
        clang = lib.getExe llvmPackages.clang-unwrapped;
      })
    ]
    ++ lib.optionals cudaSupport [
      (replaceVars ./patch-cuda-paths.patch {
        inherit (addDriverRunpath) driverLink;
        cuda_nvrtc = lib.getLib cudaPackages.cuda_nvrtc;

        # `cuda_fp16.h` and co. are needed at runtime to compile kernels
        cuda_cudart = lib.getInclude cudaPackages.cuda_cudart;
      })
    ]
    ++ lib.optionals rocmSupport [
      (replaceVars ./patch-rocm-paths.patch {
        comgr = lib.getLib rocmPackages.rocm-comgr;
        clr = lib.getLib rocmPackages.clr;
        rocm-runtime = lib.getLib rocmPackages.rocm-runtime;
        rocm-llvm-objdump = lib.getExe' rocmPackages.llvm.llvm "llvm-objdump";
        llvm-objdump = lib.getExe' llvmPackages.llvm "llvm-objdump";
        hipcc = lib.getExe' rocmPackages.hipcc "hipcc";
      })
    ];

  postPatch = lib.optionalString stdenv.hostPlatform.isLinux ''
    substituteInPlace tinygrad/runtime/autogen/opencl.py \
      --replace-fail \
        "dll = DLL('opencl', 'OpenCL')" \
        "dll = DLL('opencl', '${lib.getLib ocl-icd}/lib/libOpenCL.so')"

    substituteInPlace tinygrad/runtime/autogen/libc.py \
      --replace-fail \
        "dll = DLL('libc', 'c', use_errno=True)" \
        "dll = DLL('libc', '${lib.getLib stdenv.cc.libc}/lib/libc.so.6', use_errno=True)"
  '';

  __propagatedImpureHostDeps = lib.optional stdenv.hostPlatform.isDarwin "/usr/lib/libc.dylib";

  build-system = [ setuptools ];

  optional-dependencies = lib.fix (self: {
    arm = [ unicorn ];
    triton = [ triton ];
    testing_minimal = [
      hypothesis
      numpy
      pytest-xdist
      torch
      z3-solver
    ];
    testing_unit = self.testing_minimal ++ [
      ggml-python
      openai
      safetensors
      tabulate
      tqdm
    ];
    testing = self.testing_unit ++ [
      blobfile
      boto3
      bottle
      capstone
      librosa
      networkx
      nibabel
      onnx
      onnxruntime
      opencv4
      pandas
      pillow
      pycocotools
      sentencepiece
      tiktoken
      transformers
    ];
  });

  pythonImportsCheck = [
    "tinygrad"
    "tinygrad.runtime.autogen.libclang"
  ]
  ++ lib.optionals cudaSupport [
    "tinygrad.runtime.ops_cuda"
    "tinygrad.runtime.ops_nv"
  ]
  ++ lib.optionals rocmSupport [
    "tinygrad.runtime.ops_amd"
    "tinygrad.runtime.ops_hip"
  ]
  ++ lib.optionals stdenv.hostPlatform.isDarwin [
    "tinygrad.runtime.ops_metal"
  ];

  nativeCheckInputs = [
    llvmPackages.clang
    pytestCheckHook
    writableTmpDirAsHomeHook
  ]
  ++ finalAttrs.passthru.optional-dependencies.testing;

  disabledTests = [
    # RuntimeError: Attempting to relocate against an undefined symbol 'fmaxf'
    "test_backward_sum_acc_dtype"
    "test_failure_27"

    # Flaky:
    # AssertionError: 2.1376906810000946 not less than 2.0
    "test_recursive_pad"

    # Require internet access
    "testCopySHMtoDefault"
    "test_benchmark_openpilot_model"
    "test_bn_alone"
    "test_bn_linear"
    "test_bn_mnist"
    "test_car"
    "test_chicken"
    "test_chicken_bigbatch"
    "test_conv_mnist"
    "test_data_parallel_resnet"
    "test_dataset_is_realized"
    "test_e2e_big"
    "test_fetch_small"
    "test_hevc_parser"
    "test_huggingface_enet_safetensors"
    "test_index_mnist"
    "test_linear_mnist"
    "test_llama_basic"
    "test_llama_bytes"
    "test_llama_control_char"
    "test_llama_early_tokenize"
    "test_llama_pat"
    "test_llama_repeat"
    "test_llama_special1"
    "test_llama_special2"
    "test_load_convnext"
    "test_load_enet"
    "test_load_enet_alt"
    "test_load_gpt2_q4_1"
    "test_load_llama2bfloat"
    "test_load_resnet"
    "test_load_sample_mxfp4"
    "test_load_sample_q6_k"
    "test_load_tinyllama_q4_0"
    "test_load_tinyllama_q8_0"
    "test_mnist_val"
    "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"
  ]
  ++ lib.optionals (stdenv.hostPlatform.isLinux && stdenv.hostPlatform.isAarch64) [
    # Fail with AssertionError
    "test_casts_from"
    "test_casts_to"
    "test_float_cast_to_unsigned_underflow"
    "test_int8"
    "test_int8_to_uint16_negative"
  ]
  ++ lib.optionals stdenv.hostPlatform.isDarwin [
    # Flaky (pass when running a smaller set of tests: tests/unit/*, but not within the full test suite)
    # AttributeError: module 'tinygrad.runtime.autogen.libclang' has no attribute 'clang_parseTranslationUnit'
    "test_gen_from_header"
    "test_struct_ordering"
  ];

  disabledTestPaths = [
    # Require internet access
    "test/models/test_mnist.py"
    "test/models/test_real_world.py"
    "test/testextra/test_lr_scheduler.py"

    # Files under this directory are not considered as tests by upstream and should be skipped
    "extra/"
  ]
  ++ lib.optionals stdenv.hostPlatform.isDarwin [
    # urllib.error.URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED]
    # certificate verify failed: self-signed certificate in certificate chain (_ssl.c:1032)>
    "test/models/test_whisper.py"
  ];

  __darwinAllowLocalNetworking = true;

  passthru = {
    tests = {
      withCuda = tinygrad.override { cudaSupport = true; };
      withRocm = tinygrad.override { rocmSupport = true; };
    };

    gpuCheck = tinygrad.overridePythonAttrs (old: {
      requiredSystemFeatures = [ "cuda" ];

      pytestFlags = (old.pytestFlags or [ ]) ++ [
        # When running in parallel, with GPU support, some tests become flaky:
        # RuntimeError: Wait timeout: 30000 ms! (the signal is not set to 153, but 151)
        "--maxprocesses=1"
      ];
    });
  };

  meta = {
    description = "Simple and powerful neural network framework";
    homepage = "https://github.com/tinygrad/tinygrad";
    changelog = "https://github.com/tinygrad/tinygrad/releases/tag/${finalAttrs.src.tag}";
    license = lib.licenses.mit;
    maintainers = with lib.maintainers; [ GaetanLepage ];
  };
})