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
open Ir
let const_to_string : type a. a Dtype.t -> a -> string =
fun dt v ->
match dt with
| Dtype.Float32 -> Printf.sprintf "%gf" v
| Dtype.Int32 -> Int32.to_string v
| Dtype.Uint8 -> string_of_int v
| Dtype.Bool -> if v then "true" else "false"
| Dtype.Unit -> "0"
type lowering_ctx = {
meta : (Var.t, var_metadata) Hashtbl.t;
scalar_map : (Var.t, Var.t) Hashtbl.t;
buffer_map : (Var.t, Var.t) Hashtbl.t;
instrs : Lowered.instruction list ref;
}
let new_ctx (graph_meta : (Var.t, var_metadata) Hashtbl.t)
(kernel_meta : (Var.t, var_metadata) Hashtbl.t) =
let meta = Hashtbl.copy graph_meta in
Hashtbl.iter (Hashtbl.replace meta) kernel_meta;
{
meta;
scalar_map = Hashtbl.create 16;
buffer_map = Hashtbl.create 16;
instrs = ref [];
}
let add_instr ctx i = ctx.instrs := i :: !(ctx.instrs)
let ensure_meta ctx v meta = Hashtbl.replace ctx.meta v meta
let meta_of ctx v = Hashtbl.find_opt ctx.meta v
let ll_of_hl ctx hl ~buffer =
let tbl = if buffer then ctx.buffer_map else ctx.scalar_map in
match Hashtbl.find_opt tbl hl with
| Some ll -> ll
| None ->
let ll = Var.fresh () in
Hashtbl.add tbl hl ll;
Option.iter (ensure_meta ctx ll) (meta_of ctx hl);
ll
let gtid ctx =
let v = Var.fresh () in
ensure_meta ctx v
{ dtype = Dtype.Any_Dtype Dtype.Int32; shape = [| 1 |]; device = None };
add_instr ctx
(Lowered.L_Special { dst = v; kind = Special_index_kind.Global_task_idx 0 });
v
let load_scalar ctx ~hl_buffer ~idx ~dtype =
let buf = ll_of_hl ctx hl_buffer ~buffer:true in
let dst = Var.fresh () in
ensure_meta ctx dst
{ dtype = Dtype.Any_Dtype dtype; shape = [| 1 |]; device = None };
add_instr ctx
(Lowered.L_Load
{ dst; buf; idx; dtype = Dtype.Any_Dtype dtype; valid = None });
dst
let lower_node ctx (Any_Node n) kernel_outs =
let open Lowered in
match n with
| Placeholder { out_var; _ } ->
ignore (ll_of_hl ctx out_var ~buffer:true);
Ok ()
| Buffer { dtype; size_in_elements; device = _; out_var } ->
let ll = ll_of_hl ctx out_var ~buffer:true in
add_instr ctx
(L_Buffer
{ dtype = Dtype.Any_Dtype dtype; size = size_in_elements; out = ll });
Ok ()
| Const_Scalar { value; dtype; out_var } ->
let ll = ll_of_hl ctx out_var ~buffer:false in
add_instr ctx
(L_Const
{
dtype = Dtype.Any_Dtype dtype;
value = const_to_string dtype value;
out = ll;
});
Ok ()
| Binop { op; a_var; b_var; out_var; dtype } ->
let idx = gtid ctx in
let a_ll = load_scalar ctx ~hl_buffer:a_var ~idx ~dtype in
let b_ll = load_scalar ctx ~hl_buffer:b_var ~idx ~dtype in
let dst = Var.fresh () in
ensure_meta ctx dst
{ dtype = Dtype.Any_Dtype dtype; shape = [| 1 |]; device = None };
add_instr ctx
(L_ALU
{
dst;
op = Lowered.Binary op;
args = [ a_ll; b_ll ];
dtype = Dtype.Any_Dtype dtype;
});
Hashtbl.replace ctx.scalar_map out_var dst;
(if List.mem out_var kernel_outs then
let out_buf = ll_of_hl ctx out_var ~buffer:true in
add_instr ctx (L_Store { buf = out_buf; idx; src = dst; valid = None }));
Ok ()
| Reduce_Axis { in_var; reduce_op_kind; out_var; dtype; _ } ->
let shape =
match meta_of ctx in_var with
| Some m -> m.shape
| None ->
failwith
("No metadata for var " ^ Var.to_string in_var ^ " during Reduce")
in
let total = Array.fold_left ( * ) 1 shape in
let idx = Var.fresh () in
ensure_meta ctx idx
{ dtype = Dtype.Any_Dtype Dtype.Int32; shape = [| 1 |]; device = None };
let ub = Var.fresh () in
ensure_meta ctx ub
{ dtype = Dtype.Any_Dtype Dtype.Int32; shape = [| 1 |]; device = None };
add_instr ctx
(L_Const
{
dtype = Dtype.Any_Dtype Dtype.Int32;
value = string_of_int total;
out = ub;
});
add_instr ctx (L_Range { idx; bound = ub });
let acc = Var.fresh () in
ensure_meta ctx acc
{ dtype = Dtype.Any_Dtype dtype; shape = [| 1 |]; device = None };
let identity =
match (dtype, reduce_op_kind) with
| Dtype.Float32, Reduce_Sum -> "0.0"
| Dtype.Float32, Reduce_Max -> "-INFINITY"
| Dtype.Float32, Reduce_Prod -> "1.0"
| Dtype.Int32, Reduce_Sum -> "0"
| Dtype.Int32, Reduce_Max -> string_of_int min_int
| Dtype.Int32, Reduce_Prod -> "1"
| Dtype.Uint8, Reduce_Sum -> "0"
| Dtype.Uint8, Reduce_Max -> "0"
| Dtype.Uint8, Reduce_Prod -> "1"
| Dtype.Bool, Reduce_Sum -> "false"
| Dtype.Bool, Reduce_Max -> "true"
| Dtype.Bool, Reduce_Prod -> "true"
| Dtype.Unit, _ -> "0"
in
add_instr ctx
(L_Const { dtype = Dtype.Any_Dtype dtype; value = identity; out = acc });
let cur = load_scalar ctx ~hl_buffer:in_var ~idx ~dtype in
let op =
match reduce_op_kind with
| Reduce_Sum -> Lowered.Binary Add
| Reduce_Max -> Lowered.Binary Max
| Reduce_Prod -> Lowered.Binary Mul
in
add_instr ctx
(L_ALU
{ dst = acc; op; args = [ acc; cur ]; dtype = Dtype.Any_Dtype dtype });
add_instr ctx L_EndRange;
if List.mem out_var kernel_outs then (
let ob = ll_of_hl ctx out_var ~buffer:true in
let z = Var.fresh () in
ensure_meta ctx z
{
dtype = Dtype.Any_Dtype Dtype.Int32;
shape = [| 1 |];
device = None;
};
add_instr ctx
(L_Const { dtype = Dtype.Any_Dtype Dtype.Int32; value = "0"; out = z });
add_instr ctx (L_Store { buf = ob; idx = z; src = acc; valid = None }))
else Hashtbl.replace ctx.scalar_map out_var acc;
Ok ()
| Unary { op; in_var; out_var; dtype } ->
let idx = gtid ctx in
let in_ll = load_scalar ctx ~hl_buffer:in_var ~idx ~dtype in
let dst = Var.fresh () in
ensure_meta ctx dst
{ dtype = Dtype.Any_Dtype dtype; shape = [| 1 |]; device = None };
add_instr ctx
(L_ALU
{
dst;
op = Lowered.Unary op;
args = [ in_ll ];
dtype = Dtype.Any_Dtype dtype;
});
Hashtbl.replace ctx.scalar_map out_var dst;
(if List.mem out_var kernel_outs then
let out_buf = ll_of_hl ctx out_var ~buffer:true in
add_instr ctx (L_Store { buf = out_buf; idx; src = dst; valid = None }));
Ok ()
| Ternary
{
op = Where;
a_var = cond_var;
b_var = x_var;
c_var = y_var;
out_var;
dtype;
} ->
let idx = gtid ctx in
let cond_ll =
load_scalar ctx ~hl_buffer:cond_var ~idx ~dtype:Dtype.Bool
in
let x_ll = load_scalar ctx ~hl_buffer:x_var ~idx ~dtype in
let y_ll = load_scalar ctx ~hl_buffer:y_var ~idx ~dtype in
let dst = Var.fresh () in
ensure_meta ctx dst
{ dtype = Dtype.Any_Dtype dtype; shape = [| 1 |]; device = None };
add_instr ctx
(L_ALU
{
dst;
op = Lowered.Ternary Where;
args = [ cond_ll; x_ll; y_ll ];
dtype = Dtype.Any_Dtype dtype;
});
Hashtbl.replace ctx.scalar_map out_var dst;
(if List.mem out_var kernel_outs then
let out_buf = ll_of_hl ctx out_var ~buffer:true in
add_instr ctx (L_Store { buf = out_buf; idx; src = dst; valid = None }));
Ok ()
| Expand { in_var; out_var; _ }
| Reshape { in_var; out_var; _ }
| Permute { in_var; out_var; _ }
| Pad { in_var; out_var; _ }
| Shrink { in_var; out_var; _ }
| Flip { in_var; out_var; _ }
| Contiguous { in_var; out_var; _ }
| Copy { in_var; target_device = _; out_var; _ } ->
(match Hashtbl.find_opt ctx.scalar_map in_var with
| Some s -> Hashtbl.replace ctx.scalar_map out_var s
| None ->
let b = ll_of_hl ctx in_var ~buffer:true in
Hashtbl.replace ctx.buffer_map out_var b);
Ok ()
| Cast { in_var; target_dtype = _; out_var; dtype = _ } ->
(match Hashtbl.find_opt ctx.scalar_map in_var with
| Some s -> Hashtbl.replace ctx.scalar_map out_var s
| None ->
let b = ll_of_hl ctx in_var ~buffer:true in
Hashtbl.replace ctx.buffer_map out_var b);
Ok ()
| Cat { in_vars; axis = _; out_var; dtype = _ } ->
(if Array.length in_vars > 0 then
match Hashtbl.find_opt ctx.scalar_map in_vars.(0) with
| Some s -> Hashtbl.replace ctx.scalar_map out_var s
| None ->
let b = ll_of_hl ctx in_vars.(0) ~buffer:true in
Hashtbl.replace ctx.buffer_map out_var b);
Ok ()
| Assign { target_var; updates = _; out_var; dtype = _ } ->
(match Hashtbl.find_opt ctx.scalar_map target_var with
| Some s -> Hashtbl.replace ctx.scalar_map out_var s
| None ->
let b = ll_of_hl ctx target_var ~buffer:true in
Hashtbl.replace ctx.buffer_map out_var b);
Ok ()
| Threefry { ctr_var = _; key_var = _; out_var; dtype } ->
let idx = gtid ctx in
let dst = Var.fresh () in
ensure_meta ctx dst
{ dtype = Dtype.Any_Dtype dtype; shape = [| 1 |]; device = None };
add_instr ctx
(L_Const { dtype = Dtype.Any_Dtype dtype; value = "0"; out = dst });
Hashtbl.replace ctx.scalar_map out_var dst;
(if List.mem out_var kernel_outs then
let out_buf = ll_of_hl ctx out_var ~buffer:true in
add_instr ctx (L_Store { buf = out_buf; idx; src = dst; valid = None }));
Ok ()
| Gather { src_var; indices_var = _; axis = _; out_var; dtype = _ } ->
(match Hashtbl.find_opt ctx.scalar_map src_var with
| Some s -> Hashtbl.replace ctx.scalar_map out_var s
| None ->
let b = ll_of_hl ctx src_var ~buffer:true in
Hashtbl.replace ctx.buffer_map out_var b);
Ok ()
| Scatter
{ indices_var = _; updates_var = _; axis = _; shape = _; out_var; dtype }
->
let idx = gtid ctx in
let dst = Var.fresh () in
ensure_meta ctx dst
{ dtype = Dtype.Any_Dtype dtype; shape = [| 1 |]; device = None };
add_instr ctx
(L_Const { dtype = Dtype.Any_Dtype dtype; value = "0"; out = dst });
Hashtbl.replace ctx.scalar_map out_var dst;
(if List.mem out_var kernel_outs then
let out_buf = ll_of_hl ctx out_var ~buffer:true in
add_instr ctx (L_Store { buf = out_buf; idx; src = dst; valid = None }));
Ok ()
| Ternary { op = Mulacc; a_var; b_var; c_var; out_var; dtype } ->
let idx = gtid ctx in
let a_ll = load_scalar ctx ~hl_buffer:a_var ~idx ~dtype in
let b_ll = load_scalar ctx ~hl_buffer:b_var ~idx ~dtype in
let c_ll = load_scalar ctx ~hl_buffer:c_var ~idx ~dtype in
let dst = Var.fresh () in
ensure_meta ctx dst
{ dtype = Dtype.Any_Dtype dtype; shape = [| 1 |]; device = None };
add_instr ctx
(L_ALU
{
dst;
op = Lowered.Ternary Mulacc;
args = [ a_ll; b_ll; c_ll ];
dtype = Dtype.Any_Dtype dtype;
});
Hashtbl.replace ctx.scalar_map out_var dst;
(if List.mem out_var kernel_outs then
let out_buf = ll_of_hl ctx out_var ~buffer:true in
add_instr ctx (L_Store { buf = out_buf; idx; src = dst; valid = None }));
Ok ()
| View { in_var; shape_tracker = _; out_var; _ }
| Valid { in_var; shape_tracker = _; out_var; _ }
| Detach { in_var; out_var; _ }
| Contiguous_Backward { in_var; out_var; _ }
| Bitcast { in_var; target_dtype = _; out_var; _ } ->
(match Hashtbl.find_opt ctx.scalar_map in_var with
| Some s -> Hashtbl.replace ctx.scalar_map out_var s
| None ->
let b = ll_of_hl ctx in_var ~buffer:true in
Hashtbl.replace ctx.buffer_map out_var b);
Ok ()
| Vconst { values; dtype; out_var } ->
let ll = ll_of_hl ctx out_var ~buffer:true in
add_instr ctx
(L_Buffer
{
dtype = Dtype.Any_Dtype dtype;
size = Array.length values;
out = ll;
});
Ok ()
| Buffer_View { buffer_var; size = _; offset = _; dtype = _; out_var } ->
let b = ll_of_hl ctx buffer_var ~buffer:true in
Hashtbl.replace ctx.buffer_map out_var b;
Ok ()
| Index { in_var; idx_var; valid_var = _; out_var; dtype } ->
let base_idx = gtid ctx in
let idx =
load_scalar ctx ~hl_buffer:idx_var ~idx:base_idx ~dtype:Dtype.Int32
in
let in_ll = load_scalar ctx ~hl_buffer:in_var ~idx ~dtype in
Hashtbl.replace ctx.scalar_map out_var in_ll;
(if List.mem out_var kernel_outs then
let out_buf = ll_of_hl ctx out_var ~buffer:true in
add_instr ctx
(L_Store { buf = out_buf; idx = base_idx; src = in_ll; valid = None }));
Ok ()
| Gep { in_var; indices; out_var; dtype } ->
let ll = ll_of_hl ctx in_var ~buffer:false in
let dst = Var.fresh () in
ensure_meta ctx dst
{ dtype = Dtype.Any_Dtype dtype; shape = [| 1 |]; device = None };
add_instr ctx
(L_Gep { dst; src = ll; indices; dtype = Dtype.Any_Dtype dtype });
Hashtbl.replace ctx.scalar_map out_var dst;
Ok ()
| Vectorize { in_vars; out_var; dtype } ->
let srcs = Array.map (fun v -> ll_of_hl ctx v ~buffer:false) in_vars in
let dst = Var.fresh () in
ensure_meta ctx dst
{
dtype = Dtype.Any_Dtype dtype;
shape = [| Array.length in_vars |];
device = None;
};
add_instr ctx (L_Vectorize { dst; srcs; dtype = Dtype.Any_Dtype dtype });
Hashtbl.replace ctx.scalar_map out_var dst;
Ok ()
| Wmma { a_var; b_var; c_var; m; n; k; out_var; dtype } ->
let a_ll = ll_of_hl ctx a_var ~buffer:true in
let b_ll = ll_of_hl ctx b_var ~buffer:true in
let c_ll = ll_of_hl ctx c_var ~buffer:true in
let dst = ll_of_hl ctx out_var ~buffer:true in
add_instr ctx
(L_Wmma
{
dst;
a = a_ll;
b = b_ll;
c = c_ll;
m;
n;
k;
dtype = Dtype.Any_Dtype dtype;
});
Ok ()
| Define_Var { sym_var; out_var; dtype = _ } ->
let ll = ll_of_hl ctx out_var ~buffer:false in
add_instr ctx (L_Define_Var { sym_var; out = ll });
Ok ()
| Bind { sym_var; value; out_var; dtype = _ } ->
let sym_ll = ll_of_hl ctx sym_var ~buffer:false in
let dst = ll_of_hl ctx out_var ~buffer:false in
add_instr ctx
(L_Const
{
dtype = Dtype.Any_Dtype Dtype.Int32;
value = string_of_int value;
out = dst;
});
add_instr ctx (L_Assign { dst = sym_ll; src = dst });
Ok ()
| Multi { device_vars; axis = _; real_mask = _; out_var; dtype = _ } ->
(if Array.length device_vars > 0 then
match Hashtbl.find_opt ctx.scalar_map device_vars.(0) with
| Some s -> Hashtbl.replace ctx.scalar_map out_var s
| None ->
let b = ll_of_hl ctx device_vars.(0) ~buffer:true in
Hashtbl.replace ctx.buffer_map out_var b);
Ok ()
| Fuse { in_var; out_var; dtype = _ } ->
(match Hashtbl.find_opt ctx.scalar_map in_var with
| Some s -> Hashtbl.replace ctx.scalar_map out_var s
| None ->
let b = ll_of_hl ctx in_var ~buffer:true in
Hashtbl.replace ctx.buffer_map out_var b);
Ok ()
| Unroll { loop_var; unroll_factor; out_var; dtype = _ } ->
let ll = ll_of_hl ctx loop_var ~buffer:false in
let dst = ll_of_hl ctx out_var ~buffer:false in
add_instr ctx (L_Unroll { idx = ll; iterations = unroll_factor });
add_instr ctx (L_Assign { dst; src = ll });
Ok ()
| Contract { in_vars = _; contraction_axes = _; out_var; dtype } ->
let idx = gtid ctx in
let dst = Var.fresh () in
ensure_meta ctx dst
{ dtype = Dtype.Any_Dtype dtype; shape = [| 1 |]; device = None };
add_instr ctx
(L_Const { dtype = Dtype.Any_Dtype dtype; value = "0"; out = dst });
Hashtbl.replace ctx.scalar_map out_var dst;
(if List.mem out_var kernel_outs then
let out_buf = ll_of_hl ctx out_var ~buffer:true in
add_instr ctx (L_Store { buf = out_buf; idx; src = dst; valid = None }));
Ok ()
| Sink { deps = _; dtype = _ } ->
Ok ()
| Kernel
{ ast = _; input_vars = _; output_vars = _; metadata = _; out_var; dtype }
->
let ll = ll_of_hl ctx out_var ~buffer:true in
add_instr ctx
(L_Buffer { dtype = Dtype.Any_Dtype dtype; size = 1; out = ll });
Ok ()
| Unique { id; out_var; dtype } ->
let ll = ll_of_hl ctx out_var ~buffer:false in
add_instr ctx
(L_Const
{ dtype = Dtype.Any_Dtype dtype; value = string_of_int id; out = ll });
Ok ()
| Device { device_name = _; out_var; dtype } ->
let ll = ll_of_hl ctx out_var ~buffer:false in
add_instr ctx
(L_Const { dtype = Dtype.Any_Dtype dtype; value = "0"; out = ll });
Ok ()
| Custom { op_name; in_vars; attributes; out_var; dtype = _ } ->
let args = Array.map (fun v -> ll_of_hl ctx v ~buffer:false) in_vars in
let dst = ll_of_hl ctx out_var ~buffer:false in
add_instr ctx
(L_Custom { dst = Some dst; op_name; args; attributes; inline = false });
Ok ()
| Noop { in_var = _; out_var; dtype } ->
let ll = ll_of_hl ctx out_var ~buffer:false in
add_instr ctx
(L_Const { dtype = Dtype.Any_Dtype dtype; value = "0"; out = ll });
Ok ()
let lower_kernel ~(kernel_spec : Scheduler.kernel_spec_t)
~original_graph_vars_metadata =
let ( let* ) = Result.bind in
let ( let+ ) = fun r f -> Result.map f r in
let ctx = new_ctx original_graph_vars_metadata kernel_spec.vars_metadata in
let open Result in
let+ () =
List.fold_left
(fun acc n ->
let* _ = acc in
lower_node ctx n kernel_spec.outputs)
(Ok ()) kernel_spec.nodes
in
{
Lowered.instructions = List.rev !(ctx.instrs);
vars_metadata = ctx.meta;
kernel_input_vars =
List.map (fun v -> ll_of_hl ctx v ~buffer:true) kernel_spec.inputs;
kernel_output_vars =
List.map (fun v -> ll_of_hl ctx v ~buffer:true) kernel_spec.outputs;
symbolic_vars = [];
}