This commit applies refactored ConstantInitialzers.
- Introduce ConstantInitializer into each backend.
- Replace legacy ConstantInitializer with refactored ConstantInitializer.
Signed-off-by: jiseob.jang <jiseob.jang@samsung.com>
#include <model/Operands.h>
#include "Config.h"
+#include "ConstantInitializer.h"
#include "KernelGenerator.h"
#include "ShapeFixer.h"
#include "MemoryManager.h"
Backend(const neurun::model::Operands &operand_ctx,
std::shared_ptr<TensorBuilder> tensor_builder =
std::make_shared<TensorBuilder>(createMemoryManager()))
- : ::neurun::backend::Backend{std::make_shared<Config>(),
- std::make_shared<KernelGenerator>(operand_ctx, tensor_builder),
- std::make_shared<ShapeFixer>(operand_ctx, tensor_builder)}
+ : ::neurun::backend::Backend{
+ std::make_shared<Config>(),
+ std::make_shared<ConstantInitializer>(operand_ctx, tensor_builder),
+ std::make_shared<KernelGenerator>(operand_ctx, tensor_builder),
+ std::make_shared<ShapeFixer>(operand_ctx, tensor_builder)}
{
// DO NOTHING
}
auto tensor_obj = _tensor_builder->wrapTensor(ind);
fn(model_obj, *tensor_obj);
}
+ _init_map.clear();
}
void ConstantInitializer::visit(const model::operation::AbsNode &node)
#include <model/Operands.h>
#include "Config.h"
+#include "ConstantInitializer.h"
#include "KernelGenerator.h"
#include "ShapeFixer.h"
#include "MemoryManager.h"
Backend(const neurun::model::Operands &operand_ctx,
std::shared_ptr<TensorBuilder> tensor_builder =
std::make_shared<TensorBuilder>(createMemoryManager()))
- : ::neurun::backend::Backend{std::make_shared<Config>(),
- std::make_shared<KernelGenerator>(operand_ctx, tensor_builder),
- std::make_shared<ShapeFixer>(operand_ctx, tensor_builder)}
+ : ::neurun::backend::Backend{
+ std::make_shared<Config>(),
+ std::make_shared<ConstantInitializer>(operand_ctx, tensor_builder),
+ std::make_shared<KernelGenerator>(operand_ctx, tensor_builder),
+ std::make_shared<ShapeFixer>(operand_ctx, tensor_builder)}
{
// DO NOTHING
}
auto tensor_obj = _tensor_builder->wrapTensor(ind);
fn(model_obj, *tensor_obj);
}
+ _init_map.clear();
}
void ConstantInitializer::visit(const model::operation::AddNode &node)
#include <model/Operands.h>
#include "Config.h"
+#include "ConstantInitializer.h"
#include "KernelGenerator.h"
#include "ShapeFixer.h"
public:
Backend(const neurun::model::Operands &operand_ctx,
std::shared_ptr<TensorBuilder> tensor_builder = std::make_shared<TensorBuilder>())
- : ::neurun::backend::Backend{std::make_shared<Config>(),
- std::make_shared<KernelGenerator>(operand_ctx, tensor_builder),
- std::make_shared<ShapeFixer>(operand_ctx, tensor_builder)}
+ : ::neurun::backend::Backend{
+ std::make_shared<Config>(),
+ std::make_shared<ConstantInitializer>(operand_ctx, tensor_builder),
+ std::make_shared<KernelGenerator>(operand_ctx, tensor_builder),
+ std::make_shared<ShapeFixer>(operand_ctx, tensor_builder)}
{
// DO NOTHING
}
auto tensor_obj = _tensor_builder->wrapTensor(ind);
fn(model_obj, *tensor_obj);
}
+ _init_map.clear();
}
void ConstantInitializer::visit(const model::operation::AddNode &node)
{
struct IConfig;
+class IConstantInitializer;
class IKernelGenerator;
class IShapeFixer;
struct ITensorBuilder;
{
public:
Backend(const std::shared_ptr<neurun::backend::IConfig> &backend_config,
+ const std::shared_ptr<neurun::backend::IConstantInitializer> &constant_initializer,
const std::shared_ptr<neurun::backend::IKernelGenerator> &kernel_gen,
const std::shared_ptr<neurun::backend::IShapeFixer> &shape_fixer);
- Backend(void) : _config(nullptr), _kernel_gen(nullptr), _shape_fixer(nullptr)
+ Backend(void)
+ : _config(nullptr), _constant_initializer{nullptr}, _kernel_gen(nullptr),
+ _shape_fixer(nullptr)
{
// DO NOTHING
}
public:
const std::shared_ptr<neurun::backend::IConfig> config() const;
+ const std::shared_ptr<neurun::backend::IConstantInitializer> constant_initializer() const;
const std::shared_ptr<neurun::backend::IKernelGenerator> kernel_gen() const;
const std::shared_ptr<neurun::backend::IShapeFixer> shape_fixer() const;
const std::shared_ptr<neurun::backend::ITensorBuilder> tensor_builder() const;
private:
std::shared_ptr<neurun::backend::IConfig> _config;
+ std::shared_ptr<neurun::backend::IConstantInitializer> _constant_initializer;
std::shared_ptr<neurun::backend::IKernelGenerator> _kernel_gen;
std::shared_ptr<neurun::backend::IShapeFixer> _shape_fixer;
};
public:
using Initializer = std::function<void(const model::Operand &, backend::operand::IObject &)>;
- void generate(const model::Subgraph &subg)
- {
- _init_map.clear();
- subg.accept(*this);
- }
+ void generate(const model::Subgraph &subg) { subg.accept(*this); }
protected:
#define OP(InternalName, IsNnApi) \
{
Backend::Backend(const std::shared_ptr<neurun::backend::IConfig> &backend_config,
+ const std::shared_ptr<neurun::backend::IConstantInitializer> &constant_initializer,
const std::shared_ptr<neurun::backend::IKernelGenerator> &kernel_gen,
const std::shared_ptr<neurun::backend::IShapeFixer> &shape_fixer)
- : _config(backend_config), _kernel_gen(kernel_gen), _shape_fixer(shape_fixer)
+ : _config(backend_config), _constant_initializer{constant_initializer}, _kernel_gen(kernel_gen),
+ _shape_fixer(shape_fixer)
{
backend_config->initialize();
}
const std::shared_ptr<neurun::backend::IConfig> Backend::config() const { return _config; }
+const std::shared_ptr<neurun::backend::IConstantInitializer> Backend::constant_initializer() const
+{
+ return _constant_initializer;
+}
+
const std::shared_ptr<neurun::backend::IKernelGenerator> Backend::kernel_gen() const
{
return _kernel_gen;
#include "OperationValidator.h"
#include "SubTensorAnalyzer.h"
#include "PlanBuilder.h"
+#include "backend/IConstantInitializer.h"
#include "backend/IKernelGenerator.h"
#include "backend/IShapeFixer.h"
-#include "ConstantInitializer.h"
#include "cpp14/memory.h"
namespace neurun
// TODO Add optimization passes
plan_builder.finalize(linear.get(), tensor_builders);
- ConstantInitializer{graph, *operand_context, *linear->getLowerInfo()}();
+ // Initialize constant tensors
+ for (const auto backend : graph.backend_resolver()->getAllBackends())
+ {
+ backend->constant_initializer()->run();
+ }
// Prepare each MemoryManager on each backend
auto mem_mgrs = nnfw::cpp14::make_unique<backend::MemoryManagerSet>();
graph.subgraphs().iterate(
[&](const model::SubgraphIndex &subg_index, const model::Subgraph &subg) {
auto backend = graph.getLowerInfo(subg_index)->backend();
+ auto constant_initializer = backend->constant_initializer();
+ constant_initializer->generate(subg);
// TODO This approach is temporal. See declaration of `setNextIndex`.
execution_builder->setNextIndex(subg_index);
auto kernel_gen = backend->kernel_gen();
tensor_builder->allocate();
}
- auto lower_info = graph.releaseLowerInfo();
+ // Initialize constant tensors
+ for (const auto backend : graph.backend_resolver()->getAllBackends())
+ {
+ backend->constant_initializer()->run();
+ }
- ConstantInitializer{graph, *operand_context, *lower_info}();
+ auto lower_info = graph.releaseLowerInfo();
// Prepare each MemoryManager on each backend
auto mem_mgrs = nnfw::cpp14::make_unique<backend::MemoryManagerSet>();
#include "backend/operand/IObject.h"
#include "backend/Backend.h"
+#include "backend/IConstantInitializer.h"
#include "backend/IKernelGenerator.h"
#include "linear/Linear.h"
auto execution_builder = nnfw::cpp14::make_unique<ExecutionBuilder>(_functions);
linear->iterate([&](const linear::Element &element) {
auto backend = element.lower_info->backend();
+ auto constant_initializer = backend->constant_initializer();
+ constant_initializer->generate(*element.subgraph);
auto kernel_gen = backend->kernel_gen();
kernel_gen->generate(*element.subgraph, execution_builder.get());
});
#include "backend/ExecTime.h"
#include "backend/IConfig.h"
+#include "backend/IConstantInitializer.h"
#include "backend/IKernelGenerator.h"
#include "backend/IShapeFixer.h"
#include "backend/Backend.h"
bool SupportSubTensorAlloc() override { return false; }
};
+struct MockConstantInitializer : IConstantInitializer
+{
+ void run() {}
+};
+
struct MockKernelGenerator : IKernelGenerator
{
std::shared_ptr<ITensorBuilder> tensor_builder() final { return nullptr; }
* Required because we use pointers to backends instead of string identifiers.
*/
MockBackend()
- : Backend{std::make_shared<MockConfig>(), std::make_shared<MockKernelGenerator>(),
- std::make_shared<MockShapeFixer>()} {};
+ : Backend{std::make_shared<MockConfig>(), std::make_shared<MockConstantInitializer>(),
+ std::make_shared<MockKernelGenerator>(), std::make_shared<MockShapeFixer>()} {};
};
TEST(ExecTime, roundtrip_ok)