package owl-base

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Source file owl_computation_operator.ml

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
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
# 1 "src/base/compute/owl_computation_operator.ml"
(*
 * OWL - OCaml Scientific Computing
 * Copyright (c) 2016-2022 Liang Wang <liang@ocaml.xyz>
 *)

open Owl_types

(* Functor of making Lazy module of different number types *)

module Make (Symbol : Owl_computation_symbol_sig.Sig) = struct
  module Symbol = Symbol
  open Symbol
  open Symbol.Shape.Type
  open Symbol.Shape.Type.Device

  (* mathematical functions *)

  let noop x = make_then_connect Noop [| arr_to_node x |] |> node_to_arr

  let empty shape = make_node ~shape:[| Some shape |] (Empty shape) |> node_to_arr

  let zeros shape = make_node ~shape:[| Some shape |] (Zeros shape) |> node_to_arr

  let ones shape = make_node ~shape:[| Some shape |] (Ones shape) |> node_to_arr

  let create shape v =
    make_then_connect ~shape:[| Some shape |] (Create shape) [| elt_to_node v |]
    |> node_to_arr


  let sequential ?a ?step shape =
    let a =
      match a with
      | Some a -> a
      | None   -> const_elt "sequential_a" (A.float_to_elt 0.)
    in
    let b =
      match step with
      | Some b -> b
      | None   -> const_elt "sequential_step" (A.float_to_elt 1.)
    in
    make_then_connect
      ~shape:[| Some shape |]
      (Sequential shape)
      [| elt_to_node a; elt_to_node b |]
    |> node_to_arr


  let uniform ?a ?b shape =
    let a =
      match a with
      | Some a -> a
      | None   -> const_elt "uniform_a" (A.float_to_elt 0.)
    in
    let b =
      match b with
      | Some b -> b
      | None   -> const_elt "uniform_b" (A.float_to_elt 1.)
    in
    make_then_connect
      ~shape:[| Some shape |]
      (Uniform shape)
      [| elt_to_node a; elt_to_node b |]
    |> node_to_arr


  let gaussian ?mu ?sigma shape =
    let a =
      match mu with
      | Some a -> a
      | None   -> const_elt "sequential_a" (A.float_to_elt 0.)
    in
    let b =
      match sigma with
      | Some b -> b
      | None   -> const_elt "sequential_step" (A.float_to_elt 1.)
    in
    make_then_connect
      ~shape:[| Some shape |]
      (Gaussian shape)
      [| elt_to_node a; elt_to_node b |]
    |> node_to_arr


  let bernoulli ?p shape =
    let p =
      match p with
      | Some p -> p
      | None   -> const_elt "bernoulli_p" (A.float_to_elt 0.5)
    in
    make_then_connect ~shape:[| Some shape |] (Bernoulli shape) [| elt_to_node p |]
    |> node_to_arr


  let init shape f = make_node ~shape:[| Some shape |] (Init (shape, f)) |> node_to_arr

  let init_nd _shape _f =
    raise (Owl_exception.NOT_IMPLEMENTED "owl_computation_operator.init_nd")


  let shape x = arr_to_node x |> node_shape

  let numel x = Array.fold_left ( * ) 1 (shape x)

  let get x i = make_then_connect (Get i) [| arr_to_node x |] |> node_to_elt

  let set x i v = make_then_connect (Set i) [| arr_to_node x; elt_to_node v |] |> ignore

  let get_slice slice x =
    make_then_connect (GetSlice slice) [| arr_to_node x |] |> node_to_arr


  let set_slice slice x y =
    make_then_connect (SetSlice slice) [| arr_to_node x; arr_to_node y |] |> ignore


  let get_fancy indices x =
    make_then_connect (GetFancy indices) [| arr_to_node x |] |> node_to_arr


  let set_fancy indices x y =
    make_then_connect (SetFancy indices) [| arr_to_node x; arr_to_node y |] |> ignore


  let copy x = make_then_connect Copy [| arr_to_node x |] |> node_to_arr

  let copy_ ~out _x = failwith "Owl_computation_operator:copy_: not implemented"
    [@@warning "-27"]


  let reset x = make_then_connect Reset [| arr_to_node x |] |> node_to_arr |> ignore

  let reshape x shape =
    let n_old = numel x in
    let n_new = Array.fold_left ( * ) 1 shape in
    let exn = Owl_exception.DIFFERENT_SIZE (n_old, n_new) in
    Owl_exception.check (n_old = n_new) exn;
    make_then_connect (Reshape shape) [| arr_to_node x |] |> node_to_arr


  let reverse x = make_then_connect Reverse [| arr_to_node x |] |> node_to_arr

  let tile x axises = make_then_connect (Tile axises) [| arr_to_node x |] |> node_to_arr

  let repeat x repeats =
    make_then_connect (Repeat repeats) [| arr_to_node x |] |> node_to_arr


  let pad ?v padding x =
    let v =
      match v with
      | Some v -> v
      | None   -> const_elt "pad_v" (A.float_to_elt 0.)
    in
    make_then_connect (Pad (v, padding)) [| arr_to_node x |] |> node_to_arr


  let expand ?(hi = false) _x _d =
    ignore hi;
    failwith "expand: not implemented"


  let squeeze ?(axis = [||]) _x =
    ignore axis;
    failwith "squeeze: not implemented"


  let concatenate ?(axis = 0) xs =
    make_then_connect (Concatenate axis) (Array.map arr_to_node xs) |> node_to_arr


  let stack ?(axis = 0) xs =
    make_then_connect (Stack axis) (Array.map arr_to_node xs) |> node_to_arr


  let concat ~axis =
    axis |> ignore;
    failwith "concat: not implemented"


  let split ?(axis = 0) _parts _x = failwith "split: not implemented" [@@warning "-27"]

  let draw ?(axis = 0) x n =
    let y = make_then_connect (Draw (axis, n)) [| arr_to_node x |] |> node_to_arr in
    y, [||]


  let map f x = make_then_connect (Map f) [| arr_to_node x |] |> node_to_arr

  let fold ?(axis = -1) f a x =
    make_then_connect (Fold (axis, f)) [| arr_to_node x; elt_to_node a |] |> node_to_arr


  let scan ?(axis = -1) f x =
    make_then_connect (Scan (axis, f)) [| arr_to_node x |] |> node_to_arr


  let one_hot depth x =
    make_then_connect (OneHot depth) [| arr_to_node x |] |> node_to_arr


  let delay f x = make_then_connect (Delay f) [| arr_to_node x |] |> node_to_arr

  let delay_array shape f x =
    make_then_connect
      ~shape:[| Some shape |]
      (DelayArray (shape, f))
      (Array.map arr_to_node x)
    |> node_to_arr


  let lazy_print ?max_row ?max_col ?header ?fmt x =
    make_then_connect (LazyPrint (max_row, max_col, header, fmt)) [| arr_to_node x |]
    |> node_to_arr


  let print ?max_row ?max_col ?header ?fmt x = () [@@warning "-27"]

  let abs x = make_then_connect Abs [| arr_to_node x |] |> node_to_arr

  let neg x = make_then_connect Neg [| arr_to_node x |] |> node_to_arr

  let floor x = make_then_connect Floor [| arr_to_node x |] |> node_to_arr

  let ceil x = make_then_connect Ceil [| arr_to_node x |] |> node_to_arr

  let round x = make_then_connect Round [| arr_to_node x |] |> node_to_arr

  let sqr x = make_then_connect Sqr [| arr_to_node x |] |> node_to_arr

  let sqrt x = make_then_connect Sqrt [| arr_to_node x |] |> node_to_arr

  let log x = make_then_connect Log [| arr_to_node x |] |> node_to_arr

  let log2 x = make_then_connect Log2 [| arr_to_node x |] |> node_to_arr

  let log10 x = make_then_connect Log10 [| arr_to_node x |] |> node_to_arr

  let exp x = make_then_connect Exp [| arr_to_node x |] |> node_to_arr

  let sin x = make_then_connect Sin [| arr_to_node x |] |> node_to_arr

  let cos x = make_then_connect Cos [| arr_to_node x |] |> node_to_arr

  let tan x = make_then_connect Tan [| arr_to_node x |] |> node_to_arr

  let sinh x = make_then_connect Sinh [| arr_to_node x |] |> node_to_arr

  let cosh x = make_then_connect Cosh [| arr_to_node x |] |> node_to_arr

  let tanh x = make_then_connect Tanh [| arr_to_node x |] |> node_to_arr

  let asin x = make_then_connect Asin [| arr_to_node x |] |> node_to_arr

  let acos x = make_then_connect Acos [| arr_to_node x |] |> node_to_arr

  let atan x = make_then_connect Atan [| arr_to_node x |] |> node_to_arr

  let asinh x = make_then_connect Asinh [| arr_to_node x |] |> node_to_arr

  let acosh x = make_then_connect Acosh [| arr_to_node x |] |> node_to_arr

  let atanh x = make_then_connect Atanh [| arr_to_node x |] |> node_to_arr

  let min ?(axis = -1) ?(keep_dims = true) x =
    make_then_connect (Min (keep_dims, axis)) [| arr_to_node x |] |> node_to_arr


  let max ?(axis = -1) ?(keep_dims = true) x =
    ignore keep_dims;
    make_then_connect (Max (keep_dims, axis)) [| arr_to_node x |] |> node_to_arr


  let sum ?(axis = -1) ?(keep_dims = true) x =
    ignore keep_dims;
    make_then_connect (Sum (keep_dims, axis)) [| arr_to_node x |] |> node_to_arr


  let sum_reduce ?(axis = [| -1 |]) x =
    make_then_connect (SumReduce axis) [| arr_to_node x |] |> node_to_arr


  let signum x = make_then_connect Signum [| arr_to_node x |] |> node_to_arr

  let sigmoid x = make_then_connect Sigmoid [| arr_to_node x |] |> node_to_arr

  let relu x = make_then_connect Relu [| arr_to_node x |] |> node_to_arr

  let dawsn x = make_then_connect Dawsn [| arr_to_node x |] |> node_to_arr

  let min' x = make_then_connect Min' [| arr_to_node x |] |> node_to_elt

  let max' x = make_then_connect Max' [| arr_to_node x |] |> node_to_elt

  let sum' x = make_then_connect Sum' [| arr_to_node x |] |> node_to_elt

  let log_sum_exp' x = make_then_connect LogSumExp' [| arr_to_node x |] |> node_to_elt

  let log_sum_exp ?(axis = 0) ?(keep_dims = true) x =
    make_then_connect (LogSumExp (keep_dims, axis)) [| arr_to_node x |] |> node_to_arr


  let l1norm' x = make_then_connect L1norm' [| arr_to_node x |] |> node_to_elt

  let l2norm' x = make_then_connect L2norm' [| arr_to_node x |] |> node_to_elt

  let l2norm_sqr' x = make_then_connect L2NormSqr' [| arr_to_node x |] |> node_to_elt

  let clip_by_value ?amin ?amax x =
    let a =
      match amin with
      | Some a -> a
      | None   -> const_elt "clip_by_value_amin" (A.float_to_elt neg_infinity)
    in
    let b =
      match amax with
      | Some b -> b
      | None   -> const_elt "clip_by_value_amax" (A.float_to_elt infinity)
    in
    make_then_connect ClipByValue [| arr_to_node x; elt_to_node a; elt_to_node b |]
    |> node_to_arr


  let clip_by_l2norm a x =
    make_then_connect ClipByL2norm [| arr_to_node x; elt_to_node a |] |> node_to_arr


  let pow x y = make_then_connect Pow [| arr_to_node x; arr_to_node y |] |> node_to_arr

  let scalar_pow a x =
    make_then_connect ScalarPow [| elt_to_node a; arr_to_node x |] |> node_to_arr


  let pow_scalar x a =
    make_then_connect PowScalar [| arr_to_node x; elt_to_node a |] |> node_to_arr


  let atan2 x y =
    make_then_connect Atan2 [| arr_to_node x; arr_to_node y |] |> node_to_arr


  let scalar_atan2 a x =
    make_then_connect ScalarAtan2 [| elt_to_node a; arr_to_node x |] |> node_to_arr


  let atan2_scalar x a =
    make_then_connect Atan2Scalar [| arr_to_node x; elt_to_node a |] |> node_to_arr


  let hypot x y =
    make_then_connect Hypot [| arr_to_node x; arr_to_node y |] |> node_to_arr


  let min2 x y = make_then_connect Min2 [| arr_to_node x; arr_to_node y |] |> node_to_arr

  let max2 x y = make_then_connect Max2 [| arr_to_node x; arr_to_node y |] |> node_to_arr

  let add x y = make_then_connect Add [| arr_to_node x; arr_to_node y |] |> node_to_arr

  let sub x y = make_then_connect Sub [| arr_to_node x; arr_to_node y |] |> node_to_arr

  let mul x y = make_then_connect Mul [| arr_to_node x; arr_to_node y |] |> node_to_arr

  let div x y = make_then_connect Div [| arr_to_node x; arr_to_node y |] |> node_to_arr

  let add_scalar x a =
    make_then_connect AddScalar [| arr_to_node x; elt_to_node a |] |> node_to_arr


  let sub_scalar x a =
    make_then_connect SubScalar [| arr_to_node x; elt_to_node a |] |> node_to_arr


  let mul_scalar x a =
    make_then_connect MulScalar [| arr_to_node x; elt_to_node a |] |> node_to_arr


  let div_scalar x a =
    make_then_connect DivScalar [| arr_to_node x; elt_to_node a |] |> node_to_arr


  let scalar_add a x =
    make_then_connect ScalarAdd [| elt_to_node a; arr_to_node x |] |> node_to_arr


  let scalar_sub a x =
    make_then_connect ScalarSub [| elt_to_node a; arr_to_node x |] |> node_to_arr


  let scalar_mul a x =
    make_then_connect ScalarMul [| elt_to_node a; arr_to_node x |] |> node_to_arr


  let scalar_div a x =
    make_then_connect ScalarDiv [| elt_to_node a; arr_to_node x |] |> node_to_arr


  let fma x y z =
    make_then_connect FMA [| arr_to_node x; arr_to_node y; arr_to_node z |] |> node_to_arr


  let elt_equal x y =
    make_then_connect EltEqual [| arr_to_node x; arr_to_node y |] |> node_to_arr


  let elt_not_equal x y =
    make_then_connect EltNotEqual [| arr_to_node x; arr_to_node y |] |> node_to_arr


  let elt_less x y =
    make_then_connect EltLess [| arr_to_node x; arr_to_node y |] |> node_to_arr


  let elt_greater x y =
    make_then_connect EltGreater [| arr_to_node x; arr_to_node y |] |> node_to_arr


  let elt_less_equal x y =
    make_then_connect EltLessEqual [| arr_to_node x; arr_to_node y |] |> node_to_arr


  let elt_greater_equal x y =
    make_then_connect EltGreaterEqual [| arr_to_node x; arr_to_node y |] |> node_to_arr


  let elt_equal_scalar x a =
    make_then_connect EltEqualScalar [| arr_to_node x; elt_to_node a |] |> node_to_arr


  let elt_not_equal_scalar x a =
    make_then_connect EltNotEqualScalar [| arr_to_node x; elt_to_node a |] |> node_to_arr


  let elt_less_scalar x a =
    make_then_connect EltLessScalar [| arr_to_node x; elt_to_node a |] |> node_to_arr


  let elt_greater_scalar x a =
    make_then_connect EltGreaterScalar [| arr_to_node x; elt_to_node a |] |> node_to_arr


  let elt_less_equal_scalar x a =
    make_then_connect EltLessEqualScalar [| arr_to_node x; elt_to_node a |] |> node_to_arr


  let elt_greater_equal_scalar x a =
    make_then_connect EltGreaterEqualScalar [| arr_to_node x; elt_to_node a |]
    |> node_to_arr


  let conv1d ?(padding = SAME) input kernel stride =
    make_then_connect
      (Conv1d (padding, stride))
      [| arr_to_node input; arr_to_node kernel |]
    |> node_to_arr


  let conv2d ?(padding = SAME) input kernel stride =
    make_then_connect
      (Conv2d (padding, stride))
      [| arr_to_node input; arr_to_node kernel |]
    |> node_to_arr


  let conv3d ?(padding = SAME) input kernel stride =
    make_then_connect
      (Conv3d (padding, stride))
      [| arr_to_node input; arr_to_node kernel |]
    |> node_to_arr


  let transpose_conv1d ?(padding = SAME) input kernel stride =
    make_then_connect
      (TransposeConv1d (padding, stride))
      [| arr_to_node input; arr_to_node kernel |]
    |> node_to_arr


  let transpose_conv2d ?(padding = SAME) input kernel stride =
    make_then_connect
      (TransposeConv2d (padding, stride))
      [| arr_to_node input; arr_to_node kernel |]
    |> node_to_arr


  let transpose_conv3d ?(padding = SAME) input kernel stride =
    make_then_connect
      (TransposeConv3d (padding, stride))
      [| arr_to_node input; arr_to_node kernel |]
    |> node_to_arr


  let dilated_conv1d ?(padding = SAME) input kernel stride rate =
    make_then_connect
      (DilatedConv1d (padding, stride, rate))
      [| arr_to_node input; arr_to_node kernel |]
    |> node_to_arr


  let dilated_conv2d ?(padding = SAME) input kernel stride rate =
    make_then_connect
      (DilatedConv2d (padding, stride, rate))
      [| arr_to_node input; arr_to_node kernel |]
    |> node_to_arr


  let dilated_conv3d ?(padding = SAME) input kernel stride rate =
    make_then_connect
      (DilatedConv3d (padding, stride, rate))
      [| arr_to_node input; arr_to_node kernel |]
    |> node_to_arr


  let max_pool1d ?(padding = SAME) input kernel stride =
    make_then_connect (MaxPool1d (padding, kernel, stride)) [| arr_to_node input |]
    |> node_to_arr


  let max_pool2d ?(padding = SAME) input kernel stride =
    make_then_connect (MaxPool2d (padding, kernel, stride)) [| arr_to_node input |]
    |> node_to_arr


  let max_pool3d ?(padding = SAME) input kernel stride =
    make_then_connect (MaxPool3d (padding, kernel, stride)) [| arr_to_node input |]
    |> node_to_arr


  let avg_pool1d ?(padding = SAME) input kernel stride =
    make_then_connect (AvgPool1d (padding, kernel, stride)) [| arr_to_node input |]
    |> node_to_arr


  let avg_pool2d ?(padding = SAME) input kernel stride =
    make_then_connect (AvgPool2d (padding, kernel, stride)) [| arr_to_node input |]
    |> node_to_arr


  let avg_pool3d ?(padding = SAME) input kernel stride =
    make_then_connect (AvgPool3d (padding, kernel, stride)) [| arr_to_node input |]
    |> node_to_arr


  let upsampling2d input size =
    make_then_connect (UpSampling2d size) [| arr_to_node input |] |> node_to_arr


  let conv1d_backward_input input kernel stride output' =
    make_then_connect
      (Conv1dBackwardInput stride)
      [| arr_to_node input; arr_to_node kernel; arr_to_node output' |]
    |> node_to_arr


  let conv1d_backward_kernel input kernel stride output' =
    make_then_connect
      (Conv1dBackwardKernel stride)
      [| arr_to_node input; arr_to_node kernel; arr_to_node output' |]
    |> node_to_arr


  let conv2d_backward_input input kernel stride output' =
    make_then_connect
      (Conv2dBackwardInput stride)
      [| arr_to_node input; arr_to_node kernel; arr_to_node output' |]
    |> node_to_arr


  let conv2d_backward_kernel input kernel stride output' =
    make_then_connect
      (Conv2dBackwardKernel stride)
      [| arr_to_node input; arr_to_node kernel; arr_to_node output' |]
    |> node_to_arr


  let conv3d_backward_input input kernel stride output' =
    make_then_connect
      (Conv3dBackwardInput stride)
      [| arr_to_node input; arr_to_node kernel; arr_to_node output' |]
    |> node_to_arr


  let conv3d_backward_kernel input kernel stride output' =
    make_then_connect
      (Conv3dBackwardKernel stride)
      [| arr_to_node input; arr_to_node kernel; arr_to_node output' |]
    |> node_to_arr


  let transpose_conv1d_backward_input input kernel stride output' =
    make_then_connect
      (TransposeConv1dBackwardInput stride)
      [| arr_to_node input; arr_to_node kernel; arr_to_node output' |]
    |> node_to_arr


  let transpose_conv1d_backward_kernel input kernel stride output' =
    make_then_connect
      (TransposeConv1dBackwardKernel stride)
      [| arr_to_node input; arr_to_node kernel; arr_to_node output' |]
    |> node_to_arr


  let transpose_conv2d_backward_input input kernel stride output' =
    make_then_connect
      (TransposeConv2dBackwardInput stride)
      [| arr_to_node input; arr_to_node kernel; arr_to_node output' |]
    |> node_to_arr


  let transpose_conv2d_backward_kernel input kernel stride output' =
    make_then_connect
      (TransposeConv2dBackwardKernel stride)
      [| arr_to_node input; arr_to_node kernel; arr_to_node output' |]
    |> node_to_arr


  let transpose_conv3d_backward_input input kernel stride output' =
    make_then_connect
      (TransposeConv3dBackwardInput stride)
      [| arr_to_node input; arr_to_node kernel; arr_to_node output' |]
    |> node_to_arr


  let transpose_conv3d_backward_kernel input kernel stride output' =
    make_then_connect
      (TransposeConv3dBackwardKernel stride)
      [| arr_to_node input; arr_to_node kernel; arr_to_node output' |]
    |> node_to_arr


  let dilated_conv1d_backward_input input kernel stride rate output' =
    make_then_connect
      (DilatedConv1dBackwardInput (stride, rate))
      [| arr_to_node input; arr_to_node kernel; arr_to_node output' |]
    |> node_to_arr


  let dilated_conv1d_backward_kernel input kernel stride rate output' =
    make_then_connect
      (DilatedConv1dBackwardKernel (stride, rate))
      [| arr_to_node input; arr_to_node kernel; arr_to_node output' |]
    |> node_to_arr


  let dilated_conv2d_backward_input input kernel stride rate output' =
    make_then_connect
      (DilatedConv2dBackwardInput (stride, rate))
      [| arr_to_node input; arr_to_node kernel; arr_to_node output' |]
    |> node_to_arr


  let dilated_conv2d_backward_kernel input kernel stride rate output' =
    make_then_connect
      (DilatedConv2dBackwardKernel (stride, rate))
      [| arr_to_node input; arr_to_node kernel; arr_to_node output' |]
    |> node_to_arr


  let dilated_conv3d_backward_input input kernel stride rate output' =
    make_then_connect
      (DilatedConv3dBackwardInput (stride, rate))
      [| arr_to_node input; arr_to_node kernel; arr_to_node output' |]
    |> node_to_arr


  let dilated_conv3d_backward_kernel input kernel stride rate output' =
    make_then_connect
      (DilatedConv3dBackwardKernel (stride, rate))
      [| arr_to_node input; arr_to_node kernel; arr_to_node output' |]
    |> node_to_arr


  let max_pool1d_backward padding input kernel stride output' =
    make_then_connect
      (MaxPool1dBackward (padding, kernel, stride))
      [| arr_to_node input; arr_to_node output' |]
    |> node_to_arr


  let max_pool2d_backward padding input kernel stride output' =
    make_then_connect
      (MaxPool2dBackward (padding, kernel, stride))
      [| arr_to_node input; arr_to_node output' |]
    |> node_to_arr


  let max_pool3d_backward padding input kernel stride output' =
    make_then_connect
      (MaxPool3dBackward (padding, kernel, stride))
      [| arr_to_node input; arr_to_node output' |]
    |> node_to_arr


  let avg_pool1d_backward padding input kernel stride output' =
    make_then_connect
      (AvgPool1dBackward (padding, kernel, stride))
      [| arr_to_node input; arr_to_node output' |]
    |> node_to_arr


  let avg_pool2d_backward padding input kernel stride output' =
    make_then_connect
      (AvgPool2dBackward (padding, kernel, stride))
      [| arr_to_node input; arr_to_node output' |]
    |> node_to_arr


  let avg_pool3d_backward padding input kernel stride output' =
    make_then_connect
      (AvgPool3dBackward (padding, kernel, stride))
      [| arr_to_node input; arr_to_node output' |]
    |> node_to_arr


  let upsampling2d_backward input size output' =
    make_then_connect
      (UpSampling2dBackward size)
      [| arr_to_node input; arr_to_node output' |]
    |> node_to_arr


  let row_num x =
    let s = shape x in
    let p = Array.length s = 2 in
    let exn = Owl_exception.NOT_MATRIX s in
    Owl_exception.check p exn;
    s.(0)


  let col_num x =
    let s = shape x in
    let p = Array.length s = 2 in
    let exn = Owl_exception.NOT_MATRIX s in
    Owl_exception.check p exn;
    s.(1)


  let row x _i = make_then_connect Row [| arr_to_node x |] |> node_to_arr

  let rows x i = make_then_connect (Rows i) [| arr_to_node x |] |> node_to_arr

  let copy_row_to x _y _i = make_then_connect CopyRowTo [| arr_to_node x |] |> ignore

  let copy_col_to x _y _j = make_then_connect CopyColTo [| arr_to_node x |] |> ignore

  let trace x = make_then_connect Trace [| arr_to_node x |] |> node_to_elt

  let diag ?k _x =
    k |> ignore;
    raise (Owl_exception.NOT_IMPLEMENTED "owl_computation_operator.diag")


  let dot x y =
    let transa = false in
    let transb = false in
    let alpha = A.float_to_elt 1. |> pack_elt in
    let beta = A.float_to_elt 0. |> pack_elt in
    let op = Dot (transa, transb, alpha, beta) in
    make_then_connect op [| arr_to_node x; arr_to_node y |] |> node_to_arr


  let transpose ?axis x =
    let d = Array.length (shape x) in
    let axis =
      match axis with
      | Some a -> a
      | None   -> Array.init d (fun i -> d - i - 1)
    in
    make_then_connect (Transpose axis) [| arr_to_node x |] |> node_to_arr


  let to_rows x =
    let _ = make_then_connect ToRows [| arr_to_node x |] in
    (* FIXME: wrong shape *)
    [||]


  let of_rows xs = make_then_connect OfRows (Array.map arr_to_node xs) |> node_to_arr

  let of_array x shape =
    let parents = Array.map elt_to_node x in
    make_then_connect (OfArray shape) parents |> node_to_arr


  let of_cols _xs =
    raise (Owl_exception.NOT_IMPLEMENTED "owl_computation_operator.of_cols")


  let to_cols _xs =
    raise (Owl_exception.NOT_IMPLEMENTED "owl_computation_operator.to_cols")


  let of_arrays x =
    let shape = [| Array.length x; Array.length x.(0) |] in
    let parents =
      List.map (fun y -> Array.map elt_to_node y) (Array.to_list x) |> Array.concat
    in
    make_then_connect (OfArray shape) parents |> node_to_arr


  let to_arrays _x =
    raise (Owl_exception.NOT_IMPLEMENTED "owl_computation_operator.to_arrays")


  (** Scalar maths *)

  module Scalar = struct
    let add x y =
      make_then_connect Scalar_Add [| elt_to_node x; elt_to_node y |] |> node_to_elt


    let sub x y =
      make_then_connect Scalar_Sub [| elt_to_node x; elt_to_node y |] |> node_to_elt


    let mul x y =
      make_then_connect Scalar_Mul [| elt_to_node x; elt_to_node y |] |> node_to_elt


    let div x y =
      make_then_connect Scalar_Div [| elt_to_node x; elt_to_node y |] |> node_to_elt


    let pow x y =
      make_then_connect Scalar_Pow [| elt_to_node x; elt_to_node y |] |> node_to_elt


    let atan2 x y =
      make_then_connect Scalar_Atan2 [| elt_to_node x; elt_to_node y |] |> node_to_elt


    let abs x = make_then_connect Scalar_Abs [| elt_to_node x |] |> node_to_elt

    let neg x = make_then_connect Scalar_Neg [| elt_to_node x |] |> node_to_elt

    let sqr x = make_then_connect Scalar_Sqr [| elt_to_node x |] |> node_to_elt

    let sqrt x = make_then_connect Scalar_Sqrt [| elt_to_node x |] |> node_to_elt

    let exp x = make_then_connect Scalar_Exp [| elt_to_node x |] |> node_to_elt

    let log x = make_then_connect Scalar_Log [| elt_to_node x |] |> node_to_elt

    let log2 x = make_then_connect Scalar_Log2 [| elt_to_node x |] |> node_to_elt

    let log10 x = make_then_connect Scalar_Log10 [| elt_to_node x |] |> node_to_elt

    let signum x = make_then_connect Scalar_Signum [| elt_to_node x |] |> node_to_elt

    let floor x = make_then_connect Scalar_Floor [| elt_to_node x |] |> node_to_elt

    let ceil x = make_then_connect Scalar_Ceil [| elt_to_node x |] |> node_to_elt

    let round x = make_then_connect Scalar_Round [| elt_to_node x |] |> node_to_elt

    let sin x = make_then_connect Scalar_Sin [| elt_to_node x |] |> node_to_elt

    let cos x = make_then_connect Scalar_Cos [| elt_to_node x |] |> node_to_elt

    let tan x = make_then_connect Scalar_Tan [| elt_to_node x |] |> node_to_elt

    let sinh x = make_then_connect Scalar_Sinh [| elt_to_node x |] |> node_to_elt

    let cosh x = make_then_connect Scalar_Cosh [| elt_to_node x |] |> node_to_elt

    let tanh x = make_then_connect Scalar_Tanh [| elt_to_node x |] |> node_to_elt

    let asin x = make_then_connect Scalar_Asin [| elt_to_node x |] |> node_to_elt

    let acos x = make_then_connect Scalar_Acos [| elt_to_node x |] |> node_to_elt

    let atan x = make_then_connect Scalar_Atan [| elt_to_node x |] |> node_to_elt

    let asinh x = make_then_connect Scalar_Asinh [| elt_to_node x |] |> node_to_elt

    let acosh x = make_then_connect Scalar_Acosh [| elt_to_node x |] |> node_to_elt

    let atanh x = make_then_connect Scalar_Atanh [| elt_to_node x |] |> node_to_elt

    let relu x = make_then_connect Scalar_Relu [| elt_to_node x |] |> node_to_elt

    let dawsn x = make_then_connect Scalar_Dawsn [| elt_to_node x |] |> node_to_elt

    let sigmoid x = make_then_connect Scalar_Sigmoid [| elt_to_node x |] |> node_to_elt
  end

  module Mat = struct
    let eye _n = raise (Owl_exception.NOT_IMPLEMENTED "owl_computation_operator.eye")

    let diagm ?k _x =
      k |> ignore;
      raise (Owl_exception.NOT_IMPLEMENTED "owl_computation_operator.diagm")


    let tril ?k _x =
      k |> ignore;
      raise (Owl_exception.NOT_IMPLEMENTED "owl_computation_operator.tril")


    let triu ?k _x =
      k |> ignore;
      raise (Owl_exception.NOT_IMPLEMENTED "owl_computation_operator.triu")
  end

  module Linalg = struct
    let inv x = make_then_connect Inv [| arr_to_node x |] |> node_to_arr

    let logdet _x =
      raise (Owl_exception.NOT_IMPLEMENTED "owl_computation_operator.logdet")


    let chol ?(upper = true) _x =
      upper |> ignore;
      raise (Owl_exception.NOT_IMPLEMENTED "owl_computation_operator.chol")


    let svd ?(thin = true) _x =
      thin |> ignore;
      raise (Owl_exception.NOT_IMPLEMENTED "owl_computation_operator.svd")


    let qr _x = raise (Owl_exception.NOT_IMPLEMENTED "owl_computation_operator.qr")

    let lq _x = raise (Owl_exception.NOT_IMPLEMENTED "owl_computation_operator.lq")

    let sylvester _a _b _c =
      raise (Owl_exception.NOT_IMPLEMENTED "owl_computation_operator.sylvester")


    let lyapunov _a _q =
      raise (Owl_exception.NOT_IMPLEMENTED "owl_computation_operator.lyapunov")


    let discrete_lyapunov ?(solver = `default) _a _q =
      solver |> ignore;
      raise (Owl_exception.NOT_IMPLEMENTED "owl_computation_operator.discrete_lyapunov")


    let linsolve ?trans ?(typ = `n) _a _b =
      trans |> ignore;
      typ |> ignore;
      raise (Owl_exception.NOT_IMPLEMENTED "owl_computation_operator.linsolve")


    let care ?(diag_r = false) _a _b _q _r =
      diag_r |> ignore;
      raise (Owl_exception.NOT_IMPLEMENTED "owl_computation_operator.care")


    let dare ?(diag_r = false) _a _b _q _r =
      diag_r |> ignore;
      raise (Owl_exception.NOT_IMPLEMENTED "owl_computation_operator.dare")
  end
end

(* Make functor ends *)
OCaml

Innovation. Community. Security.