forked from huawei-noah/bolt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
model_optimizer.hpp
131 lines (105 loc) · 5.33 KB
/
model_optimizer.hpp
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
// Copyright (C) 2019. Huawei Technologies Co., Ltd. All rights reserved.
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#ifndef _H_MODELOPTIMIZER
#define _H_MODELOPTIMIZER
#include <vector>
#include <memory>
#include "model_tools.h"
#include "model_serialize_deserialize.hpp"
#include "OPOptimizers/OPOptimizer.hpp"
#include "OPOptimizers/DeprecatedOPOptimizer.hpp"
#include "OPOptimizers/ConvBNOptimizer.hpp"
#include "OPOptimizers/BNScaleOptimizer.hpp"
#include "OPOptimizers/ConvScaleOptimizer.hpp"
#include "OPOptimizers/PadConvOptimizer.hpp"
#include "OPOptimizers/InPlaceOptimizer.hpp"
#include "OPOptimizers/ConvActivationOptimizer.hpp"
#include "OPOptimizers/ChannelPaddingOptimizer.hpp"
#include "OPOptimizers/DepthwisePointwiseOptimizer.hpp"
#include "OPOptimizers/TransposeMulToScaleOptimizer.hpp"
#include "OPOptimizers/TransposeMatMulToFCOptimizer.hpp"
#include "OPOptimizers/FlattenGemmOptimizer.hpp"
#include "OPOptimizers/FCFCOptimizer.hpp"
#include "OPOptimizers/ClipClipOptimizer.hpp"
#include "OPOptimizers/SqueezeReshapeOptimizer.hpp"
#include "OPOptimizers/NoQuantLabelOptimizer.hpp"
#include "OPOptimizers/MemoryReuseOptimizer.hpp"
class ConvEltwisePoolingOptimizer: public OPOptimizer {
virtual bool optimize(ModelSpec* spec) override {
if(spec == nullptr)
return false;
bool hasOptimized = false;
// TODO: add fusion(low priority)
return hasOptimized;
}
};
class FCEltwiseOptimizer: public OPOptimizer {
virtual bool optimize(ModelSpec* spec) override {
if(spec == nullptr)
return false;
bool hasOptimized = false;
// TODO: add fusion(middle priority)
return hasOptimized;
}
};
class ModelSpecOptimizer {
public:
ModelSpecOptimizer() { }
/**
* @param model
*/
bool optimize(ModelSpec* spec) {
bool optimizeOrNot = false;
for (auto opo: opos) {
if (opo->optimize(spec)) {
optimizeOrNot = true;
}
}
return optimizeOrNot;
}
void suggest(float clipVal=0)
{
// strict order
this->opos.push_back(std::shared_ptr<OPOptimizer>(new DeprecatedOPOptimizer()));
// Removing ConvBN leads to error
this->opos.push_back(std::shared_ptr<OPOptimizer>(new ConvBNOptimizer()));
this->opos.push_back(std::shared_ptr<OPOptimizer>(new BNScaleOptimizer()));
this->opos.push_back(std::shared_ptr<OPOptimizer>(new ConvScaleOptimizer()));
this->opos.push_back(std::shared_ptr<OPOptimizer>(new PadConvOptimizer()));
this->opos.push_back(std::shared_ptr<OPOptimizer>(new InPlaceOptimizer()));
this->opos.push_back(std::shared_ptr<OPOptimizer>(new ConvActivationOptimizer()));
this->opos.push_back(std::shared_ptr<OPOptimizer>(new ChannelPaddingOptimizer()));
this->opos.push_back(std::shared_ptr<OPOptimizer>(new DepthwisePointwiseOptimizer()));
this->opos.push_back(std::shared_ptr<OPOptimizer>(new TransposeMulToScaleOptimizer()));
this->opos.push_back(std::shared_ptr<OPOptimizer>(new TransposeMatMulToFCOptimizer()));
this->opos.push_back(std::shared_ptr<OPOptimizer>(new FlattenGemmOptimizer()));
this->opos.push_back(std::shared_ptr<OPOptimizer>(new FCFCOptimizer()));
this->opos.push_back(std::shared_ptr<OPOptimizer>(new ClipClipOptimizer()));
this->opos.push_back(std::shared_ptr<OPOptimizer>(new SqueezeReshapeOptimizer()));
this->opos.push_back(std::shared_ptr<OPOptimizer>(new NoQuantLabelOptimizer(clipVal)));
// Please leave MemoryReuseOptimizer at last
this->opos.push_back(std::shared_ptr<OPOptimizer>(new MemoryReuseOptimizer()));
}
void suggest_for_training()
{
// strict order
this->opos.push_back(std::shared_ptr<OPOptimizer>(new DeprecatedOPOptimizer()));
this->opos.push_back(std::shared_ptr<OPOptimizer>(new PadConvOptimizer()));
this->opos.push_back(std::shared_ptr<OPOptimizer>(new NoQuantLabelOptimizer(0)));
}
void empty() {}
private:
// ModelSpecOptimizer() { }
/**
* @param opo
*/
std::vector<std::shared_ptr<OPOptimizer>> opos;
};
#endif