mirror of
https://github.com/opencv/opencv.git
synced 2026-07-30 15:53:03 +04:00
changed OpData to LayerInfo
This commit is contained in:
@@ -264,22 +264,22 @@ CV__DNN_INLINE_NS_BEGIN
|
||||
|
||||
/** @brief Backend-independent description of a graph operation (node).
|
||||
*
|
||||
* %OpData carries everything needed to reason about an operation *without* executing it:
|
||||
* %LayerInfo carries everything needed to reason about an operation *without* executing it:
|
||||
* its parameters (#blobs and type-specific fields of derived classes), graph wiring
|
||||
* (#inputs / #outputs as Arg indices) and shape/type/layout inference. The new DNN graph
|
||||
* engine stores a topologically sorted sequence of %OpData nodes (see Graph::prog());
|
||||
* engine stores a topologically sorted sequence of %LayerInfo nodes (see Graph::prog());
|
||||
* executable, backend-specific instances (Layer subclasses) are constructed from an
|
||||
* %OpData during Net::finalizeNet().
|
||||
* %LayerInfo during Net::finalizeNet().
|
||||
*
|
||||
* Each operation type registers a `static Ptr<OpData> create(const LayerParams&)` factory
|
||||
* Each operation type registers a `static Ptr<LayerInfo> create(const LayerParams&)` factory
|
||||
* via @ref CV_DNN_REGISTER_OP_CLASS_STATIC.
|
||||
*/
|
||||
class CV_EXPORTS_W OpData : public Algorithm
|
||||
class CV_EXPORTS_W LayerInfo : public Algorithm
|
||||
{
|
||||
public:
|
||||
OpData();
|
||||
explicit OpData(const LayerParams& params);
|
||||
virtual ~OpData();
|
||||
LayerInfo();
|
||||
explicit LayerInfo(const LayerParams& params);
|
||||
virtual ~LayerInfo();
|
||||
|
||||
void setParamsFrom(const LayerParams& params);
|
||||
|
||||
@@ -335,19 +335,19 @@ CV__DNN_INLINE_NS_BEGIN
|
||||
|
||||
/** @brief This interface class allows to build new Layers - are building blocks of networks.
|
||||
*
|
||||
* A %Layer is the *executable*, backend-specific counterpart of an OpData node: it
|
||||
* A %Layer is the *executable*, backend-specific counterpart of an LayerInfo node: it
|
||||
* implements forward() (and finalize()) for a particular backend/target. In the new graph
|
||||
* engine a %Layer is created from an OpData (held in #data) by Net::finalizeNet(); its
|
||||
* inference methods (getMemoryShapes() etc., inherited from OpData) delegate to #data.
|
||||
* engine a %Layer is created from an LayerInfo (held in #data) by Net::finalizeNet(); its
|
||||
* inference methods (getMemoryShapes() etc., inherited from LayerInfo) delegate to #data.
|
||||
*
|
||||
* Each class, derived from Layer, must implement forward() method to compute outputs.
|
||||
* Also before using the new layer into networks you must register your layer by using one of @ref dnnLayerFactory "LayerFactory" macros.
|
||||
*/
|
||||
class CV_EXPORTS_W Layer : public OpData
|
||||
class CV_EXPORTS_W Layer : public LayerInfo
|
||||
{
|
||||
public:
|
||||
|
||||
Ptr<OpData> data;
|
||||
Ptr<LayerInfo> data;
|
||||
|
||||
/** @brief Computes and sets internal parameters according to inputs, outputs and blobs.
|
||||
* @deprecated Use Layer::finalize(InputArrayOfArrays, OutputArrayOfArrays) instead
|
||||
@@ -514,15 +514,15 @@ CV__DNN_INLINE_NS_BEGIN
|
||||
virtual bool empty() const = 0;
|
||||
virtual void clear() = 0;
|
||||
virtual std::string name() const = 0;
|
||||
virtual const std::vector<Arg>& append(Ptr<OpData>& op,
|
||||
virtual const std::vector<Arg>& append(Ptr<LayerInfo>& op,
|
||||
const std::vector<std::string>& outnames=std::vector<std::string>()) = 0;
|
||||
virtual Arg append(Ptr<OpData>& op, const std::string& outname=std::string()) = 0;
|
||||
virtual Arg append(Ptr<LayerInfo>& op, const std::string& outname=std::string()) = 0;
|
||||
virtual std::ostream& dump(std::ostream& strm, int indent, bool comma) = 0;
|
||||
virtual const std::vector<Arg>& inputs() const = 0;
|
||||
virtual const std::vector<Arg>& outputs() const = 0;
|
||||
virtual void setOutputs(const std::vector<Arg>& outputs) = 0;
|
||||
virtual const std::vector<Ptr<OpData> >& prog() const = 0;
|
||||
virtual void setProg(const std::vector<Ptr<OpData> >& newprog) = 0;
|
||||
virtual const std::vector<Ptr<LayerInfo> >& prog() const = 0;
|
||||
virtual void setProg(const std::vector<Ptr<LayerInfo> >& newprog) = 0;
|
||||
virtual int opBackend(int opidx) const = 0;
|
||||
};
|
||||
|
||||
|
||||
@@ -45,9 +45,9 @@ Ptr<Layer> __LayerStaticRegisterer_func_##type(LayerParams ¶ms) \
|
||||
{ return Ptr<Layer>(new class(params)); } \
|
||||
static cv::dnn::details::_LayerStaticRegisterer __LayerStaticRegisterer_##type(#type, __LayerStaticRegisterer_func_##type);
|
||||
|
||||
/** @brief Registers an OpData (metadata node) class for the new graph engine.
|
||||
/** @brief Registers an LayerInfo (metadata node) class for the new graph engine.
|
||||
* @param type string, containing the operation type name.
|
||||
* @param class C++ class derived from OpData, providing `static Ptr<OpData> create(const LayerParams&)`.
|
||||
* @param class C++ class derived from LayerInfo, providing `static Ptr<LayerInfo> create(const LayerParams&)`.
|
||||
* @details This macro must be placed inside the function code (e.g. initializeLayerFactory()).
|
||||
*/
|
||||
#define CV_DNN_REGISTER_OP_CLASS(type, class) \
|
||||
@@ -57,7 +57,7 @@ static cv::dnn::details::_LayerStaticRegisterer __LayerStaticRegisterer_##type(#
|
||||
* @param type string, containing the operation type name.
|
||||
* @param backendId backend id the executor targets (e.g. DNN_BACKEND_OPENCV, DNN_BACKEND_CUDA).
|
||||
* @param class C++ class derived from Layer, providing
|
||||
* `static Ptr<Layer> create(const Ptr<OpData>&, void* backendCtx)` (null Ptr if unsupported).
|
||||
* `static Ptr<Layer> create(const Ptr<LayerInfo>&, void* backendCtx)` (null Ptr if unsupported).
|
||||
* @details This macro must be placed inside the function code.
|
||||
*/
|
||||
#define CV_DNN_REGISTER_EXEC_CLASS(type, backendId, class) \
|
||||
@@ -72,13 +72,13 @@ Ptr<Layer> _layerDynamicRegisterer(LayerParams ¶ms)
|
||||
}
|
||||
|
||||
template<typename OpClass>
|
||||
Ptr<OpData> _opDynamicRegisterer(const LayerParams ¶ms)
|
||||
Ptr<LayerInfo> _opDynamicRegisterer(const LayerParams ¶ms)
|
||||
{
|
||||
return Ptr<OpData>(OpClass::create(params));
|
||||
return Ptr<LayerInfo>(OpClass::create(params));
|
||||
}
|
||||
|
||||
template<typename ExecClass>
|
||||
Ptr<Layer> _execDynamicRegisterer(const Ptr<OpData>& data, void* backendCtx)
|
||||
Ptr<Layer> _execDynamicRegisterer(const Ptr<LayerInfo>& data, void* backendCtx)
|
||||
{
|
||||
return Ptr<Layer>(ExecClass::create(data, backendCtx));
|
||||
}
|
||||
|
||||
@@ -78,16 +78,16 @@ public:
|
||||
|
||||
|
||||
// Builds the abstract (metadata) node for an operation type.
|
||||
typedef Ptr<OpData>(*OpConstructor)(const LayerParams& params);
|
||||
//! Builds a backend-specific executor from an OpData; returns null Ptr if unsupported.
|
||||
typedef Ptr<Layer>(*ExecConstructor)(const Ptr<OpData>& data, void* backendCtx);
|
||||
typedef Ptr<LayerInfo>(*OpConstructor)(const LayerParams& params);
|
||||
//! Builds a backend-specific executor from an LayerInfo; returns null Ptr if unsupported.
|
||||
typedef Ptr<Layer>(*ExecConstructor)(const Ptr<LayerInfo>& data, void* backendCtx);
|
||||
|
||||
static void registerOp(const String& type, OpConstructor constructor);
|
||||
static Ptr<OpData> createOp(const String& type, const LayerParams& params);
|
||||
static Ptr<LayerInfo> createOp(const String& type, const LayerParams& params);
|
||||
|
||||
static void registerExec(const String& type, int backendId, ExecConstructor constructor);
|
||||
static Ptr<Layer> createExec(const String& type, int backendId,
|
||||
const Ptr<OpData>& data, void* backendCtx);
|
||||
const Ptr<LayerInfo>& data, void* backendCtx);
|
||||
|
||||
private:
|
||||
LayerFactory();
|
||||
|
||||
@@ -11,7 +11,7 @@ CV__DNN_INLINE_NS_BEGIN
|
||||
using std::vector;
|
||||
using std::string;
|
||||
|
||||
using PLayer = Ptr<OpData>;
|
||||
using PLayer = Ptr<LayerInfo>;
|
||||
using PGraph = Ptr<Graph>;
|
||||
|
||||
/* Inserts layout conversion operations (if needed) into the model graph and subgraphs.
|
||||
|
||||
@@ -226,7 +226,7 @@ struct BufferAllocator
|
||||
}
|
||||
}
|
||||
}
|
||||
const std::vector<Ptr<OpData> >& prog = graph->prog();
|
||||
const std::vector<Ptr<LayerInfo> >& prog = graph->prog();
|
||||
for (const auto& layer: prog) {
|
||||
bool inplace = false;
|
||||
Arg reuseArg;
|
||||
|
||||
@@ -36,14 +36,14 @@ struct ConstArgs
|
||||
|
||||
void processGraph(Ptr<Graph>& graph)
|
||||
{
|
||||
const std::vector<Ptr<OpData> >& prog = graph->prog();
|
||||
const std::vector<Ptr<LayerInfo> >& prog = graph->prog();
|
||||
size_t i, nops = prog.size();
|
||||
std::vector<Arg> removed_args;
|
||||
std::vector<Arg> saved_tail_inputs;
|
||||
|
||||
for (i = 0; i < nops; i++) {
|
||||
const Ptr<OpData>& layer = prog[i];
|
||||
OpData* layer_ptr = const_cast<OpData*>(layer.get());
|
||||
const Ptr<LayerInfo>& layer = prog[i];
|
||||
LayerInfo* layer_ptr = const_cast<LayerInfo*>(layer.get());
|
||||
std::vector<Ptr<Graph> >* subgraphs = layer->subgraphs();
|
||||
if (subgraphs) {
|
||||
for (Ptr<Graph>& g: *subgraphs) {
|
||||
|
||||
@@ -30,7 +30,7 @@ struct ConstFolding
|
||||
netimpl->scratchBufs.clear();
|
||||
}
|
||||
|
||||
OpData* getLayer(std::vector<Ptr<OpData> >& newprog, int op_idx) const
|
||||
LayerInfo* getLayer(std::vector<Ptr<LayerInfo> >& newprog, int op_idx) const
|
||||
{
|
||||
return op_idx >= 0 ? newprog.at(op_idx).get() : 0;
|
||||
}
|
||||
@@ -47,16 +47,16 @@ struct ConstFolding
|
||||
{
|
||||
netimpl->scratchBufs.clear();
|
||||
bool modified = false;
|
||||
const std::vector<Ptr<OpData> >& prog = graph->prog();
|
||||
const std::vector<Ptr<LayerInfo> >& prog = graph->prog();
|
||||
size_t i, nops = prog.size();
|
||||
std::vector<Ptr<OpData> > newprog;
|
||||
std::vector<Ptr<LayerInfo> > newprog;
|
||||
std::vector<Arg> removed_args;
|
||||
std::vector<Mat> inpMats, tempMats;
|
||||
std::vector<int> inpTypes, outTypes, tempTypes;
|
||||
std::vector<MatShape> inpShapes, outShapes, tempShapes;
|
||||
|
||||
for (i = 0; i < nops; i++) {
|
||||
const Ptr<OpData>& layer = prog[i];
|
||||
const Ptr<LayerInfo>& layer = prog[i];
|
||||
std::vector<Ptr<Graph> >* subgraphs = layer->subgraphs();
|
||||
if (subgraphs) {
|
||||
for (Ptr<Graph>& g: *subgraphs) {
|
||||
|
||||
@@ -32,35 +32,35 @@ struct ModelFusionAttention
|
||||
return it->second[0];
|
||||
}
|
||||
|
||||
bool isReshape(const vector<Ptr<OpData>>& prog, int idx) const
|
||||
bool isReshape(const vector<Ptr<LayerInfo>>& prog, int idx) const
|
||||
{
|
||||
if (idx < 0 || idx >= (int)prog.size() || !prog[idx])
|
||||
return false;
|
||||
return dynamic_cast<Reshape2Layer*>(prog[idx].get()) != nullptr;
|
||||
}
|
||||
|
||||
bool isTranspose(const vector<Ptr<OpData>>& prog, int idx) const
|
||||
bool isTranspose(const vector<Ptr<LayerInfo>>& prog, int idx) const
|
||||
{
|
||||
if (idx < 0 || idx >= (int)prog.size() || !prog[idx])
|
||||
return false;
|
||||
return dynamic_cast<TransposeLayer*>(prog[idx].get()) != nullptr;
|
||||
}
|
||||
|
||||
bool isSoftmax(const vector<Ptr<OpData>>& prog, int idx) const
|
||||
bool isSoftmax(const vector<Ptr<LayerInfo>>& prog, int idx) const
|
||||
{
|
||||
if (idx < 0 || idx >= (int)prog.size() || !prog[idx])
|
||||
return false;
|
||||
return prog[idx]->type == "Softmax";
|
||||
}
|
||||
|
||||
bool isMatMul(const vector<Ptr<OpData>>& prog, int idx) const
|
||||
bool isMatMul(const vector<Ptr<LayerInfo>>& prog, int idx) const
|
||||
{
|
||||
if (idx < 0 || idx >= (int)prog.size() || !prog[idx])
|
||||
return false;
|
||||
return dynamic_cast<MatMulLayer*>(prog[idx].get()) != nullptr;
|
||||
}
|
||||
|
||||
static bool isProjCandidate(const Ptr<OpData>& l)
|
||||
static bool isProjCandidate(const Ptr<LayerInfo>& l)
|
||||
{
|
||||
if (l->blobs.empty() || l->inputs.size() != 1) return false;
|
||||
if (dynamic_cast<MatMulLayer*>(l.get()))
|
||||
@@ -74,7 +74,7 @@ struct ModelFusionAttention
|
||||
|
||||
// Returns the projection weight in [K, N] (input_hidden, output_hidden)
|
||||
// layout, transposing if the source is a Gemm with trans_b.
|
||||
static Mat getProjWeight(const Ptr<OpData>& l)
|
||||
static Mat getProjWeight(const Ptr<LayerInfo>& l)
|
||||
{
|
||||
const Mat& W = l->blobs[0];
|
||||
GemmLayer* g = dynamic_cast<GemmLayer*>(l.get());
|
||||
@@ -86,7 +86,7 @@ struct ModelFusionAttention
|
||||
return W;
|
||||
}
|
||||
|
||||
bool isScalarBinOp(const vector<Ptr<OpData>>& prog, int idx,
|
||||
bool isScalarBinOp(const vector<Ptr<LayerInfo>>& prog, int idx,
|
||||
NaryEltwiseLayer::OPERATION op, float* val) const
|
||||
{
|
||||
if (idx < 0 || idx >= (int)prog.size() || !prog[idx])
|
||||
@@ -109,18 +109,18 @@ struct ModelFusionAttention
|
||||
return false;
|
||||
}
|
||||
|
||||
bool isScalarMul(const vector<Ptr<OpData>>& prog, int idx, float* val) const
|
||||
bool isScalarMul(const vector<Ptr<LayerInfo>>& prog, int idx, float* val) const
|
||||
{
|
||||
return isScalarBinOp(prog, idx, NaryEltwiseLayer::OPERATION::PROD, val);
|
||||
}
|
||||
|
||||
bool isScalarDiv(const vector<Ptr<OpData>>& prog, int idx, float* val) const
|
||||
bool isScalarDiv(const vector<Ptr<LayerInfo>>& prog, int idx, float* val) const
|
||||
{
|
||||
return isScalarBinOp(prog, idx, NaryEltwiseLayer::OPERATION::DIV, val);
|
||||
}
|
||||
|
||||
// True if `arg` is produced by the dynamic scale chain Sqrt<-Cast<-Div(1,.)<-Sqrt<-Cast<-Slice<-Shape; visited ops are appended to `chain_ops`.
|
||||
bool isRuntimeQKScaleChain(const vector<Ptr<OpData>>& prog, Arg arg,
|
||||
bool isRuntimeQKScaleChain(const vector<Ptr<LayerInfo>>& prog, Arg arg,
|
||||
std::set<int>& chain_ops) const
|
||||
{
|
||||
const std::vector<std::string> expected = {
|
||||
@@ -133,7 +133,7 @@ struct ModelFusionAttention
|
||||
if (it == producer_.end()) return false;
|
||||
int idx = it->second;
|
||||
if (idx < 0 || idx >= (int)prog.size() || !prog[idx]) return false;
|
||||
const Ptr<OpData>& l = prog[idx];
|
||||
const Ptr<LayerInfo>& l = prog[idx];
|
||||
if (want == "NaryEltwise") {
|
||||
NaryEltwiseLayer* elt = dynamic_cast<NaryEltwiseLayer*>(l.get());
|
||||
if (!elt || elt->op != NaryEltwiseLayer::OPERATION::DIV) return false;
|
||||
@@ -171,7 +171,7 @@ struct ModelFusionAttention
|
||||
|
||||
// Accept Add op with exactly two inputs; identify the non-constant runtime
|
||||
// input (the mask tensor). Returns false if the Add doesn't match.
|
||||
bool isMaskAdd(const vector<Ptr<OpData>>& prog, int idx, Arg* out_mask) const
|
||||
bool isMaskAdd(const vector<Ptr<LayerInfo>>& prog, int idx, Arg* out_mask) const
|
||||
{
|
||||
if (idx < 0 || idx >= (int)prog.size() || !prog[idx])
|
||||
return false;
|
||||
@@ -186,7 +186,7 @@ struct ModelFusionAttention
|
||||
|
||||
// Extract a scalar integer from a const-valued arg, possibly wrapped in an
|
||||
// Unsqueeze of a scalar const. Returns -1 if extraction fails.
|
||||
int extractConstInt(const vector<Ptr<OpData>>& prog, Arg a) const
|
||||
int extractConstInt(const vector<Ptr<LayerInfo>>& prog, Arg a) const
|
||||
{
|
||||
auto readScalar = [&](Arg x) -> int {
|
||||
if (!netimpl->isConstArg(x)) return -1;
|
||||
@@ -207,7 +207,7 @@ struct ModelFusionAttention
|
||||
return -1;
|
||||
}
|
||||
|
||||
void collectShapeChain(const vector<Ptr<OpData>>& prog, int concat_idx,
|
||||
void collectShapeChain(const vector<Ptr<LayerInfo>>& prog, int concat_idx,
|
||||
std::set<int>& chain) const
|
||||
{
|
||||
if (concat_idx < 0 || concat_idx >= (int)prog.size() || !prog[concat_idx])
|
||||
@@ -238,7 +238,7 @@ struct ModelFusionAttention
|
||||
}
|
||||
|
||||
template <class Pred>
|
||||
int findMatchingConsumer(const vector<Ptr<OpData>>& prog, Arg out,
|
||||
int findMatchingConsumer(const vector<Ptr<LayerInfo>>& prog, Arg out,
|
||||
Pred pred, std::set<int>* extra_shape_ops) const
|
||||
{
|
||||
auto it = consumers_.find(out.idx);
|
||||
@@ -258,7 +258,7 @@ struct ModelFusionAttention
|
||||
return matched;
|
||||
}
|
||||
|
||||
int followProjChain(const vector<Ptr<OpData>>& prog,
|
||||
int followProjChain(const vector<Ptr<LayerInfo>>& prog,
|
||||
int proj_matmul_idx,
|
||||
int* out_reshape_idx,
|
||||
int* out_num_heads,
|
||||
@@ -268,7 +268,7 @@ struct ModelFusionAttention
|
||||
if (proj_matmul_idx < 0) return -1;
|
||||
Arg proj_out = prog[proj_matmul_idx]->outputs[0];
|
||||
int reshape_idx = findMatchingConsumer(prog, proj_out,
|
||||
[](OpData* L){ return dynamic_cast<Reshape2Layer*>(L) != nullptr; },
|
||||
[](LayerInfo* L){ return dynamic_cast<Reshape2Layer*>(L) != nullptr; },
|
||||
extra_ops_to_remove);
|
||||
if (!isReshape(prog, reshape_idx)) return -1;
|
||||
|
||||
@@ -311,9 +311,9 @@ struct ModelFusionAttention
|
||||
|
||||
// Combined-QKV attention: QKV proj -> Reshape ->
|
||||
// Transpose -> 3 Gathers -> QK^T -> Softmax(no mask) -> *V.
|
||||
bool tryFuseCombinedQKV(const vector<Ptr<OpData>>& prog, int qkv_matmul_idx,
|
||||
bool tryFuseCombinedQKV(const vector<Ptr<LayerInfo>>& prog, int qkv_matmul_idx,
|
||||
std::set<int>& removed_ops,
|
||||
vector<std::pair<int, Ptr<OpData>>>& replacements)
|
||||
vector<std::pair<int, Ptr<LayerInfo>>>& replacements)
|
||||
{
|
||||
if (qkv_matmul_idx < 0 || qkv_matmul_idx >= (int)prog.size() || !prog[qkv_matmul_idx])
|
||||
return false;
|
||||
@@ -456,7 +456,7 @@ struct ModelFusionAttention
|
||||
int out_trans_idx = singleConsumer(prog[av_matmul_idx]->outputs[0]);
|
||||
if (!isTranspose(prog, out_trans_idx)) return false;
|
||||
int out_reshape_idx = findMatchingConsumer(prog, prog[out_trans_idx]->outputs[0],
|
||||
[](OpData* L){ return dynamic_cast<Reshape2Layer*>(L) != nullptr; },
|
||||
[](LayerInfo* L){ return dynamic_cast<Reshape2Layer*>(L) != nullptr; },
|
||||
&extra_ops);
|
||||
if (!isReshape(prog, out_reshape_idx)) return false;
|
||||
|
||||
@@ -487,7 +487,7 @@ struct ModelFusionAttention
|
||||
attn_params.blobs.push_back(W_qkv);
|
||||
if (has_bias) attn_params.blobs.push_back(bias_qkv);
|
||||
|
||||
Ptr<OpData> attn_layer = LayerFactory::createLayerInstance(attn_params.type, attn_params);
|
||||
Ptr<LayerInfo> attn_layer = LayerFactory::createLayerInstance(attn_params.type, attn_params);
|
||||
CV_Assert(attn_layer);
|
||||
Arg shared_input = prog[qkv_matmul_idx]->inputs[0];
|
||||
attn_layer->inputs = { shared_input };
|
||||
@@ -514,7 +514,7 @@ struct ModelFusionAttention
|
||||
|
||||
// CLIP-branch trace: arg -> [Transpose3D(K^T)] -> Reshape3D -> Transpose ->
|
||||
// Reshape4D -> [Mul(Q scale)] -> [Add(bias)] -> proj_MatMul.
|
||||
int traceClipBranch(const vector<Ptr<OpData>>& prog, Arg arg,
|
||||
int traceClipBranch(const vector<Ptr<LayerInfo>>& prog, Arg arg,
|
||||
bool is_q_branch, bool is_k_branch,
|
||||
Mat& out_W, Mat& out_bias, int& out_num_heads,
|
||||
float& out_q_scale,
|
||||
@@ -660,9 +660,9 @@ struct ModelFusionAttention
|
||||
|
||||
// 3 separate q/k/v projections, Q scaled, K^T at
|
||||
// the QK^T matmul, output reshaped+transposed back to (B,S,H*D).
|
||||
bool tryFuseClipAttention(const vector<Ptr<OpData>>& prog, int softmax_idx,
|
||||
bool tryFuseClipAttention(const vector<Ptr<LayerInfo>>& prog, int softmax_idx,
|
||||
std::set<int>& removed_ops,
|
||||
vector<std::pair<int, Ptr<OpData>>>& replacements)
|
||||
vector<std::pair<int, Ptr<LayerInfo>>>& replacements)
|
||||
{
|
||||
if (softmax_idx < 0 || softmax_idx >= (int)prog.size() || !prog[softmax_idx])
|
||||
return false;
|
||||
@@ -782,7 +782,7 @@ struct ModelFusionAttention
|
||||
attn_params.blobs.push_back(W_qkv);
|
||||
if (!bias_qkv.empty()) attn_params.blobs.push_back(bias_qkv);
|
||||
|
||||
Ptr<OpData> attn_layer =
|
||||
Ptr<LayerInfo> attn_layer =
|
||||
LayerFactory::createLayerInstance(attn_params.type, attn_params);
|
||||
if (!attn_layer) return false;
|
||||
attn_layer->inputs = { shared_input };
|
||||
@@ -804,7 +804,7 @@ struct ModelFusionAttention
|
||||
|
||||
bool fuseGraph(Ptr<Graph>& graph)
|
||||
{
|
||||
const vector<Ptr<OpData>>& prog = graph->prog();
|
||||
const vector<Ptr<LayerInfo>>& prog = graph->prog();
|
||||
size_t nops = prog.size();
|
||||
|
||||
producer_.clear();
|
||||
@@ -885,7 +885,7 @@ struct ModelFusionAttention
|
||||
Arg k_tr_out = prog[transpose_idx[k_slot]]->outputs[0];
|
||||
// Tolerate a Shape consumer alongside the Mul/MatMul: the runtime-scale chain (Shape->Slice->Cast->Sqrt...) branches off the Q/K transpose.
|
||||
int k_next = findMatchingConsumer(prog, k_tr_out,
|
||||
[](OpData* L) {
|
||||
[](LayerInfo* L) {
|
||||
return dynamic_cast<NaryEltwiseLayer*>(L) != nullptr ||
|
||||
dynamic_cast<MatMulLayer*>(L) != nullptr;
|
||||
},
|
||||
@@ -939,7 +939,7 @@ struct ModelFusionAttention
|
||||
|
||||
if (vit_style) {
|
||||
int q_next = findMatchingConsumer(prog, q_tr_out,
|
||||
[](OpData* L) {
|
||||
[](LayerInfo* L) {
|
||||
return dynamic_cast<NaryEltwiseLayer*>(L) != nullptr ||
|
||||
dynamic_cast<MatMulLayer*>(L) != nullptr;
|
||||
},
|
||||
@@ -1020,7 +1020,7 @@ struct ModelFusionAttention
|
||||
|
||||
Arg out_tr_out = prog[out_transpose_idx]->outputs[0];
|
||||
int out_reshape_idx = findMatchingConsumer(prog, out_tr_out,
|
||||
[](OpData* L){ return dynamic_cast<Reshape2Layer*>(L) != nullptr; },
|
||||
[](LayerInfo* L){ return dynamic_cast<Reshape2Layer*>(L) != nullptr; },
|
||||
&extra_ops);
|
||||
if (!isReshape(prog, out_reshape_idx)) continue;
|
||||
|
||||
@@ -1094,7 +1094,7 @@ struct ModelFusionAttention
|
||||
if (has_bias)
|
||||
attn_params.blobs.push_back(bias_qkv);
|
||||
|
||||
Ptr<OpData> attn_layer = LayerFactory::createLayerInstance(
|
||||
Ptr<LayerInfo> attn_layer = LayerFactory::createLayerInstance(
|
||||
attn_params.type, attn_params);
|
||||
CV_Assert(attn_layer);
|
||||
|
||||
@@ -1157,7 +1157,7 @@ struct ModelFusionAttention
|
||||
}
|
||||
|
||||
if (modified) {
|
||||
vector<Ptr<OpData>> newprog;
|
||||
vector<Ptr<LayerInfo>> newprog;
|
||||
std::sort(attention_replacements_.begin(), attention_replacements_.end(),
|
||||
[](auto& a, auto& b) { return a.first < b.first; });
|
||||
|
||||
@@ -1183,7 +1183,7 @@ struct ModelFusionAttention
|
||||
private:
|
||||
std::map<int, int> producer_;
|
||||
std::map<int, vector<int>> consumers_;
|
||||
vector<std::pair<int, Ptr<OpData>>> attention_replacements_;
|
||||
vector<std::pair<int, Ptr<LayerInfo>>> attention_replacements_;
|
||||
};
|
||||
|
||||
void Net::Impl::fuseAttention()
|
||||
|
||||
@@ -30,7 +30,7 @@ struct ModelFusionBasic
|
||||
}
|
||||
|
||||
template<typename _LayerType> _LayerType*
|
||||
getLayer(std::vector<Ptr<OpData> >& newprog, int op_idx) const
|
||||
getLayer(std::vector<Ptr<LayerInfo> >& newprog, int op_idx) const
|
||||
{
|
||||
return op_idx >= 0 ? dynamic_cast<_LayerType*>(newprog.at(op_idx).get()) : 0;
|
||||
}
|
||||
@@ -39,14 +39,14 @@ struct ModelFusionBasic
|
||||
{
|
||||
vector<Arg> removed_args;
|
||||
bool modified = false;
|
||||
const std::vector<Ptr<OpData> >& prog = graph->prog();
|
||||
const std::vector<Ptr<LayerInfo> >& prog = graph->prog();
|
||||
size_t i, nargs = netimpl->args.size(), nops = prog.size();
|
||||
std::vector<int> producer_of(nargs, -1);
|
||||
std::vector<Ptr<OpData> > newprog;
|
||||
std::vector<Ptr<LayerInfo> > newprog;
|
||||
std::vector<Arg> fused_inputs;
|
||||
|
||||
for (i = 0; i < nops; i++) {
|
||||
const Ptr<OpData>& layer = prog[i];
|
||||
const Ptr<LayerInfo>& layer = prog[i];
|
||||
Layer* layer_ptr = (Layer*)layer.get();
|
||||
int fused_layer_idx = -1;
|
||||
std::vector<Ptr<Graph> >* subgraphs = layer->subgraphs();
|
||||
@@ -209,7 +209,7 @@ struct ModelFusionBasic
|
||||
gnparams.type = "GroupNormalization";
|
||||
gnparams.set("epsilon", instnorm->epsilon);
|
||||
gnparams.set("num_groups", num_groups);
|
||||
Ptr<OpData> gnlayer = GroupNormLayer::create(gnparams);
|
||||
Ptr<LayerInfo> gnlayer = GroupNormLayer::create(gnparams);
|
||||
gnlayer->netimpl = netimpl;
|
||||
gnlayer->inputs = {orig_inp, mul_scale_arg, add_bias_arg};
|
||||
newprog[instnorm_idx] = gnlayer;
|
||||
@@ -219,9 +219,9 @@ struct ModelFusionBasic
|
||||
removed_args.push_back(reshape2_inp);
|
||||
removed_args.push_back(reshape2_out);
|
||||
removed_args.push_back(mul_out);
|
||||
newprog[reshape1_idx] = Ptr<OpData>();
|
||||
newprog[reshape2_idx] = Ptr<OpData>();
|
||||
newprog[mul_idx] = Ptr<OpData>();
|
||||
newprog[reshape1_idx] = Ptr<LayerInfo>();
|
||||
newprog[reshape2_idx] = Ptr<LayerInfo>();
|
||||
newprog[mul_idx] = Ptr<LayerInfo>();
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -293,15 +293,15 @@ struct FuseBNPass
|
||||
|
||||
void fuseGraph(Ptr<Graph>& graph)
|
||||
{
|
||||
const std::vector<Ptr<OpData> >& prog = graph->prog();
|
||||
const std::vector<Ptr<LayerInfo> >& prog = graph->prog();
|
||||
size_t nops = prog.size(), nargs = netimpl->args.size();
|
||||
std::vector<Ptr<OpData> > newprog;
|
||||
std::vector<Ptr<LayerInfo> > newprog;
|
||||
newprog.reserve(nops);
|
||||
std::vector<int> producer_of((int)nargs, -1);
|
||||
bool modified = false;
|
||||
|
||||
for (size_t i = 0; i < nops; i++) {
|
||||
const Ptr<OpData>& layer = prog[i];
|
||||
const Ptr<LayerInfo>& layer = prog[i];
|
||||
Layer* layer_ptr = (Layer*)layer.get();
|
||||
|
||||
std::vector<Ptr<Graph> >* subgraphs = layer->subgraphs();
|
||||
@@ -324,7 +324,7 @@ struct FuseBNPass
|
||||
usecounts[conv_inp0.idx] = 0;
|
||||
if (bn_inp0.idx >= 0)
|
||||
usecounts[bn_inp0.idx]++;
|
||||
newprog[bn_idx] = Ptr<OpData>();
|
||||
newprog[bn_idx] = Ptr<LayerInfo>();
|
||||
modified = true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -43,7 +43,7 @@ struct ModelFusionMatMulToGemm
|
||||
|
||||
bool fuseGraph(Ptr<Graph>& graph)
|
||||
{
|
||||
const vector<Ptr<OpData>>& prog = graph->prog();
|
||||
const vector<Ptr<LayerInfo>>& prog = graph->prog();
|
||||
size_t nops = prog.size();
|
||||
bool modified = false;
|
||||
|
||||
@@ -56,11 +56,11 @@ struct ModelFusionMatMulToGemm
|
||||
}
|
||||
}
|
||||
|
||||
vector<Ptr<OpData>> newprog = prog;
|
||||
vector<Ptr<LayerInfo>> newprog = prog;
|
||||
bool changed = false;
|
||||
|
||||
for (size_t i = 0; i < nops; i++) {
|
||||
const Ptr<OpData>& layer = newprog[i];
|
||||
const Ptr<LayerInfo>& layer = newprog[i];
|
||||
if (!layer) continue;
|
||||
|
||||
MatMulLayer* mm = dynamic_cast<MatMulLayer*>(layer.get());
|
||||
@@ -120,7 +120,7 @@ struct ModelFusionMatMulToGemm
|
||||
gp.blobs.push_back(B);
|
||||
if (have_bias) gp.blobs.push_back(layer->blobs[1]);
|
||||
|
||||
Ptr<OpData> gemm = LayerFactory::createLayerInstance("Gemm", gp);
|
||||
Ptr<LayerInfo> gemm = LayerFactory::createLayerInstance("Gemm", gp);
|
||||
if (!gemm) continue;
|
||||
gemm->inputs = layer->inputs;
|
||||
gemm->outputs = layer->outputs;
|
||||
|
||||
@@ -32,7 +32,7 @@ struct ModelFusionQDQ
|
||||
}
|
||||
|
||||
template<typename _LayerType> _LayerType*
|
||||
getLayer(std::vector<Ptr<OpData> >& newprog, int op_idx) const
|
||||
getLayer(std::vector<Ptr<LayerInfo> >& newprog, int op_idx) const
|
||||
{
|
||||
return op_idx >= 0 ? dynamic_cast<_LayerType*>(newprog.at(op_idx).get()) : 0;
|
||||
}
|
||||
@@ -48,10 +48,10 @@ struct ModelFusionQDQ
|
||||
return params;
|
||||
}
|
||||
|
||||
Ptr<OpData> createFusedLayer(const LayerParams& src) const
|
||||
Ptr<LayerInfo> createFusedLayer(const LayerParams& src) const
|
||||
{
|
||||
LayerParams params = src;
|
||||
Ptr<OpData> layer = LayerFactory::createLayerInstance(params.type, params);
|
||||
Ptr<LayerInfo> layer = LayerFactory::createLayerInstance(params.type, params);
|
||||
if (!layer.empty())
|
||||
layer->netimpl = netimpl;
|
||||
return layer;
|
||||
@@ -94,7 +94,7 @@ struct ModelFusionQDQ
|
||||
size_t ninputs,
|
||||
const std::vector<Arg>& inputs,
|
||||
const std::vector<int>& producer_of,
|
||||
std::vector<Ptr<OpData> >& newprog,
|
||||
std::vector<Ptr<LayerInfo> >& newprog,
|
||||
Arg& q_data_in,
|
||||
Arg& out_scale,
|
||||
Arg& out_zp,
|
||||
@@ -118,10 +118,10 @@ struct ModelFusionQDQ
|
||||
{
|
||||
vector<Arg> removed_args;
|
||||
bool modified = false;
|
||||
const std::vector<Ptr<OpData> >& prog = graph->prog();
|
||||
const std::vector<Ptr<LayerInfo> >& prog = graph->prog();
|
||||
size_t i, nargs = netimpl->args.size(), nops = prog.size();
|
||||
std::vector<int> producer_of(nargs, -1);
|
||||
std::vector<Ptr<OpData> > newprog;
|
||||
std::vector<Ptr<LayerInfo> > newprog;
|
||||
std::vector<Arg> fused_inputs;
|
||||
std::set<int> skip_indices;
|
||||
std::vector<Arg> override_outputs;
|
||||
@@ -129,7 +129,7 @@ struct ModelFusionQDQ
|
||||
|
||||
for (i = 0; i < nops; i++) {
|
||||
if (skip_indices.count((int)i)) continue;
|
||||
const Ptr<OpData>& layer = prog[i];
|
||||
const Ptr<LayerInfo>& layer = prog[i];
|
||||
Layer* layer_ptr = (Layer*)layer.get();
|
||||
int fused_layer_idx = -1;
|
||||
std::vector<Ptr<Graph> >* subgraphs = layer->subgraphs();
|
||||
@@ -219,7 +219,7 @@ struct ModelFusionQDQ
|
||||
removed_args.push_back(add_inp);
|
||||
|
||||
for (int dq_prog_idx : dq_prog_indices)
|
||||
newprog[dq_prog_idx] = Ptr<OpData>();
|
||||
newprog[dq_prog_idx] = Ptr<LayerInfo>();
|
||||
|
||||
break;
|
||||
}
|
||||
@@ -289,7 +289,7 @@ struct ModelFusionQDQ
|
||||
fused_inputs.assign(1, dq->inputs[0]);
|
||||
removed_args.push_back(q_data_in);
|
||||
removed_args.push_back(relu_in);
|
||||
newprog[dq_idx] = Ptr<OpData>();
|
||||
newprog[dq_idx] = Ptr<LayerInfo>();
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -363,7 +363,7 @@ struct ModelFusionQDQ
|
||||
fused_inputs.assign(1, dq->inputs[0]);
|
||||
removed_args.push_back(q_data_in);
|
||||
removed_args.push_back(sig_in);
|
||||
newprog[dq_idx] = Ptr<OpData>();
|
||||
newprog[dq_idx] = Ptr<LayerInfo>();
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -479,7 +479,7 @@ struct ModelFusionQDQ
|
||||
if (relu_out_uc <= 1) {
|
||||
fused_layer_idx = add_idx2;
|
||||
newprog[add_idx2] = eltInt8;
|
||||
newprog[relu_layer_idx2] = Ptr<OpData>();
|
||||
newprog[relu_layer_idx2] = Ptr<LayerInfo>();
|
||||
removed_args.push_back(q_inp); // relu_out
|
||||
removed_args.push_back(relu_in2); // add_out
|
||||
for (size_t dk = 0; dk < add2->inputs.size(); dk++) {
|
||||
@@ -487,7 +487,7 @@ struct ModelFusionQDQ
|
||||
}
|
||||
for (int dq_prog_idx : dq_prog_indices2) {
|
||||
if (dq_prog_idx >= 0)
|
||||
newprog[dq_prog_idx] = Ptr<OpData>();
|
||||
newprog[dq_prog_idx] = Ptr<LayerInfo>();
|
||||
}
|
||||
} else {
|
||||
int new_idx = (int)newprog.size();
|
||||
@@ -665,13 +665,13 @@ struct ModelFusionQDQ
|
||||
if (conv->inputs.size() == 3) {
|
||||
removed_args.push_back(conv->inputs[2]);
|
||||
if (dq_bias_idx >= 0)
|
||||
newprog[dq_bias_idx] = Ptr<OpData>();
|
||||
newprog[dq_bias_idx] = Ptr<LayerInfo>();
|
||||
}
|
||||
if (usecounts.at(conv_x.idx) == 1) {
|
||||
removed_args.push_back(conv_x);
|
||||
newprog[dq_x_idx] = Ptr<OpData>();
|
||||
newprog[dq_x_idx] = Ptr<LayerInfo>();
|
||||
}
|
||||
newprog[dq_w_idx] = Ptr<OpData>();
|
||||
newprog[dq_w_idx] = Ptr<LayerInfo>();
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -770,8 +770,8 @@ struct ModelFusionQDQ
|
||||
removed_args.push_back(q_data_in);
|
||||
removed_args.push_back(mm_x);
|
||||
removed_args.push_back(mm_w);
|
||||
newprog[dq_x_idx] = Ptr<OpData>();
|
||||
newprog[dq_w_idx] = Ptr<OpData>();
|
||||
newprog[dq_x_idx] = Ptr<LayerInfo>();
|
||||
newprog[dq_w_idx] = Ptr<LayerInfo>();
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -896,9 +896,9 @@ struct ModelFusionQDQ
|
||||
removed_args.push_back(add_bias->inputs[mm_inp_k]); // matmul out
|
||||
removed_args.push_back(mm_x);
|
||||
removed_args.push_back(mm_w);
|
||||
newprog[mm2_idx] = Ptr<OpData>();
|
||||
newprog[dq_x_idx] = Ptr<OpData>();
|
||||
newprog[dq_w_idx] = Ptr<OpData>();
|
||||
newprog[mm2_idx] = Ptr<LayerInfo>();
|
||||
newprog[dq_x_idx] = Ptr<LayerInfo>();
|
||||
newprog[dq_w_idx] = Ptr<LayerInfo>();
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -1026,8 +1026,8 @@ struct ModelFusionQDQ
|
||||
removed_args.push_back(q_data_in);
|
||||
removed_args.push_back(gemm_a);
|
||||
removed_args.push_back(gemm_b);
|
||||
newprog[dq_a_idx] = Ptr<OpData>();
|
||||
newprog[dq_b_idx] = Ptr<OpData>();
|
||||
newprog[dq_a_idx] = Ptr<LayerInfo>();
|
||||
newprog[dq_b_idx] = Ptr<LayerInfo>();
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -1089,7 +1089,7 @@ struct ModelFusionQDQ
|
||||
fused_inputs.assign(1, dq->inputs[0]);
|
||||
removed_args.push_back(q_data_in);
|
||||
removed_args.push_back(pool_in);
|
||||
newprog[dq_idx] = Ptr<OpData>();
|
||||
newprog[dq_idx] = Ptr<LayerInfo>();
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -1142,7 +1142,7 @@ struct ModelFusionQDQ
|
||||
fused_inputs.push_back(shuffle_layer->inputs[si]);
|
||||
removed_args.push_back(ql_data);
|
||||
removed_args.push_back(shuffle_inp);
|
||||
newprog[dq_idx] = Ptr<OpData>();
|
||||
newprog[dq_idx] = Ptr<LayerInfo>();
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -1304,7 +1304,7 @@ struct ModelFusionQDQ
|
||||
|
||||
conv->float_input = true;
|
||||
conv->inputs[0] = ql_data;
|
||||
newprog[ql_idx] = Ptr<OpData>();
|
||||
newprog[ql_idx] = Ptr<LayerInfo>();
|
||||
modified = true;
|
||||
}
|
||||
|
||||
@@ -1339,7 +1339,7 @@ struct ModelFusionQDQ
|
||||
if (inp.idx >= 0 && inp.idx < (int)nargs)
|
||||
uc[inp.idx]--;
|
||||
}
|
||||
layer = Ptr<OpData>();
|
||||
layer = Ptr<LayerInfo>();
|
||||
changed = true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -46,7 +46,7 @@ struct ModelFusionReshapeTranspose
|
||||
|
||||
bool fuseGraph(Ptr<Graph>& graph)
|
||||
{
|
||||
const vector<Ptr<OpData>>& prog = graph->prog();
|
||||
const vector<Ptr<LayerInfo>>& prog = graph->prog();
|
||||
size_t nops = prog.size();
|
||||
bool modified = false;
|
||||
|
||||
@@ -72,7 +72,7 @@ struct ModelFusionReshapeTranspose
|
||||
vector<bool> dropped(nops, false);
|
||||
|
||||
for (size_t i = 0; i < nops; i++) {
|
||||
const Ptr<OpData>& layer = prog[i];
|
||||
const Ptr<LayerInfo>& layer = prog[i];
|
||||
if (!layer || dropped[i]) continue;
|
||||
if (layer->inputs.empty() || layer->outputs.empty()) continue;
|
||||
|
||||
@@ -95,7 +95,7 @@ struct ModelFusionReshapeTranspose
|
||||
if (it != producer.end()) {
|
||||
int prod_idx = it->second;
|
||||
if (prod_idx >= 0 && !dropped[prod_idx]) {
|
||||
const Ptr<OpData>& pl = prog[prod_idx];
|
||||
const Ptr<LayerInfo>& pl = prog[prod_idx];
|
||||
TransposeLayer* prevTr = dynamic_cast<TransposeLayer*>(pl.get());
|
||||
Arg prevOut = layer->inputs[0];
|
||||
bool single_consumer = usecounts[prevOut.idx] == 1
|
||||
@@ -135,7 +135,7 @@ struct ModelFusionReshapeTranspose
|
||||
if (it != producer.end()) {
|
||||
int prod_idx = it->second;
|
||||
if (prod_idx >= 0 && !dropped[prod_idx]) {
|
||||
const Ptr<OpData>& pl = prog[prod_idx];
|
||||
const Ptr<LayerInfo>& pl = prog[prod_idx];
|
||||
Reshape2Layer* prevRs = dynamic_cast<Reshape2Layer*>(pl.get());
|
||||
Arg prevOut = layer->inputs[0];
|
||||
bool single_consumer = usecounts[prevOut.idx] == 1
|
||||
@@ -154,7 +154,7 @@ struct ModelFusionReshapeTranspose
|
||||
}
|
||||
|
||||
if (modified) {
|
||||
vector<Ptr<OpData>> newprog;
|
||||
vector<Ptr<LayerInfo>> newprog;
|
||||
newprog.reserve(nops);
|
||||
for (size_t i = 0; i < nops; i++) {
|
||||
if (!dropped[i] && prog[i])
|
||||
@@ -166,7 +166,7 @@ struct ModelFusionReshapeTranspose
|
||||
return modified;
|
||||
}
|
||||
|
||||
void redirectConsumers(const vector<Ptr<OpData>>& prog,
|
||||
void redirectConsumers(const vector<Ptr<LayerInfo>>& prog,
|
||||
const vector<bool>& dropped,
|
||||
size_t start_idx, Arg from, Arg to)
|
||||
{
|
||||
|
||||
@@ -40,7 +40,7 @@ struct ModelFusionScaleSoftmax
|
||||
|
||||
bool fuseGraph(Ptr<Graph>& graph)
|
||||
{
|
||||
const vector<Ptr<OpData>>& prog = graph->prog();
|
||||
const vector<Ptr<LayerInfo>>& prog = graph->prog();
|
||||
size_t nops = prog.size();
|
||||
bool modified = false;
|
||||
|
||||
@@ -70,7 +70,7 @@ struct ModelFusionScaleSoftmax
|
||||
vector<bool> dropped(nops, false);
|
||||
|
||||
for (size_t i = 0; i < nops; i++) {
|
||||
const Ptr<OpData>& layer = prog[i];
|
||||
const Ptr<LayerInfo>& layer = prog[i];
|
||||
if (!layer || dropped[i]) continue;
|
||||
|
||||
SoftmaxLayer* sm = dynamic_cast<SoftmaxLayer*>(layer.get());
|
||||
@@ -83,7 +83,7 @@ struct ModelFusionScaleSoftmax
|
||||
int prod_idx = it->second;
|
||||
if (prod_idx < 0 || dropped[prod_idx]) continue;
|
||||
|
||||
const Ptr<OpData>& pl = prog[prod_idx];
|
||||
const Ptr<LayerInfo>& pl = prog[prod_idx];
|
||||
NaryEltwiseLayer* elt = dynamic_cast<NaryEltwiseLayer*>(pl.get());
|
||||
if (!elt) continue;
|
||||
const auto op = elt->op;
|
||||
@@ -123,7 +123,7 @@ struct ModelFusionScaleSoftmax
|
||||
}
|
||||
|
||||
if (modified) {
|
||||
vector<Ptr<OpData>> newprog;
|
||||
vector<Ptr<LayerInfo>> newprog;
|
||||
newprog.reserve(nops);
|
||||
for (size_t i = 0; i < nops; i++) {
|
||||
if (!dropped[i] && prog[i])
|
||||
|
||||
@@ -35,7 +35,7 @@ using std::string;
|
||||
|
||||
namespace {
|
||||
|
||||
static bool readGemmWeight(const Ptr<OpData>& l, bool trans_b, Mat& W_out)
|
||||
static bool readGemmWeight(const Ptr<LayerInfo>& l, bool trans_b, Mat& W_out)
|
||||
{
|
||||
if (l->blobs.empty()) return false;
|
||||
const Mat& W = l->blobs[0];
|
||||
@@ -48,7 +48,7 @@ static bool readGemmWeight(const Ptr<OpData>& l, bool trans_b, Mat& W_out)
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool readGemmBias(const Ptr<OpData>& l, Mat& b_out)
|
||||
static bool readGemmBias(const Ptr<LayerInfo>& l, Mat& b_out)
|
||||
{
|
||||
if (l->blobs.size() < 2) { b_out.release(); return true; }
|
||||
const Mat& b = l->blobs[1];
|
||||
@@ -76,10 +76,10 @@ struct ModelFusionSharedGemm
|
||||
bool flatten_a = true;
|
||||
};
|
||||
|
||||
bool inspectGemm(const vector<Ptr<OpData>>& prog, int idx, GemmInfo& info) const
|
||||
bool inspectGemm(const vector<Ptr<LayerInfo>>& prog, int idx, GemmInfo& info) const
|
||||
{
|
||||
if (idx < 0 || idx >= (int)prog.size() || !prog[idx]) return false;
|
||||
const Ptr<OpData>& l = prog[idx];
|
||||
const Ptr<LayerInfo>& l = prog[idx];
|
||||
GemmLayer* g = dynamic_cast<GemmLayer*>(l.get());
|
||||
if (!g) return false;
|
||||
|
||||
@@ -125,7 +125,7 @@ struct ModelFusionSharedGemm
|
||||
|
||||
bool fuseGraph(Ptr<Graph>& graph)
|
||||
{
|
||||
const vector<Ptr<OpData>>& prog = graph->prog();
|
||||
const vector<Ptr<LayerInfo>>& prog = graph->prog();
|
||||
size_t nops = prog.size();
|
||||
|
||||
struct Key { int input_idx; int trans_b; int K; };
|
||||
@@ -145,7 +145,7 @@ struct ModelFusionSharedGemm
|
||||
|
||||
bool modified = false;
|
||||
std::set<int> removed_ops;
|
||||
vector<std::pair<int, vector<Ptr<OpData>>>> insertions; // (insert_pos, fused-and-slice layers)
|
||||
vector<std::pair<int, vector<Ptr<LayerInfo>>>> insertions; // (insert_pos, fused-and-slice layers)
|
||||
|
||||
for (auto& group : groups) {
|
||||
auto infos = group.second;
|
||||
@@ -226,7 +226,7 @@ struct ModelFusionSharedGemm
|
||||
fp.blobs.push_back(W_concat);
|
||||
if (all_have_bias) fp.blobs.push_back(b_concat);
|
||||
|
||||
Ptr<OpData> fused = LayerFactory::createLayerInstance("Gemm", fp);
|
||||
Ptr<LayerInfo> fused = LayerFactory::createLayerInstance("Gemm", fp);
|
||||
if (!fused) continue;
|
||||
|
||||
string fused_out_name = fp.name + "_out";
|
||||
@@ -235,7 +235,7 @@ struct ModelFusionSharedGemm
|
||||
fused->outputs = { fused_out_arg };
|
||||
fused->netimpl = netimpl;
|
||||
|
||||
vector<Ptr<OpData>> slices;
|
||||
vector<Ptr<LayerInfo>> slices;
|
||||
int col_cursor = 0;
|
||||
for (size_t s = 0; s < infos.size(); s++) {
|
||||
int N_s = infos[s].N;
|
||||
@@ -257,7 +257,7 @@ struct ModelFusionSharedGemm
|
||||
Arg axes_arg = netimpl->newConstArg(sp.name + "_axes", axes);
|
||||
Arg steps_arg = netimpl->newConstArg(sp.name + "_steps", steps);
|
||||
|
||||
Ptr<OpData> slice = LayerFactory::createLayerInstance("Slice2", sp);
|
||||
Ptr<LayerInfo> slice = LayerFactory::createLayerInstance("Slice2", sp);
|
||||
if (!slice) { uniform = false; break; }
|
||||
slice->inputs = { fused_out_arg, starts_arg, ends_arg, axes_arg, steps_arg };
|
||||
slice->outputs = prog[infos[s].layer_idx]->outputs;
|
||||
@@ -267,7 +267,7 @@ struct ModelFusionSharedGemm
|
||||
if (!uniform) continue;
|
||||
|
||||
for (auto& info : infos) removed_ops.insert(info.layer_idx);
|
||||
vector<Ptr<OpData>> bundle;
|
||||
vector<Ptr<LayerInfo>> bundle;
|
||||
bundle.push_back(fused);
|
||||
for (auto& s : slices) bundle.push_back(s);
|
||||
insertions.emplace_back(insert_pos, std::move(bundle));
|
||||
@@ -279,7 +279,7 @@ struct ModelFusionSharedGemm
|
||||
std::sort(insertions.begin(), insertions.end(),
|
||||
[](auto& a, auto& b) { return a.first < b.first; });
|
||||
|
||||
vector<Ptr<OpData>> newprog;
|
||||
vector<Ptr<LayerInfo>> newprog;
|
||||
size_t ins_idx = 0;
|
||||
for (size_t i = 0; i < nops; i++) {
|
||||
while (ins_idx < insertions.size() &&
|
||||
|
||||
@@ -38,7 +38,7 @@ struct ModelFusionTransposeMatMul
|
||||
|
||||
bool fuseGraph(Ptr<Graph>& graph)
|
||||
{
|
||||
const vector<Ptr<OpData>>& prog = graph->prog();
|
||||
const vector<Ptr<LayerInfo>>& prog = graph->prog();
|
||||
size_t nops = prog.size();
|
||||
bool modified = false;
|
||||
|
||||
@@ -68,7 +68,7 @@ struct ModelFusionTransposeMatMul
|
||||
vector<bool> dropped(nops, false);
|
||||
|
||||
for (size_t i = 0; i < nops; i++) {
|
||||
const Ptr<OpData>& layer = prog[i];
|
||||
const Ptr<LayerInfo>& layer = prog[i];
|
||||
if (!layer || dropped[i]) continue;
|
||||
|
||||
MatMulLayer* mm = dynamic_cast<MatMulLayer*>(layer.get());
|
||||
@@ -83,7 +83,7 @@ struct ModelFusionTransposeMatMul
|
||||
int prod_idx = it->second;
|
||||
if (prod_idx < 0 || dropped[prod_idx]) continue;
|
||||
|
||||
const Ptr<OpData>& pl = prog[prod_idx];
|
||||
const Ptr<LayerInfo>& pl = prog[prod_idx];
|
||||
TransposeLayer* tr = dynamic_cast<TransposeLayer*>(pl.get());
|
||||
if (!tr || pl->outputs.size() != 1) continue;
|
||||
if (!isLastTwoSwap(tr->perm)) continue;
|
||||
@@ -103,7 +103,7 @@ struct ModelFusionTransposeMatMul
|
||||
}
|
||||
|
||||
if (modified) {
|
||||
vector<Ptr<OpData>> newprog;
|
||||
vector<Ptr<LayerInfo>> newprog;
|
||||
newprog.reserve(nops);
|
||||
for (size_t i = 0; i < nops; i++) {
|
||||
if (!dropped[i] && prog[i])
|
||||
|
||||
+20
-20
@@ -10,11 +10,11 @@ namespace dnn {
|
||||
CV__DNN_INLINE_NS_BEGIN
|
||||
|
||||
|
||||
OpData::OpData() {
|
||||
LayerInfo::LayerInfo() {
|
||||
netimpl = nullptr;
|
||||
}
|
||||
|
||||
OpData::OpData(const LayerParams& params)
|
||||
LayerInfo::LayerInfo(const LayerParams& params)
|
||||
: blobs(params.blobs)
|
||||
, name(params.name)
|
||||
, type(params.type)
|
||||
@@ -22,21 +22,21 @@ OpData::OpData(const LayerParams& params)
|
||||
netimpl = nullptr;
|
||||
}
|
||||
|
||||
OpData::~OpData() {}
|
||||
LayerInfo::~LayerInfo() {}
|
||||
|
||||
void OpData::setParamsFrom(const LayerParams& params)
|
||||
void LayerInfo::setParamsFrom(const LayerParams& params)
|
||||
{
|
||||
blobs = params.blobs;
|
||||
name = params.name;
|
||||
type = params.type;
|
||||
}
|
||||
|
||||
int OpData::inputNameToIndex(String)
|
||||
int LayerInfo::inputNameToIndex(String)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
int OpData::outputNameToIndex(const String&)
|
||||
int LayerInfo::outputNameToIndex(const String&)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
@@ -47,7 +47,7 @@ Layer::Layer() {
|
||||
}
|
||||
|
||||
Layer::Layer(const LayerParams& params)
|
||||
: OpData(params)
|
||||
: LayerInfo(params)
|
||||
{
|
||||
preferableTarget = DNN_TARGET_CPU;
|
||||
}
|
||||
@@ -113,13 +113,13 @@ void Layer::forwardCUDA(const std::vector<Ptr<BackendWrapper> >&,
|
||||
CV_Error(Error::StsNotImplemented, "CUDA forward of " + type + " layers is not defined.");
|
||||
}
|
||||
|
||||
void OpData::getScaleShift(Mat& scale, Mat& shift) const
|
||||
void LayerInfo::getScaleShift(Mat& scale, Mat& shift) const
|
||||
{
|
||||
scale = Mat();
|
||||
shift = Mat();
|
||||
}
|
||||
|
||||
void OpData::getScaleZeropoint(float& scale, int& zeropoint) const
|
||||
void LayerInfo::getScaleZeropoint(float& scale, int& zeropoint) const
|
||||
{
|
||||
scale = 1.f;
|
||||
zeropoint = 0;
|
||||
@@ -266,7 +266,7 @@ void Layer::run(const std::vector<Mat>& inputs, std::vector<Mat>& outputs, std::
|
||||
|
||||
Layer::~Layer() {}
|
||||
|
||||
bool OpData::getMemoryShapes(const std::vector<MatShape>& inputs,
|
||||
bool LayerInfo::getMemoryShapes(const std::vector<MatShape>& inputs,
|
||||
const int requiredOutputs,
|
||||
std::vector<MatShape>& outputs,
|
||||
std::vector<MatShape>& internals) const
|
||||
@@ -276,7 +276,7 @@ bool OpData::getMemoryShapes(const std::vector<MatShape>& inputs,
|
||||
return false;
|
||||
}
|
||||
|
||||
void OpData::getTypes(const std::vector<MatType>&inputs,
|
||||
void LayerInfo::getTypes(const std::vector<MatType>&inputs,
|
||||
const int requiredOutputs,
|
||||
const int requiredInternals,
|
||||
std::vector<MatType>&outputs,
|
||||
@@ -290,7 +290,7 @@ void OpData::getTypes(const std::vector<MatType>&inputs,
|
||||
internals.assign(requiredInternals, inputs[0]);
|
||||
}
|
||||
|
||||
int OpData::getLayouts(const std::vector<DataLayout>& actualInputs,
|
||||
int LayerInfo::getLayouts(const std::vector<DataLayout>& actualInputs,
|
||||
std::vector<DataLayout>& desiredInputs,
|
||||
const int requiredOutputs,
|
||||
std::vector<DataLayout>& outputs) const
|
||||
@@ -300,43 +300,43 @@ int OpData::getLayouts(const std::vector<DataLayout>& actualInputs,
|
||||
return 0;
|
||||
}
|
||||
|
||||
int64 OpData::getFLOPS(const std::vector<MatShape>&,
|
||||
int64 LayerInfo::getFLOPS(const std::vector<MatShape>&,
|
||||
const std::vector<MatShape>&) const
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
bool OpData::updateMemoryShapes(const std::vector<MatShape>& inputs)
|
||||
bool LayerInfo::updateMemoryShapes(const std::vector<MatShape>& inputs)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
std::vector<Ptr<Graph> >* OpData::subgraphs() const
|
||||
std::vector<Ptr<Graph> >* LayerInfo::subgraphs() const
|
||||
{
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
bool OpData::alwaysSupportInplace() const
|
||||
bool LayerInfo::alwaysSupportInplace() const
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
bool OpData::dynamicOutputShapes() const
|
||||
bool LayerInfo::dynamicOutputShapes() const
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
bool OpData::isDataShuffling() const
|
||||
bool LayerInfo::isDataShuffling() const
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
std::ostream& OpData::dumpAttrs(std::ostream& strm, int) const
|
||||
std::ostream& LayerInfo::dumpAttrs(std::ostream& strm, int) const
|
||||
{
|
||||
return strm;
|
||||
}
|
||||
|
||||
std::ostream& OpData::dump(std::ostream& strm, int indent, bool comma) const
|
||||
std::ostream& LayerInfo::dump(std::ostream& strm, int indent, bool comma) const
|
||||
{
|
||||
CV_Assert(netimpl);
|
||||
size_t ninputs = inputs.size();
|
||||
|
||||
@@ -128,7 +128,7 @@ void LayerFactory::registerOp(const String& type, OpConstructor constructor)
|
||||
getOpFactoryImpl()[type] = constructor; // last registration wins
|
||||
}
|
||||
|
||||
Ptr<OpData> LayerFactory::createOp(const String& type, const LayerParams& params)
|
||||
Ptr<LayerInfo> LayerFactory::createOp(const String& type, const LayerParams& params)
|
||||
{
|
||||
CV_TRACE_FUNCTION();
|
||||
CV_TRACE_ARG_VALUE(type, "type", type.c_str());
|
||||
@@ -137,7 +137,7 @@ Ptr<OpData> LayerFactory::createOp(const String& type, const LayerParams& params
|
||||
OpFactory_Impl::const_iterator it = impl.find(type);
|
||||
if (it != impl.end())
|
||||
return it->second(params);
|
||||
return Ptr<OpData>(); // NULL: no OpData constructor for this type yet
|
||||
return Ptr<LayerInfo>(); // NULL: no LayerInfo constructor for this type yet
|
||||
}
|
||||
|
||||
void LayerFactory::registerExec(const String& type, int backendId, ExecConstructor constructor)
|
||||
@@ -150,7 +150,7 @@ void LayerFactory::registerExec(const String& type, int backendId, ExecConstruct
|
||||
}
|
||||
|
||||
Ptr<Layer> LayerFactory::createExec(const String& type, int backendId,
|
||||
const Ptr<OpData>& data, void* backendCtx)
|
||||
const Ptr<LayerInfo>& data, void* backendCtx)
|
||||
{
|
||||
CV_TRACE_FUNCTION();
|
||||
CV_TRACE_ARG_VALUE(type, "type", type.c_str());
|
||||
|
||||
@@ -788,7 +788,7 @@ class CUDAConv2Layer : public Layer
|
||||
public:
|
||||
CUDAConv2Layer(const Ptr<Conv2LayerImpl>& conv_, void* ctx_) : conv(conv_), ctx(ctx_) {}
|
||||
|
||||
static Ptr<Layer> create(const Ptr<OpData>& data, void* backendCtx)
|
||||
static Ptr<Layer> create(const Ptr<LayerInfo>& data, void* backendCtx)
|
||||
{
|
||||
Ptr<Conv2LayerImpl> conv = data.dynamicCast<Conv2LayerImpl>();
|
||||
if (!conv || !backendCtx || !conv->cudaSupported())
|
||||
|
||||
@@ -274,7 +274,7 @@ Ptr<Layer> Net::Impl::getLayer(int layerId) const
|
||||
CV_Assert(0 <= layerId && layerId < totalLayers);
|
||||
int graph_ofs = 0;
|
||||
for (const Ptr<Graph>& graph : allgraphs) {
|
||||
const std::vector<Ptr<OpData> >& prog = graph->prog();
|
||||
const std::vector<Ptr<LayerInfo> >& prog = graph->prog();
|
||||
int nops = (int)prog.size();
|
||||
CV_Assert(layerId >= graph_ofs);
|
||||
if (layerId < graph_ofs + nops)
|
||||
@@ -1679,7 +1679,7 @@ void Net::Impl::setParam(const std::string& outputTensorName, int numParam, cons
|
||||
}
|
||||
|
||||
int targetIdx = (int)it->second;
|
||||
const std::vector<Ptr<OpData>>& prog = mainGraph->prog();
|
||||
const std::vector<Ptr<LayerInfo>>& prog = mainGraph->prog();
|
||||
for (const auto& layer : prog) {
|
||||
bool produces = false;
|
||||
for (const Arg& out : layer->outputs)
|
||||
@@ -2385,8 +2385,8 @@ std::vector<String> Net::Impl::getLayerNames() const
|
||||
if (mainGraph) {
|
||||
res.reserve(totalLayers);
|
||||
for (const Ptr<Graph>& graph: allgraphs) {
|
||||
const std::vector<Ptr<OpData> >& prog = graph->prog();
|
||||
for (const Ptr<OpData>& layer: prog)
|
||||
const std::vector<Ptr<LayerInfo> >& prog = graph->prog();
|
||||
for (const Ptr<LayerInfo>& layer: prog)
|
||||
res.push_back(layer->name);
|
||||
}
|
||||
} else {
|
||||
@@ -2416,7 +2416,7 @@ std::vector<int> Net::Impl::getUnconnectedOutLayers() const
|
||||
|
||||
int graph_ofs = 0;
|
||||
for (const auto& graph : allgraphs) {
|
||||
const std::vector<Ptr<OpData>>& prog = graph->prog();
|
||||
const std::vector<Ptr<LayerInfo>>& prog = graph->prog();
|
||||
for (int i = 0; i < (int)prog.size(); i++) {
|
||||
for (const auto& layerOut : prog[i]->outputs) {
|
||||
if (outArgIdxs.count(layerOut.idx)) {
|
||||
@@ -2501,9 +2501,9 @@ int64 Net::Impl::getFLOPSGraph(const Ptr<Graph>& graph,
|
||||
return 0;
|
||||
|
||||
int64 flops = 0;
|
||||
const std::vector<Ptr<OpData>>& prog = graph->prog();
|
||||
const std::vector<Ptr<LayerInfo>>& prog = graph->prog();
|
||||
|
||||
for (const Ptr<OpData>& layer : prog) {
|
||||
for (const Ptr<LayerInfo>& layer : prog) {
|
||||
if (!layer)
|
||||
continue;
|
||||
|
||||
@@ -2595,7 +2595,7 @@ int64 Net::Impl::getFLOPS(
|
||||
for (const Ptr<Graph>& graph : allgraphs) {
|
||||
int progSize = (int)graph->prog().size();
|
||||
if (localIdx < progSize) {
|
||||
const Ptr<OpData>& layer = graph->prog()[localIdx];
|
||||
const Ptr<LayerInfo>& layer = graph->prog()[localIdx];
|
||||
if (!layer)
|
||||
return 0;
|
||||
|
||||
@@ -2694,8 +2694,8 @@ void Net::Impl::collectLayerInfo(std::vector<String>& names, std::vector<String>
|
||||
names.reserve(totalLayers);
|
||||
types.reserve(totalLayers);
|
||||
for (const Ptr<Graph>& graph : allgraphs) {
|
||||
const std::vector<Ptr<OpData>>& prog = graph->prog();
|
||||
for (const Ptr<OpData>& layer : prog) {
|
||||
const std::vector<Ptr<LayerInfo>>& prog = graph->prog();
|
||||
for (const Ptr<LayerInfo>& layer : prog) {
|
||||
names.push_back(layer ? layer->name : "null");
|
||||
types.push_back(layer ? layer->type : "null");
|
||||
}
|
||||
@@ -3009,8 +3009,8 @@ void Net::Impl::getLayerTypes(std::vector<String>& layersTypes) const
|
||||
if (mainGraph) {
|
||||
std::set<std::string> layersTypesSet;
|
||||
for (const Ptr<Graph>& g: allgraphs) {
|
||||
const std::vector<Ptr<OpData> >& prog = g->prog();
|
||||
for (const Ptr<OpData>& layer: prog) {
|
||||
const std::vector<Ptr<LayerInfo> >& prog = g->prog();
|
||||
for (const Ptr<LayerInfo>& layer: prog) {
|
||||
if (!layer)
|
||||
continue;
|
||||
layersTypesSet.insert(layer->type);
|
||||
@@ -3042,8 +3042,8 @@ int Net::Impl::getLayersCount(const String& layerType) const
|
||||
if (mainGraph) {
|
||||
int count = 0;
|
||||
for (const Ptr<Graph>& g: allgraphs) {
|
||||
const std::vector<Ptr<OpData> >& prog = g->prog();
|
||||
for (const Ptr<OpData>& layer: prog) {
|
||||
const std::vector<Ptr<LayerInfo> >& prog = g->prog();
|
||||
for (const Ptr<LayerInfo>& layer: prog) {
|
||||
if (!layer)
|
||||
continue;
|
||||
if (layer->type == layerType)
|
||||
|
||||
@@ -434,7 +434,7 @@ struct Net::Impl : public detail::NetImplBase
|
||||
// if useBufferPool==true, the method uses 'buffers'
|
||||
// for outputs (according to bufidxs)
|
||||
// instead of allocating fresh outputs
|
||||
void allocateLayerOutputs(const Ptr<OpData>& layer,
|
||||
void allocateLayerOutputs(const Ptr<LayerInfo>& layer,
|
||||
const std::vector<int>& inpTypes,
|
||||
const std::vector<MatShape>& inpShapes,
|
||||
std::vector<int>& outTypes,
|
||||
@@ -532,7 +532,7 @@ struct Net::Impl : public detail::NetImplBase
|
||||
|
||||
}; // Net::Impl
|
||||
|
||||
inline Net::Impl* getNetImpl(const OpData* op)
|
||||
inline Net::Impl* getNetImpl(const LayerInfo* op)
|
||||
{
|
||||
return reinterpret_cast<Net::Impl*>(op->netimpl);
|
||||
}
|
||||
|
||||
@@ -316,7 +316,7 @@ public:
|
||||
return g;
|
||||
}*/
|
||||
|
||||
virtual const std::vector<Arg>& append(Ptr<OpData>& op,
|
||||
virtual const std::vector<Arg>& append(Ptr<LayerInfo>& op,
|
||||
const std::vector<std::string>& outnames) override
|
||||
{
|
||||
CV_Assert(op);
|
||||
@@ -335,7 +335,7 @@ public:
|
||||
return op->outputs;
|
||||
}
|
||||
|
||||
virtual Arg append(Ptr<OpData>& op,
|
||||
virtual Arg append(Ptr<LayerInfo>& op,
|
||||
const std::string& outname) override
|
||||
{
|
||||
std::vector<std::string> outnames = {outname};
|
||||
@@ -378,7 +378,7 @@ public:
|
||||
for (size_t i = 0; i < nlayers; i++) {
|
||||
prindent(strm, argindent);
|
||||
strm << "// op #" << i << "\n";
|
||||
const Ptr<OpData>& op = prog_[i];
|
||||
const Ptr<LayerInfo>& op = prog_[i];
|
||||
op->dump(strm, argindent, i+1 < nlayers);
|
||||
}
|
||||
prindent(strm, subindent);
|
||||
@@ -400,13 +400,13 @@ public:
|
||||
netimpl_->checkArgs(outputs);
|
||||
outputs_ = outputs;
|
||||
}
|
||||
virtual const std::vector<Ptr<OpData> >& prog() const override { return prog_; }
|
||||
virtual const std::vector<Ptr<LayerInfo> >& prog() const override { return prog_; }
|
||||
virtual int opBackend(int opidx) const override
|
||||
{
|
||||
return (opidx >= 0 && opidx < (int)execBackend_.size()) ? execBackend_[opidx]
|
||||
: DNN_BACKEND_OPENCV;
|
||||
}
|
||||
virtual void setProg(const std::vector<Ptr<OpData> >& newprog) override
|
||||
virtual void setProg(const std::vector<Ptr<LayerInfo> >& newprog) override
|
||||
{
|
||||
prog_ = newprog;
|
||||
exec_.clear();
|
||||
@@ -419,7 +419,7 @@ public:
|
||||
std::string name_;
|
||||
std::vector<Arg> inputs_;
|
||||
std::vector<Arg> outputs_;
|
||||
std::vector<Ptr<OpData> > prog_;
|
||||
std::vector<Ptr<LayerInfo> > prog_;
|
||||
std::vector<Ptr<Layer> > exec_;
|
||||
std::vector<int> execBackend_;
|
||||
std::vector<std::vector<uchar> > inH2D_;
|
||||
@@ -580,13 +580,13 @@ void Net::Impl::prepareForInference()
|
||||
void Net::Impl::finalizeGraph(const Ptr<Graph>& graph, bool useCUDA)
|
||||
{
|
||||
GraphImpl* g = static_cast<GraphImpl*>(graph.get());
|
||||
const std::vector<Ptr<OpData> >& prog = g->prog_;
|
||||
const std::vector<Ptr<LayerInfo> >& prog = g->prog_;
|
||||
size_t i, nops = prog.size();
|
||||
g->exec_.assign(nops, Ptr<Layer>());
|
||||
g->execBackend_.assign(nops, DNN_BACKEND_OPENCV);
|
||||
|
||||
for (i = 0; i < nops; i++) {
|
||||
const Ptr<OpData>& op = prog[i];
|
||||
const Ptr<LayerInfo>& op = prog[i];
|
||||
if (!op)
|
||||
continue;
|
||||
|
||||
@@ -670,7 +670,7 @@ void Net::Impl::finalize()
|
||||
}
|
||||
|
||||
void Net::Impl::allocateLayerOutputs(
|
||||
const Ptr<OpData>& layer,
|
||||
const Ptr<LayerInfo>& layer,
|
||||
const std::vector<int>& inpTypes,
|
||||
const std::vector<MatShape>& inpShapes,
|
||||
std::vector<int>& outTypes,
|
||||
@@ -1324,7 +1324,7 @@ void Net::Impl::forwardGraph(Ptr<Graph>& graph, InputArrayOfArrays inputs_,
|
||||
}
|
||||
std::ostream& strm_ = dump_strm ? *dump_strm : std::cout;
|
||||
GraphImpl* gimpl = static_cast<GraphImpl*>(graph.get());
|
||||
const std::vector<Ptr<OpData> >& prog = graph->prog();
|
||||
const std::vector<Ptr<LayerInfo> >& prog = graph->prog();
|
||||
size_t i, nops = prog.size();
|
||||
const std::vector<Arg>& gr_inputs = graph->inputs();
|
||||
const std::vector<Arg>& gr_outputs = graph->outputs();
|
||||
@@ -1357,7 +1357,7 @@ void Net::Impl::forwardGraph(Ptr<Graph>& graph, InputArrayOfArrays inputs_,
|
||||
}
|
||||
|
||||
for (size_t opidx = 0; opidx < nops; opidx++) {
|
||||
const Ptr<OpData>& op = prog.at(opidx);
|
||||
const Ptr<LayerInfo>& op = prog.at(opidx);
|
||||
if (!op) // in theory we shouldn't have any 'nops' at this stage, but just in case we skip them.
|
||||
continue;
|
||||
Ptr<Layer> layer = (opidx < gimpl->exec_.size()) ? gimpl->exec_[opidx] : Ptr<Layer>();
|
||||
@@ -1720,8 +1720,8 @@ void Net::Impl::updateUseCounts(const Ptr<Graph>& graph, std::vector<int>& useco
|
||||
CV_Assert(output.idx < (int)usecounts.size());
|
||||
usecounts[output.idx]++;
|
||||
}
|
||||
const std::vector<Ptr<OpData> >& prog = graph->prog();
|
||||
for (const Ptr<OpData>& layer: prog) {
|
||||
const std::vector<Ptr<LayerInfo> >& prog = graph->prog();
|
||||
for (const Ptr<LayerInfo>& layer: prog) {
|
||||
const std::vector<Arg>& inputs = layer->inputs;
|
||||
for (const Arg& input: inputs) {
|
||||
CV_Assert(input.idx < (int)usecounts.size());
|
||||
@@ -1752,14 +1752,14 @@ int Net::Impl::updateGraphOfs(const Ptr<Graph>& graph, int currofs, bool ismain)
|
||||
allgraphs.clear();
|
||||
layerNameToId.clear();
|
||||
}
|
||||
const std::vector<Ptr<OpData> >& prog = graph->prog();
|
||||
const std::vector<Ptr<LayerInfo> >& prog = graph->prog();
|
||||
size_t i, nops = prog.size();
|
||||
int subgraph_ofs = currofs + (int)nops;
|
||||
std::string name = graph->name();
|
||||
graphofs.insert(std::make_pair(name, currofs));
|
||||
allgraphs.push_back(graph);
|
||||
for (i = 0; i < nops; i++) {
|
||||
const Ptr<OpData>& layer = prog[i];
|
||||
const Ptr<LayerInfo>& layer = prog[i];
|
||||
layerNameToId.insert(std::make_pair(layer->name, currofs + (int)i));
|
||||
const std::vector<Ptr<Graph> >* subgraphs = layer->subgraphs();
|
||||
if (subgraphs) {
|
||||
@@ -1906,12 +1906,12 @@ bool Net::Impl::tryInferGraphShapes(const Ptr<Graph>& graph,
|
||||
if (!graph)
|
||||
return true;
|
||||
|
||||
const std::vector<Ptr<OpData> >& prog = graph->prog();
|
||||
const std::vector<Ptr<LayerInfo> >& prog = graph->prog();
|
||||
|
||||
std::vector<MatShape> inpShapes, outShapes, tempShapes;
|
||||
std::vector<int> inpTypes, outTypes, tempTypes;
|
||||
|
||||
for (const Ptr<OpData>& layer: prog) {
|
||||
for (const Ptr<LayerInfo>& layer: prog) {
|
||||
if (!layer)
|
||||
continue;
|
||||
|
||||
|
||||
@@ -167,7 +167,7 @@ protected:
|
||||
std::string onnxBasePath;
|
||||
Ptr<Graph> curr_graph;
|
||||
opencv_onnx::GraphProto* curr_graph_proto;
|
||||
std::vector<Ptr<OpData> > curr_prog;
|
||||
std::vector<Ptr<LayerInfo> > curr_prog;
|
||||
std::vector<Arg> node_inputs, node_outputs;
|
||||
|
||||
std::string framework_name;
|
||||
@@ -892,7 +892,7 @@ Ptr<Graph> ONNXImporter2::parseGraph(opencv_onnx::GraphProto* graph_proto, bool
|
||||
|
||||
opencv_onnx::GraphProto* saved_graph_proto = curr_graph_proto;
|
||||
Ptr<Graph> saved_graph = curr_graph;
|
||||
std::vector<Ptr<OpData> > saved_prog;
|
||||
std::vector<Ptr<LayerInfo> > saved_prog;
|
||||
|
||||
curr_graph_proto = graph_proto;
|
||||
std::vector<Arg> inputs, outputs;
|
||||
@@ -1724,7 +1724,7 @@ void ONNXImporter2::parseLoop(LayerParams& layerParams,
|
||||
|
||||
CV_Assert(!subgraphs[0].empty());
|
||||
|
||||
Ptr<OpData>& loopLayer = curr_prog.back();
|
||||
Ptr<LayerInfo>& loopLayer = curr_prog.back();
|
||||
*loopLayer->subgraphs() = subgraphs;
|
||||
}
|
||||
|
||||
@@ -1789,7 +1789,7 @@ void ONNXImporter2::parseIf(LayerParams& layerParams,
|
||||
|
||||
CV_Assert_N(!thenelse[0].empty(), !thenelse[1].empty());
|
||||
|
||||
Ptr<OpData>& ifLayer = curr_prog.back();
|
||||
Ptr<LayerInfo>& ifLayer = curr_prog.back();
|
||||
*ifLayer->subgraphs() = thenelse;
|
||||
}
|
||||
|
||||
|
||||
@@ -18,7 +18,7 @@ class CUDALegacyExec : public Layer
|
||||
public:
|
||||
CUDALegacyExec(const Ptr<Layer>& impl_, void* ctx_) : impl(impl_), ctx(ctx_) {}
|
||||
|
||||
static Ptr<Layer> create(const Ptr<OpData>& data, void* backendCtx)
|
||||
static Ptr<Layer> create(const Ptr<LayerInfo>& data, void* backendCtx)
|
||||
{
|
||||
Ptr<Layer> impl = data.dynamicCast<Layer>();
|
||||
if (!impl || !backendCtx || !impl->supportBackend(DNN_BACKEND_CUDA))
|
||||
|
||||
@@ -576,7 +576,7 @@ protected:
|
||||
std::map<String, int> layer_id;
|
||||
|
||||
bool newEngine;
|
||||
std::vector<Ptr<OpData>> curProg;
|
||||
std::vector<Ptr<LayerInfo>> curProg;
|
||||
std::vector<std::vector<std::string>> layersOutputs;
|
||||
std::vector<Arg> modelInputs;
|
||||
std::unordered_map<std::string, MatShape> tensorsShape;
|
||||
|
||||
@@ -34,7 +34,7 @@ private:
|
||||
const flatbuffers::Vector<flatbuffers::Offset<opencv_tflite::Tensor> >* modelTensors;
|
||||
std::map<int, Mat> allTensors;
|
||||
Net& dstNet;
|
||||
std::vector<Ptr<OpData>> curProg;
|
||||
std::vector<Ptr<LayerInfo>> curProg;
|
||||
|
||||
// This is a vector of pairs (layerId, outputId) where we iterate over
|
||||
// indices from TFLite notation and get created OpenCV layers.
|
||||
|
||||
@@ -297,9 +297,8 @@ class ClassInfo(GeneralInfo):
|
||||
base_class = re.sub(r"^cv::", "", base_class)
|
||||
base_class = base_class.replace('::', '.')
|
||||
base_info = ClassInfo(('class {}'.format(base_class), '', [], [], None, None), [self.namespace])
|
||||
# Use resolved base name; don't strip this class's own name.
|
||||
base_type_name = base_info.name
|
||||
if not base_type_name in type_dict:
|
||||
base_type_name = re.sub(r"^.*:", "", decl[1].split(",")[0]).strip().replace(self.jname, "")
|
||||
self.base = base_type_name
|
||||
self.addImports(self.base)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user