forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
list_model_parameters.cpp
164 lines (144 loc) · 4.91 KB
/
list_model_parameters.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
#include <torch/csrc/jit/passes/onnx/list_model_parameters.h>
#include <torch/csrc/jit/jit_log.h>
#include <torch/csrc/jit/passes/dead_code_elimination.h>
namespace torch {
namespace jit {
// findSubModuleAttr function chases getAttr chains to locate the submodules.
// For example:
// module M {
// attributes {
// A = <SubModule at ...>
// }
// ...
// %A = prim::GetAttr[name="A"](%self)
// ...
// %B = prim::GetAttr[name="B"](%A)
// ...
// %weight = prim::GetAttr[name="scale"](%B)
// ...
std::deque<std::string> findSubModuleAttr(
Value* input,
std::string& name,
Module& attrModule,
std::shared_ptr<Graph>& graph) {
Node* node = input->node();
std::deque<std::string> moduleNames;
while (!(node->outputs()[0]->type() == graph->inputs()[0]->type())) {
if (node->kind() == prim::GetAttr) {
moduleNames.push_front(node->s(attr::name));
node = node->inputs()[0]->node();
} else {
return moduleNames;
}
}
for (auto& moduleName : moduleNames) {
attrModule = attrModule.attr(moduleName).toModule();
}
return moduleNames;
}
Value* addParamAsArgument(Function* function, std::string& name, IValue& attr) {
auto schema = function->getSchema();
auto args = schema.arguments();
args.emplace_back(Argument(name, nullptr, c10::nullopt, attr));
auto new_schema = FunctionSchema(
schema.name(),
schema.overload_name(),
args,
schema.returns(),
schema.is_vararg(),
schema.is_varret());
function->setSchema(new_schema);
return function->graph()->addInput(name)->setType(attr.type());
}
std::vector<IValue> getParamAttributes(
std::shared_ptr<Graph>& graph,
const Module& module_,
Function* function_) {
std::vector<IValue> attrValues;
auto isEval = !module_.hasattr("training") || !module_.is_training();
auto block = graph->block();
std::vector<Block*> blocks({block});
Node* m = *block->nodes().begin();
WithInsertPoint guard(m);
while (!blocks.empty()) {
Block* block = blocks.back();
blocks.pop_back();
for (auto it = block->nodes().begin(); it != block->nodes().end();) {
Node* n = *it;
it++; // node n can be destroyed
for (Block* sub_block : n->blocks()) {
blocks.emplace_back(sub_block);
}
if (n->kind() == prim::SetAttr &&
n->s(attr::name) == "num_batches_tracked") {
n->destroy();
} else if (n->kind() == prim::GetAttr) {
auto name = n->s(attr::name);
auto attrModule = module_;
auto input = n->inputs()[0];
auto moduleNames = findSubModuleAttr(input, name, attrModule, graph);
if (!attrModule.hasattr(name)) {
continue;
}
Value* paramConst = nullptr;
auto attr = attrModule.attr(name);
std::string fullName("self_");
for (auto& name : moduleNames) {
fullName += name + '_';
}
fullName += name;
auto type = attrModule.type();
auto slot = *type->findAttributeSlot(name);
if (type->is_parameter(slot) || type->is_buffer(slot) ||
name == "training") {
if (type->is_parameter(slot) || type->is_buffer(slot) ||
name == "training") {
if (attr.isTensor()) {
TORCH_INTERNAL_ASSERT(attr.isTensor());
auto tensor_ = attr.toTensor();
if (isEval && tensor_.requires_grad()) {
tensor_ = tensor_.detach();
tensor_.set_requires_grad(false);
attr = IValue(tensor_);
}
attrValues.emplace_back(attr.toTensor());
paramConst = addParamAsArgument(function_, fullName, attr);
} else if (attr.isNone() || name == "training") {
auto attrVal = tryInsertConstant(*graph, attr);
paramConst = *attrVal;
}
n->output()->replaceAllUsesWith(paramConst);
n->removeAllInputs();
GRAPH_UPDATE(
"Folding GetAttr %",
n->outputs()[0]->debugName(),
" with ",
paramConst->debugName());
}
}
}
}
}
return attrValues;
}
std::pair<Module, std::vector<IValue>> list_module_parameters(
const Module& module) {
Module moduleClone = module.clone(true);
Method method = moduleClone.get_method("forward");
std::unordered_set<Function*> preservedMethods_;
preservedMethods_.insert(&method.function());
std::vector<IValue> modelParams;
for (auto function : preservedMethods_) {
GRAPH_DEBUG("List attributes for function: " + function->name());
auto graph = function->graph();
auto attributes = getParamAttributes(graph, moduleClone, function);
for (auto& attr_ : attributes) {
modelParams.push_back(attr_);
}
GRAPH_DEBUG("Cleaning up module");
EliminateDeadCode(graph->block());
}
return std::make_pair(moduleClone, modelParams);
}
} // namespace jit
} // namespace torch