mlx-sys 0.7.0

Low-level interface and binding generation for the mlx library
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
def __implement_mlx_fast_custom_kernel(backend, backend_specific_code, implementation):
    if implementation:
        code_config = """
struct mlx_fast_custom_kernel_config_cpp_ {
  std::vector<mlx::core::Shape> output_shapes;
  std::vector<mlx::core::Dtype> output_dtypes;
  std::tuple<int, int, int> grid;
  std::tuple<int, int, int> thread_group;
  std::vector<std::pair<std::string, mlx::core::fast::TemplateArg>>
      template_args;
  std::optional<float> init_value;
  bool verbose;
};

inline mlx_fast_custom_kernel_config mlx_fast_custom_kernel_config_new_() {
  return mlx_fast_custom_kernel_config(
      {new mlx_fast_custom_kernel_config_cpp_()});
}

inline mlx_fast_custom_kernel_config_cpp_& mlx_fast_custom_kernel_config_get_(
    mlx_fast_custom_kernel_config d) {
  if (!d.ctx) {
    throw std::runtime_error(
        "expected a non-empty mlx_fast_custom_kernel_config");
  }
  return *static_cast<mlx_fast_custom_kernel_config_cpp_*>(d.ctx);
}

inline void mlx_fast_custom_kernel_config_free_(mlx_fast_custom_kernel_config d) {
  if (d.ctx) {
    delete static_cast<mlx_fast_custom_kernel_config_cpp_*>(d.ctx);
  }
}

extern "C" mlx_fast_custom_kernel_config mlx_fast_custom_kernel_config_new(void) {
  try {
    return mlx_fast_custom_kernel_config_new_();
  } catch (std::exception& e) {
    mlx_error(e.what());
  }
  return {nullptr};
}

extern "C" void mlx_fast_custom_kernel_config_free(
    mlx_fast_custom_kernel_config cls) {
  mlx_fast_custom_kernel_config_free_(cls);
}

extern "C" int mlx_fast_custom_kernel_config_add_output_arg(
    mlx_fast_custom_kernel_config cls,
    const int* shape,
    size_t size,
    mlx_dtype dtype) {
  try {
    mlx_fast_custom_kernel_config_get_(cls).output_shapes.push_back(
        mlx::core::Shape(shape, shape + size));
    mlx_fast_custom_kernel_config_get_(cls).output_dtypes.push_back(
        mlx_dtype_to_cpp(dtype));
  } catch (std::exception& e) {
    mlx_error(e.what());
    return 1;
  }
  return 0;
}
extern "C" int mlx_fast_custom_kernel_config_set_grid(
    mlx_fast_custom_kernel_config cls,
    int grid1,
    int grid2,
    int grid3) {
  try {
    mlx_fast_custom_kernel_config_get_(cls).grid =
        std::make_tuple(grid1, grid2, grid3);
  } catch (std::exception& e) {
    mlx_error(e.what());
    return 1;
  }
  return 0;
}
extern "C" int mlx_fast_custom_kernel_config_set_thread_group(
    mlx_fast_custom_kernel_config cls,
    int thread1,
    int thread2,
    int thread3) {
  try {
    mlx_fast_custom_kernel_config_get_(cls).thread_group =
        std::make_tuple(thread1, thread2, thread3);
  } catch (std::exception& e) {
    mlx_error(e.what());
    return 1;
  }
  return 0;
}
extern "C" int mlx_fast_custom_kernel_config_set_init_value(
    mlx_fast_custom_kernel_config cls,
    float value) {
  try {
    mlx_fast_custom_kernel_config_get_(cls).init_value = value;
  } catch (std::exception& e) {
    mlx_error(e.what());
    return 1;
  }
  return 0;
}
extern "C" int mlx_fast_custom_kernel_config_set_verbose(
    mlx_fast_custom_kernel_config cls,
    bool verbose) {
  try {
    mlx_fast_custom_kernel_config_get_(cls).verbose = verbose;
  } catch (std::exception& e) {
    mlx_error(e.what());
    return 1;
  }
  return 0;
}
extern "C" int mlx_fast_custom_kernel_config_add_template_arg_dtype(
    mlx_fast_custom_kernel_config cls,
    const char* name,
    mlx_dtype dtype) {
  try {
    mlx_fast_custom_kernel_config_get_(cls).template_args.push_back(
        std::make_pair(std::string(name), mlx_dtype_to_cpp(dtype)));
  } catch (std::exception& e) {
    mlx_error(e.what());
    return 1;
  }
  return 0;
}
extern "C" int mlx_fast_custom_kernel_config_add_template_arg_int(
    mlx_fast_custom_kernel_config cls,
    const char* name,
    int value) {
  try {
    mlx_fast_custom_kernel_config_get_(cls).template_args.push_back(
        std::make_pair(std::string(name), value));
  } catch (std::exception& e) {
    mlx_error(e.what());
    return 1;
  }
  return 0;
}
extern "C" int mlx_fast_custom_kernel_config_add_template_arg_bool(
    mlx_fast_custom_kernel_config cls,
    const char* name,
    bool value) {
  try {
    mlx_fast_custom_kernel_config_get_(cls).template_args.push_back(
        std::make_pair(std::string(name), value));
  } catch (std::exception& e) {
    mlx_error(e.what());
    return 1;
  }
  return 0;
}
        """
        code_def = """
struct mlx_fast_custom_kernel_cpp_ {
  mlx::core::fast::CustomKernelFunction mkf;
  mlx_fast_custom_kernel_cpp_(mlx::core::fast::CustomKernelFunction mkf)
      : mkf(mkf) {};
};
        """

        code = """
inline mlx::core::fast::CustomKernelFunction& mlx_fast_custom_kernel_get_(
    mlx_fast_custom_kernel d) {
  if (!d.ctx) {
    throw std::runtime_error("expected a non-empty mlx_fast_custom_kernel");
  }
  return static_cast<mlx_fast_custom_kernel_cpp_*>(d.ctx)->mkf;
}

inline void mlx_fast_custom_kernel_free_(mlx_fast_custom_kernel d) {
  if (d.ctx) {
    delete static_cast<mlx_fast_custom_kernel_cpp_*>(d.ctx);
  }
}

extern "C" void mlx_fast_custom_kernel_free(mlx_fast_custom_kernel cls) {
  mlx_fast_custom_kernel_free_(cls);
}

extern "C" int mlx_fast_custom_kernel_apply(
    mlx_vector_array* outputs,
    mlx_fast_custom_kernel cls,
    const mlx_vector_array inputs,
    const mlx_fast_custom_kernel_config config,
    const mlx_stream stream) {
  try {
    auto config_ctx = mlx_fast_custom_kernel_config_get_(config);
    mlx_vector_array_set_(
        *outputs,
        mlx_fast_custom_kernel_get_(cls)(
            mlx_vector_array_get_(inputs),
            config_ctx.output_shapes,
            config_ctx.output_dtypes,
            config_ctx.grid,
            config_ctx.thread_group,
            config_ctx.template_args,
            config_ctx.init_value,
            config_ctx.verbose,
            mlx_stream_get_(stream)));
  } catch (std::exception& e) {
    mlx_error(e.what());
    return 1;
  }
  return 0;
}
        """
    else:
        code_config = """
typedef struct mlx_fast_custom_kernel_config_ {
  void* ctx;
} mlx_fast_custom_kernel_config;
mlx_fast_custom_kernel_config mlx_fast_custom_kernel_config_new(void);
void mlx_fast_custom_kernel_config_free(mlx_fast_custom_kernel_config cls);

int mlx_fast_custom_kernel_config_add_output_arg(
    mlx_fast_custom_kernel_config cls,
    const int* shape,
    size_t size,
    mlx_dtype dtype);
int mlx_fast_custom_kernel_config_set_grid(
    mlx_fast_custom_kernel_config cls,
    int grid1,
    int grid2,
    int grid3);
int mlx_fast_custom_kernel_config_set_thread_group(
    mlx_fast_custom_kernel_config cls,
    int thread1,
    int thread2,
    int thread3);
int mlx_fast_custom_kernel_config_set_init_value(
    mlx_fast_custom_kernel_config cls,
    float value);
int mlx_fast_custom_kernel_config_set_verbose(
    mlx_fast_custom_kernel_config cls,
    bool verbose);
int mlx_fast_custom_kernel_config_add_template_arg_dtype(
    mlx_fast_custom_kernel_config cls,
    const char* name,
    mlx_dtype dtype);
int mlx_fast_custom_kernel_config_add_template_arg_int(
    mlx_fast_custom_kernel_config cls,
    const char* name,
    int value);
int mlx_fast_custom_kernel_config_add_template_arg_bool(
    mlx_fast_custom_kernel_config cls,
    const char* name,
    bool value);
        """

        code_def = """
typedef struct mlx_fast_custom_kernel_ {
  void* ctx;
} mlx_fast_custom_kernel;
        """

        code = """
void mlx_fast_custom_kernel_free(mlx_fast_custom_kernel cls);

int mlx_fast_custom_kernel_apply(
    mlx_vector_array* outputs,
    mlx_fast_custom_kernel cls,
    const mlx_vector_array inputs,
    const mlx_fast_custom_kernel_config config,
    const mlx_stream stream);
        """

    code_config = code_config.replace("custom", backend)
    code_def = code_def.replace("custom", backend)
    code = code.replace("custom", backend)

    print(code_config)
    print(code_def)
    print(backend_specific_code)
    print(code)


