/
Nodes.cpp
1910 lines (1670 loc) · 76.4 KB
/
Nodes.cpp
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) Glow Contributors. See CONTRIBUTORS file.
*
* 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 "glow/Graph/Nodes.h"
#include "glow/Base/Type.h"
#include "glow/Graph/Graph.h"
#include "glow/Graph/VerifierHelper.h"
#include "glow/Support/Support.h"
using namespace glow;
bool Storage::isEqual(const Storage &other) const {
/// A storage should be equal only to itself!
return this == &other;
}
llvm::hash_code Constant::getHash() const {
return llvm::hash_combine(getName(), getType());
}
llvm::hash_code Placeholder::getHash() const {
return llvm::hash_combine(getName());
}
//===----------------------------------------------------------------------===//
// Visitor methods
//===----------------------------------------------------------------------===//
void Storage::visit(Node *parent, NodeWalker *visitor) {
if (!visitor->shouldVisit(parent, this)) {
return;
}
visitor->pre(parent, this);
visitor->post(parent, this);
}
void Storage::visit(const Node *parent, NodeWalker *visitor) const {
if (!visitor->shouldVisit(parent, this)) {
return;
}
visitor->pre(parent, this);
visitor->post(parent, this);
}
//===----------------------------------------------------------------------===//
// Edge getters methods
//===----------------------------------------------------------------------===//
unsigned Storage::getNumInputs() const { return 0; }
std::string Storage::getInputName(unsigned idx) const {
llvm_unreachable("Invalid index");
}
NodeValue Storage::getNthInput(unsigned idx) {
llvm_unreachable("Invalid index");
}
llvm::StringRef Storage::getOutputName(unsigned idx) const {
if (idx == 0) {
return "Output";
}
llvm_unreachable("Invalid index");
}
bool Storage::hasSideEffects() const { return false; }
Node *Storage::clone() const { llvm_unreachable("Storage can't be cloned."); }
//===----------------------------------------------------------------------===//
// Debug description methods
//===----------------------------------------------------------------------===//
std::string Constant::getDebugDesc() const {
DescriptionBuilder db(getKindName());
db.addParam("name", quote(getName()))
.addParam("layout", getLayout())
.addParam("output", *getType())
.addParam("users", getNumUsers());
return db;
}
std::string Placeholder::getDebugDesc() const {
DescriptionBuilder db(getKindName());
db.addParam("name", quote(getName()))
.addParam("layout", getLayout())
.addParam("output", *getType())
.addParam("users", getNumUsers())
.addParam("trainable", isTraining());
return db;
}
//===----------------------------------------------------------------------===//
// Nodes verification
//===----------------------------------------------------------------------===//
static bool verifyConvFilter(const Node *parent, NodeValue filter,
const ShapeNHWC &idim, const ShapeNHWC &odim,
const ShapeHW &kdim, unsigned_t group) {
const size_t filterDims[] = {odim.c, kdim.height, kdim.width,
idim.c / (size_t)group};
return expectCompareTrue("Invalid filter dimensions",
filter.getType()->dims(),
llvm::makeArrayRef(filterDims), parent);
}
static bool verifyConvFilter(const Node *parent, NodeValue filter,
const ShapeNCHW &idim, const ShapeNCHW &odim,
const ShapeHW &kdim, unsigned_t group) {
const size_t filterDims[] = {odim.c, idim.c / (size_t)group, kdim.height,
kdim.width};
return expectCompareTrue("Invalid filter dimensions",
filter.getType()->dims(),
llvm::makeArrayRef(filterDims), parent);
}
template <typename Shape>
static bool verifyConvolution(NodeValue src, NodeValue dest, NodeValue filter,
NodeValue bias,
llvm::ArrayRef<unsigned_t> kernels,
llvm::ArrayRef<unsigned_t> strides,
llvm::ArrayRef<unsigned_t> pads, unsigned_t group,
unsigned_t dilation, bool checkBiasType = true) {
const Node *parent = dest.getNode();
bool isValid = checkType(src, dest.getElementType(), parent);
isValid &= checkType(src, filter.getElementType(), parent);
if (checkBiasType) {
// Non quantization type check.
if (src.getElementType() == ElemKind::FloatTy) {
isValid &= checkType(bias, ElemKind::FloatTy, parent);
}
// Quantization type check.
if (src.getElementType() == ElemKind::Int8QTy) {
isValid &=
expectCompareTrue("Bias type should be Int8 or Int32 for Conv",
bias.getElementType() == ElemKind::Int8QTy ||
bias.getElementType() == ElemKind::Int32QTy,
true, parent);
}
}
Shape idim(src.getType()->dims());
Shape odim(dest.getType()->dims());
PaddingTLBR pdim(pads);
ShapeHW kdim(kernels);
isValid &= expectCompareTrue("buffer height too small for selected stride",
idim.h + pdim.top + pdim.bottom, kdim.height,
parent, CompareOperatorGreaterEqual<size_t>());
isValid &= expectCompareTrue("buffer width too small for selected stride",
idim.w + pdim.left + pdim.right, kdim.width,
parent, CompareOperatorGreaterEqual<size_t>());
isValid &= expectCompareTrue("channels number must be divisible by groups",
idim.c % group, size_t(0), parent);
auto outSz = calculateConvPoolOutputDims(idim.h, idim.w, kernels, strides,
pads, dilation);
isValid &=
expectCompareTrue("Invalid output dimension N", odim.n, idim.n, parent);
isValid &= expectCompareTrue("Invalid output dimension H", odim.h,
outSz.first, parent);
isValid &= expectCompareTrue("Invalid output dimension W", odim.w,
outSz.second, parent);
isValid &= expectCompareTrue("Invalid output dimension C", odim.c % group,
size_t(0), parent);
isValid &= verifyConvFilter(parent, filter, idim, odim, kdim, group);
const size_t biasDims[] = {odim.c};
isValid &=
expectCompareTrue("Invalid bias dimensions", bias.getType()->dims(),
llvm::makeArrayRef(biasDims), parent);
return isValid;
}
static bool verifyConvolution3D(NodeValue src, NodeValue dest, NodeValue filter,
NodeValue bias,
llvm::ArrayRef<unsigned_t> kernels,
llvm::ArrayRef<unsigned_t> strides,
llvm::ArrayRef<unsigned_t> pads,
unsigned_t group) {
const Node *parent = dest.getNode();
bool isValid = checkType(src, dest.getElementType(), parent);
isValid &= checkType(src, filter.getElementType(), parent);
// Non quantization type check.
if (src.getElementType() == ElemKind::FloatTy) {
isValid &= checkType(bias, ElemKind::FloatTy, parent);
}
// Quantization type check.
if (src.getElementType() == ElemKind::Int8QTy) {
isValid &=
expectCompareTrue("Bias type should be Int8 or Int32 for Conv3D",
bias.getElementType() == ElemKind::Int8QTy ||
bias.getElementType() == ElemKind::Int32QTy,
true, parent);
}
ShapeNHWDC idim(src.getType()->dims());
ShapeNHWDC odim(dest.getType()->dims());
PaddingTLNBRF pdim(pads);
ShapeHWD kdim(kernels);
isValid &= expectCompareTrue("buffer height too small for selected stride",
idim.h + pdim.top + pdim.bottom, kdim.height,
parent, CompareOperatorGreaterEqual<size_t>());
isValid &= expectCompareTrue("buffer width too small for selected stride",
idim.w + pdim.left + pdim.right, kdim.width,
parent, CompareOperatorGreaterEqual<size_t>());
isValid &= expectCompareTrue("buffer time too small for selected stride",
idim.d + pdim.near + pdim.far, kdim.depth,
parent, CompareOperatorGreaterEqual<size_t>());
isValid &= expectCompareTrue("channels number must be divisible by groups",
idim.c % group, size_t(0), parent);
auto outSz = calculate3DConvPoolOutputDims(idim.h, idim.w, idim.d, kernels,
strides, pads);
isValid &=
expectCompareTrue("Invalid output dimension N", odim.n, idim.n, parent);
isValid &= expectCompareTrue("Invalid output dimension H", odim.h,
outSz.height, parent);
isValid &= expectCompareTrue("Invalid output dimension W", odim.w,
outSz.width, parent);
isValid &= expectCompareTrue("Invalid output dimension D", odim.d,
outSz.depth, parent);
isValid &= expectCompareTrue("Invalid output dimension C", odim.c % group,
size_t(0), parent);
const size_t filterDims[] = {odim.c, kdim.height, kdim.width, kdim.depth,
idim.c / (size_t)group};
isValid &=
expectCompareTrue("Invalid filter dimensions", filter.getType()->dims(),
llvm::makeArrayRef(filterDims), parent);
const size_t biasDims[] = {odim.c};
isValid &=
expectCompareTrue("Invalid bias dimensions", bias.getType()->dims(),
llvm::makeArrayRef(biasDims), parent);
return isValid;
}
static bool verifyFullyConnected(NodeValue src, NodeValue weights,
NodeValue bias, NodeValue dest) {
const Node *parent = dest.getNode();
bool isValid = expectCompareTrue("FC input must be 2D", size_t(2),
src.dims().size(), parent);
isValid &= expectCompareTrue("FC weights must be 2D", size_t(2),
weights.dims().size(), parent);
isValid &= expectCompareTrue("Mismatch between source and dest dimensions",
src.dims()[0], dest.dims()[0], parent);
isValid &= expectCompareTrue("Mismatch between source and weight dimensions",
src.dims()[1], weights.dims()[0], parent);
isValid &= expectCompareTrue("Inconsistent bias/dest sizes", bias.dims()[0],
weights.dims()[1], parent);
isValid &= expectCompareTrue("Inconsistent weights/dest sizes",
weights.dims()[1], dest.dims()[1], parent);
if (src.getElementType() == ElemKind::Int8QTy) {
isValid &=
expectCompareTrue("Bias type should be Int8 or Int32 for FC",
bias.getElementType() == ElemKind::Int8QTy ||
bias.getElementType() == ElemKind::Int32QTy,
true, parent);
}
return isValid;
}
template <typename Shape>
static bool verifyPool(NodeValue src, NodeValue dest,
llvm::ArrayRef<unsigned_t> kernels,
llvm::ArrayRef<unsigned_t> strides,
llvm::ArrayRef<unsigned_t> pads, bool isAvgPool = true) {
const Node *parent = dest.getNode();
Shape idim(src.getType()->dims());
Shape odim(dest.getType()->dims());
PaddingTLBR pdim(pads);
ShapeHW kdim(kernels);
bool isValid =
expectCompareTrue("buffer height too small for selected stride",
idim.h + pdim.top + pdim.bottom, kdim.height, parent,
CompareOperatorGreaterEqual<size_t>());
isValid &= expectCompareTrue("buffer width too small for selected stride",
idim.w + pdim.left + pdim.right, kdim.width,
parent, CompareOperatorGreaterEqual<size_t>());
auto outSz =
calculateConvPoolOutputDims(idim.h, idim.w, kernels, strides, pads);
Shape exp(idim);
exp.h = outSz.first;
exp.w = outSz.second;
isValid &=
expectCompareTrue("Unexpected output dimensions", exp, odim, parent);
// For quantized AvgPool, the scale and offset of its input and output could
// be different. But for quantized MaxPool, the scale and offset of its input
// and output should be the same.
isValid &= checkSameIsQuantized(src.getType(), dest.getType(), parent);
if (!isAvgPool) {
isValid &= checkTypeIgnoreShape(src, dest, parent);
}
return isValid;
}
static bool verifyBatchNormalization(NodeValue src, NodeValue dest,
NodeValue bias, NodeValue scale,
NodeValue mean, NodeValue var,
unsigned_t channel) {
const Node *parent = dest.getNode();
bool isValid = checkSameType(dest, src, parent);
// Figure out how many channels are in the tensor.
size_t channels = src.dims()[channel];
const size_t expArray[] = {channels};
auto exp = llvm::makeArrayRef(expArray);
isValid &= expectCompareTrue("Invalid bias dimension", bias.getType()->dims(),
exp, parent);
isValid &= expectCompareTrue("Invalid scale dimension",
scale.getType()->dims(), exp, parent);
isValid &= expectCompareTrue("Invalid mean dimension", mean.getType()->dims(),
exp, parent);
isValid &= expectCompareTrue("Invalid var dimension", var.getType()->dims(),
exp, parent);
return isValid;
}
static bool verifySigmoid(NodeValue src, NodeValue dest) {
const Node *parent = dest.getNode();
bool isValid = checkSameIsQuantized(src.getType(), dest.getType(), parent);
if (src.getType()->isQuantizedType()) {
isValid &= checkType(src, dest.getElementType(), dest.getNode());
isValid &= checkSameShape(src, dest, parent);
} else {
isValid &= checkSameType(src, dest, parent);
}
return isValid;
}
static bool verifyTanh(NodeValue src, NodeValue dest) {
const Node *parent = dest.getNode();
bool isValid = checkSameIsQuantized(src.getType(), dest.getType(), parent);
if (src.getType()->isQuantizedType()) {
isValid &= checkType(src, dest.getElementType(), dest.getNode());
isValid &= checkSameShape(src, dest, parent);
} else {
isValid &= checkSameType(src, dest, parent);
}
return isValid;
}
static bool verifySoftMax(NodeValue src, NodeValue dest) {
const Node *parent = dest.getNode();
if (src.getType()->isQuantizedType()) {
return checkType(src, dest.getElementType(), parent) &&
checkSameShape(src, dest, parent);
}
return checkSameType(src, dest, parent);
}
static bool verifyCrossEntropyLoss(NodeValue P, NodeValue CE,
NodeValue labels) {
const Node *parent = CE.getNode();
bool isValid = checkType(P, CE.getElementType(), parent);
isValid &= expectCompareTrue("Mismatching shape", P.dims()[0],
labels.dims()[0], parent);
return isValid;
}
static bool verifyLocalResponseNormalization(NodeValue src, NodeValue dest) {
return checkSameType(src, dest, dest.getNode());
}
static bool verifyArithmetic(NodeValue LHS, NodeValue RHS, NodeValue res) {
return checkSameShape(res, LHS, res.getNode()) &&
checkSameShape(LHS, RHS, res.getNode());
}
static bool verifyRelu(NodeValue result, NodeValue input) {
const Node *parent = result.getNode();
if (input.getType()->isQuantizedType()) {
return checkSameIsQuantized(input.getType(), result.getType(), parent) &&
checkSameShape(result, input, parent);
}
return checkSameType(result, input, parent);
}
static bool verifyPRelu(NodeValue result, NodeValue input, NodeValue slope) {
const Node *parent = result.getNode();
if (input.getType()->isQuantizedType()) {
return checkSameIsQuantized(input.getType(), result.getType(), parent) &&
checkSameIsQuantized(input.getType(), slope.getType(), parent) &&
checkSameShape(result, input, parent) &&
checkSameShape(slope, input, parent);
}
return checkSameType(result, input, parent) &&
checkSameType(slope, input, parent) &&
checkSameShape(slope, input, parent);
}
static bool verifyRegression(NodeValue src, NodeValue dest,
NodeValue expected) {
return checkSameType(src, dest, dest.getNode()) &&
checkSameType(dest, expected, dest.getNode());
}
static bool verifySparseLengthsSum(NodeValue dest, NodeValue data,
NodeValue indices, NodeValue lengths) {
bool isValid = checkType(dest, data.getElementType(), dest.getNode());
isValid &= checkType(indices, ElemKind::Int64ITy, dest.getNode());
isValid &= checkType(lengths, ElemKind::Int32ITy, dest.getNode());
isValid &=
expectCompareTrue("Indices must be a 1D vector", indices.dims().size(),
size_t(1), dest.getNode());
isValid &=
expectCompareTrue("Lengths must be a 1D vector", lengths.dims().size(),
size_t(1), dest.getNode());
return isValid;
}
static bool verifySparseLengthsWeightedSum(NodeValue dest, NodeValue data,
NodeValue weights, NodeValue indices,
NodeValue lengths) {
bool isValid = checkType(dest, data.getElementType(), dest.getNode());
isValid &= checkType(weights, data.getElementType(), dest.getNode());
isValid &= checkType(indices, ElemKind::Int64ITy, dest.getNode());
isValid &= checkType(lengths, ElemKind::Int32ITy, dest.getNode());
isValid &=
expectCompareTrue("Indices must be a 1D vector", indices.dims().size(),
size_t(1), dest.getNode());
isValid &=
expectCompareTrue("Lengths must be a 1D vector", lengths.dims().size(),
size_t(1), dest.getNode());
isValid &=
expectCompareTrue("Weights must be a 1D vector", weights.dims().size(),
size_t(1), dest.getNode());
isValid &=
expectCompareTrue("Weights and Indices must have the same size",
weights.dims()[0], indices.dims()[0], dest.getNode());
return isValid;
}
static bool verifyEmbeddingBag(NodeValue dest, NodeValue data,
NodeValue weights, NodeValue indices,
NodeValue offsets) {
bool isValid = checkType(dest, data.getElementType(), dest.getNode());
isValid &= checkType(weights, data.getElementType(), dest.getNode());
isValid &= checkType(indices, ElemKind::Int64ITy, dest.getNode());
isValid &= checkType(offsets, ElemKind::Int64ITy, dest.getNode());
isValid &=
expectCompareTrue("Indices must be a 1D vector", indices.dims().size(),
size_t(1), dest.getNode());
isValid &=
expectCompareTrue("Offsets must be a 1D vector", offsets.dims().size(),
size_t(1), dest.getNode());
isValid &=
expectCompareTrue("Weights must be a 1D vector", weights.dims().size(),
size_t(1), dest.getNode());
isValid &=
expectCompareTrue("Weights and Indices must have the same size",
weights.dims()[0], indices.dims()[0], dest.getNode());
return isValid;
}
bool PadNode::verify() const {
// Pad is currently only supported for constant padding.
return expectCompareTrue("only the 'constant' mode is currrently supported",
getMode() == PaddingMode::CONSTANT, true,
getResult().getNode());
}
bool ConvolutionNode::verify() const {
if (getLayout() == NHWC) {
return verifyConvolution<ShapeNHWC>(getInput(), getResult(), getFilter(),
getBias(), Kernels_, Strides_, Pads_,
Group_, Dilation_);
} else {
return verifyConvolution<ShapeNCHW>(getInput(), getResult(), getFilter(),
getBias(), Kernels_, Strides_, Pads_,
Group_, Dilation_);
}
}
bool ChannelwiseQuantizedConvolutionNode::verify() const {
bool isValid = expectCompareTrue("Only groupwise quantization is supported.",
getGroupwise(), true, this);
if (!isValid) {
return false;
}
isValid =
verifyConvolution<ShapeNHWC>(getInput(), getResult(), getFilter(),
getBias(), Kernels_, Strides_, Pads_, Group_,
/* dilation */ 1, /* checkBiasType */ false);
isValid &= checkType(getBias(), ElemKind::FloatTy, this);
isValid &= checkType(getInput(), ElemKind::Int8QTy, this);
// check qparam types
isValid &= checkType(getOffsets(), ElemKind::Int32ITy, this);
isValid &= checkType(getScales(), ElemKind::FloatTy, this);
// check qparam dimensions
isValid &= expectCompareTrue("Offsets must be a 1D vector",
getOffsets().dims().size(), size_t(1), this);
isValid &= expectCompareTrue("Scales must be a 1D vector",
getScales().dims().size(), size_t(1), this);
// check qparam sizes
isValid &=
expectCompareTrue("There must be one filter offset qparam per group",
getOffsets().dims()[0], size_t(getGroup()), this);
isValid &=
expectCompareTrue("There must be one filter scale qparam per group",
getScales().dims()[0], size_t(getGroup()), this);
return isValid;
}
bool Convolution3DNode::verify() const {
return verifyConvolution3D(getInput(), getResult(), getFilter(), getBias(),
Kernels_, Strides_, Pads_, Group_);
}
/// Verify that types of an input and its gradient are the same.
static bool verifyInputAndGradInputTypes(NodeValue input, NodeValue gradInput,
const Node *parent) {
return checkSameType(input, gradInput, parent);
}
/// Verify that types of an output and its gradient are the same.
static bool verifyOutputAndGradOutputTypes(NodeValue output,
NodeValue gradOutput,
const Node *parent) {
return checkSameType(output, gradOutput, parent);
}
bool Constant::verify() const {
return expectCompareTrue("Underlying tensor type doesn't match constant type",
*getType(), getPayload().getType(), this);
}
bool ConvolutionGradNode::verify() const {
bool isValid = verifyInputAndGradInputTypes(getInput(),
getGradOfInputNamedInput(), this);
isValid &= verifyInputAndGradInputTypes(getFilter(),
getGradOfInputNamedFilter(), this);
isValid &=
verifyInputAndGradInputTypes(getBias(), getGradOfInputNamedBias(), this);
isValid &= verifyOutputAndGradOutputTypes(
getOriginalOutputForResult(), getGradOfOriginalOutputNamedResult(), this);
if (getLayout() == NHWC) {
isValid &= verifyConvolution<ShapeNHWC>(
getGradOfInputNamedInput(), getGradOfOriginalOutputNamedResult(),
getGradOfInputNamedFilter(), getGradOfInputNamedBias(), Kernels_,
Strides_, Pads_, Group_, Dilation_);
} else {
isValid &= verifyConvolution<ShapeNCHW>(
getGradOfInputNamedInput(), getGradOfOriginalOutputNamedResult(),
getGradOfInputNamedFilter(), getGradOfInputNamedBias(), Kernels_,
Strides_, Pads_, Group_, Dilation_);
}
return isValid;
}
bool Convolution3DGradNode::verify() const {
bool isValid = verifyInputAndGradInputTypes(getInput(),
getGradOfInputNamedInput(), this);
isValid &= verifyInputAndGradInputTypes(getFilter(),
getGradOfInputNamedFilter(), this);
isValid &=
verifyInputAndGradInputTypes(getBias(), getGradOfInputNamedBias(), this);
isValid &= verifyOutputAndGradOutputTypes(
getOriginalOutputForResult(), getGradOfOriginalOutputNamedResult(), this);
isValid &= verifyConvolution3D(
getGradOfInputNamedInput(), getGradOfOriginalOutputNamedResult(),
getGradOfInputNamedFilter(), getGradOfInputNamedBias(), Kernels_,
Strides_, Pads_, Group_);
return isValid;
}
/// \returns the number of columns of data from a fused \p type (i.e. not
/// considering the columns for per row scale/offsets).
static size_t getNumDataColumnsFromFused(TypeRef type) {
size_t n = type->dims()[1];
switch (type->getElementType()) {
case ElemKind::UInt8FusedQTy:
return n - 2 * sizeof(float);
case ElemKind::UInt8FusedFP16QTy:
return n - 2 * sizeof(float16_t);
default:
llvm_unreachable("Not supported Fused ElemKind");
}
}
bool ConvertToNode::verify() const {
TypeRef srcTy = getInput().getType();
TypeRef dstTy = getResult().getType();
const bool srcIsFused = isFusedQuantizedElemKind(srcTy->getElementType());
const bool dstIsFused = isFusedQuantizedElemKind(dstTy->getElementType());
bool isValid = expectCompareTrue(
"Conversion of src and dst with mismatched fused property is not yet "
"implemented",
(srcIsFused && dstIsFused) || (!srcIsFused && !dstIsFused), true, this);
if (srcIsFused && dstIsFused) {
size_t srcNumCols = getNumDataColumnsFromFused(srcTy);
size_t dstNumCols = getNumDataColumnsFromFused(dstTy);
return expectCompareTrue("Shapes of data for fused kinds do not match",
srcNumCols, dstNumCols, this);
}
isValid &= checkSameShape(getInput(), getResult(), this);
isValid &= expectCompareTrue(
"Quantized conversion should use Dequantize, Quantize and Rescale",
srcTy->isQuantizedType() || dstTy->isQuantizedType(), false, this);
return isValid;
}
bool MaxPoolNode::verify() const {
if (getLayout() == NHWC) {
return verifyPool<ShapeNHWC>(getInput(), getResult(), Kernels_, Strides_,
Pads_,
/* isAvgPool */ false);
} else {
return verifyPool<ShapeNCHW>(getInput(), getResult(), Kernels_, Strides_,
Pads_,
/* isAvgPool */ false);
}
}
bool AvgPoolNode::verify() const {
if (getLayout() == NHWC) {
return verifyPool<ShapeNHWC>(getInput(), getResult(), Kernels_, Strides_,
Pads_);
} else {
return verifyPool<ShapeNCHW>(getInput(), getResult(), Kernels_, Strides_,
Pads_);
}
}
bool AdaptiveAvgPoolGradNode::verify() const {
bool isValid = verifyInputAndGradInputTypes(getInput(),
getGradOfInputNamedInput(), this);
isValid &= verifyOutputAndGradOutputTypes(
getOriginalOutputForResult(), getGradOfOriginalOutputNamedResult(), this);
ShapeNHWC idim(getInput().getType()->dims());
ShapeNHWC odim(getOriginalOutputForResult().getType()->dims());
isValid &= expectCompareTrue(
"expected the same number of channels for input and output", odim.c,
idim.c, this);
isValid &= expectCompareTrue(
"expected the same number of batches for input and output", odim.n,
idim.n, this);
isValid &=
expectCompareTrue("height too small for averaging area", odim.h, idim.h,
this, CompareOperatorLessEqual<size_t>());
isValid &=
expectCompareTrue("width too small for averaging area", odim.w, idim.w,
this, CompareOperatorLessEqual<size_t>());
return isValid;
}
bool AdaptiveAvgPoolNode::verify() const {
bool isValid = checkTypeIgnoreShape(getInput(), getResult(), this);
TypeRef inTy = getInput().getType();
TypeRef outTy = getResult().getType();
isValid &= expectCompareTrue("Input should have 4 dimensions",
inTy->dims().size(), (size_t)4, this);
isValid &= expectCompareTrue("Output should have 4 dimensions",
outTy->dims().size(), (size_t)4, this);
if (!isValid) {
return false;
}
isValid &= expectCompareTrue(
"Output should have the same number of batches as the input",
inTy->dims()[0], outTy->dims()[0], this);
isValid &= expectCompareTrue(
"Output should have the same number of channels as the input",
inTy->dims()[3], outTy->dims()[3], this);
isValid &= expectCompareTrue(
"Output should not have more height than the input", inTy->dims()[1],
outTy->dims()[1], this, CompareOperatorGreaterEqual<size_t>());
isValid &= expectCompareTrue(
"Output should not have more width than the input", inTy->dims()[2],
outTy->dims()[2], this, CompareOperatorGreaterEqual<size_t>());
return isValid;
}
bool MaxPoolGradNode::verify() const {
bool isValid = verifyInputAndGradInputTypes(getInput(),
getGradOfInputNamedInput(), this);
isValid &= verifyOutputAndGradOutputTypes(
getOriginalOutputForResult(), getGradOfOriginalOutputNamedResult(), this);
if (getLayout() == NHWC) {
isValid &= verifyPool<ShapeNHWC>(
getGradOfInputNamedInput(), getGradOfOriginalOutputNamedResult(),
Kernels_, Strides_, Pads_, /* isAvgPool */ false);
} else {
isValid &= verifyPool<ShapeNCHW>(
getGradOfInputNamedInput(), getGradOfOriginalOutputNamedResult(),
Kernels_, Strides_, Pads_, /* isAvgPool */ false);
}
return isValid;
}
bool AvgPoolGradNode::verify() const {
bool isValid = verifyInputAndGradInputTypes(getInput(),
getGradOfInputNamedInput(), this);
isValid &= verifyOutputAndGradOutputTypes(
getOriginalOutputForResult(), getGradOfOriginalOutputNamedResult(), this);
if (getLayout() == NHWC) {
isValid &= verifyPool<ShapeNHWC>(getGradOfInputNamedInput(),
getGradOfOriginalOutputNamedResult(),
Kernels_, Strides_, Pads_);
} else {
isValid &= verifyPool<ShapeNCHW>(getGradOfInputNamedInput(),
getGradOfOriginalOutputNamedResult(),
Kernels_, Strides_, Pads_);
}
return isValid;
}
bool MatMulNode::verify() const {
auto lhs = getLHS();
auto rhs = getRHS();
auto dest = getResult();
auto LDims = lhs.dims();
auto RDims = rhs.dims();
auto DDims = dest.dims();
bool isValid = expectCompareTrue("Invalid MatMul dimensions", size_t(2),
DDims.size(), this);
auto elem = dest.getType()->getElementType();
isValid &= checkType(lhs, elem, this);
isValid &= checkType(rhs, elem, this);
isValid &=
expectCompareTrue("Invalid row dimensions", LDims[0], DDims[0], this);
isValid &=
expectCompareTrue("Invalid column dimensions", RDims[1], DDims[1], this);
return isValid;
}
bool BatchMatMulNode::verify() const {
auto LHS = getLHS();
auto RHS = getRHS();
auto dest = getResult();
bool isValid = expectCompareTrue("LHS input must be 3 dimensional.",
LHS.dims().size(), size_t(3), this);
isValid &= expectCompareTrue("RHS input must be 3 dimensional.",
RHS.dims().size(), size_t(3), this);
isValid &= expectCompareTrue("Result must be 3 dimensional.",
dest.dims().size(), size_t(3), this);
isValid &= expectCompareTrue("LHS and RHS inputs must have same batch size.",
LHS.dims()[0], RHS.dims()[0], this);
isValid &= expectCompareTrue("Result must have same batch size as inputs.",
LHS.dims()[0], dest.dims()[0], this);
const size_t numBatches = LHS.dims()[0];
const size_t N = LHS.dims()[1];
const size_t M = LHS.dims()[2];
const size_t P = RHS.dims()[2];
isValid &= expectCompareTrue("Inputs have invalid dimensions.", RHS.dims()[1],
M, this);
isValid &= expectCompareTrue("Result has invalid dimensions given inputs.",
dest.dims(), {numBatches, N, P}, this);
auto elemType = dest.getType()->getElementType();
isValid &= checkType(LHS, elemType, this);
isValid &= checkType(RHS, elemType, this);
return isValid;
}
bool SigmoidNode::verify() const {
return verifySigmoid(getInput(), getResult());
}
bool SigmoidGradNode::verify() const {
bool isValid = verifyInputAndGradInputTypes(getInput(),
getGradOfInputNamedInput(), this);
isValid &= verifyOutputAndGradOutputTypes(
getOriginalOutputForResult(), getGradOfOriginalOutputNamedResult(), this);
isValid &= verifySigmoid(getGradOfInputNamedInput(),
getGradOfOriginalOutputNamedResult());
return isValid;
}
bool TanhNode::verify() const { return verifyTanh(getInput(), getResult()); }
bool TanhGradNode::verify() const {
bool isValid = verifyInputAndGradInputTypes(getInput(),
getGradOfInputNamedInput(), this);
isValid &= verifyOutputAndGradOutputTypes(
getOriginalOutputForResult(), getGradOfOriginalOutputNamedResult(), this);
isValid &= verifyTanh(getGradOfInputNamedInput(),
getGradOfOriginalOutputNamedResult());
return isValid;
}
bool ExpNode::verify() const {
const Node *parent = getResult().getNode();
bool isValid =
checkSameIsQuantized(getInput().getType(), getResult().getType(), parent);
if (getInput().getType()->isQuantizedType()) {
return false;
}
isValid &= checkSameType(getInput(), getResult(), parent);
isValid &= checkSameShape(getInput(), getResult(), parent);
return isValid;
}
bool BucketizeNode::verify() const {
bool isValid = checkSameShape(getInput(), getResult(), this);
isValid &= !getBoundaries().empty();
isValid &= std::is_sorted(getBoundaries().begin(), getBoundaries().end());
return isValid;
}
bool SoftMaxNode::verify() const {
return verifySoftMax(getInput(), getResult());
}
bool SoftMaxGradNode::verify() const {
bool isValid = verifyInputAndGradInputTypes(getInput(),
getGradOfInputNamedInput(), this);
isValid &= verifyInputAndGradInputTypes(getSelected(),
getGradOfInputNamedSelected(), this);
isValid &= verifyOutputAndGradOutputTypes(
getOriginalOutputForResult(), getGradOfOriginalOutputNamedResult(), this);
isValid &= verifySoftMax(getGradOfInputNamedInput(),
getGradOfOriginalOutputNamedResult());
return isValid;
}
bool CrossEntropyLossNode::verify() const {
return verifyCrossEntropyLoss(getP(), getCE(), getLabels());
}
bool CrossEntropyLossGradNode::verify() const {
bool isValid = verifyInputAndGradInputTypes(
getLabels(), getGradOfInputNamedLabels(), this);
isValid &= verifyInputAndGradInputTypes(getP(), getGradOfInputNamedP(), this);
isValid &= verifyOutputAndGradOutputTypes(
getOriginalOutputForCE(), getGradOfOriginalOutputNamedCE(), this);
isValid &= verifyCrossEntropyLoss(getGradOfInputNamedP(),
getGradOfOriginalOutputNamedCE(),
getGradOfInputNamedLabels());
return isValid;
}
bool ReshapeNode::verify() const {
bool isValid = expectCompareTrue("Reshape into a different size",
getResult().getType()->size(),
getInput().getType()->size(), this);
isValid &= checkTypeIgnoreShape(getResult(), getInput(), this);
return isValid;
}
bool TransposeNode::verify() const {
auto dest = getResult();
auto src = getInput();
ShapeVector shape;
auto dims = src.dims();
for (size_t i = 0; i < dims.size(); i++) {
shape.push_back(dims[Shuffle_[i]]);
}
bool isValid = expectCompareTrue("Invalid transpose dims", dest.dims(),
llvm::makeArrayRef(shape), this);
isValid &= checkTypeIgnoreShape(dest, src, this);
return isValid;
}
bool ChannelShuffleNode::verify() const {
bool isValid = expectCompareTrue("Channel shuffle into a different size.",
getResult().getType()->size(),
getInput().getType()->size(), this);
isValid &= checkTypeIgnoreShape(getResult(), getInput(), this);
return isValid;
}
bool SplatNode::verify() const { return true; }
bool TraceEventNode::verify() const { return true; }
bool ClipNode::verify() const {
return checkSameType(getInput(), getResult(), this);
}
bool InsertTensorNode::verify() const {
auto dest = getBig();
auto src = getSmall();
auto offsets = getStart();
size_t numDims = dest.dims().size();
size_t axis = getAxis();
size_t count = getCount();
bool isValid = expectCompareTrue("Invalid number of dimensions", numDims,
src.dims().size(), this);
isValid &= expectCompareTrue("Invalid number of dimensions for offsets",
numDims, offsets.size(), this);
if (!isValid) {
// The following loop may be out-of-bound if the previous
// comparisons failed.
return false;
}
isValid &= checkType(dest, src.getType()->getElementType(), this);
if (dest.getType()->isQuantizedType()) {
isValid &= expectCompareTrue("Scales of Big and Small must match.",
src.getType()->getScale(),
dest.getType()->getScale(), this);
isValid &= expectCompareTrue("Offsets of Big and Small must match.",
src.getType()->getOffset(),
dest.getType()->getOffset(), this);
}
for (unsigned i = 0; i < numDims; i++) {
// TODO: We could come up with a mechanism to lazy compute that
// string since it is going to be used only in case of an error.
// However, this function is not performance critical so leave it
// this way for now.
std::string msg = std::to_string(i);
msg = "out of bounds for index " + msg;
isValid &= expectCompareTrue(msg.c_str(), src.dims()[i] + offsets[i],
dest.dims()[i], this,
CompareOperatorLessEqual<size_t>());
}
isValid &= expectCompareTrue("Invalid axis", axis, src.dims().size(), this,
CompareOperatorLessEqual<size_t>());
for (size_t i = 0; i < src.dims().size(); i++) {
size_t mul = (i == axis) ? count : 1;
std::string msg = std::to_string(i);
msg = "Small does not fit inside Big for index " + msg;
isValid &=
expectCompareTrue(msg.c_str(), src.dims()[i] * mul, dest.dims()[i],
this, CompareOperatorLessEqual<size_t>());
}
return isValid;
}
bool SliceNode::verify() const {
auto dest = getResult();
auto src = getInput();
auto offsets = getStart();
size_t numDims = dest.dims().size();
bool isValid = expectCompareTrue("Invalid number of dimensions", numDims,
src.dims().size(), this);
isValid &= expectCompareTrue("Invalid number of dimensions", numDims,
offsets.size(), this);
if (!isValid) {
// The following loop may be out-of-bound if the previous
// comparisons failed.
return false;
}
for (unsigned i = 0; i < numDims; i++) {
std::string msg = std::to_string(i);
msg = "out of bounds for index " + msg;
isValid &= expectCompareTrue(msg.c_str(), dest.dims()[i] + offsets[i],
src.dims()[i], this,
CompareOperatorLessEqual<size_t>());