1
0
mirror of https://github.com/opencv/opencv.git synced 2026-07-29 23:33:05 +04:00

Added reshape() functionality to CPU backend

This commit is contained in:
Ruslan Garnov
2022-03-01 15:09:05 +03:00
parent a332509e02
commit ecb30409f6
5 changed files with 140 additions and 24 deletions
+33 -10
View File
@@ -2,7 +2,7 @@
// It is subject to the license terms in the LICENSE file found in the top-level directory
// of this distribution and at http://opencv.org/license.html.
//
// Copyright (C) 2018-2021 Intel Corporation
// Copyright (C) 2018-2022 Intel Corporation
#include "precomp.hpp"
@@ -88,7 +88,7 @@ cv::gimpl::GCPUExecutable::GCPUExecutable(const ade::Graph &g,
{
case NodeType::OP:
{
m_script.push_back({nh, GModel::collectOutputMeta(m_gm, nh)});
m_opNodes.push_back(nh);
// If kernel is stateful then prepare storage for its state.
GCPUKernel k = gcm.metadata(nh).get<CPUUnit>().k;
@@ -107,19 +107,12 @@ cv::gimpl::GCPUExecutable::GCPUExecutable(const ade::Graph &g,
auto rc = RcDesc{desc.rc, desc.shape, desc.ctor};
magazine::bindInArg(m_res, rc, m_gm.metadata(nh).get<ConstValue>().arg);
}
//preallocate internal Mats in advance
if (desc.storage == Data::Storage::INTERNAL && desc.shape == GShape::GMAT)
{
const auto mat_desc = util::get<cv::GMatDesc>(desc.meta);
auto& mat = m_res.slot<cv::Mat>()[desc.rc];
createMat(mat_desc, mat);
}
break;
}
default: util::throw_error(std::logic_error("Unsupported NodeType type"));
}
}
makeReshape();
// For each stateful kernel call 'setup' user callback to initialize state.
setupKernelStates();
}
@@ -176,8 +169,38 @@ void cv::gimpl::GCPUExecutable::setupKernelStates()
}
}
void cv::gimpl::GCPUExecutable::makeReshape() {
// Prepare the execution script
m_script.clear();
for (auto &nh : m_opNodes) {
m_script.push_back({nh, GModel::collectOutputMeta(m_gm, nh)});
}
// Preallocate internal mats
for (auto& nh : m_dataNodes) {
const auto& desc = m_gm.metadata(nh).get<Data>();
if (desc.storage == Data::Storage::INTERNAL && desc.shape == GShape::GMAT) {
const auto mat_desc = util::get<cv::GMatDesc>(desc.meta);
auto& mat = m_res.slot<cv::Mat>()[desc.rc];
createMat(mat_desc, mat);
}
}
}
void cv::gimpl::GCPUExecutable::reshape(ade::Graph&, const GCompileArgs& args) {
m_compileArgs = args;
makeReshape();
// Signal to reset stateful kernels` state.
// There can be no handleNewStream() call to set this flag
// if user didn't call GCompiled`s prepareForNewStream()
m_newStreamStarted = true;
}
void cv::gimpl::GCPUExecutable::handleNewStream()
{
// Signal to reset stateful kernels` state.
// No need to call reshape() here since it'll
// be called automatically if input meta was changed
m_newStreamStarted = true;
}
+7 -10
View File
@@ -2,7 +2,7 @@
// It is subject to the license terms in the LICENSE file found in the top-level directory
// of this distribution and at http://opencv.org/license.html.
//
// Copyright (C) 2018-2020 Intel Corporation
// Copyright (C) 2018-2022 Intel Corporation
#ifndef OPENCV_GAPI_GCPUBACKEND_HPP
@@ -33,7 +33,7 @@ class GCPUExecutable final: public GIslandExecutable
{
const ade::Graph &m_g;
GModel::ConstGraph m_gm;
const cv::GCompileArgs m_compileArgs;
cv::GCompileArgs m_compileArgs;
struct OperationInfo
{
@@ -51,6 +51,7 @@ class GCPUExecutable final: public GIslandExecutable
// List of all resources in graph (both internal and external)
std::vector<ade::NodeHandle> m_dataNodes;
std::vector<ade::NodeHandle> m_opNodes;
// Actual data of all resources in graph (both internal and external)
Mag m_res;
@@ -61,19 +62,15 @@ class GCPUExecutable final: public GIslandExecutable
GArg packArg(const GArg &arg);
void setupKernelStates();
void makeReshape();
public:
GCPUExecutable(const ade::Graph &graph,
const cv::GCompileArgs &compileArgs,
const std::vector<ade::NodeHandle> &nodes);
virtual inline bool canReshape() const override { return false; }
virtual inline void reshape(ade::Graph&, const GCompileArgs&) override
{
// FIXME: CPU plugin is in fact reshapeable (as it was initially,
// even before outMeta() has been introduced), so this limitation
// should be dropped.
util::throw_error(std::logic_error("GCPUExecutable::reshape() should never be called"));
}
virtual inline bool canReshape() const override { return true; }
virtual void reshape(ade::Graph&, const GCompileArgs&) override;
virtual void handleNewStream() override;
@@ -2,7 +2,7 @@
// It is subject to the license terms in the LICENSE file found in the top-level directory
// of this distribution and at http://opencv.org/license.html.
//
// Copyright (C) 2018-2020 Intel Corporation
// Copyright (C) 2018-2022 Intel Corporation
#include "precomp.hpp"
@@ -954,7 +954,7 @@ namespace
GFluidModel fg(graph);
for (const auto& node : g.nodes())
{
if (g.metadata(node).get<NodeType>().t == NodeType::DATA)
if (fg.metadata(node).contains<FluidData>())
{
auto& fd = fg.metadata(node).get<FluidData>();
fd.latency = 0;