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
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
use std::{
cell::Cell,
collections::HashMap,
env,
fmt::Display,
fs,
hash::Hash,
marker::PhantomData,
path::Path,
process::Command,
rc::Rc,
sync::{Arc, RwLock, RwLockReadGuard},
};
use crate::{device::Dev, tensor::concretetensor::from_storage, DType, Result, Shape, Tensor};
use petgraph::Graph as PetGraph;
use petgraph::{dot::Dot, graph::NodeIndex};
#[derive(Clone, Debug)]
pub struct GraphNode<T: DType> {
pub op: Op<T>,
pub shape: Vec<usize>,
pub strides: Vec<usize>,
pub id: GraphTensorId,
}
#[derive(Clone)]
pub struct Graph<T: DType> {
data: Arc<RwLock<Vec<GraphNode<T>>>>,
id: Arc<RwLock<usize>>,
}
impl<T: DType> Graph<T> {
/// Create an empty Graph
pub fn empty() -> Self {
Self {
data: Arc::new(RwLock::new(Vec::new())),
id: Arc::new(RwLock::new(0)),
}
}
/// Read-only access to the list of operations
pub fn get_ops(&self) -> RwLockReadGuard<Vec<GraphNode<T>>> {
self.data.read().unwrap()
}
/// Append an operation to the graph
pub(crate) fn add_op<S: Shape>(&self, op: Op<T>, strides: &[usize], id: &GraphTensorId) {
self.data.write().unwrap().push(GraphNode {
op,
shape: S::shape(),
strides: strides.to_vec(),
id: id.clone(),
});
}
/// Generate the next unique tensor ID
#[must_use]
pub(crate) fn next_id(&mut self) -> GraphTensorId {
let next = GraphTensorId::out_of_place(*self.id.read().unwrap());
*self.id.write().unwrap() += 1;
next
}
pub fn to_petgraph(&self) -> PetGraph<String, String> {
let ops = self.data.read().unwrap();
let mut g = PetGraph::<String, String>::new();
// map from op‐index → Some(node) if we created a node, or None if it was a NoOp
let mut idx_map: Vec<Option<NodeIndex>> = Vec::with_capacity(ops.len());
// 1) Add only non‐NoOp nodes
for op in ops.iter() {
match op.op {
Op::NoOp => {
idx_map.push(None);
}
_ => {
let label = match &op.op {
Op::Fill { v, .. } => format!("Fill({v:?})"),
Op::Arange {
start, step, stop, ..
} => {
format!("Arange(start={start:?}, step={step:?}, stop={stop:?})")
}
Op::Rand => "Rand".to_string(),
Op::Randn { mean, std } => {
format!("Randn(mean={mean:?}, std={std:?})")
}
Op::BinaryOp { operator, .. } => format!("BinOp({})", operator.as_c_op()),
Op::UnaryOp { operator, .. } => format!("UnOp({operator:?})"),
Op::FusedMulAdd { .. } => "FMA".to_string(),
// Matrix multiplication
Op::MatMul { .. } => "MatMul".to_string(),
Op::Permute { v_id: _ } => "Permute".to_string(),
// we already matched NoOp above
Op::NoOp => unreachable!(),
};
let node = g.add_node(label);
idx_map.push(Some(node));
}
}
}
// 2) Walk ops again and only connect edges for those dst nodes that exist
for (i, op) in ops.iter().enumerate() {
// if this op was NoOp, skip entirely
let dst = match idx_map[i] {
Some(dst) => dst,
None => continue,
};
match &op.op {
Op::BinaryOp { l_id, r_id, .. } => {
if let Some(src) = idx_map[l_id.get()] {
let mut label = "l".to_string();
if l_id.is_inplace() {
label.push('*');
}
g.add_edge(src, dst, label.clone());
}
if let Some(src) = idx_map[r_id.get()] {
let mut label = "r".to_string();
if r_id.is_inplace() {
label.push('*');
}
g.add_edge(src, dst, label.clone());
}
}
Op::UnaryOp { v_id, .. } => {
if let Some(src) = idx_map[v_id.get()] {
let mut label = "v".to_string();
if v_id.is_inplace() {
label.push('*');
}
g.add_edge(src, dst, label.clone());
}
}
Op::FusedMulAdd {
a_id, b_id, c_id, ..
} => {
for (prefix, src_id) in [("a", a_id), ("b", b_id), ("c", c_id)].iter() {
if let Some(src) = idx_map[src_id.get()] {
let mut label = prefix.to_string();
if src_id.is_inplace() {
label.push('*');
}
g.add_edge(src, dst, label.clone());
}
}
}
Op::MatMul {
l_id, r_id, o_id, ..
} => {
if let Some(src) = idx_map[l_id.get()] {
let mut label = "l".to_string();
if l_id.is_inplace() {
label.push('*');
}
g.add_edge(src, dst, label.clone());
}
if let Some(src) = idx_map[r_id.get()] {
let mut label = "r".to_string();
if r_id.is_inplace() {
label.push('*');
}
g.add_edge(src, dst, label.clone());
}
if let Some(o_id) = o_id {
if let Some(src) = idx_map[o_id.get()] {
let mut label = "o".to_string();
if o_id.is_inplace() {
label.push('*');
}
g.add_edge(src, dst, label.clone());
}
}
}
Op::Permute { v_id, .. } => {
if let Some(src) = idx_map[v_id.get()] {
let mut label = "v".to_string();
if v_id.is_inplace() {
label.push('*');
}
g.add_edge(src, dst, label.clone());
}
}
// NoOp, Fill/Arange, Rand/Randn don’t create incoming edges
Op::NoOp | Op::Fill { .. } | Op::Arange { .. } | Op::Rand | Op::Randn { .. } => {}
}
}
g
}
/// Produce a DOT format string of this graph.
pub fn to_dot(&self) -> String {
let g = self.to_petgraph();
format!("{:?}", Dot::with_config(&g, &[]))
}
/// Visualize the graph by saving it to this file.
///
/// Install graphvis:
/// - brew install graphviz
/// - apt install graphviz
pub fn visualize<P: AsRef<Path>>(&self, filename: P) -> Result<()> {
let path = filename.as_ref();
let tmp_dir = env::temp_dir();
let dot_path = tmp_dir.join("graph.dot");
let png_path = path.to_path_buf();
fs::write(&dot_path, self.to_dot())?;
let status = Command::new("dot")
.args([
"-Tpng",
&dot_path.display().to_string(),
"-o",
&png_path.display().to_string(),
])
.status()?;
if !status.success() {
panic!("Graphviz failed");
}
Ok(())
}
/// Optimize by performing constant folding:
/// - Fold BinaryOp and UnaryOp when all operands are constant Fill ops.
fn optimize_const(&mut self) {
// Clone current ops for inspection
let ops = self.data.read().unwrap().clone();
let mut new_ops = ops.clone();
for (i, node) in ops.iter().enumerate() {
match &node.op {
Op::BinaryOp {
l_id,
r_id,
operator,
} => {
let l_idx = l_id.get();
let r_idx = r_id.get();
// both operands are constant fills
if let Op::Fill { v: v1 } = &new_ops[l_idx].op {
if let Op::Fill { v: v2 } = &new_ops[r_idx].op {
let v = operator.as_closure()(*v1, *v2);
new_ops[i] = GraphNode {
op: Op::Fill { v },
..node.clone()
};
}
}
}
Op::UnaryOp { v_id, operator } => {
let idx = v_id.get();
// operand is a constant fill
if let Op::Fill { v: v0 } = &new_ops[idx].op {
let v = operator.to_closure()(*v0);
new_ops[i] = GraphNode {
op: Op::Fill { v },
..node.clone()
};
}
}
_ => {}
}
}
// Commit folded constants
*self.data.write().unwrap() = new_ops;
}
/// Optimize by looking for mul-add pairs, convert to FMA
fn optimize_fma(&mut self) {
let ops = self.data.write().unwrap().clone();
let mut new_ops = ops.clone();
// This contains the indices of the first of the pair.
for (x_id, x) in ops.iter().enumerate() {
if let Op::BinaryOp {
l_id: a_id,
r_id: b_id,
operator: BinaryOpType::Mul,
} = &x.op
{
// Check if next op uses this
if let Op::BinaryOp {
l_id: l_y,
r_id: r_y,
operator: BinaryOpType::Add,
} = &ops[x_id + 1].op
{
let y_id = x_id + 1;
if l_y.get() == x_id || r_y.get() == x_id && x.shape == ops[x_id + 1].shape {
// Want to see what is being added to the result of the mul
let rhs_add = if l_y.get() == x_id { r_y } else { l_y };
new_ops[y_id] = GraphNode {
op: Op::FusedMulAdd {
a_id: a_id.clone(),
b_id: b_id.clone(),
c_id: rhs_add.clone(),
},
..x.clone()
};
new_ops[x_id] = GraphNode {
op: Op::NoOp,
..x.clone()
};
// Look for ops which actually use this one
for user in new_ops.iter() {
let ids = match &user.op {
Op::Arange {
start: _,
step: _,
stop: _,
..
} => vec![],
Op::Rand => vec![],
Op::Randn { mean: _, std: _ } => vec![],
Op::BinaryOp { l_id, r_id, .. } => vec![l_id, r_id],
Op::Fill { v: _, .. } => vec![],
Op::UnaryOp {
v_id, operator: _, ..
} => vec![v_id],
Op::FusedMulAdd {
a_id, b_id, c_id, ..
} => {
vec![a_id, b_id, c_id]
}
Op::MatMul {
l_id, r_id, o_id, ..
} => o_id
.as_ref()
.map(|o| vec![l_id, r_id, o])
.unwrap_or(vec![l_id, r_id]),
Op::Permute { v_id } => vec![v_id],
Op::NoOp => vec![],
};
// We are going to remove the noop so this is necessary to fix the indices.
let used_ids = ids
.into_iter()
.filter(|id| id.get() == y_id)
.collect::<Vec<_>>();
if !used_ids.is_empty() {
for id in used_ids {
// Tell the ops which use the result of the fma to source from there
id.set(x_id);
}
}
}
}
}
}
}
// Remove any NoOp entries before storing back to the graph
let filtered_ops = new_ops
.into_iter()
.filter(|op| !matches!(op.op, Op::NoOp))
.collect::<Vec<_>>();
*self.data.write().unwrap() = filtered_ops;
}
/// Count how often each tensor id is used as an input.
#[allow(clippy::mutable_key_type)]
fn count_input_usage(ops: &[GraphNode<T>]) -> HashMap<GraphTensorId, usize> {
#[allow(clippy::mutable_key_type)]
let mut usage: HashMap<GraphTensorId, usize> = HashMap::new();
for op in ops {
match &op.op {
Op::BinaryOp { l_id, r_id, .. } => {
*usage.entry(l_id.clone()).or_default() += 1;
*usage.entry(r_id.clone()).or_default() += 1;
}
Op::UnaryOp { v_id, .. } => {
*usage.entry(v_id.clone()).or_default() += 1;
}
Op::FusedMulAdd {
a_id, b_id, c_id, ..
} => {
*usage.entry(a_id.clone()).or_default() += 1;
*usage.entry(b_id.clone()).or_default() += 1;
*usage.entry(c_id.clone()).or_default() += 1;
}
Op::MatMul {
l_id, r_id, o_id, ..
} => {
*usage.entry(l_id.clone()).or_default() += 1;
*usage.entry(r_id.clone()).or_default() += 1;
if let Some(o_id) = o_id {
*usage.entry(o_id.clone()).or_default() += 1;
}
}
Op::Permute { v_id } => {
*usage.entry(v_id.clone()).or_default() += 1;
}
// No input usage for these ops
Op::NoOp | Op::Fill { .. } | Op::Arange { .. } | Op::Rand | Op::Randn { .. } => {}
}
}
usage
}
/// Optimize by inplacing binary operations when inputs are not reused.
fn optimize_inplace_bin(&mut self) {
let ops = self.data.write().unwrap().clone();
let mut new_ops = ops.clone();
#[allow(clippy::mutable_key_type)]
let usage = Self::count_input_usage(&ops);
// Transform eligible BinaryOps into InplaceBinaryOps.
for (i, op) in ops.iter().enumerate() {
if let Op::BinaryOp {
l_id,
r_id,
operator,
} = &op.op
{
let l_use = usage.get(l_id).copied().unwrap_or(0);
let r_use = usage.get(r_id).copied().unwrap_or(0);
if l_use <= 1 || r_use <= 1 {
// Choose target for in-place: if both, default to lhs.
let target = if r_use > l_use {
r_id.clone()
} else {
l_id.clone()
};
// Replace with InplaceBinaryOp
new_ops[i] = GraphNode {
op: Op::BinaryOp {
l_id: l_id.clone().to_inplace_if(&target == l_id),
r_id: r_id.clone().to_inplace_if(&target == r_id),
operator: *operator,
},
..op.clone()
};
}
}
}
// Commit the transformed op list.
*self.data.write().unwrap() = new_ops;
}
/// Optimize by inplacing fused multiply-add (FMA) operations when inputs are not reused.
fn optimize_inplace_fma(&mut self) {
let ops = self.data.write().unwrap().clone();
let mut new_ops = ops.clone();
#[allow(clippy::mutable_key_type)]
let usage = Self::count_input_usage(&ops);
for (i, op) in ops.iter().enumerate() {
if let Op::FusedMulAdd { a_id, b_id, c_id } = &op.op {
let mut target = None;
// If an input is used only once, we can reuse its buffer; default order: a_id, then b_id, then c_id
if *usage.get(a_id).unwrap_or(&0) <= 1 {
target = Some(a_id.clone());
} else if *usage.get(b_id).unwrap_or(&0) <= 1 {
target = Some(b_id.clone());
} else if *usage.get(c_id).unwrap_or(&0) <= 1 {
target = Some(c_id.clone());
}
if let Some(out) = target {
new_ops[i] = GraphNode {
op: Op::FusedMulAdd {
a_id: a_id.clone().to_inplace_if(&out == a_id),
b_id: b_id.clone().to_inplace_if(&out == b_id),
c_id: c_id.clone().to_inplace_if(&out == c_id),
},
..op.clone()
};
}
}
}
*self.data.write().unwrap() = new_ops;
}
/// Optimize by inplacing the output of a matmul when inputs are not reused.
fn optimize_inplace_matmul(&mut self) {
let ops = self.data.write().unwrap().clone();
let mut new_ops = ops.clone();
#[allow(clippy::mutable_key_type)]
let usage = Self::count_input_usage(&ops);
// Transform eligible BinaryOps into InplaceBinaryOps.
for (i, op) in ops.iter().enumerate() {
if let Op::MatMul {
o_id: Some(o_id),
l_id,
r_id,
k,
alpha,
beta,
} = &op.op
{
let o_use = usage.get(o_id).copied().unwrap_or(0);
if o_use <= 1 {
// Replace with InplaceBinaryOp
new_ops[i] = GraphNode {
op: Op::MatMul {
o_id: Some(o_id.to_inplace()),
l_id: l_id.clone(),
r_id: r_id.clone(),
k: *k,
alpha: *alpha,
beta: *beta,
},
..op.clone()
};
}
}
}
// Commit the transformed op list.
*self.data.write().unwrap() = new_ops;
}
/// Remove nodes whose outputs are never used, except the final output node.
fn optimize_dead_code(&mut self) {
// Clone current ops
let old_ops = self.data.read().unwrap().clone();
let n = old_ops.len();
// Mark reachable nodes: start from final output
let mut keep = vec![false; n];
if n > 0 {
keep[n - 1] = true;
}
// Propagate reachability backwards
for i in (0..n).rev() {
if keep[i] {
match &old_ops[i].op {
Op::BinaryOp { l_id, r_id, .. } => {
keep[l_id.get()] = true;
keep[r_id.get()] = true;
}
Op::UnaryOp { v_id, .. } => {
keep[v_id.get()] = true;
}
Op::FusedMulAdd {
a_id, b_id, c_id, ..
} => {
keep[a_id.get()] = true;
keep[b_id.get()] = true;
keep[c_id.get()] = true;
}
Op::MatMul {
l_id, r_id, o_id, ..
} => {
keep[l_id.get()] = true;
keep[r_id.get()] = true;
if let Some(o_id) = o_id {
keep[o_id.get()] = true;
}
}
Op::Permute { v_id, .. } => {
keep[v_id.get()] = true;
}
Op::NoOp
| Op::Fill { .. }
| Op::Arange { .. }
| Op::Rand
| Op::Randn { .. } => (),
}
}
}
// Build new ops and map old indices to new indices
let mut index_map = std::collections::HashMap::new();
let mut new_ops = Vec::new();
for (old_idx, node) in old_ops.into_iter().enumerate() {
if keep[old_idx] {
let new_idx = new_ops.len();
index_map.insert(old_idx, new_idx);
new_ops.push(node);
}
}
// Update tensor IDs in remaining ops
for node in new_ops.iter_mut() {
match &mut node.op {
Op::BinaryOp { l_id, r_id, .. } => {
let old_l = l_id.get();
let old_r = r_id.get();
l_id.set(*index_map.get(&old_l).unwrap());
r_id.set(*index_map.get(&old_r).unwrap());
}
Op::UnaryOp { v_id, .. } => {
let old_v = v_id.get();
v_id.set(*index_map.get(&old_v).unwrap());
}
Op::FusedMulAdd {
a_id, b_id, c_id, ..
} => {
let old_a = a_id.get();
let old_b = b_id.get();
let old_c = c_id.get();
a_id.set(*index_map.get(&old_a).unwrap());
b_id.set(*index_map.get(&old_b).unwrap());
c_id.set(*index_map.get(&old_c).unwrap());
}
Op::MatMul {
l_id, r_id, o_id, ..
} => {
let old_l = l_id.get();
let old_r = r_id.get();
l_id.set(*index_map.get(&old_l).unwrap());
r_id.set(*index_map.get(&old_r).unwrap());
if let Some(o_id) = o_id {
let old_o = o_id.get();
o_id.set(*index_map.get(&old_o).unwrap());
}
}
_ => {}
}
}
// Commit pruned graph
*self.data.write().unwrap() = new_ops;
}
/// Optimize this graph.
///
/// Apply the following optimizations:
/// - Constant folding of elementwise fills
/// - Fuse mul-add into FMA
/// - Inplace binary operations when safe
/// - Inplace fused multiply-add when safe
/// - Inplace matrix-multiplication when safe
/// - Dead code removal
pub fn optimize(&mut self) {
// Constant folding first
self.optimize_const();
// Fuse mul-add into FMA
self.optimize_fma();
self.optimize_inplace_bin();
self.optimize_inplace_fma();
self.optimize_inplace_matmul();
// Remove dead code
self.optimize_dead_code();
}
/// Compile this graph and insert device-specific optimizations such as CUDA streams.
pub fn compile<S: Shape, D: Dev>(self) -> Result<CompiledGraph<S, T, D>> {
if self
.data
.read()
.unwrap()
.last()
.is_some_and(|last| last.shape != S::shape())
{
let read = self.data.read();
let last = read.as_ref().unwrap().last().unwrap();
crate::bail!(
"Graph compiled shape is {:?} does not match the last node shape {:?}!",
&last.shape,
S::shape()
);
}
let device = D::resolve()?;
device.compile(self.data.read().unwrap().clone())
}
}
/// A representation of the compiled graph. The shape is the output shape.
pub enum CompiledGraph<S: Shape, T: DType, D: Dev> {
Cpu {
order: Vec<usize>,
graph: Vec<GraphNode<T>>,
ghost: PhantomData<(S, T, D)>,
},
#[cfg(feature = "cuda")]
Cuda {
kernels: Vec<crate::cuda_backend::CudaCompiledKernel<T>>,
ghost: PhantomData<(S, T, D)>,
},
}
impl<S: Shape, T: DType, D: Dev> CompiledGraph<S, T, D> {
/// Run the precompiled graph. This executes all nodes on the specified backend device and returns a concrete tensor.
pub fn run(&self) -> Result<Tensor<S, T, D>> {
let device = D::resolve()?;
let storage = device.run_graph(self)?;
Ok(from_storage(Arc::new(storage)))
}
}
#[derive(PartialEq, Debug, Clone, Copy)]
pub enum BinaryOpType {
Add,
Div,
Sub,
Mul,
}
impl BinaryOpType {
pub fn as_c_op(&self) -> &'static str {
match self {
Self::Add => "+",
Self::Div => "/",
Self::Sub => "-",
Self::Mul => "*",
}
}
pub fn as_closure<T: DType>(&self) -> impl Fn(T, T) -> T {
match self {
Self::Add => |x, y| x + y,
Self::Div => |x, y| x / y,
Self::Sub => |x, y| x - y,
Self::Mul => |x, y| x * y,
}
}
}
#[derive(PartialEq, Debug, Clone)]
pub enum UnaryOpType {
Neg,
Sqrt,
}
impl UnaryOpType {
pub fn fill_in_c_op(&self, val: impl Display) -> String {
match self {
Self::Neg => format!("-{val}"),
Self::Sqrt => format!("static_cast<T>( sqrt( static_cast<double>({val}) ) )"),
}
}
pub fn to_closure<T: DType>(&self) -> impl Fn(T) -> T {
match self {
Self::Neg => T::maybe_neg,
Self::Sqrt => |x: T| x.sqrt(),
}
}
}
#[derive(PartialEq, Debug, Clone)]
pub enum Op<T: DType> {
Fill {
v: T,
},
Arange {
start: T,
step: T,
stop: T,
},
BinaryOp {
l_id: GraphTensorId,
r_id: GraphTensorId,
operator: BinaryOpType,
},
UnaryOp {
v_id: GraphTensorId,
operator: UnaryOpType,
},
/// a * b + c
FusedMulAdd {
a_id: GraphTensorId,
b_id: GraphTensorId,
c_id: GraphTensorId,
},
/// (B x M x K) * (B x K x N) = (B x M x N)
/// out = out * alpha + beta * lhs * rhs
MatMul {
l_id: GraphTensorId,
r_id: GraphTensorId,
o_id: Option<GraphTensorId>,
k: usize,
alpha: T,
beta: T,
},
/// Fill with uniform random values in [0, 1).
Rand,
/// Fill with normally distributed random values (mean, std).
Randn {
mean: T,
std: T,
},
// Permutation operator.
Permute {
v_id: GraphTensorId,
},
NoOp,
}
#[derive(Clone, PartialEq, Debug, Eq)]
/// Graph tensor IDs can be cloned.
pub enum GraphTensorId {
OutOfPlace(Rc<Cell<usize>>),
InPlace(Rc<Cell<usize>>),
}
impl Hash for GraphTensorId {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
state.write_usize(self.get());
}
}
impl GraphTensorId {
pub fn out_of_place(value: usize) -> Self {
Self::OutOfPlace(Rc::new(Cell::new(value)))
}
pub fn inplace(value: usize) -> Self {
Self::InPlace(Rc::new(Cell::new(value)))
}
pub fn to_inplace(&self) -> Self {
match self {
Self::OutOfPlace(x) | Self::InPlace(x) => Self::inplace(x.get()),
}
}
pub fn to_inplace_if(&self, predicate: bool) -> Self {
match self {
Self::OutOfPlace(x) | Self::InPlace(x) if predicate => Self::inplace(x.get()),
_ => self.clone(),
}
}
pub fn get(&self) -> usize {
match self {
GraphTensorId::InPlace(x) | GraphTensorId::OutOfPlace(x) => x.get(),
}
}
pub fn set(&self, value: usize) {
match self {
GraphTensorId::InPlace(x) | GraphTensorId::OutOfPlace(x) => x.set(value),
}
}
pub fn is_inplace(&self) -> bool {
matches!(self, Self::InPlace(_))
}
}