FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
mlx-c/python/c.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
/
c.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
238 lines (204 loc) · 6.96 KB
Breadcrumbs
mlx-c
/
python
/
c.py
Copy path
File metadata and controls
238 lines (204 loc) · 6.96 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
# Copyright © 2023-2024 Apple Inc.
import
re
import
sys
import
mlxtypes
as
mt
import
mlxhooks
as
hooks
import
mlxvariants
as
variants
def
to_snake_letters
(
name
):
name
=
re
.
sub
(
r"([A-Z]+)([A-Z][a-z])"
,
r"\1_\2"
,
name
)
name
=
re
.
sub
(
r"([a-z\d])([A-Z])"
,
r"\1_\2"
,
name
)
name
=
name
.
lower
()
return
name
def
c_namespace
(
namespace
):
c_prefix
=
namespace
.
split
(
"::"
)
if
c_prefix
[
0
]
==
"mlx"
and
c_prefix
[
1
]
==
"core"
:
c_prefix
.
pop
(
1
)
# we pop core
if
len
(
c_prefix
)
==
2
and
c_prefix
[
1
]
==
"cu"
:
c_prefix
[
1
]
=
"cuda"
return
"_"
.
join
(
c_prefix
)
def
uniq_defs
(
defs
):
uniq_defs
=
[]
variants
=
{}
for
d
in
defs
:
variant
=
d
[
"variant"
]
if
"variant"
in
d
else
""
if
variant
in
variants
:
continue
else
:
variants
[
variant
]
=
True
uniq_defs
.
append
(
d
)
return
uniq_defs
def
generate
(
funcs
,
enums
,
header
,
headername
,
implementation
,
docstring
):
sorted_funcs
=
[]
for
name
,
defs
in
funcs
.
items
():
name_split
=
name
.
split
(
"::"
)
namespace
=
"_"
.
join
(
name_split
[:
-
1
])
name
=
name_split
[
-
1
]
defs
.
sort
(
key
=
lambda
x
:
len
(
x
[
"params_name"
]),
reverse
=
True
)
# handle duplicates and other exceptions
if
hasattr
(
variants
,
namespace
):
defs
=
getattr
(
variants
,
namespace
)(
name
,
defs
)
sorted_funcs
+=
uniq_defs
(
defs
)
sorted_funcs
.
sort
(
key
=
lambda
x
:
x
[
"name"
])
print
(
"""/* Copyright © 2023-2024 Apple Inc. */
/* */
/* This file is auto-generated. Do not edit manually. */
/* */
"""
)
if
implementation
:
print
(
'#include "mlx/c/'
+
headername
+
'.h"'
)
for
include
in
header
.
split
(
";"
):
# Extract path from the last mlx/ onwards
# This handles paths like ../mlx/mlx/backend/... -> mlx/backend/...
parts
=
include
.
split
(
"/"
)
mlx_idx
=
None
for
i
,
part
in
enumerate
(
parts
):
if
part
==
"mlx"
:
mlx_idx
=
i
if
mlx_idx
is
not
None
:
print
(
'#include "'
+
"/"
.
join
(
parts
[
mlx_idx
:])
+
'"'
)
print
(
'#include "mlx/c/error.h"'
)
print
(
'#include "mlx/c/private/mlx.h"'
)
print
()
else
:
print
(
"#ifndef MLX_"
+
headername
.
upper
()
+
"_H"
)
print
(
"#define MLX_"
+
headername
.
upper
()
+
"_H"
)
print
(
"""
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include "mlx/c/array.h"
#include "mlx/c/closure.h"
#include "mlx/c/distributed_group.h"
#include "mlx/c/io_types.h"
#include "mlx/c/map.h"
#include "mlx/c/stream.h"
#include "mlx/c/string.h"
#include "mlx/c/vector.h"
#ifdef __cplusplus
extern "C" {
#endif
"""
)
if
docstring
:
docstring
=
docstring
.
replace
(
"
\n
"
,
"
\n
* "
)
print
(
"/**"
)
print
(
r" * \defgroup "
+
headername
+
" "
+
docstring
)
print
(
" */"
)
print
(
"/**@{*/"
)
print
()
# blank line after /**@{*/
for
_
,
enum
in
enums
.
items
():
c_typename
=
"mlx_"
+
to_snake_letters
(
enum
[
"name"
])
c_vals
=
[]
for
value
in
enum
[
"values"
]:
c_vals
.
append
(
"MLX_"
+
to_snake_letters
(
enum
[
"name"
]).
upper
()
+
"_"
+
value
.
upper
()
)
if
implementation
:
pass
else
:
decl
=
[
"typedef enum "
]
decl
.
append
(
c_typename
+
"_"
)
decl
.
append
(
"{"
)
decl
.
append
(
", "
.
join
(
c_vals
))
decl
.
append
(
"}"
)
decl
.
append
(
c_typename
)
decl
.
append
(
";"
)
print
(
" "
.
join
(
decl
))
print
()
for
f
in
sorted_funcs
:
if
"variant"
in
f
:
func_name
=
(
c_namespace
(
f
[
"namespace"
])
+
"_"
+
f
[
"name"
]
+
"_"
+
f
[
"variant"
]
)
else
:
func_name
=
c_namespace
(
f
[
"namespace"
])
+
"_"
+
f
[
"name"
]
if
hasattr
(
hooks
,
func_name
):
hook_result
=
getattr
(
hooks
,
func_name
)(
f
,
implementation
)
if
not
hook_result
:
print
()
# blank line after hook output
continue
signature
=
[]
return_t
=
f
[
"return_t"
]
if
return_t
in
mt
.
cpptypes
:
return_t
=
mt
.
cpptypes
[
return_t
]
elif
return_t
in
mt
.
alttypes
:
return_t
=
mt
.
alttypes
[
return_t
]
else
:
print
(
"unsupported return type: "
+
return_t
,
file
=
sys
.
stderr
)
print
(
"skipping"
,
f
,
file
=
sys
.
stderr
)
continue
signature
.
append
(
"int"
)
signature
.
append
(
func_name
+
"("
)
c_call
=
[]
cpp_call
=
[]
# return values as first arguments
res_arg
=
return_t
[
"c_return_arg"
](
"res"
)
if
res_arg
:
c_call
.
append
(
res_arg
)
pt
=
f
[
"params_t"
]
pn
=
f
[
"params_name"
]
pd
=
f
[
"params_default"
]
use_defaults
=
"use_defaults"
in
f
and
f
[
"use_defaults"
]
encountered_unsupported_type
=
False
for
i
in
range
(
len
(
pt
)):
if
use_defaults
and
pd
[
i
]:
continue
pti
=
pt
[
i
]
pni
=
pn
[
i
]
if
pni
is
None
:
pni
=
"param"
# good luck
if
pti
in
mt
.
cpptypes
:
pti
=
mt
.
cpptypes
[
pti
]
elif
pti
in
mt
.
alttypes
:
pti
=
mt
.
alttypes
[
pti
]
else
:
print
(
"unsupported argument type: "
+
pti
,
file
=
sys
.
stderr
)
encountered_unsupported_type
=
True
print
(
"skipping"
,
f
,
file
=
sys
.
stderr
)
break
c_call
.
append
(
pti
[
"c_arg"
](
pni
))
cpp_call
.
append
(
pti
[
"c_to_cpp"
](
pni
))
if
encountered_unsupported_type
:
print
(
"skipping"
,
f
,
file
=
sys
.
stderr
)
continue
# print(f)
c_call
=
", "
.
join
(
c_call
)
if
len
(
c_call
)
>
0
else
"void"
cpp_call
=
", "
.
join
(
cpp_call
)
signature
.
append
(
c_call
)
signature
.
append
(
")"
)
# Join signature parts, then clean up spaces around parens
signature
=
" "
.
join
(
signature
)
signature
=
signature
.
replace
(
"( "
,
"("
).
replace
(
" )"
,
")"
)
c_code
=
signature
+
";"
if
implementation
:
cpp_call
=
f
[
"namespace"
]
+
"::"
+
f
[
"name"
]
+
"("
+
cpp_call
+
")"
cpp_code
=
[
'extern "C" '
+
signature
+
" {"
,
" try {"
,
" "
+
return_t
[
"c_assign_from_cpp"
](
"res"
,
cpp_call
)
+
";"
,
" } catch (std::exception& e) {"
,
" mlx_error(e.what());"
,
" return 1;"
,
" }"
,
" return 0;"
,
"}"
,
]
print
(
"
\n
"
.
join
(
cpp_code
))
else
:
print
(
c_code
)
if
implementation
:
pass
else
:
if
docstring
:
print
()
# blank line before /**@}*/
print
(
"/**@}*/"
)
print
(
"""
\
#ifdef __cplusplus
}
#endif
#endif"""
)
# no trailing newline
Back
|
FazBrowse Home
|
New Git URL