def mlx_fast_cuda_kernel(f, implementation):
    if implementation:
        custom_code = """
inline mlx_fast_cuda_kernel mlx_fast_cuda_kernel_new_(
    const std::string& name,
    const std::vector<std::string>& input_names,
    const std::vector<std::string>& output_names,
    const std::string& source,
    const std::string& header,
    bool ensure_row_contiguous,
    int shared_memory) {
  return mlx_fast_cuda_kernel(
      {new mlx_fast_cuda_kernel_cpp_(mlx::core::fast::cuda_kernel(
          name,
          input_names,
          output_names,
          source,
          header,
          ensure_row_contiguous,
          shared_memory))});
}

extern "C" mlx_fast_cuda_kernel mlx_fast_cuda_kernel_new(
    const char* name,
    const mlx_vector_string input_names,
    const mlx_vector_string output_names,
    const char* source,
    const char* header,
    bool ensure_row_contiguous,
    int shared_memory) {
  try {
    return mlx_fast_cuda_kernel_new_(
        name,
        mlx_vector_string_get_(input_names),
        mlx_vector_string_get_(output_names),
        source,
        header,
        ensure_row_contiguous,
        shared_memory);
  } catch (std::exception& e) {
    mlx_error(e.what());
  }
  return {nullptr};
}
        """
    else:
        custom_code = """
mlx_fast_cuda_kernel mlx_fast_cuda_kernel_new(
    const char* name,
    const mlx_vector_string input_names,
    const mlx_vector_string output_names,
    const char* source,
    const char* header,
    bool ensure_row_contiguous,
    int shared_memory);
        """
    __implement_mlx_fast_custom_kernel("cuda", custom_code, implementation)


