FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
mlx-c/python/closure_generator.py at main · Open-Less/mlx-c · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
Open-Less
/
mlx-c
Public
forked from
jimersylee/mlx-c
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
mlx-c
/
python
/
closure_generator.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
395 lines (352 loc) · 10 KB
Breadcrumbs
mlx-c
/
python
/
closure_generator.py
Copy path
File metadata and controls
395 lines (352 loc) · 10 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
import
argparse
import
regex
import
string
import
mlxtypes
as
mt
import
type_private_generator
as
tpg
parser
=
argparse
.
ArgumentParser
(
"MLX C closure code generator"
,
add_help
=
False
)
parser
.
add_argument
(
"--implementation"
,
default
=
False
,
action
=
"store_true"
)
parser
.
add_argument
(
"--private"
,
default
=
False
,
action
=
"store_true"
)
args
=
parser
.
parse_args
()
def
replace_match_parenthesis
(
string
,
keyword
,
fun
):
pattern
=
regex
.
compile
(
keyword
+
r"(\((?:[^()]++|(?1))++\))"
)
res
=
[]
pos
=
0
for
m
in
pattern
.
finditer
(
string
):
res
.
append
(
string
[
pos
:
m
.
start
()])
res
.
append
(
fun
(
m
[
1
][
1
:
-
1
]))
pos
=
m
.
end
()
res
.
append
(
string
[
pos
:])
return
""
.
join
(
res
)
decl_code
=
r"""
typedef struct NAME_ {
void* ctx;
} NAME;
NAME NAME_new(void);
int NAME_free(NAME cls);
NAME NAME_new_func(int (*fun)(RCARGS_UNNAMED, CARGS_UNNAMED));
NAME NAME_new_func_payload(
int (*fun)(RCARGS_UNNAMED, CARGS_UNNAMED, void*),
void* payload,
void (*dtor)(void*));
int NAME_set(NAME *cls, const NAME src);
int NAME_apply(RCARGS, NAME cls, CARGS);
"""
def
generate
(
code
,
name
,
rcpptype
,
cpptypes
):
rcpparg
=
mt
.
cpptypes
[
rcpptype
][
"cpp"
].
replace
(
"@"
,
""
)
cppargs
=
", "
.
join
([
mt
.
cpptypes
[
cpptype
][
"cpp_arg"
](
""
)
for
cpptype
in
cpptypes
])
if
code
is
None
:
return
tpg
.
generate
(
name
,
"std::function<"
+
rcpparg
+
"("
+
cppargs
+
")>"
)
cargs_untyped
=
[]
cargs
=
[]
cppargs_type_name
=
[]
cppargs_to_cargs
=
[]
cargs_free
=
[]
cargs_ctx
=
[]
for
i
in
range
(
len
(
cpptypes
)):
cpptype
=
mt
.
cpptypes
[
cpptypes
[
i
]]
cpparg
=
cpptype
[
"cpp"
]
suffix
=
"_"
+
str
(
i
)
if
len
(
cpptypes
)
>
1
else
""
cargs_untyped
.
append
(
cpptype
[
"c_arg"
](
"input"
+
suffix
,
untyped
=
True
))
cargs
.
append
(
cpptype
[
"c_arg"
](
"input"
+
suffix
))
cppargs_type_name
.
append
(
cpptype
[
"cpp_arg"
](
"cpp_input"
+
suffix
))
cargs_free
.
append
(
cpptype
[
"free"
](
"input"
+
suffix
)
+
";"
)
cargs_ctx
.
append
(
cpptype
[
"c_to_cpp"
](
"input"
+
suffix
))
cppargs_to_cargs
.
append
(
cpptype
[
"c_new"
](
"input"
+
suffix
)
+
";"
)
cppargs_to_cargs
.
append
(
cpptype
[
"c_assign_from_cpp"
](
"input"
+
suffix
,
"cpp_input"
+
suffix
,
returned
=
False
)
+
";"
)
rcargs_new
=
mt
.
cpptypes
[
rcpptype
][
"c_new"
](
"res"
)
+
";"
rcargs_free
=
mt
.
cpptypes
[
rcpptype
][
"free"
](
"res"
)
+
";"
rcargs_to_cpp
=
"auto cpp_res = "
+
mt
.
cpptypes
[
rcpptype
][
"c_to_cpp"
](
"res"
)
+
";"
cargs_untyped
=
", "
.
join
(
cargs_untyped
)
cargs
=
", "
.
join
(
cargs
)
cppargs_type_name
=
", "
.
join
(
cppargs_type_name
)
cppargs_to_cargs
=
"
\n
"
.
join
(
cppargs_to_cargs
)
cargs_free
=
"
\n
"
.
join
(
cargs_free
)
cargs_ctx
=
", "
.
join
(
cargs_ctx
)
cargs_unnamed
=
" "
.
join
(
[
mt
.
cpptypes
[
cpptype
][
"c_arg"
](
""
)
for
cpptype
in
cpptypes
]
)
rcargs_unnamed
=
mt
.
cpptypes
[
rcpptype
][
"c_return_arg"
](
""
)
rcargs
=
mt
.
cpptypes
[
rcpptype
][
"c_return_arg"
](
"res"
)
rcargs_untyped
=
mt
.
cpptypes
[
rcpptype
][
"c_return_arg"
](
"res"
,
untyped
=
True
)
code
=
code
.
replace
(
"RCARGS_UNTYPED"
,
rcargs_untyped
)
code
=
code
.
replace
(
"RCARGS_UNNAMED"
,
rcargs_unnamed
)
code
=
code
.
replace
(
"CPPARGS_TYPE_NAME"
,
cppargs_type_name
)
code
=
code
.
replace
(
"CPPARGS_TO_CARGS"
,
cppargs_to_cargs
)
code
=
code
.
replace
(
"RCARGS_NEW"
,
rcargs_new
)
code
=
code
.
replace
(
"RCARGS_FREE"
,
rcargs_free
)
code
=
code
.
replace
(
"RCARGS_TO_CPP"
,
rcargs_to_cpp
)
code
=
code
.
replace
(
"CARGS_UNTYPED"
,
cargs_untyped
)
code
=
code
.
replace
(
"CARGS_CTX"
,
cargs_ctx
)
code
=
code
.
replace
(
"CARGS_FREE"
,
cargs_free
)
code
=
code
.
replace
(
"RCPPARG"
,
rcpparg
)
code
=
code
.
replace
(
"CARGS_UNNAMED"
,
", "
.
join
([
mt
.
cpptypes
[
cpptype
][
"c_arg"
](
""
)
for
cpptype
in
cpptypes
]),
)
code
=
code
.
replace
(
"ASSIGN_CLS_TO_RCARGS"
,
mt
.
cpptypes
[
rcpptype
][
"c_assign_from_cpp"
](
"res"
,
"NAME_get_(cls)("
+
cargs_ctx
+
")"
,
returned
=
True
)
+
";"
,
)
code
=
code
.
replace
(
"CPPARGS"
,
cppargs
)
code
=
code
.
replace
(
"NAME"
,
name
)
code
=
code
.
replace
(
"RCARGS"
,
rcargs
)
code
=
code
.
replace
(
"CARGS"
,
cargs
)
return
code
impl_code
=
r"""
extern "C" NAME NAME_new(void) {
try {
return NAME_new_();
} catch (std::exception& e) {
mlx_error(e.what());
return NAME_new_();
}
}
extern "C" int NAME_set(NAME *cls, const NAME src) {
try {
NAME_set_(*cls, NAME_get_(src));
} catch (std::exception& e) {
mlx_error(e.what());
return 1;
}
return 0;
}
extern "C" int NAME_free(NAME cls) {
try {
NAME_free_(cls);
return 0;
} catch (std::exception& e) {
mlx_error(e.what());
return 1;
}
}
extern "C" NAME NAME_new_func(int (*fun)(RCARGS_UNNAMED, CARGS_UNNAMED)) {
try {
auto cpp_closure = [fun](CPPARGS_TYPE_NAME) {
CPPARGS_TO_CARGS
RCARGS_NEW
auto status = fun(RCARGS_UNTYPED, CARGS_UNTYPED);
CARGS_FREE
if(status) {
RCARGS_FREE
throw std::runtime_error("NAME returned a non-zero value");
}
RCARGS_TO_CPP
RCARGS_FREE
return cpp_res;
};
return NAME_new_(cpp_closure);
} catch (std::exception& e) {
mlx_error(e.what());
return NAME_new_();
}
}
extern "C" NAME NAME_new_func_payload(
int (*fun)(RCARGS_UNNAMED, CARGS_UNNAMED, void*),
void* payload,
void (*dtor)(void*)) {
try {
std::shared_ptr<void> cpp_payload = nullptr;
if (dtor) {
cpp_payload = std::shared_ptr<void>(payload, dtor);
} else {
cpp_payload = std::shared_ptr<void>(payload, [](void*) {});
}
auto cpp_closure = [fun, cpp_payload](CPPARGS_TYPE_NAME) {
CPPARGS_TO_CARGS
RCARGS_NEW
auto status = fun(RCARGS_UNTYPED, CARGS_UNTYPED, cpp_payload.get());
CARGS_FREE
if(status) {
RCARGS_FREE
throw std::runtime_error("NAME returned a non-zero value");
}
RCARGS_TO_CPP
RCARGS_FREE
return cpp_res;
};
return NAME_new_(cpp_closure);
} catch (std::exception& e) {
mlx_error(e.what());
return NAME_new_();
}
}
extern "C" int NAME_apply(RCARGS, NAME cls, CARGS) {
try {
ASSIGN_CLS_TO_RCARGS
} catch (std::exception& e) {
mlx_error(e.what());
return 1;
}
return 0;
}
"""
priv_code
=
None
decl_begin
=
r"""/* Copyright © 2023-2024 Apple Inc. */
/* */
/* This file is auto-generated. Do not edit manually. */
/* */
#ifndef MLX_CLOSURE_H
#define MLX_CLOSURE_H
#include "mlx/c/array.h"
#include "mlx/c/map.h"
#include "mlx/c/optional.h"
#include "mlx/c/stream.h"
#include "mlx/c/vector.h"
#ifdef __cplusplus
extern "C" {
#endif
/**
* \defgroup mlx_closure Closures
* MLX closure objects.
*/
/**@{*/
"""
decl_end
=
r"""
/**@}*/
#ifdef __cplusplus
}
#endif
#endif
"""
impl_begin
=
r"""/* Copyright © 2023-2024 Apple Inc. */
/* */
/* This file is auto-generated. Do not edit manually. */
/* */
#include "mlx/c/closure.h"
#include "mlx/c/error.h"
#include "mlx/c/private/mlx.h"
"""
impl_end
=
"""
"""
priv_begin
=
r"""/* Copyright © 2023-2024 Apple Inc. */
/* */
/* This file is auto-generated. Do not edit manually. */
/* */
#ifndef MLX_CLOSURE_PRIVATE_H
#define MLX_CLOSURE_PRIVATE_H
#include "mlx/c/closure.h"
#include "mlx/mlx.h"
"""
priv_end
=
r"""
#endif
"""
if
args
.
implementation
:
code
=
impl_code
begin
=
impl_begin
end
=
impl_end
elif
args
.
private
:
code
=
priv_code
begin
=
priv_begin
end
=
priv_end
else
:
code
=
decl_code
begin
=
decl_begin
end
=
decl_end
print
(
begin
)
print
(
generate
(
code
,
"mlx_closure"
,
"std::vector<mlx::core::array>"
,
[
"std::vector<mlx::core::array>"
],
)
)
if
args
.
implementation
:
print
(
r"""
extern "C" mlx_closure mlx_closure_new_unary(
int (*fun)(mlx_array*, const mlx_array)) {
try {
auto cpp_closure = [fun](const std::vector<mlx::core::array>& cpp_input) {
if (cpp_input.size() != 1) {
throw std::runtime_error("closure: expected unary input");
}
auto input = mlx_array_new_(cpp_input[0]);
auto res = mlx_array_new_();
auto status = fun(&res, input);
if(status) {
mlx_array_free_(res);
throw std::runtime_error("mlx_closure returned a non-zero value");
}
mlx_array_free(input);
std::vector<mlx::core::array> cpp_res = {mlx_array_get_(res)};
mlx_array_free(res);
return cpp_res;
};
return mlx_closure_new_(cpp_closure);
} catch (std::exception& e) {
mlx_error(e.what());
return mlx_closure_new_();
}
}
"""
)
elif
args
.
private
:
pass
else
:
print
(
r"""
mlx_closure mlx_closure_new_unary(int (*fun)(mlx_array*, const mlx_array));
"""
)
print
(
generate
(
code
,
"mlx_closure_kwargs"
,
"std::vector<mlx::core::array>"
,
[
"std::vector<mlx::core::array>"
,
"std::unordered_map<std::string, mlx::core::array>"
,
],
)
)
print
(
generate
(
code
,
"mlx_closure_value_and_grad"
,
"std::pair<std::vector<mlx::core::array>, std::vector<mlx::core::array>>"
,
[
"std::vector<mlx::core::array>"
],
)
)
print
(
generate
(
code
,
"mlx_closure_custom"
,
"std::vector<mlx::core::array>"
,
[
"std::vector<mlx::core::array>"
]
*
3
,
)
)
print
(
generate
(
code
,
"mlx_closure_custom_jvp"
,
"std::vector<mlx::core::array>"
,
[
"std::vector<mlx::core::array>"
,
"std::vector<mlx::core::array>"
,
"std::vector<int>"
,
],
)
)
print
(
generate
(
code
,
"mlx_closure_custom_vmap"
,
"std::pair<std::vector<mlx::core::array>, @std::vector<int>>"
,
[
"std::vector<mlx::core::array>"
,
"std::vector<int>"
],
)
)
if
args
.
private
:
print
(
"""
"""
)
print
(
end
)
Back
|
FazBrowse Home
|
New Git URL