ONE - On-device Neural Engine
Loading...
Searching...
No Matches
KernelGenerator.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2020 Samsung Electronics Co., Ltd. All Rights Reserved
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "KernelGenerator.h"
18
20
21#include <memory>
22#include <stdexcept>
23
24namespace onert::backend::cpu
25{
26
28 const std::shared_ptr<TensorBuilder> &tensor_builder,
29 const std::shared_ptr<basic::TensorRegistry> &tensor_reg,
30 const std::shared_ptr<ExternalContext> &external_context)
31 : basic::KernelGeneratorBase{graph}, _ctx(graph.operands()), _operations_ctx{graph.operations()},
32 _tensor_builder(tensor_builder), _tensor_reg{tensor_reg}, _external_context(external_context)
33{
34 // DO NOTHING
35}
36
37std::unique_ptr<exec::FunctionSequence> KernelGenerator::generate(ir::OperationIndex ind)
38{
39 auto ret = std::make_unique<exec::FunctionSequence>();
40
41 assert(_tensor_builder->dynamicTensorManager());
42 assert(_tensor_reg);
43
44 // Prepare to handle dynamic tensors later
45 auto dyn_ctx = std::make_shared<exec::FunctionSequence::DynamicTensorCtx>();
46 {
47 dyn_ctx->op = &_operations_ctx.at(ind);
48 dyn_ctx->dynamic_shape_inferer = std::make_shared<exec::DynamicShapeInferer>(_tensor_reg);
49 }
50 ret->dynamic_tensor_ctx(dyn_ctx);
51
52 auto &op = _graph.operations().at(ind);
53 op.accept(*this);
54 assert(_return_fn); // _return_fn must have been generated
55 ret->append(std::move(_return_fn));
56
57 for (auto &&ind : (op.getInputs() | ir::Remove::UNDEFINED) + op.getOutputs())
58 {
59 auto tensor = _tensor_reg->getNativeTensor(ind);
60 if (tensor)
61 {
62 tensor->increase_ref();
63 }
64 }
65 return ret;
66}
67
68// Visitors for each operation is in ops/<InternalName>Layer.cc file
69
70} // namespace onert::backend::cpu
std::unique_ptr< exec::IFunction > _return_fn
KernelGenerator(const ir::Graph &graph, const std::shared_ptr< TensorBuilder > &tensor_builder, const std::shared_ptr< basic::TensorRegistry > &tensor_reg, const std::shared_ptr< ExternalContext > &external_context)
std::unique_ptr< exec::FunctionSequence > generate(ir::OperationIndex op_ind) override
const Operations & operations() const override
Definition Graph.h:105
const Object & at(const Index &index) const
Get the object that is associated with the given index.