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
/* Copyright © 2023-2024 Apple Inc. */
#ifndef MLX_CLOSURE_H
#define MLX_CLOSURE_H
#include "mlx/c/array.h"
#ifdef __cplusplus
extern "C" {
#endif
/**
* \defgroup mlx_closure Closures
* MLX closure objects.
*/
/**@{*/
/**
* A closure encapsulating a function taking a vector of arrays, and
* returning a vector of arrays
*/
typedef struct mlx_closure_* mlx_closure;
/**
* Returns a closure encapsulating the specified function.
*/
mlx_closure mlx_closure_new(
mlx_vector_array (*fun)(const mlx_vector_array arrs));
/**
* Returns a closure encapsulating the specified function.
* This is a convenience function, mapping `mlx_array` to `mlx_vector_array`
* under the hood.
*/
mlx_closure mlx_closure_new_unary(mlx_array (*fun)(const mlx_array));
/**
* Returns a closure encapsulating the specified function, with the given
* payload. If `dtor` is not `NULL`, it will called when the closure is
* destroyed to free the payload.
*/
mlx_closure mlx_closure_new_with_payload(
mlx_vector_array (*fun)(const mlx_vector_array, void*),
void* payload,
void (*dtor)(void*));
/**
* Applies the closure over the given vector of arrays argument.
*/
mlx_vector_array mlx_closure_apply(mlx_closure cls, const mlx_vector_array);
/**
* A closure which takes a vector of arrays, and returns a vector of vector of
* arrays of size 2.
*/
typedef struct mlx_closure_value_and_grad_* mlx_closure_value_and_grad;
/**
* Applies the closure over the given vector of arrays argument.
*/
mlx_vector_vector_array mlx_closure_value_and_grad_apply(
mlx_closure_value_and_grad cls,
const mlx_vector_array inputs);
/**@}*/
#ifdef __cplusplus
}
#endif
#endif