Source file owl_computation_shape.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
# 1 "src/base/compute/owl_computation_shape.ml"
(*
 * OWL - OCaml Scientific and Engineering Computing
 * Copyright (c) 2016-2020 Liang Wang <liang.wang@cl.cam.ac.uk>
 *)

open Owl_types

(* Functor of making the shape inference of a computation graph. *)

module Make (Type : Owl_computation_type_sig.Sig) = struct
  module Type = Type
  open Type

  (* infer the shape of outcome from inputs *)

  let _infer_shape_00 _input_shapes = [| Some [||] |]

  let _infer_shape_01 input_shapes =
    match input_shapes.(0).(0) with
    | Some s -> [| Some Array.(copy s) |]
    | None   -> [| None |]


  let _infer_shape_02 input_shapes =
    match input_shapes.(1).(0) with
    | Some s -> [| Some Array.(copy s) |]
    | None   -> [| None |]


  let _infer_shape_03 input_shapes =
    let s0 = input_shapes.(0).(0) in
    let s1 = input_shapes.(1).(0) in
    match s0, s1 with
    | Some s0, Some s1 -> [| Some Owl_utils_infer_shape.(broadcast1 s0 s1) |]
    | _, _             -> [| None |]


  let _infer_shape_04 input_shapes axis =
    match input_shapes.(0).(0) with
    | Some s -> [| Some Owl_utils_infer_shape.(fold s axis) |]
    | None   -> [| None |]


  let _infer_shape_05 input_shapes repeats =
    match input_shapes.(0).(0) with
    | Some s -> [| Some Owl_utils_infer_shape.(tile s repeats) |]
    | None   -> [| None |]


  let _infer_shape_06 input_shapes repeats =
    match input_shapes.(0).(0) with
    | Some s -> [| Some Owl_utils_infer_shape.(repeat s repeats) |]
    | None   -> [| None |]


  let _infer_shape_07 input_shapes axis =
    let s0 = Array.map (fun s -> s.(0)) input_shapes in
    if Array.exists
         (function
           | Some _ -> false
           | None   -> true)
         s0
    then [| None |]
    else (
      let s1 =
        Array.map
          (function
            | Some a -> a
            | None   -> failwith "_infer_shape_07")
          s0
      in
      [| Some Owl_utils_infer_shape.(concatenate s1 axis) |])


  let _infer_shape_08 input_shapes axis parts =
    match input_shapes.(0).(0) with
    | Some s ->
      let s0 = Owl_utils_infer_shape.(split s axis parts) in
      Array.map (fun s -> Some s) s0
    | None   -> Array.(make (length parts) None)


  let _infer_shape_09 input_shapes axis n =
    match input_shapes.(0).(0) with
    | Some s -> [| Some Owl_utils_infer_shape.(draw s axis n) |]
    | None   -> [| None |]


  let _infer_shape_10 input_shapes axis =
    match input_shapes.(0).(0) with
    | Some s -> [| Some Owl_utils_infer_shape.(reduce s axis) |]
    | None   -> [| None |]


  let _infer_shape_11 input_shapes padding stride =
    let input_shape = input_shapes.(0).(0) in
    let kernel_shape = input_shapes.(1).(0) in
    match input_shape, kernel_shape with
    | Some input, Some kernel ->
      [| Some Owl_utils_infer_shape.(conv1d input padding kernel stride) |]
    | _, _                    -> [| None |]


  let _infer_shape_12 input_shapes padding stride =
    let input_shape = input_shapes.(0).(0) in
    let kernel_shape = input_shapes.(1).(0) in
    match input_shape, kernel_shape with
    | Some input, Some kernel ->
      [| Some Owl_utils_infer_shape.(conv2d input padding kernel stride) |]
    | _, _                    -> [| None |]


  let _infer_shape_13 input_shapes padding stride =
    let input_shape = input_shapes.(0).(0) in
    let kernel_shape = input_shapes.(1).(0) in
    match input_shape, kernel_shape with
    | Some input, Some kernel ->
      [| Some Owl_utils_infer_shape.(conv3d input padding kernel stride) |]
    | _, _                    -> [| None |]


  let _infer_shape_14 input_shapes padding stride =
    let input_shape = input_shapes.(0).(0) in
    let kernel_shape = input_shapes.(1).(0) in
    match input_shape, kernel_shape with
    | Some input, Some kernel ->
      [| Some Owl_utils_infer_shape.(transpose_conv2d input padding kernel stride) |]
    | _, _                    -> [| None |]


  let _infer_shape_15 input_shapes padding kernel stride =
    let input_shape = input_shapes.(0).(0) in
    match input_shape with
    | Some input -> [| Some Owl_utils_infer_shape.(conv1d input padding kernel stride) |]
    | _          -> [| None |]


  let _infer_shape_16 input_shapes padding kernel stride =
    let input_shape = input_shapes.(0).(0) in
    match input_shape with
    | Some input -> [| Some Owl_utils_infer_shape.(conv2d input padding kernel stride) |]
    | _          -> [| None |]


  let _infer_shape_17 input_shapes padding kernel stride =
    let input_shape = input_shapes.(0).(0) in
    match input_shape with
    | Some input -> [| Some Owl_utils_infer_shape.(conv3d input padding kernel stride) |]
    | _          -> [| None |]


  let _infer_shape_18 input_shapes axis =
    match input_shapes.(0).(0) with
    | Some s -> [| Some Owl_utils_infer_shape.(transpose s axis) |]
    | None   -> [| None |]


  let _infer_shape_19 input_shapes =
    let x_shape = input_shapes.(0).(0) in
    let y_shape = input_shapes.(1).(0) in
    match x_shape, y_shape with
    | Some s0, Some s1 -> [| Some Owl_utils_infer_shape.(dot s0 s1) |]
    | _, _             -> [| None |]


  let _infer_shape_20 input_shapes axis =
    match input_shapes.(0).(0) with
    | Some s ->
      let axis = List.map (fun i -> R_ (Array.of_list i)) axis |> Array.of_list in
      let axis = Owl_base_slicing.check_slice_definition axis s in
      [| Some Owl_base_slicing.(calc_slice_shape axis) |]
    | None   -> [| None |]


  let _infer_shape_21 input_shapes padding kernel stride =
    let input_shape = input_shapes.(0).(0) in
    match input_shape with
    | Some input -> [| Some Owl_utils_infer_shape.(pool2d input padding kernel stride) |]
    | _          -> [| None |]


  let _infer_shape_22 input_shapes depth =
    match input_shapes.(0).(0) with
    | Some s -> [| Some Owl_utils_infer_shape.(onehot s depth) |]
    | None   -> [| None |]


  let _infer_shape_23 input_shapes =
    let s0 = input_shapes.(0).(0) in
    let s1 = input_shapes.(1).(0) in
    let s2 = input_shapes.(2).(0) in
    match s0, s1, s2 with
    | Some s0, Some s1, Some s2 -> [| Some Owl_utils_infer_shape.(broadcast2 s0 s1 s2) |]
    | _, _, _                   -> [| None |]


  let _infer_shape_24 input_shapes padding stride =
    let input_shape = input_shapes.(0).(0) in
    let kernel_shape = input_shapes.(1).(0) in
    match input_shape, kernel_shape with
    | Some input, Some kernel ->
      [| Some Owl_utils_infer_shape.(transpose_conv1d input padding kernel stride) |]
    | _, _                    -> [| None |]


  let _infer_shape_25 input_shapes padding stride =
    let input_shape = input_shapes.(0).(0) in
    let kernel_shape = input_shapes.(1).(0) in
    match input_shape, kernel_shape with
    | Some input, Some kernel ->
      [| Some Owl_utils_infer_shape.(transpose_conv3d input padding kernel stride) |]
    | _, _                    -> [| None |]


  let _infer_shape_26 input_shapes padding stride rate =
    let input_shape = input_shapes.(0).(0) in
    let kernel_shape = input_shapes.(1).(0) in
    match input_shape, kernel_shape with
    | Some input, Some kernel ->
      [| Some Owl_utils_infer_shape.(dilated_conv1d input padding kernel stride rate) |]
    | _, _                    -> [| None |]


  let _infer_shape_27 input_shapes padding stride rate =
    let input_shape = input_shapes.(0).(0) in
    let kernel_shape = input_shapes.(1).(0) in
    match input_shape, kernel_shape with
    | Some input, Some kernel ->
      [| Some Owl_utils_infer_shape.(dilated_conv2d input padding kernel stride rate) |]
    | _, _                    -> [| None |]


  let _infer_shape_28 input_shapes padding stride rate =
    let input_shape = input_shapes.(0).(0) in
    let kernel_shape = input_shapes.(1).(0) in
    match input_shape, kernel_shape with
    | Some input, Some kernel ->
      [| Some Owl_utils_infer_shape.(dilated_conv3d input padding kernel stride rate) |]
    | _, _                    -> [| None |]


  let _infer_shape_29 input_shapes size =
    let input_shape = input_shapes.(0).(0) in
    match input_shape with
    | Some input -> [| Some Owl_utils_infer_shape.(upsampling2d input size) |]
    | _          -> [| None |]


  let _infer_shape_30 input_shapes pad =
    let input_shape = input_shapes.(0).(0) in
    let pad = Owl_utils.llss2aarr pad in
    match input_shape with
    | Some input -> [| Some (Array.map2 (fun m n -> m + n.(0) + n.(1)) input pad) |]
    | _          -> [| None |]


  let infer_shape operator args =
    let input_shapes = Array.map (fun a -> (Owl_graph.attr a).shape) args in
    match operator with
    | Noop -> _infer_shape_01 input_shapes
    | Create shape -> [| Some shape |]
    | Get _ -> _infer_shape_00 input_shapes
    | GetSlice slice -> _infer_shape_20 input_shapes slice
    | Copy -> _infer_shape_01 input_shapes
    | Reshape shape -> [| Some shape |]
    | Reverse -> _infer_shape_01 input_shapes
    | Tile repeats -> _infer_shape_05 input_shapes repeats
    | Repeat repeats -> _infer_shape_06 input_shapes repeats
    | Pad (_v, padding) -> _infer_shape_30 input_shapes padding
    | Concatenate axis -> _infer_shape_07 input_shapes axis
    | Split (axis, parts) -> _infer_shape_08 input_shapes axis parts
    | Draw (axis, n) -> _infer_shape_09 input_shapes axis n
    | Map _ -> _infer_shape_01 input_shapes
    | Fold (axis, _f) -> _infer_shape_04 input_shapes axis
    | Scan (_axis, _f) -> _infer_shape_01 input_shapes
    | OneHot depth -> _infer_shape_22 input_shapes depth
    | OfArray shape -> [| Some shape |]
    | Abs -> _infer_shape_01 input_shapes
    | Delay _f -> _infer_shape_01 input_shapes
    | DelayArray (shape, _f) -> [| Some shape |]
    | LazyPrint (_max_row, _max_col, _header, _fmt) -> _infer_shape_01 input_shapes
    | Neg -> _infer_shape_01 input_shapes
    | Floor -> _infer_shape_01 input_shapes
    | Ceil -> _infer_shape_01 input_shapes
    | Round -> _infer_shape_01 input_shapes
    | Sqr -> _infer_shape_01 input_shapes
    | Sqrt -> _infer_shape_01 input_shapes
    | Log -> _infer_shape_01 input_shapes
    | Log2 -> _infer_shape_01 input_shapes
    | Log10 -> _infer_shape_01 input_shapes
    | Exp -> _infer_shape_01 input_shapes
    | Sin -> _infer_shape_01 input_shapes
    | Cos -> _infer_shape_01 input_shapes
    | Tan -> _infer_shape_01 input_shapes
    | Sinh -> _infer_shape_01 input_shapes
    | Cosh -> _infer_shape_01 input_shapes
    | Tanh -> _infer_shape_01 input_shapes
    | Asin -> _infer_shape_01 input_shapes
    | Acos -> _infer_shape_01 input_shapes
    | Atan -> _infer_shape_01 input_shapes
    | Asinh -> _infer_shape_01 input_shapes
    | Acosh -> _infer_shape_01 input_shapes
    | Atanh -> _infer_shape_01 input_shapes
    | Min axis -> _infer_shape_04 input_shapes axis
    | Max axis -> _infer_shape_04 input_shapes axis
    | Sum axis -> _infer_shape_04 input_shapes axis
    | SumReduce axis -> _infer_shape_10 input_shapes axis
    | Signum -> _infer_shape_01 input_shapes
    | Sigmoid -> _infer_shape_01 input_shapes
    | Relu -> _infer_shape_01 input_shapes
    | Dawsn -> _infer_shape_01 input_shapes
    | Min' -> _infer_shape_00 input_shapes
    | Max' -> _infer_shape_00 input_shapes
    | Sum' -> _infer_shape_00 input_shapes
    | L1norm' -> _infer_shape_00 input_shapes
    | L2norm' -> _infer_shape_00 input_shapes
    | L2NormSqr' -> _infer_shape_00 input_shapes
    | ClipByValue -> _infer_shape_01 input_shapes
    | ClipByL2norm -> _infer_shape_01 input_shapes
    | Pow -> _infer_shape_01 input_shapes
    | ScalarPow -> _infer_shape_02 input_shapes
    | PowScalar -> _infer_shape_01 input_shapes
    | Atan2 -> _infer_shape_03 input_shapes
    | ScalarAtan2 -> _infer_shape_02 input_shapes
    | Atan2Scalar -> _infer_shape_01 input_shapes
    | Hypot -> _infer_shape_01 input_shapes
    | Min2 -> _infer_shape_01 input_shapes
    | Max2 -> _infer_shape_01 input_shapes
    | Add -> _infer_shape_03 input_shapes
    | Sub -> _infer_shape_03 input_shapes
    | Mul -> _infer_shape_03 input_shapes
    | Div -> _infer_shape_03 input_shapes
    | AddScalar -> _infer_shape_01 input_shapes
    | SubScalar -> _infer_shape_01 input_shapes
    | MulScalar -> _infer_shape_01 input_shapes
    | DivScalar -> _infer_shape_01 input_shapes
    | ScalarAdd -> _infer_shape_02 input_shapes
    | ScalarSub -> _infer_shape_02 input_shapes
    | ScalarMul -> _infer_shape_02 input_shapes
    | ScalarDiv -> _infer_shape_02 input_shapes
    | FMA -> _infer_shape_23 input_shapes
    | EltEqual -> _infer_shape_01 input_shapes
    | EltNotEqual -> _infer_shape_01 input_shapes
    | EltLess -> _infer_shape_01 input_shapes
    | EltGreater -> _infer_shape_01 input_shapes
    | EltLessEqual -> _infer_shape_01 input_shapes
    | EltGreaterEqual -> _infer_shape_01 input_shapes
    | EltEqualScalar -> _infer_shape_01 input_shapes
    | EltNotEqualScalar -> _infer_shape_01 input_shapes
    | EltLessScalar -> _infer_shape_01 input_shapes
    | EltGreaterScalar -> _infer_shape_01 input_shapes
    | EltLessEqualScalar -> _infer_shape_01 input_shapes
    | EltGreaterEqualScalar -> _infer_shape_01 input_shapes
    | Conv1d (padding, stride) -> _infer_shape_11 input_shapes padding stride
    | Conv2d (padding, stride) -> _infer_shape_12 input_shapes padding stride
    | Conv3d (padding, stride) -> _infer_shape_13 input_shapes padding stride
    | TransposeConv1d (padding, stride) -> _infer_shape_24 input_shapes padding stride
    | TransposeConv2d (padding, stride) -> _infer_shape_14 input_shapes padding stride
    | TransposeConv3d (padding, stride) -> _infer_shape_25 input_shapes padding stride
    | DilatedConv1d (padding, stride, rate) ->
      _infer_shape_26 input_shapes padding stride rate
    | DilatedConv2d (padding, stride, rate) ->
      _infer_shape_27 input_shapes padding stride rate
    | DilatedConv3d (padding, stride, rate) ->
      _infer_shape_28 input_shapes padding stride rate
    | MaxPool1d (padding, kernel, stride) ->
      _infer_shape_15 input_shapes padding kernel stride
    | MaxPool2d (padding, kernel, stride) ->
      _infer_shape_21 input_shapes padding kernel stride
    | MaxPool3d (padding, kernel, stride) ->
      _infer_shape_17 input_shapes padding kernel stride
    | AvgPool1d (padding, kernel, stride) ->
      _infer_shape_15 input_shapes padding kernel stride
    | AvgPool2d (padding, kernel, stride) ->
      _infer_shape_21 input_shapes padding kernel stride
    | AvgPool3d (padding, kernel, stride) ->
      _infer_shape_17 input_shapes padding kernel stride
    | UpSampling2d size -> _infer_shape_29 input_shapes size
    | Conv1dBackwardInput _stride -> _infer_shape_01 input_shapes
    | Conv1dBackwardKernel _stride -> _infer_shape_02 input_shapes
    | Conv2dBackwardInput _stride -> _infer_shape_01 input_shapes
    | Conv2dBackwardKernel _stride -> _infer_shape_02 input_shapes
    | Conv3dBackwardInput _stride -> _infer_shape_01 input_shapes
    | Conv3dBackwardKernel _stride -> _infer_shape_02 input_shapes
    | TransposeConv1dBackwardInput _stride -> _infer_shape_01 input_shapes
    | TransposeConv1dBackwardKernel _stride -> _infer_shape_02 input_shapes
    | TransposeConv2dBackwardInput _stride -> _infer_shape_01 input_shapes
    | TransposeConv2dBackwardKernel _stride -> _infer_shape_02 input_shapes
    | TransposeConv3dBackwardInput _stride -> _infer_shape_01 input_shapes
    | TransposeConv3dBackwardKernel _stride -> _infer_shape_02 input_shapes
    | DilatedConv1dBackwardInput (_stride, _rate) -> _infer_shape_01 input_shapes
    | DilatedConv1dBackwardKernel (_stride, _rate) -> _infer_shape_02 input_shapes
    | DilatedConv2dBackwardInput (_stride, _rate) -> _infer_shape_01 input_shapes
    | DilatedConv2dBackwardKernel (_stride, _rate) -> _infer_shape_02 input_shapes
    | DilatedConv3dBackwardInput (_stride, _rate) -> _infer_shape_01 input_shapes
    | DilatedConv3dBackwardKernel (_stride, _rate) -> _infer_shape_02 input_shapes
    | MaxPool1dBackward (_padding, _kernel, _stride) -> _infer_shape_01 input_shapes
    | MaxPool2dBackward (_padding, _kernel, _stride) -> _infer_shape_01 input_shapes
    | MaxPool3dBackward (_padding, _kernel, _stride) -> _infer_shape_01 input_shapes
    | AvgPool1dBackward (_padding, _kernel, _stride) -> _infer_shape_01 input_shapes
    | AvgPool2dBackward (_padding, _kernel, _stride) -> _infer_shape_01 input_shapes
    | AvgPool3dBackward (_padding, _kernel, _stride) -> _infer_shape_01 input_shapes
    | UpSampling2dBackward _size -> _infer_shape_01 input_shapes
    | Row -> _infer_shape_09 input_shapes 0 1
    | Rows i -> _infer_shape_09 input_shapes 0 Array.(length i)
    | Dot (_transa, _transb, _alpha, _beta) -> _infer_shape_19 input_shapes
    | Inv -> _infer_shape_01 input_shapes
    | Trace -> _infer_shape_00 input_shapes
    | Transpose axis -> _infer_shape_18 input_shapes axis
    | ToRows -> failwith "_infer_shape:ToRows not implemented"
    | OfRows -> failwith "_infer_shape:OfRows not implemented"
    | Scalar_Add -> _infer_shape_00 input_shapes
    | Scalar_Sub -> _infer_shape_00 input_shapes
    | Scalar_Mul -> _infer_shape_00 input_shapes
    | Scalar_Div -> _infer_shape_00 input_shapes
    | Scalar_Pow -> _infer_shape_00 input_shapes
    | Scalar_Atan2 -> _infer_shape_00 input_shapes
    | Scalar_Abs -> _infer_shape_00 input_shapes
    | Scalar_Neg -> _infer_shape_00 input_shapes
    | Scalar_Sqr -> _infer_shape_00 input_shapes
    | Scalar_Sqrt -> _infer_shape_00 input_shapes
    | Scalar_Exp -> _infer_shape_00 input_shapes
    | Scalar_Log -> _infer_shape_00 input_shapes
    | Scalar_Log2 -> _infer_shape_00 input_shapes
    | Scalar_Log10 -> _infer_shape_00 input_shapes
    | Scalar_Signum -> _infer_shape_00 input_shapes
    | Scalar_Floor -> _infer_shape_00 input_shapes
    | Scalar_Ceil -> _infer_shape_00 input_shapes
    | Scalar_Round -> _infer_shape_00 input_shapes
    | Scalar_Sin -> _infer_shape_00 input_shapes
    | Scalar_Cos -> _infer_shape_00 input_shapes
    | Scalar_Tan -> _infer_shape_00 input_shapes
    | Scalar_Sinh -> _infer_shape_00 input_shapes
    | Scalar_Cosh -> _infer_shape_00 input_shapes
    | Scalar_Tanh -> _infer_shape_00 input_shapes
    | Scalar_Asin -> _infer_shape_00 input_shapes
    | Scalar_Acos -> _infer_shape_00 input_shapes
    | Scalar_Atan -> _infer_shape_00 input_shapes
    | Scalar_Asinh -> _infer_shape_00 input_shapes
    | Scalar_Acosh -> _infer_shape_00 input_shapes
    | Scalar_Atanh -> _infer_shape_00 input_shapes
    | Scalar_Relu -> _infer_shape_00 input_shapes
    | Scalar_Dawsn -> _infer_shape_00 input_shapes
    | Scalar_Sigmoid -> _infer_shape_00 input_shapes
    | Fused_Adagrad (_rate, _eps) -> _infer_shape_01 input_shapes
    | _ -> [| None |]
end

(* Make functor ends *)