forked from Samsung/ONE
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCircleShapeInferenceRule.cpp
More file actions
2297 lines (1775 loc) · 73.4 KB
/
CircleShapeInferenceRule.cpp
File metadata and controls
2297 lines (1775 loc) · 73.4 KB
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
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2020 Samsung Electronics Co., Ltd. All Rights Reserved
* Copyright 2017 The TensorFlow Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "luci/Service/CircleShapeInferenceRule.h"
#include "Check.h"
#include "CircleShapeInferenceHelper.h"
#include <luci/IR/CircleNodes.h>
#include <luci/IR/CircleDialect.h>
#include <luci/IR/CircleNodeVisitor.h>
#include <luci/Log.h>
#include <oops/InternalExn.h>
#include <algorithm>
#include <cassert>
#include <cmath>
#include <limits>
#include <stdexcept>
namespace
{
std::ostream &operator<<(std::ostream &os, const loco::TensorShape &tensor_shape)
{
os << "[";
for (uint32_t r = 0; r < tensor_shape.rank(); ++r)
{
if (r)
os << ",";
if (tensor_shape.dim(r).known())
os << tensor_shape.dim(r).value();
else
os << "?";
}
os << "]";
return os;
}
loco::TensorShape own_shape(const luci::CircleNode *node)
{
loco::TensorShape shape;
shape.rank(node->rank());
for (uint32_t r = 0; r < node->rank(); ++r)
{
// Shape inference rules in this file did not consider unknown dimension.
// If some node has unknown dimension, 0 is inserted and wrong shape
// inference was done as a result.
// To fix this, new shape inference algorithm is being implemented.
// Until new inference algorithm is fully implemented, unknown dimension
// would be represented as 1 along with TFLite expression.
shape.dim(r) = node->dim(r).known() ? node->dim(r).value() : 1;
}
return shape;
}
loco::NodeShape use_own(const luci::CircleNode *node)
{
loco::TensorShape shape = own_shape(node);
return loco::NodeShape{shape};
}
/**
* @brief Create a higher-rank TensorShape following NumPy broadcasting semantics
*
* HOW TO USE:
*
* auto expanded_tensor_shape = expand(tensor_shape).to(N);
*/
class TensorShapeExpander
{
public:
TensorShapeExpander(const loco::TensorShape &shape) : _shape{shape}
{
// DO NOTHING
}
public:
loco::TensorShape to(uint32_t output_rank)
{
auto const &input_shape = _shape;
uint32_t const input_rank = input_shape.rank();
assert(input_rank <= output_rank && "Cannot shrink rank");
uint32_t const axis_shift = output_rank - input_rank;
loco::TensorShape output_shape;
output_shape.rank(output_rank);
for (uint32_t axis = 0; axis < output_rank; ++axis)
{
output_shape.dim(axis) = (axis < axis_shift) ? 1 : input_shape.dim(axis - axis_shift);
}
return output_shape;
}
private:
const loco::TensorShape _shape;
};
/**
* @brief Expand shape x and y to same rank by align right and filling with 1
*/
void expand_rank(loco::TensorShape &x, loco::TensorShape &y)
{
auto x_rank = x.rank();
auto y_rank = y.rank();
if (x_rank == y_rank)
return;
TensorShapeExpander x_exp(x);
TensorShapeExpander y_exp(y);
auto xy_rank = std::max(x_rank, y_rank);
x = x_rank > y_rank ? x : x_exp.to(xy_rank);
y = y_rank > x_rank ? y : y_exp.to(xy_rank);
}
/**
* @brief Returns shape of expanded dimension of input x and y having same rank
*/
loco::TensorShape expand_dimension(const loco::TensorShape &x, const loco::TensorShape &y)
{
assert(x.rank() == y.rank());
auto rank = x.rank();
loco::TensorShape output_shape;
output_shape.rank(rank);
for (uint32_t axis = 0; axis < rank; ++axis)
{
auto x_dim = x.dim(axis).known() ? x.dim(axis).value() : 1;
auto y_dim = y.dim(axis).known() ? y.dim(axis).value() : 1;
// each dimension of x and y should be same or one must be 1 if different
if (!((x_dim == y_dim) || (x_dim == 1 || y_dim == 1)))
INTERNAL_EXN("Cannot produce expand_dimension of two shapes");
output_shape.dim(axis) = std::max(x_dim, y_dim);
}
return output_shape;
}
loco::TensorShape broadcast_shape(const loco::TensorShape &x, const loco::TensorShape &y)
{
auto x_match = x;
auto y_match = y;
expand_rank(x_match, y_match);
auto output_shape = expand_dimension(x_match, y_match);
return output_shape;
}
/**
* @brief vector_from_constant will return int64_t vector from CircleConst node
*/
template <loco::DataType T> std::vector<int64_t> vector_from_constant(luci::CircleConst *const_node)
{
std::vector<int64_t> result;
for (uint32_t idx = 0; idx < const_node->size<T>(); ++idx)
result.push_back(const_node->at<T>(idx));
return result;
}
template <class CIRCLENODE> loco::NodeShape broadcast_xy(const CIRCLENODE *node)
{
auto x_shape = luci::shape_get(node->x()).template as<loco::TensorShape>();
auto y_shape = luci::shape_get(node->y()).template as<loco::TensorShape>();
auto output_shape = broadcast_shape(x_shape, y_shape);
return loco::NodeShape{output_shape};
}
#define DECLARE_USE_SINGLE(NAME) \
template <class CIRCLENODE> loco::NodeShape use_##NAME(const CIRCLENODE *node) \
{ \
auto inputs_shape = luci::shape_get(node->NAME()).template as<loco::TensorShape>(); \
return loco::NodeShape{inputs_shape}; \
}
DECLARE_USE_SINGLE(input);
DECLARE_USE_SINGLE(inputs);
DECLARE_USE_SINGLE(x);
DECLARE_USE_SINGLE(logits);
#undef DECLARE_USE_SINGLE
template <class CIRCLENODE>
loco::NodeShape use_paddings(const CIRCLENODE *node, const luci::CircleConst *paddings)
{
const loco::DataType S32 = loco::DataType::S32;
const loco::DataType S64 = loco::DataType::S64;
auto input_shape = luci::shape_get(node->input()).template as<loco::TensorShape>();
// TODO support other data type
LUCI_ASSERT(paddings->dtype() == S32 || paddings->dtype() == S64, "Support int 32/64 for now");
LUCI_ASSERT(paddings->rank() == 2, "paddings should be rank 2")
int32_t n = paddings->dim(0).value();
int32_t v = paddings->dim(1).value();
LUCI_ASSERT(v == 2, "paddings should be [n, 2]");
LUCI_ASSERT(n == int32_t(input_shape.rank()),
"paddings [n, 2] should have same value of input rank");
loco::TensorShape output_shape;
output_shape.rank(input_shape.rank());
for (int32_t ni = 0; ni < n; ++ni)
{
int32_t idx = ni * 2;
int value = input_shape.dim(ni).value();
if (paddings->dtype() == S32)
{
value += paddings->at<S32>(idx + 0); // left
value += paddings->at<S32>(idx + 1); // right
}
else
{
auto pl = paddings->at<S64>(idx + 0);
auto pr = paddings->at<S64>(idx + 1);
auto max = static_cast<int64_t>(std::numeric_limits<int32_t>::max());
auto low = static_cast<int64_t>(std::numeric_limits<int32_t>::lowest());
LUCI_ASSERT(pl <= max, "paddings is over 32 bit limit");
LUCI_ASSERT(pl >= low, "paddings is over 32 bit limit");
LUCI_ASSERT(pr <= max, "paddings is over 32 bit limit");
LUCI_ASSERT(pr >= low, "paddings is over 32 bit limit");
value += static_cast<int32_t>(pl); // left
value += static_cast<int32_t>(pr); // right
}
output_shape.dim(ni) = value;
}
return loco::NodeShape{output_shape};
}
loco::NodeShape infer_add_n(const luci::CircleAddN *node)
{
auto shape = luci::shape_get(node->inputs(0)).as<loco::TensorShape>();
for (uint32_t idx = 1; idx < node->arity(); ++idx)
{
auto shape_idx = luci::shape_get(node->inputs(idx)).as<loco::TensorShape>();
if (!(shape == shape_idx))
{
INTERNAL_EXN_V("ADD_N shape not same as the first input: ", idx);
}
}
return loco::NodeShape{shape};
}
template <class CIRCLENODE> loco::NodeShape infer_arg_maxmin(const CIRCLENODE *node)
{
auto input_shape = luci::shape_get(node->input()).template as<loco::TensorShape>();
auto dimension_shape = luci::shape_get(node->dimension()).template as<loco::TensorShape>();
int64_t select_axis = 0;
{
LUCI_ASSERT(node->dimension(), "2nd input dimension() should not be nullptr");
// Only support node's shape() is CircleConst with S32/S64
// Support S32 for now.
auto const_shape_node = luci::must_cast<luci::CircleConst *>(node->dimension());
LUCI_ASSERT(const_shape_node->dtype() == loco::DataType::S32,
"Only support int32 CircleConst for CircleArgMax/CircleArgMin");
if (const_shape_node->rank() > 1)
INTERNAL_EXN_V("Only support rank 0/1 CircleConst",
oops::to_uint32(const_shape_node->rank()));
select_axis = const_shape_node->template scalar<loco::DataType::S32>();
}
assert(select_axis < input_shape.rank());
if (select_axis < 0)
select_axis += static_cast<int64_t>(input_shape.rank());
// NOTE select_axis is removed
loco::TensorShape shape_output;
uint32_t rank = input_shape.rank();
uint32_t shrink = static_cast<uint32_t>(select_axis);
assert(rank > 0);
shape_output.rank(rank - 1);
for (uint32_t r = 0, d = 0; r < rank; ++r)
{
if (r == shrink)
continue;
shape_output.dim(d++) = input_shape.dim(r);
}
return loco::NodeShape{shape_output};
}
// Call this for CircleAvgPool2D and CircleMaxPool2D only
template <class Pool2DType> loco::NodeShape infer_pool_2d_shape(const Pool2DType *node)
{
auto ifm_shape = luci::shape_get(node->value()).template as<loco::TensorShape>();
assert(ifm_shape.rank() == 4);
assert(ifm_shape.dim(1).known());
assert(ifm_shape.dim(2).known());
uint32_t input_height = ifm_shape.dim(1).value();
uint32_t input_width = ifm_shape.dim(2).value();
uint32_t stride_height = node->stride()->h();
uint32_t stride_width = node->stride()->w();
uint32_t window_height = node->filter()->h();
uint32_t window_width = node->filter()->w();
uint32_t dilation_height = 1; // dilation for CircleAvgPool2D and CircleMaxPool2D is 1
uint32_t dilation_width = 1;
uint32_t effective_window_height = dilation_height * (window_height - 1) + 1;
uint32_t effective_window_width = dilation_width * (window_width - 1) + 1;
uint32_t output_height = 0;
uint32_t output_width = 0;
if (node->padding() == luci::Padding::VALID)
{
LUCI_ASSERT(input_height + stride_height > effective_window_height, "Invalid shape");
LUCI_ASSERT(input_width + stride_width > effective_window_width, "Invalid shape");
output_height = (input_height + stride_height - effective_window_height) / stride_height;
output_width = (input_width + stride_width - effective_window_width) / stride_width;
}
else if (node->padding() == luci::Padding::SAME)
{
output_height = (input_height + stride_height - 1) / stride_height;
output_width = (input_width + stride_width - 1) / stride_width;
}
else
LUCI_ASSERT(false, "Wrong padding type");
loco::TensorShape ofm_shape;
ofm_shape.rank(4);
ofm_shape.dim(0) = ifm_shape.dim(0);
ofm_shape.dim(1) = output_height;
ofm_shape.dim(2) = output_width;
ofm_shape.dim(3) = ifm_shape.dim(3);
return loco::NodeShape{ofm_shape};
}
loco::NodeShape infer_batch_to_space_nd(const luci::CircleBatchToSpaceND *node)
{
const loco::DataType S32 = loco::DataType::S32;
auto input_shape = luci::shape_get(node->input()).as<loco::TensorShape>();
// Support only input rank is 3 and 4
assert(input_shape.rank() == 3 || input_shape.rank() == 4);
// Only support block_shape() with S32 type CircleConst for now
auto const_block_shape = luci::must_cast<luci::CircleConst *>(node->block_shape());
LUCI_ASSERT(const_block_shape->dtype() == loco::DataType::S32, "Only support int32 block_shape");
// Only support crops() with S32 type CircleConst for now
auto const_crops = luci::must_cast<luci::CircleConst *>(node->crops());
LUCI_ASSERT(const_crops->dtype() == loco::DataType::S32, "Only support int32 crops");
auto const_block_shape_shape = luci::shape_get(const_block_shape).as<loco::TensorShape>();
auto const_crops_shape = luci::shape_get(const_crops).as<loco::TensorShape>();
assert(const_block_shape_shape.rank() == 1);
assert(const_crops_shape.rank() == 2);
int32_t input_spatial_dim = input_shape.rank() - 2;
assert(const_block_shape_shape.dim(0) == input_spatial_dim);
assert(const_crops_shape.dim(0) == input_spatial_dim);
assert(const_crops_shape.dim(1) == 2);
loco::TensorShape shape_output;
shape_output.rank(input_shape.rank());
int32_t output_batch_size = input_shape.dim(0).value();
for (int32_t dim = 0; dim < input_spatial_dim; ++dim)
{
int dim_size = input_shape.dim(dim + 1).value() * const_block_shape->at<S32>(dim);
dim_size -= const_crops->at<S32>(dim * 2);
dim_size -= const_crops->at<S32>(dim * 2 + 1);
shape_output.dim(dim + 1) = dim_size;
assert(output_batch_size % const_block_shape->at<S32>(dim) == 0);
output_batch_size = output_batch_size / const_block_shape->at<S32>(dim);
}
shape_output.dim(0) = output_batch_size;
shape_output.dim(input_shape.rank() - 1) = input_shape.dim(input_shape.rank() - 1);
return loco::NodeShape{shape_output};
}
struct OutputSize
{
uint32_t height = 0;
uint32_t width = 0;
};
template <class Conv2DType> OutputSize infer_conv2d_type(const Conv2DType *node)
{
auto ifm_shape = luci::shape_get(node->input()).template as<loco::TensorShape>();
auto ker_shape = luci::shape_get(node->filter()).template as<loco::TensorShape>();
assert(ifm_shape.rank() == 4);
assert(ker_shape.rank() == 4);
assert(ifm_shape.dim(1).known());
assert(ifm_shape.dim(2).known());
assert(ker_shape.dim(1).known());
assert(ker_shape.dim(2).known());
uint32_t input_height = ifm_shape.dim(1).value();
uint32_t input_width = ifm_shape.dim(2).value();
uint32_t stride_height = node->stride()->h();
uint32_t stride_width = node->stride()->w();
uint32_t ker_height = ker_shape.dim(1).value();
uint32_t ker_width = ker_shape.dim(2).value();
uint32_t dilation_height = node->dilation()->h();
uint32_t dilation_width = node->dilation()->w();
uint32_t effective_ker_height = dilation_height * (ker_height - 1) + 1;
uint32_t effective_ker_width = dilation_width * (ker_width - 1) + 1;
uint32_t output_height = 0;
uint32_t output_width = 0;
if (node->padding() == luci::Padding::VALID)
{
LUCI_ASSERT(input_height + stride_height > effective_ker_height, "Invalid shape");
LUCI_ASSERT(input_width + stride_width > effective_ker_width, "Invalid shape");
output_height = (input_height + stride_height - effective_ker_height) / stride_height;
output_width = (input_width + stride_width - effective_ker_width) / stride_width;
}
else if (node->padding() == luci::Padding::SAME)
{
output_height = (input_height + stride_height - 1) / stride_height;
output_width = (input_width + stride_width - 1) / stride_width;
}
else
LUCI_ASSERT(false, "Wrong padding type");
OutputSize os{output_height, output_width};
return os;
}
loco::NodeShape infer_broadcast_to(const luci::CircleBroadcastTo *node)
{
const loco::DataType S32 = loco::DataType::S32;
loco::TensorShape shape_by_input;
{
LUCI_ASSERT(node->shape(), "2nd input shape() should not be nullptr");
// Only support node's shape() is CircleConst with S32
auto const_shape_node = dynamic_cast<luci::CircleConst *>(node->shape());
if (const_shape_node != nullptr)
{
LUCI_ASSERT(const_shape_node->dtype() == S32, "Only support int32 CircleConst");
shape_by_input.rank(const_shape_node->size<S32>());
for (uint32_t axis = 0; axis < shape_by_input.rank(); ++axis)
{
shape_by_input.dim(axis) = const_shape_node->at<S32>(axis);
}
}
else
{
// We use shape from the node itself
shape_by_input = own_shape(node);
}
}
return loco::NodeShape{shape_by_input};
}
loco::NodeShape infer_conv2d(const luci::CircleConv2D *node)
{
LOGGER(l);
auto ifm_shape = luci::shape_get(node->input()).as<loco::TensorShape>(); // in NHWC
auto ker_shape = luci::shape_get(node->filter()).as<loco::TensorShape>(); // in OHWI
assert(ifm_shape.rank() == 4);
assert(ker_shape.rank() == 4);
assert(ifm_shape.dim(3) == ker_shape.dim(3));
auto os = infer_conv2d_type(node);
loco::TensorShape ofm_shape;
ofm_shape.rank(4);
ofm_shape.dim(0) = ifm_shape.dim(0);
ofm_shape.dim(1) = os.height;
ofm_shape.dim(2) = os.width;
ofm_shape.dim(3) = ker_shape.dim(0);
INFO(l) << "[luci] CircleConv2D ShapeInf ifm(" << ifm_shape.rank() << ") ker(" << ker_shape.rank()
<< ") output(" << ofm_shape.dim(0).value() << "," << ofm_shape.dim(1).value() << ","
<< ofm_shape.dim(2).value() << "," << ofm_shape.dim(3).value() << ") " << node->name()
<< std::endl;
return loco::NodeShape{ofm_shape};
}
loco::NodeShape infer_depth_to_space(const luci::CircleDepthToSpace *node)
{
auto input_shape = luci::shape_get(node->input()).as<loco::TensorShape>();
LUCI_ASSERT(input_shape.rank() == 4, "Only input rank 4 is supported");
// Only data format NHWC is supported
// TODO need to clarify what to do with layout in this operator
int32_t height = input_shape.dim(1).value();
int32_t width = input_shape.dim(2).value();
int32_t depth = input_shape.dim(3).value();
int block_size = node->block_size();
if (block_size < 2)
INTERNAL_EXN("Block size must be >= 2");
if (depth % (block_size * block_size))
{
INTERNAL_EXN("The input tensor's depth must be divisible by block_size^2");
}
loco::TensorShape output_shape;
output_shape.rank(4);
output_shape.dim(0) = input_shape.dim(0).value();
output_shape.dim(1) = height * block_size;
output_shape.dim(2) = width * block_size;
output_shape.dim(3) = depth / (block_size * block_size);
return loco::NodeShape{output_shape};
}
loco::NodeShape infer_depthwise_conv2d(const luci::CircleDepthwiseConv2D *node)
{
auto ifm_shape = luci::shape_get(node->input()).as<loco::TensorShape>(); // in NHWC
auto ker_shape = luci::shape_get(node->filter()).as<loco::TensorShape>(); // in 1 H W CM
assert(ifm_shape.rank() == 4);
assert(ker_shape.rank() == 4);
assert(ker_shape.dim(0).value() == 1);
assert(ifm_shape.dim(3).value() * node->depthMultiplier() == ker_shape.dim(3).value());
auto os = infer_conv2d_type(node);
loco::TensorShape ofm_shape;
ofm_shape.rank(4);
ofm_shape.dim(0) = ifm_shape.dim(0);
ofm_shape.dim(1) = os.height;
ofm_shape.dim(2) = os.width;
ofm_shape.dim(3) = ker_shape.dim(3);
return loco::NodeShape{ofm_shape};
}
loco::NodeShape infer_expand_dims(const luci::CircleExpandDims *node)
{
const loco::DataType S32 = loco::DataType::S32;
auto x_shape = luci::shape_get(node->input()).as<loco::TensorShape>();
if (x_shape.rank() == 0)
{
// This maybe for unknown shape. We use shape from the node itself.
return use_own(node);
}
auto const_axis = luci::must_cast<luci::CircleConst *>(node->axis());
LUCI_ASSERT(const_axis->dtype() == S32, "Only support int32 CircleConst for axis");
if (const_axis->rank() != 0 && const_axis->rank() != 1)
{
INTERNAL_EXN_V("Non-scalar axis in OP", node->opnum());
}
int32_t axis = const_axis->at<S32>(0);
LUCI_ASSERT((axis <= static_cast<int32_t>(x_shape.rank())) &&
(axis >= -1 - static_cast<int32_t>(x_shape.rank())),
"Axis has to be between [-(D+1), D], where D is rank of input.");
size_t positive_axis = axis < 0 ? x_shape.rank() + axis + 1 : axis;
loco::TensorShape output_shape;
output_shape.rank(x_shape.rank() + 1);
size_t i = 0;
for (; i < positive_axis; i++)
output_shape.dim(i) = x_shape.dim(i);
output_shape.dim(i) = loco::Dimension(1);
for (; i < x_shape.rank(); i++)
output_shape.dim(i + 1) = x_shape.dim(i);
return loco::NodeShape{output_shape};
}
loco::NodeShape infer_fill(const luci::CircleFill *node)
{
loco::TensorShape shape;
{
LUCI_ASSERT(node->dims(), "dims input should not be nullptr");
auto dims_node = dynamic_cast<luci::CircleConst *>(node->dims());
if (dims_node != nullptr)
{
// Only support node with S32
LUCI_ASSERT(dims_node->dtype() == loco::DataType::S32, "Only support int32 CircleConst");
if (dims_node->rank() != 1)
INTERNAL_EXN_V("Only support rank 1 CircleConst", oops::to_uint32(dims_node->rank()));
shape.rank(dims_node->dim(0).value());
for (uint32_t axis = 0; axis < shape.rank(); ++axis)
{
shape.dim(axis) = dims_node->at<loco::DataType::S32>(axis);
}
}
else
{
shape = own_shape(node);
}
}
return loco::NodeShape{shape};
}
loco::NodeShape infer_gather(const luci::CircleGather *node)
{
loco::TensorShape output_shape;
const auto input_shape = luci::shape_get(node->params()).as<loco::TensorShape>();
const auto positions_shape = luci::shape_get(node->indices()).as<loco::TensorShape>();
int32_t axis = node->axis();
// If CircleGather input has a dynamic shape, it can't inference this shape. So, it returns the
// shape that node already has.
if (input_shape.rank() == 0 || positions_shape.rank() == 0)
return use_own(node);
if (axis < 0)
axis += input_shape.rank();
output_shape.rank(input_shape.rank() - 1 + positions_shape.rank());
int32_t outdim_index = 0;
for (int32_t i = 0; i < axis; ++i)
output_shape.dim(outdim_index++) = input_shape.dim(i);
for (uint32_t i = 0; i < positions_shape.rank(); ++i)
output_shape.dim(outdim_index++) = positions_shape.dim(i);
for (uint32_t i = axis + 1; i < input_shape.rank(); ++i)
output_shape.dim(outdim_index++) = input_shape.dim(i);
return loco::NodeShape{output_shape};
}
loco::NodeShape infer_gather_nd(const luci::CircleGatherNd *node)
{
loco::TensorShape output_shape;
const auto params_shape = luci::shape_get(node->params()).as<loco::TensorShape>();
const auto indices_shape = luci::shape_get(node->indices()).as<loco::TensorShape>();
const auto params_rank = params_shape.rank();
const auto indices_rank = indices_shape.rank();
// see https://www.tensorflow.org/api_docs/python/tf/gather_nd
// output.shape = indices.shape[:-1] + params.shape[indices.shape[-1]:]
// batch_dims isn't supported in tflite
// TODO: replace exceptions with setting shape to unknown?
if (!indices_shape.dim(indices_rank - 1).known())
INTERNAL_EXN("Last indices dimension is unknown");
auto indices_last_dim = indices_shape.dim(indices_rank - 1).value();
if (indices_last_dim > params_rank)
INTERNAL_EXN("Last indices dimension should be <= params rank");
const uint32_t output_rank = indices_rank + params_rank - indices_last_dim - 1;
output_shape.rank(output_rank);
uint32_t output_index = 0;
for (uint32_t i = 0; i < indices_rank - 1; ++i)
{
auto &dim = indices_shape.dim(i);
if (!dim.known())
INTERNAL_EXN("Unknown indices dimension is unsupported");
output_shape.dim(output_index++).set(dim.value());
}
for (uint32_t i = indices_last_dim; i < params_rank; ++i)
{
auto &dim = params_shape.dim(i);
if (!dim.known())
INTERNAL_EXN("Unknown params dimension is unsupported");
output_shape.dim(output_index++).set(dim.value());
}
return loco::NodeShape{output_shape};
}
loco::NodeShape infer_matrix_diag(const luci::CircleMatrixDiag *node)
{
loco::TensorShape output_shape;
auto diagonal_shape = luci::shape_get(node->diagonal()).as<loco::TensorShape>();
auto rank = diagonal_shape.rank();
output_shape.rank(rank + 1);
for (uint32_t i = 0; i < rank; i++)
{
output_shape.dim(i) = diagonal_shape.dim(i);
}
output_shape.dim(rank) = diagonal_shape.dim(rank - 1);
return loco::NodeShape{output_shape};
}
loco::NodeShape infer_matrix_set_diag(const luci::CircleMatrixSetDiag *node)
{
auto input_shape = luci::shape_get(node->input()).as<loco::TensorShape>();
auto diagonal_shape = luci::shape_get(node->diagonal()).as<loco::TensorShape>();
auto rank = diagonal_shape.rank();
LUCI_ASSERT(rank == input_shape.rank() - 1, "diagonal rank = input rank - 1");
for (uint32_t i = 0; i < rank - 1; i++)
{
LUCI_ASSERT(diagonal_shape.dim(i) == input_shape.dim(i), "diagonal dims = input dims");
}
auto dim = std::min(input_shape.dim(rank - 1).value(), input_shape.dim(rank).value());
LUCI_ASSERT(dim == diagonal_shape.dim(rank - 1), "Max diag len error");
return loco::NodeShape{input_shape};
}
loco::TensorShape infer_reducer(const loco::Node *input, const loco::Node *indices, bool keep_dims)
{
const loco::DataType S32 = loco::DataType::S32;
auto input_shape = luci::shape_get(input).as<loco::TensorShape>();
auto reduction_indices = loco::must_cast<const luci::CircleConst *>(indices);
{ // Exceptions
// TODO support non-const case
// TODO support other data type
LUCI_ASSERT(reduction_indices->dtype() == S32, "Only support int 32");
}
std::vector<int32_t> reduction_values;
for (uint32_t i = 0; i < reduction_indices->size<S32>(); ++i)
{
int32_t axis = reduction_indices->at<S32>(i);
if (axis < 0)
axis += input_shape.rank();
if (not(0 <= axis and axis < static_cast<int32_t>(input_shape.rank())))
INTERNAL_EXN_V("Invalid reduction axis for REDUCER", oops::to_uint32(axis));
reduction_values.push_back(axis);
}
loco::TensorShape output_shape;
if (keep_dims)
{
output_shape.rank(input_shape.rank());
for (uint32_t i = 0; i < input_shape.rank(); ++i)
output_shape.dim(i) = input_shape.dim(i);
for (uint32_t i = 0; i < reduction_values.size(); ++i)
output_shape.dim(reduction_values.at(i)) = 1;
}
else
{
std::vector<bool> check_reduce(input_shape.rank(), false);
for (uint32_t i = 0; i < reduction_values.size(); ++i)
check_reduce.at(reduction_values.at(i)) = true;
uint32_t reduce_cnt = 0;
for (uint32_t i = 0; i < check_reduce.size(); ++i)
if (check_reduce.at(i))
++reduce_cnt;
output_shape.rank(input_shape.rank() - reduce_cnt);
for (uint32_t i = 0, j = 0; i < check_reduce.size(); ++i)
if (check_reduce.at(i) == false)
output_shape.dim(j++) = input_shape.dim(i);
}
return output_shape;
}
loco::NodeShape infer_mirror_pad(const luci::CircleMirrorPad *node)
{
// TODO support non-const case
auto paddings = luci::must_cast<luci::CircleConst *>(node->paddings());
return use_paddings(node, paddings);
}
loco::NodeShape infer_one_hot(const luci::CircleOneHot *node)
{
const loco::DataType S32 = loco::DataType::S32;
auto indices_shape = luci::shape_get(node->indices()).as<loco::TensorShape>();
// Only support OneHot node's depth() is CircleConst with type S32
// TODO support depth with other types
auto depth = luci::must_cast<luci::CircleConst *>(node->depth());
LUCI_ASSERT(depth->dtype() == S32, "Only support int32 CircleConst");
if (depth->rank() != 0)
INTERNAL_EXN_V("Only support rank 0 CircleOneHot in Depth", oops::to_uint32(depth->rank()));
loco::TensorShape output_shape;
output_shape.rank(indices_shape.rank() + 1);
auto axis = node->axis();
if (axis < 0)
axis += indices_shape.rank() + 1;
LUCI_ASSERT(0 <= axis, "Axis is out of range");
LUCI_ASSERT(static_cast<uint32_t>(axis) <= indices_shape.rank(), "Axis is out of range");
uint32_t j = 0;
for (uint32_t i = 0; i < output_shape.rank(); i++)
{
if (i == static_cast<uint32_t>(axis))
{
output_shape.dim(i) = depth->at<S32>(0);
}
else
{
output_shape.dim(i) = indices_shape.dim(j++);
}
}
return loco::NodeShape{output_shape};
}
loco::NodeShape infer_pack(const luci::CirclePack *node)
{
LUCI_ASSERT(node->values_count() > 0, "Only support one or more inputs");
auto first_shape = luci::shape_get(node->values(0)).as<loco::TensorShape>();
// Make sure all inputs have the same shape.
for (uint32_t i = 1; i < node->values_count(); ++i)
{
auto in_shape = luci::shape_get(node->values(i)).as<loco::TensorShape>();
LUCI_ASSERT(loco::NodeShape{first_shape} == loco::NodeShape{in_shape},
"All inputs must have the same shape");
}
// Checking shape capability for pack layer
// Input: tensors [D1, D2, ... Dn]
// Axis: K
// Output: [D1, D2, ... , D_K-1, n, D_K+1, ... Dn]
auto axis = node->axis();
if (axis < 0)
axis += first_shape.rank() + 1;
LUCI_ASSERT(0 <= axis, "Axis is out of range");
LUCI_ASSERT(static_cast<uint32_t>(axis) <= first_shape.rank(), "Axis is out of range");
loco::TensorShape output_shape;
output_shape.rank(first_shape.rank() + 1);
uint32_t j = 0;
for (uint32_t i = 0; i < output_shape.rank(); ++i)
{
if (i == static_cast<uint32_t>(axis))
{
output_shape.dim(i) = node->values_count();
}
else
{
output_shape.dim(i) = first_shape.dim(j++);
}
}
return loco::NodeShape{output_shape};
}
loco::NodeShape infer_pad_v2(const luci::CirclePadV2 *node)
{
// TODO support non-const case
auto paddings = dynamic_cast<luci::CircleConst *>(node->paddings());
if (!paddings)
{
auto node_shape = own_shape(node);
return loco::NodeShape{node_shape};
}
return use_paddings(node, paddings);
}
loco::NodeShape infer_p_relu(const luci::CirclePRelu *node)
{
auto input_shape = luci::shape_get(node->input()).as<loco::TensorShape>();
auto alpha_shape = luci::shape_get(node->alpha()).as<loco::TensorShape>();
auto output_shape = broadcast_shape(input_shape, alpha_shape);
return loco::NodeShape{output_shape};
}
template <class CIRCLENODE> loco::NodeShape infer_resize_type(const CIRCLENODE *node)
{
auto input_shape = luci::shape_get(node->input()).template as<loco::TensorShape>();
if (input_shape.rank() != 4)
INTERNAL_EXN("Expected input to have rank 4");
auto *const_node = luci::must_cast<luci::CircleConst *>(node->size());
if (const_node->dtype() != loco::DataType::S32)
INTERNAL_EXN("Only S32 datatype is supported for size");
if (const_node->rank() != 1)
INTERNAL_EXN("Expected size tensor of rank 1");
if (const_node->dim(0).value() != 2)
INTERNAL_EXN("Expected size tensor with shape [2]");
loco::TensorShape output_shape;
output_shape.rank(4);
output_shape.dim(0) = input_shape.dim(0);
output_shape.dim(1) = const_node->template at<loco::DataType::S32>(0);
output_shape.dim(2) = const_node->template at<loco::DataType::S32>(1);
output_shape.dim(3) = input_shape.dim(3);
return loco::NodeShape{output_shape};
}
loco::NodeShape infer_scatter_nd(const luci::CircleScatterNd *node)
{
loco::TensorShape output_shape;
auto shape_node = luci::must_cast<luci::CircleConst *>(node->shape());
const loco::DataType S32 = loco::DataType::S32;
const loco::DataType S64 = loco::DataType::S64;
std::vector<int64_t> vect_shape;
if (shape_node->dtype() == S32)
vect_shape = vector_from_constant<S32>(shape_node);
else if (shape_node->dtype() == S64)
vect_shape = vector_from_constant<S64>(shape_node);
else
LUCI_ASSERT(false, "Only support int32/int64 for shape()");
output_shape.rank(vect_shape.size());
for (uint32_t i = 0; i < vect_shape.size(); ++i)
output_shape.dim(i) = vect_shape[i];
return loco::NodeShape{output_shape};
}
loco::NodeShape infer_segment_sum(const luci::CircleSegmentSum *node)
{
auto input_shape = luci::shape_get(node->input()).as<loco::TensorShape>();
auto segment_shape = luci::shape_get(node->segment_ids()).as<loco::TensorShape>();
LUCI_ASSERT(segment_shape.rank() == 1, "segment_ids must be 1-D tensor");
LUCI_ASSERT(segment_shape.dim(0).value() == input_shape.dim(0).value(),
"segment_ids size must be equal to the size of data's first dimension");
auto ids_shape_value = luci::must_cast<luci::CircleConst *>(node->segment_ids());
std::vector<int64_t> vect_ids;
if (ids_shape_value->dtype() == loco::DataType::S32)
vect_ids = vector_from_constant<loco::DataType::S32>(ids_shape_value);
LUCI_ASSERT(std::is_sorted(vect_ids.begin(), vect_ids.end()),
"segment_ids values should be sorted")
loco::TensorShape output_shape;
output_shape.rank(input_shape.rank());
for (uint32_t i = 1; i < input_shape.rank(); ++i)
output_shape.dim(i) = input_shape.dim(i);
output_shape.dim(0) = vect_ids.back() + 1;
return loco::NodeShape{output_shape};
}
loco::NodeShape infer_select(const luci::CircleSelect *node)
{