ONE - On-device Neural Engine
Loading...
Searching...
No Matches
KernelGenerator.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2025 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
19#include <backend/Backend.h>
20#include <backend/IConfig.h>
21#include <memory>
22#include <util/Utils.h>
23#include <util/logging.h>
25
26#include <stdexcept>
27
29{
30
31std::unique_ptr<exec::FunctionSequence> KernelGenerator::generate(ir::OperationIndex ind)
32{
33 auto ret = std::make_unique<exec::FunctionSequence>();
34
35 assert(_tensor_builder->dynamicTensorManager());
36 assert(_tensor_reg);
37
38 // Prepare to handle dynamic tensors later
39 auto dyn_ctx = std::make_shared<exec::FunctionSequence::DynamicTensorCtx>();
40 {
41 dyn_ctx->op = &_operations_ctx.at(ind);
42 dyn_ctx->dynamic_shape_inferer = std::make_shared<exec::DynamicShapeInferer>(_tensor_reg);
43 }
44 ret->dynamic_tensor_ctx(dyn_ctx);
45
46 auto &op = _graph.operations().at(ind);
47 op.accept(*this);
48 assert(_return_fn); // _return_fn must have been generated
49 ret->append(std::move(_return_fn));
50
51 for (const auto &ind : (op.getInputs() | ir::Remove::UNDEFINED) + op.getOutputs())
52 {
53 auto tensor = _tensor_reg->getNativeTensor(ind);
54 if (tensor)
55 {
56 tensor->increase_ref();
57 }
58 }
59 return ret;
60}
61
63 const std::shared_ptr<TensorBuilder> &tensor_builder,
64 const std::shared_ptr<basic::TensorRegistry> &tensor_reg,
65 const std::shared_ptr<ExternalContext> &external_context)
66 : basic::KernelGeneratorBase{graph}, _ctx(graph.operands()), _operations_ctx{graph.operations()},
67 _tensor_builder(tensor_builder), _tensor_reg{tensor_reg}, _external_context(external_context)
68{
69 // DO NOTHING
70}
71
72// Visitors for each operation is in ops/<InternalName>Layer.cc file
73
74} // namespace onert::backend::ggml
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 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.
This file contains utility macro.