def mlx_fast_metal_kernel(f, implementation):
    if implementation:
        custom_code = """
inline mlx_fast_metal_kernel mlx_fast_metal_kernel_new_(
    const std::string& name,
    const std::vector<std::string>& input_names,
    const std::vector<std::string>& output_names,
    const std::string& source,
    const std::string& header,
    bool ensure_row_contiguous,
    bool atomic_outputs) {
  return mlx_fast_metal_kernel(
      {new mlx_fast_metal_kernel_cpp_(mlx::core::fast::metal_kernel(
          name,
          input_names,
          output_names,
          source,
          header,
          ensure_row_contiguous,
          atomic_outputs))});
}

extern "C" mlx_fast_metal_kernel mlx_fast_metal_kernel_new(
    const char* name,
    const mlx_vector_string input_names,
    const mlx_vector_string output_names,
    const char* source,
    const char* header,
    bool ensure_row_contiguous,
    bool atomic_outputs) {
  try {
    return mlx_fast_metal_kernel_new_(
        name,
        mlx_vector_string_get_(input_names),
        mlx_vector_string_get_(output_names),
        source,
        header,
        ensure_row_contiguous,
        atomic_outputs);
  } catch (std::exception& e) {
    mlx_error(e.what());
  }
  return {nullptr};
}
        """
    else:
        custom_code = """
mlx_fast_metal_kernel mlx_fast_metal_kernel_new(
    const char* name,
    const mlx_vector_string input_names,
    const mlx_vector_string output_names,
    const char* source,
    const char* header,
    bool ensure_row_contiguous,
    bool atomic_outputs);
        """
    __implement_mlx_fast_custom_kernel("metal", custom_code, implementation)


