-
Notifications
You must be signed in to change notification settings - Fork 21.3k
/
insert_observers.cpp
1726 lines (1586 loc) · 64.5 KB
/
insert_observers.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
#include <c10/util/irange.h>
#include <torch/csrc/jit/passes/quantization/insert_observers.h>
#include <torch/csrc/jit/frontend/schema_matching.h>
#include <torch/csrc/jit/ir/subgraph_matcher.h>
#include <torch/csrc/jit/jit_log.h>
#include <torch/csrc/jit/passes/constant_pooling.h>
#include <torch/csrc/jit/passes/constant_propagation.h>
#include <torch/csrc/jit/passes/fuse_linear.h>
#include <torch/csrc/jit/passes/graph_rewrite_helper.h>
#include <torch/csrc/jit/passes/inline_fork_wait.h>
#include <torch/csrc/jit/passes/quantization/helper.h>
#include <torch/csrc/jit/passes/remove_mutation.h>
#include <memory>
#include <stack>
#include <string>
#include <utility>
namespace torch {
namespace jit {
using ModuleQConfigMap = std::unordered_map<ModulePtr, std::optional<QConfig>>;
namespace {
struct OptionalQConfigHash {
inline size_t operator()(const std::optional<QConfig>& qconfig_opt) const {
if (qconfig_opt.has_value()) {
const auto& m1 = std::get<0>(*qconfig_opt);
const auto& m2 = std::get<1>(*qconfig_opt);
constexpr int CONST = 7;
return std::hash<Module>()(m1) + CONST * std::hash<Module>()(m2);
}
return 0;
}
};
using QConfigTypePtrMap =
std::unordered_map<std::optional<QConfig>, TypePtr, OptionalQConfigHash>;
using NameModuleVector = std::vector<std::pair<std::string, Module>>;
using OptionalModuleVector = std::vector<std::optional<Module>>;
using ModuleMethodVector = std::vector<std::pair<Module, std::string>>;
using graph_rewrite_helper::PatternInfo;
using graph_rewrite_helper::replaceConvolutionWithAtenConv;
// helper functions
void fillQConfigMap(
const Module& module,
const QConfigDict& qconfig_dict,
ModuleQConfigMap& map,
const std::string& key = "",
const std::optional<QConfig>& parent_qconfig = c10::nullopt) {
std::optional<QConfig> qconfig;
if (qconfig_dict.find(key) != qconfig_dict.end()) {
GRAPH_DEBUG("Got module config for key:", key);
qconfig = qconfig_dict.at(key);
} else {
GRAPH_DEBUG("Inheriting qconfig from parent module:", key);
qconfig = parent_qconfig;
}
map[module._ivalue()] = qconfig;
for (const NameModule& s : module.named_children()) {
std::string child_key;
if (key.empty()) {
child_key = s.name;
} else {
child_key = key + "." + s.name;
}
fillQConfigMap(s.value._ivalue(), qconfig_dict, map, child_key, qconfig);
}
}
Module getObserverModuleFor(Value* v, const QConfig& qconfig) {
return isWeight(v) ? std::get<1>(qconfig) : std::get<0>(qconfig);
}
// helper classes
class ModuleCloneHelper {
public:
/** Clone according to module qconfig map, this is for handling the case
* where we have two module instances sharing the same ClassType
* but configured with different QConfig
* code is copied and modified from
* https://github.com/pytorch/pytorch/blob/master/torch/csrc/jit/api/module.cpp
* inplace option means if the copy of the Tensor is deepcopy or not
* if inplace is true, the cloned module will share the tensors with
* original model instead of deepcopy them
*/
Module clone(
const Module& module,
const ModuleQConfigMap& module_qconfig_map,
bool inplace = false) {
std::unordered_map<TypePtr, QConfigTypePtrMap> type_remap;
IValue::HashIdentityIValueMap memo;
return clone_impl(
module, module_qconfig_map, type_remap, inplace, std::move(memo));
}
private:
Module clone_impl(
const Module& module,
const ModuleQConfigMap& module_qconfig_map,
std::unordered_map<TypePtr, QConfigTypePtrMap>& type_remap,
bool inplace,
IValue::HashIdentityIValueMap memo) {
auto qconfig = module_qconfig_map.at(module._ivalue());
auto type = module.type();
// Create a new _ivalue in the same compilation unit.
// Since now we have shared ClassType, we need to preserve the shared
// ClassType during cloning, so we first use type and qconfig to check if
// the type is already cloned, if so, we'll create a new module with the
// cloned ClassType, if not, we'll create a new module and a new ClassType.
bool type_already_cloned = type_remap.find(type) != type_remap.end() &&
type_remap.at(type).find(qconfig) != type_remap.at(type).end();
Module r;
if (type_already_cloned) {
// if we cloned the class type before, we'll reuse it
Module new_module(
module._ivalue()->compilation_unit(),
type_remap.at(type).at(qconfig)->cast<ClassType>());
r = new_module;
} else {
Module new_module(
*type->name(), module._ivalue()->compilation_unit(), true);
r = new_module;
type_remap[type][module_qconfig_map.at(module._ivalue())] = r.type();
}
// Copy slots. If a slot is a module - recursively clone it.
size_t N = type->numAttributes();
for (const auto i : c10::irange(N)) {
IValue s = module._ivalue()->getSlot(i);
std::string attr_name = type->getAttributeName(i);
TypePtr attr_type = type->getAttribute(i);
if (attr_type->is_module()) {
const Module& orig = Module(s.toObject());
Module cloned =
clone_impl(orig, module_qconfig_map, type_remap, inplace, memo);
// NOTE: why do we need to manually setattr on object instead of using
// register_module here? because the attr can be a module interface
// type and hold a Module object still. register_module will not let us
// correctly set up the type for this attr, so we had to do this
// manually. In the case it's an interface type, the type will be shared
// by the new cloned instance in the same compilation unit bc it only
// contains a list of functionSchema
r.type()->addOrCheckAttribute(
attr_name,
attr_type->cast<ClassType>() ? cloned.type() : attr_type);
r._ivalue()->setAttr(attr_name, cloned._ivalue());
} else {
// we'll deepcopy the IValue in non inplace option
r.register_attribute(
type->getAttributeName(i),
type->getAttribute(i),
inplace ? s : s.deepcopy(memo),
type->is_parameter(i),
type->is_buffer(i));
}
}
// only clone the methods and constants if the ClassType is not cloned
// before
if (!type_already_cloned) {
for (size_t i = 0; i < type->numConstants(); ++i) {
r.type()->addConstant(type->getConstantName(i), type->getConstant(i));
}
// Clone methods remapping the types to the cloned ones.
for (auto& fn : type->methods()) {
clone_method(module, r, *fn, module_qconfig_map, type_remap);
}
// Execute __setstate__(__getstate__()) to initialize custom class
// members.
if (auto setstate_method = r.find_method("__setstate__")) {
auto getstate_method = r.find_method("__getstate__");
TORCH_INTERNAL_ASSERT(getstate_method, "expect __getstate__");
auto state = (*getstate_method)(Stack{});
(*setstate_method)(Stack{std::move(state)});
}
}
return r;
}
void remapTypes(
Block* block,
Value* self,
const Module& source,
Module& target,
const ModuleQConfigMap& module_qconfig_map,
const std::function<TypePtr(TypePtr, std::optional<QConfig>)>&
type_remap_fn) {
// remap of %self will be done outside of the function
// and we don't support the case when people pass in
// module as argument of the method because in that case
// we need to do more comprehensive analysis to decide the
// QConfig for the module
for (size_t i = 1; i < block->inputs().size(); ++i) {
TORCH_CHECK(
!block->inputs()[i]->type()->cast<ClassType>(),
"We don't support quantizing methods that has Object as arguments");
}
for (Node* node : block->nodes()) {
// remapping type for module instance
if (node->kind() == prim::CallMethod || node->kind() == prim::GetAttr) {
Value* instance = node->inputs()[0];
auto child_opt = getInvokedModuleOpt(source, node, self);
if (child_opt.has_value()) {
auto qconfig = module_qconfig_map.at(child_opt->_ivalue());
instance->setType(type_remap_fn(instance->type(), qconfig));
}
}
// We don't remap output and the remapping of module type
// will be done in CallMethod, we don't support type remapping
// for modules returned from methods or functions
for (Block* sub_block : node->blocks()) {
remapTypes(
sub_block, self, source, target, module_qconfig_map, type_remap_fn);
}
for (Symbol name : node->attributeNames()) {
if (node->kindOf(name) == AttributeKind::g) {
remapTypes(
node->g(name).get(),
source,
target,
module_qconfig_map,
type_remap_fn);
} else if (node->kindOf(name) == AttributeKind::gs) {
for (const auto& g : node->gs(name)) {
remapTypes(
g.get(), source, target, module_qconfig_map, type_remap_fn);
}
}
}
}
}
void remapTypes(
Graph* graph,
const Module& source,
Module& target,
const ModuleQConfigMap& module_qconfig_map,
const std::function<TypePtr(TypePtr, std::optional<QConfig>)>&
type_remap_fn) {
remapTypes(
graph->block(),
graph->inputs()[0],
source,
target,
module_qconfig_map,
type_remap_fn);
}
void clone_method(
const Module& source,
Module& target,
const Function& method,
const ModuleQConfigMap& module_qconfig_map,
const std::unordered_map<TypePtr, QConfigTypePtrMap>& type_remap) {
auto type_remap_fn = [&](TypePtr type_ptr,
const std::optional<QConfig>& qconfig) {
if (type_remap.find(type_ptr) != type_remap.end()) {
const auto& qconfig_map = type_remap.at(type_ptr);
if (qconfig_map.find(qconfig) != qconfig_map.end()) {
return qconfig_map.at(qconfig);
}
}
return type_ptr;
};
auto graph = toGraphFunction(method).graph()->copy();
remapTypes(graph.get(), source, target, module_qconfig_map, type_remap_fn);
// remap self
graph->inputs()[0]->setType(target.type());
// we only support %self being Module in the arguments of function
auto schema_type_remap_fn = [&](TypePtr type_ptr) {
return type_remap_fn(
std::move(type_ptr), module_qconfig_map.at(source._ivalue()));
};
auto schema =
method.getSchema().cloneWithRemappedTypes(schema_type_remap_fn);
const auto this_method_name =
c10::QualifiedName(*target.type()->name(), method.name());
auto copied = target._ivalue()->compilation_unit()->create_function(
this_method_name, std::move(graph));
target.type()->addMethod(copied);
copied->setSchema(std::move(schema));
}
};
class InsertObserversHelper {
public:
explicit InsertObserversHelper(
const ModuleQConfigMap& map,
QuantType quant_type)
: module_qconfig_map_(map), quant_type_(quant_type) {}
// TODO: replace (module, method_name) with graph?
// preprocess to clean up the graph from tracing
void preprocess(Module& module, const std::string& method_name);
// Fill the map between the caller input/output to input/output
// of called graph, this is used to navigate through the graph
// to find the observer for a given value
void fillBoundaryValueMap(Module& module, const std::string& method_name);
// analyze the graph and record necessary information that can
// be used in insert observers
void analyze(Module& module, const std::string& method_name);
void removeActivationObservers();
/**
* Recursively insert observers for the method, also we'll process
* the nodes in the graph in the order of execution of these nodes
* since we need the context information to decide whether we want to
* observe/quantize a value a not, we don't want to observe a value multiple
* times.
*
* argument: is_entry_point means whether the current method is the forward
* method of the top level module.
*
* Since we want to insert observers in the call site instead of in the called
* graph, we'll postpone inserting observer to caller as much as possible, if
* we know the current method is the outer most method, then
* we will insert all observers in the graph instead of postpone this to the
* parent, note that this assumes we don't have recursive method
* calls
*
* returns a tuple of vectors of observer modules for input and output, these
* are used for inserting observers for the input/output values
* since we need to insert these values at call site.
* And a vector of indexes of outputs that indicates whether the output value
* is already observed or not, this is used for propagating the observed
* property of a value through CallMethods, because we should skip inserting
* observers for ops that don't require observation
*/
std::tuple<OptionalModuleVector, OptionalModuleVector, std::vector<size_t>>
insertObservers(
Module& module,
const std::string& method_name,
bool is_entry_point = false,
std::unordered_set<Value*> graph_observed_values =
std::unordered_set<Value*>());
void setInsertResetObserverMethod(
bool insert_reset_observer_method,
const std::string& method_name) {
insert_reset_observer_method_ = insert_reset_observer_method;
reset_observer_method_name_ = "reset_observers_" + method_name;
}
private:
std::tuple<OptionalModuleVector, OptionalModuleVector, std::vector<size_t>>
insertObserversFor(
Block* block,
script::Module& module,
// this is a reference because when we insert observer for a value
// in one block it is also observed in another block, we don't want to
// insert multiple observers for the same value
std::unordered_set<Value*>& block_observed_values,
bool is_entry_point = false,
bool is_user_defined_function = false);
// Record v as "ready for observation" by storing it in values_to_observe.
// If v is a part of a delayed observation pattern, record v's descendant
// (per delay rules) instead. The observers are inserted at a later stage
// by reading the state created by this function.
void recordObserved(
Value* v,
const Module& observer_module,
std::unordered_map<Value*, Module>& values_to_observe,
std::unordered_set<Value*>& block_observed_values);
ModuleMethodVector getInvokedMethods(
Module& module,
const std::string& method_name);
bool valueNeedsToBeQuantized(Value* v, const QConfig& qconfig);
bool isObserved(
Value* v,
const std::unordered_set<Value*>& block_observed_values) {
return block_observed_values.count(v) || observed_values_.count(v);
}
// Fill the map from value to the corresponding observer module
// this map is used in insertObservers to actually insert
// observers to the module
void fillValueObserverMap(Module& module, const std::string& method_name);
// Clone observer module and add it to the original module,
// and insert a call to observer forward function
void insertObserverFor(
Value* v,
Module& module,
const Module& observer_module,
NameModuleVector& observer_name_and_modules);
void insertObserverResetMinMax(
Module& module,
const NameModuleVector& observer_name_and_modules);
// Uses the state created by fillBoundaryValueMap and fillValueObserverMap
// to return an observer configured for a value, if it is needed.
std::optional<Module> getObserverFor(Value* v);
// Uses the state created by fillPassThroughValueMap to propagage observed
// property which should pass through from inputs to outputs.
void propagateObservedProperty(
Value* output,
std::unordered_set<Value*>& block_observed_values);
// for cat/add/mul we will only observe their output if their input
// are observed
bool shouldObserve(
Node* n,
const std::unordered_set<Value*>& block_observed_values,
QuantType quant_type) {
// Check whether node output uses can be quantized, eg cat followed by
// linear op
for (Value* v : n->outputs()) {
for (const auto& use : v->uses()) {
if (useQuantizable(use, quant_type)) {
return true;
}
}
}
if (isPropagateQuantSingleInputOp(n)) {
return isObserved(n->input(0), block_observed_values);
} else if (isPropagateQuantBinaryOp(n)) {
// This checks both of the input should be tensor and observed.
// There is one check that we didn't do here, which is
// !isScalar(isObserved(n->input(1), block_observed_values)
// to make sure input 1 is not a scalar, because scalar tensor input
// for add/mul won't be observed with current rule, we can omit
// this check here
return isObserved(n->input(0), block_observed_values) &&
isObserved(n->input(1), block_observed_values);
}
return true;
}
void delayObservingValuesInPattern(Graph& graph, const PatternInfo& pattern);
// Find and mark known patterns such as conv-relu (and others) where
// we should not insert observers in the middle of the pattern.
void addValuesToDelayObservation(
const Module& module,
const std::string& method_name);
// Fill the map from values to the list of values that can pass the observed
// property to it
void fillPassThroughValueMap(const std::shared_ptr<Graph>& graph);
bool insertResetObserverMethod() {
return insert_reset_observer_method_;
}
const ModuleQConfigMap& module_qconfig_map_;
// Values we want to delay observation, used to delay the observation for
// values in the middle of the ops that are supposed to be fused, e.g.
// the output value of conv in the conv - relu pattern
// the key is the intermediate output, e.g. output of conv
// the value is the value we want to observe, e.g. output of relu
//
// example, assuming we want to delay conv-relu:
// %x1 = conv(%x0)
// %x2 = relu(%x1)
//
// delay_observation_map_ = {
// %x1: %x2,
// }
std::unordered_map<Value*, Value*> delay_observation_map_;
std::unordered_set<Graph*> visited_graph_of_observer_map_;
// Map of value to observer module configured for that value.
std::unordered_map<Value*, Module> observer_for_value_;
// Map from values from callsite into the values in the CallMethod graph
// key of the map is the value from caller graph, and the value of the map
// is the list of values in the callee graph (the graph
// corresponding to the called method),
// the reason it is a set is that a value in the caller graph
// can both correspond to the output of one callee graph and input of another
// callee graph.
//
// example:
// // top level module
// %x1 = conv(%x0)
// %x2 = prim::CallFunction(%foo, %x1)
//
// // graph of %foo
// %y2 = conv(%y1)
// return %y2
//
// boundary_value_map = {
// // current module's output values to corresponding return values from
// subgraph %x2: %y2,
// // current module's input values to corresponding input value to subgraph
// %x1: %y1,
// }
std::unordered_map<Value*, std::unordered_set<Value*>> boundary_value_map_;
std::unordered_set<Value*> observed_values_;
// This is used for the observed values to pass through the ops like flatten,
// so that output value of flatten does not need to be observed
// key is the output of the op, value is a vector of values that need
// to be observed in order to pass the observed property to the output
//
// example:
// %x1 = flatten(%x0) // pass_through
// %x2 = conv(%x1) // not pass_through
//
// pass_through_value_map_ = {
// %x1: [%x0],
// }
std::unordered_map<Value*, std::vector<Value*>> pass_through_value_map_;
// Unique id generator for observer module, used for generating
// unique observer names when we insert observer module, we
// record the current unique id used to avoid incrementing from 0
// every time to find a unique id.
int uid_ = 0;
// Set of observer forward call nodes
std::unordered_set<Node*> observer_nodes_;
// Map from block to a vector of observer name and observer modules we
// want to add to the module instance that has the block
std::unordered_map<Block*, NameModuleVector> block_observer_map_;
// Type of quantization for this pass.
QuantType quant_type_ = QuantType::STATIC;
// These are the IR patterns we match to skip inserting observers.
// They are compiled once on construction and used repeatedly within
// the pass.
// nn.Linear + nn.ReLU
const PatternInfo nn_linear_nn_relu = PatternInfo::parse_from_str(
R"(
graph(%input, %linear, %relu):
%first_output = prim::CallMethod[name="forward"](%linear, %input)
%second_output = prim::CallMethod[name="forward\\d*"](%relu, %first_output)
return (%second_output) )",
{is_linear_module, is_relu_module});
// nn.Linear + F.relu
const PatternInfo nn_linear_f_relu = PatternInfo::parse_from_str(
R"(
graph(%input, %linear, %relu, %inplace):
%first_output = prim::CallMethod[name="forward"](%linear, %input)
%second_output = prim::CallFunction(%relu, %first_output, %inplace)
return (%second_output) )",
{is_linear_module, is_functional_relu});
// nn.Linear + aten::relu
const PatternInfo nn_linear_aten_relu = PatternInfo::parse_from_str(
R"(
graph(%input, %linear, %relu):
%first_output = prim::CallMethod[name="forward"](%linear, %input)
%second_output = aten::relu(%first_output)
return (%second_output) )",
{is_linear_module});
// nn.Linear + aten::relu_
const PatternInfo nn_linear_aten_relu_ = PatternInfo::parse_from_str(
R"(
graph(%input, %linear, %relu):
%first_output = prim::CallMethod[name="forward"](%linear, %input)
%second_output = aten::relu_(%first_output)
return (%second_output) )",
{is_linear_module});
// aten::linear + nn.ReLU
const PatternInfo aten_linear_nn_relu = PatternInfo::parse_from_str(
R"(
graph(%input, %weight, %bias, %relu):
%first_output = aten::linear(%input, %weight, %bias)
%second_output = prim::CallMethod[name="forward\\d*"](%relu, %first_output)
return (%second_output) )",
{is_relu_module});
// aten::linear + F.relu
const PatternInfo aten_linear_f_relu = PatternInfo::parse_from_str(
R"(
graph(%input, %weight, %bias, %relu, %inplace):
%first_output = aten::linear(%input, %weight, %bias)
%second_output = prim::CallFunction(%relu, %first_output, %inplace)
return (%second_output) )",
{is_functional_relu});
// aten::linear + aten::relu
const PatternInfo aten_linear_aten_relu = PatternInfo::parse_from_str(
R"(
graph(%input, %weight, %bias):
%first_output = aten::linear(%input, %weight, %bias)
%second_output = aten::relu(%first_output)
return (%second_output) )");
// aten::linear + aten::relu_
const PatternInfo aten_linear_aten_relu_ = PatternInfo::parse_from_str(
R"(
graph(%input, %weight, %bias):
%first_output = aten::linear(%input, %weight, %bias)
%second_output = aten::relu_(%first_output)
return (%second_output) )");
const PatternInfo nn_conv1d_f_relu = PatternInfo::parse_from_str(
R"(
graph(%self, %input, %conv, %relu, %inplace):
%first_output = prim::CallMethod[name="forward"](%conv, %input)
%second_output = prim::CallFunction(%relu, %first_output, %inplace)
return (%second_output) )",
{is_conv1d_module, is_functional_relu});
const PatternInfo nn_conv1d_nn_relu = PatternInfo::parse_from_str(
R"(
graph(%self, %input, %conv, %relu):
%first_output = prim::CallMethod[name="forward"](%conv, %input)
%second_output = prim::CallMethod[name="forward\\d*"](%relu, %first_output)
return (%second_output) )",
{is_conv1d_module, is_relu_module});
const PatternInfo nn_conv1d_aten_relu = PatternInfo::parse_from_str(
R"(
graph(%self, %input, %conv):
%first_output = prim::CallMethod[name="forward"](%conv, %input)
%second_output = aten::relu(%first_output)
return (%second_output) )",
{is_conv1d_module});
const PatternInfo nn_conv1d_aten_relu_ = PatternInfo::parse_from_str(
R"(
graph(%self, %input, %conv):
%first_output = prim::CallMethod[name="forward"](%conv, %input)
%second_output = aten::relu_(%first_output)
return (%second_output) )",
{is_conv1d_module});
const PatternInfo nn_conv2d_f_relu = PatternInfo::parse_from_str(
R"(
graph(%self, %input, %conv, %relu, %inplace):
%first_output = prim::CallMethod[name="forward"](%conv, %input)
%second_output = prim::CallFunction(%relu, %first_output, %inplace)
return (%second_output) )",
{is_conv2d_module, is_functional_relu});
const PatternInfo nn_conv2d_nn_relu = PatternInfo::parse_from_str(
R"(
graph(%self, %input, %conv, %relu):
%first_output = prim::CallMethod[name="forward"](%conv, %input)
%second_output = prim::CallMethod[name="forward\\d*"](%relu, %first_output)
return (%second_output) )",
{is_conv2d_module, is_relu_module});
const PatternInfo nn_conv2d_aten_relu = PatternInfo::parse_from_str(
R"(
graph(%self, %input, %conv):
%first_output = prim::CallMethod[name="forward"](%conv, %input)
%second_output = aten::relu(%first_output)
return (%second_output) )",
{is_conv2d_module});
const PatternInfo nn_conv2d_aten_relu_ = PatternInfo::parse_from_str(
R"(
graph(%self, %input, %conv):
%first_output = prim::CallMethod[name="forward"](%conv, %input)
%second_output = aten::relu_(%first_output)
return (%second_output) )",
{is_conv2d_module});
const PatternInfo nn_conv3d_f_relu = PatternInfo::parse_from_str(
R"(
graph(%self, %input, %conv, %relu, %inplace):
%first_output = prim::CallMethod[name="forward"](%conv, %input)
%second_output = prim::CallFunction(%relu, %first_output, %inplace)
return (%second_output) )",
{is_conv3d_module, is_functional_relu});
const PatternInfo nn_conv3d_nn_relu = PatternInfo::parse_from_str(
R"(
graph(%self, %input, %conv, %relu):
%first_output = prim::CallMethod[name="forward"](%conv, %input)
%second_output = prim::CallMethod[name="forward\\d*"](%relu, %first_output)
return (%second_output) )",
{is_conv3d_module, is_relu_module});
const PatternInfo nn_conv3d_aten_relu = PatternInfo::parse_from_str(
R"(
graph(%self, %conv, %input):
%first_output = prim::CallMethod[name="forward"](%conv, %input)
%second_output = aten::relu(%first_output)
return (%second_output) )",
{is_conv3d_module});
const PatternInfo nn_conv3d_aten_relu_ = PatternInfo::parse_from_str(
R"(
graph(%self, %input, %conv):
%first_output = prim::CallMethod[name="forward"](%conv, %input)
%second_output = aten::relu_(%first_output)
return (%second_output) )",
{is_conv3d_module});
const PatternInfo add_nn_relu = PatternInfo::parse_from_str(
R"(
graph(%self, %a, %b, %alpha, %relu):
%first_output = aten::add(%a, %b, %alpha)
%second_output = prim::CallMethod[name="forward\\d*"](%relu, %first_output)
return (%second_output) )",
{aten_add_alpha_is_one, is_relu_module});
const PatternInfo add_f_relu = PatternInfo::parse_from_str(
R"(
graph(%self, %a, %b, %alpha, %relu, %inplace):
%first_output = aten::add(%a, %b, %alpha)
%second_output = prim::CallFunction(%relu, %first_output, %inplace)
return (%second_output) )",
{aten_add_alpha_is_one, is_functional_relu});
const PatternInfo inplace_add_nn_relu = PatternInfo::parse_from_str(
R"(
graph(%self, %a, %b, %alpha, %relu):
%first_output = aten::add_(%a, %b, %alpha)
%second_output = prim::CallMethod[name="forward\\d*"](%relu, %first_output)
return (%second_output) )",
{aten_add_alpha_is_one, is_relu_module});
const PatternInfo inplace_add_f_relu = PatternInfo::parse_from_str(
R"(
graph(%self, %a, %b, %alpha, %relu, %inplace):
%first_output = aten::add_(%a, %b, %alpha)
%second_output = prim::CallFunction(%relu, %first_output, %inplace)
return (%second_output) )",
{aten_add_alpha_is_one, is_functional_relu});
const PatternInfo add_aten_relu = PatternInfo::parse_from_str(R"(
graph(%self, %a, %b, %alpha):
%first_output = aten::add(%a, %b, %alpha)
%second_output = aten::relu(%first_output)
return (%second_output) )");
const PatternInfo add_aten_relu_ = PatternInfo::parse_from_str(R"(
graph(%self, %a, %b, %alpha):
%first_output = aten::add(%a, %b, %alpha)
%second_output = aten::relu_(%first_output)
return (%second_output) )");
const PatternInfo inplace_add_aten_relu = PatternInfo::parse_from_str(R"(
graph(%self, %a, %b, %alpha):
%first_output = aten::add_(%a, %b, %alpha)
%second_output = aten::relu(%first_output)
return (%second_output) )");
const PatternInfo inplace_add_aten_relu_ = PatternInfo::parse_from_str(R"(
graph(%self, %a, %b, %alpha):
%first_output = aten::add_(%a, %b, %alpha)
%second_output = aten::relu_(%first_output)
return (%second_output) )");
const PatternInfo nn_bn2d_nn_relu = PatternInfo::parse_from_str(
R"(
graph(%self, %input, %batchnorm, %relu):
%first_output = prim::CallMethod[name="forward"](%batchnorm, %input)
%second_output = prim::CallMethod[name="forward\\d*"](%relu, %first_output)
return (%second_output) )",
{is_batchnorm2d_module, is_relu_module});
const PatternInfo nn_bn2d_f_relu = PatternInfo::parse_from_str(
R"(
graph(%self, %input, %batchnorm, %relu, %inplace):
%first_output = prim::CallMethod[name="forward"](%batchnorm, %input)
%second_output = prim::CallFunction(%relu, %first_output, %inplace)
return (%second_output) )",
{is_batchnorm2d_module, is_functional_relu});
const PatternInfo nn_bn2d_aten_relu = PatternInfo::parse_from_str(
R"(
graph(%self, %input, %batchnorm):
%first_output = prim::CallMethod[name="forward"](%batchnorm, %input)
%second_output = aten::relu(%first_output)
return (%second_output) )",
{is_batchnorm2d_module});
const PatternInfo nn_bn2d_aten_relu_ = PatternInfo::parse_from_str(
R"(
graph(%self, %input, %batchnorm):
%first_output = prim::CallMethod[name="forward"](%batchnorm, %input)
%second_output = aten::relu_(%first_output)
return (%second_output) )",
{is_batchnorm2d_module});
const PatternInfo nn_bn3d_nn_relu = PatternInfo::parse_from_str(
R"(
graph(%self, %input, %batchnorm, %relu):
%first_output = prim::CallMethod[name="forward"](%batchnorm, %input)
%second_output = prim::CallMethod[name="forward\\d*"](%relu, %first_output)
return (%second_output) )",
{is_batchnorm3d_module, is_relu_module});
const PatternInfo nn_bn3d_f_relu = PatternInfo::parse_from_str(
R"(
graph(%self, %input, %batchnorm, %relu, %inplace):
%first_output = prim::CallMethod[name="forward"](%batchnorm, %input)
%second_output = prim::CallFunction(%relu, %first_output, %inplace)
return (%second_output) )",
{is_batchnorm3d_module, is_functional_relu});
const PatternInfo nn_bn3d_aten_relu = PatternInfo::parse_from_str(
R"(
graph(%self, %input, %batchnorm):
%first_output = prim::CallMethod[name="forward"](%batchnorm, %input)
%second_output = aten::relu(%first_output)
return (%second_output) )",
{is_batchnorm3d_module});
const PatternInfo nn_bn3d_aten_relu_ = PatternInfo::parse_from_str(
R"(
graph(%self, %input, %batchnorm):
%first_output = prim::CallMethod[name="forward"](%batchnorm, %input)
%second_output = aten::relu_(%first_output)
return (%second_output) )",
{is_batchnorm3d_module});
const PatternInfo mul_nn_relu = PatternInfo::parse_from_str(
R"(
graph(%self, %a, %b, %relu):
%first_output = aten::mul(%a, %b)
%second_output = prim::CallMethod[name="forward"](%relu, %first_output)
return (%second_output) )",
{is_relu_module});
const PatternInfo mul_f_relu = PatternInfo::parse_from_str(
R"(
graph(%self, %a, %b, %relu, %inplace):
%first_output = aten::mul(%a, %b)
%second_output = prim::CallFunction(%relu, %first_output, %inplace)
return (%second_output) )",
{is_functional_relu});
const PatternInfo inplace_mul_nn_relu = PatternInfo::parse_from_str(
R"(
graph(%self, %a, %b, %relu):
%first_output = aten::mul_(%a, %b)
%second_output = prim::CallMethod[name="forward"](%relu, %first_output)
return (%second_output) )",
{is_relu_module});
const PatternInfo inplace_mul_f_relu = PatternInfo::parse_from_str(
R"(
graph(%self, %a, %b, %relu, %inplace):
%first_output = aten::mul_(%a, %b)
%second_output = prim::CallFunction(%relu, %first_output, %inplace)
return (%second_output) )",
{is_functional_relu});
const PatternInfo mul_aten_relu = PatternInfo::parse_from_str(R"(
graph(%self, %a, %b):
%first_output = aten::mul(%a, %b)
%second_output = aten::relu(%first_output)
return (%second_output) )");
const PatternInfo mul_aten_relu_ = PatternInfo::parse_from_str(R"(
graph(%self, %a, %b):
%first_output = aten::mul(%a, %b)
%second_output = aten::relu_(%first_output)
return (%second_output) )");
const PatternInfo inplace_mul_aten_relu = PatternInfo::parse_from_str(R"(
graph(%self, %a, %b):
%first_output = aten::mul_(%a, %b)
%second_output = aten::relu(%first_output)
return (%second_output) )");
const PatternInfo inplace_mul_aten_relu_ = PatternInfo::parse_from_str(R"(
graph(%self, %a, %b):
%first_output = aten::mul_(%a, %b)
%second_output = aten::relu_(%first_output)
return (%second_output) )");
const std::vector<std::reference_wrapper<const PatternInfo>> delay_patterns =
{
nn_linear_f_relu, nn_linear_nn_relu,
nn_linear_aten_relu, nn_linear_aten_relu_,
aten_linear_f_relu, aten_linear_nn_relu,
aten_linear_aten_relu, aten_linear_aten_relu_,
nn_conv1d_f_relu, nn_conv1d_nn_relu,
nn_conv1d_aten_relu, nn_conv1d_aten_relu_,
nn_conv2d_f_relu, nn_conv2d_nn_relu,
nn_conv2d_aten_relu, nn_conv2d_aten_relu_,
nn_conv3d_f_relu, nn_conv3d_nn_relu,
nn_conv3d_aten_relu, nn_conv3d_aten_relu_,
add_nn_relu, add_f_relu,
inplace_add_nn_relu, inplace_add_f_relu,
add_aten_relu, add_aten_relu_,
inplace_add_aten_relu, inplace_add_aten_relu_,
nn_bn2d_nn_relu, nn_bn2d_f_relu,
nn_bn2d_aten_relu, nn_bn2d_aten_relu_,
nn_bn3d_nn_relu, nn_bn3d_f_relu,
nn_bn3d_aten_relu, nn_bn3d_aten_relu_,
mul_nn_relu, mul_f_relu,
inplace_mul_nn_relu, inplace_mul_f_relu,
mul_aten_relu, mul_aten_relu_,
inplace_mul_aten_relu, inplace_mul_aten_relu_,
};
bool insert_reset_observer_method_{false};
std::string reset_observer_method_name_;
};
ModuleMethodVector InsertObserversHelper::getInvokedMethods(
Module& module,
const std::string& method_name) {
ModuleMethodVector invoked_methods;
Method method = module.get_method(method_name);
auto graph = method.graph();
std::stack<Block*> blocks_to_visit;
blocks_to_visit.push(graph->block());
while (!blocks_to_visit.empty()) {
Block* b = blocks_to_visit.top();
blocks_to_visit.pop();
for (Node* n : b->nodes()) {
// Skip observer nodes
if (observer_nodes_.count(n)) {
continue;
}
if (n->kind() == prim::CallMethod) {
auto m_opt = getInvokedModuleOpt(module, n, graph->inputs()[0]);
if (m_opt.has_value()) {
invoked_methods.emplace_back(*m_opt, n->s(attr::name));
}
}
for (Block* subblock : n->blocks()) {
blocks_to_visit.push(subblock);
}
}
}
return invoked_methods;
}
void InsertObserversHelper::insertObserverFor(
Value* v,
Module& module,
const Module& observer_module,
NameModuleVector& observer_name_and_modules) {
if (observed_values_.count(v)) {
return;
}
GRAPH_DEBUG("Inserting observer for:", v->debugName());
Module observer = observer_module.deepcopy();
std::string observer_name = "_observer_" + c10::to_string(uid_++);
while (module.hasattr(observer_name)) {
observer_name = "_observer_" + c10::to_string(uid_++);
}
module.register_module(observer_name, observer);
observer_name_and_modules.emplace_back(observer_name, observer);
auto* g = v->owningGraph();
// Get handle of observer module
Node* observer_instance =
g->createGetAttr(g->inputs()[0], observer_name)->insertAfter(v->node());
observer_instance->output()->setDebugName(observer_name);
{
WithInsertPoint guard(observer_instance->next());
// Match arguments to types of observer's arguments
MatchedSchema forward_matched_schema = matchSchema(
observer.get_method("forward").function().getSchema(),
v->node()->sourceRange(),
*g,
{observer_instance->output(), v},
{});
// Insert call to observer's forward
Node* call = g->insertMethodCall("forward", forward_matched_schema)->node();
call->output()->copyMetadata(v);
// Replace v with the output of observer
v->replaceAllUsesWith(call->output());
// The above also replaced the input to `call`, so switch it back to
// the correct value
call->replaceInput(1, v);
observer_nodes_.emplace(call);
observed_values_.insert(call->output());
}
}
void InsertObserversHelper::insertObserverResetMinMax(
Module& module,
const NameModuleVector& observer_name_and_modules) {
if (observer_name_and_modules.empty()) {
return;
}
auto reset_min_max_opt = module.find_method(reset_observer_method_name_);
if (!reset_min_max_opt.has_value()) {
std::shared_ptr<Graph> reset_observer_graph = std::make_shared<Graph>();