def mlx_load_gguf(f, implementation):
    if not implementation:
        print(
            """
int mlx_load_gguf(mlx_io_gguf* gguf, const char* file, const mlx_stream s);
        """
        )
    else:
        print(
            """\
extern "C" int mlx_load_gguf(mlx_io_gguf* gguf, const char* file, const mlx_stream s) {
  try {
    auto cpp_gguf = mlx::core::load_gguf(file, mlx_stream_get_(s));
    mlx_io_gguf_set_(*gguf, std::move(cpp_gguf));
    return 0;
  } catch (std::exception& e) {
    mlx_error(e.what());
    return 1;
  }
}"""
        )


def mlx_save_gguf(f, implementation):
    if not implementation:
        print(
            """\
int mlx_save_gguf(const char* file, mlx_io_gguf gguf);"""
        )
    else:
        print(
            """\
extern "C" int mlx_save_gguf(const char* file, mlx_io_gguf gguf) {
  try {
    auto cpp_gguf = mlx_io_gguf_get_(gguf);
        mlx::core::save_gguf(file, cpp_gguf.first, cpp_gguf.second);
    return 0;
  } catch (std::exception& e) {
    mlx_error(e.what());
    return 1;
  }
}"""
        )


def mlx_export_to_dot(f, implementation):
    if not implementation:
        print(
            """\
typedef struct mlx_node_namer_ {
  void* ctx;
} mlx_node_namer;

mlx_node_namer mlx_node_namer_new();
int mlx_node_namer_free(mlx_node_namer namer);
int mlx_node_namer_set_name(
    mlx_node_namer namer,
    const mlx_array arr,
    const char* name);
int mlx_node_namer_get_name(
    const char** name,
    mlx_node_namer namer,
    const mlx_array arr);
"""
        )
    else:
        print(
            """\
extern "C" mlx_node_namer mlx_node_namer_new() {
  try {
    return mlx_node_namer_new_(mlx::core::NodeNamer());
  } catch (std::exception& e) {
    mlx_error(e.what());
  }
  return {nullptr};
}
extern "C" int mlx_node_namer_free(mlx_node_namer namer) {
  try {
    mlx_node_namer_free_(namer);
    return 0;
  } catch (std::exception& e) {
    mlx_error(e.what());
    return 1;
  }
}
extern "C" int mlx_node_namer_set_name(
    mlx_node_namer namer,
    const mlx_array arr,
    const char* name) {
  try {
    mlx_node_namer_get_(namer).set_name(mlx_array_get_(arr), name);
    return 0;
  } catch (std::exception& e) {
    mlx_error(e.what());
    return 1;
  }
}
extern "C" int mlx_node_namer_get_name(
    const char** name,
    mlx_node_namer namer,
    const mlx_array arr) {
  try {
    *name = mlx_node_namer_get_(namer).get_name(mlx_array_get_(arr)).c_str();
    return 0;
  } catch (std::exception& e) {
    mlx_error(e.what());
    return 1;
  }
}"""
        )
        pass
    return True


def mlx_compile(f, implementation):
    if not implementation:
        print(
            """\
typedef struct mlx_compile_cache_ {
  void* ctx;
} mlx_compile_cache;
mlx_compile_cache mlx_compile_cache_new();
int mlx_compile_cache_free(mlx_compile_cache cache);
            """
        )
    else:
        print(
            """\
extern "C" mlx_compile_cache mlx_compile_cache_new() {
  try {
    return mlx_compile_cache_new_();
  } catch (std::exception& e) {
    mlx_error(e.what());
  }
  return {nullptr};
}
extern "C" int mlx_compile_cache_free(mlx_compile_cache cache) {
  try {
    mlx_compile_cache_free_(cache);
    return 0;
  } catch (std::exception& e) {
    mlx_error(e.what());
    return 1;
  }
}
            """
        )

    return True