ONE - On-device Neural Engine
Loading...
Searching...
No Matches
BatchMatMul.cpp
Go to the documentation of this file.
1/*
2 * Copyright (c) 2022 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 "Builders.h"
18
19#include "kernels/BatchMatMul.h"
21
22namespace luci_interpreter
23{
24
25std::unique_ptr<Kernel> build_kernel_CircleBatchMatMul(const luci::CircleNode *circle_node,
26 KernelBuilderHelper &helper)
27{
28 const auto *node = loco::must_cast<const luci::CircleBatchMatMul *>(circle_node);
29 assert(node->arity() == 2);
30
31 const Tensor *lhs = helper.getInputTensor(node->x());
32 const Tensor *rhs = helper.getInputTensor(node->y());
33 Tensor *output = helper.getOutputTensor(node);
34
35 auto lhs_scratchpad =
36 std::make_unique<Tensor>(lhs->element_type(), Shape({}), AffineQuantization{}, "");
37 lhs_scratchpad->set_observable(false);
38 lhs_scratchpad->set_data_buffer(nullptr);
39 auto rhs_scratchpad =
40 std::make_unique<Tensor>(rhs->element_type(), Shape({}), AffineQuantization{}, "");
41 rhs_scratchpad->set_observable(false);
42 rhs_scratchpad->set_data_buffer(nullptr);
43 // If node has execution plan then read memory offsets for scratchpad temporary tensor
44 // from the beginning of shared memory buffer.
45 // Used in Static Memory Manager.
46 // TODO move tensors offset initialization to one place
48 {
49 const auto execution_plan = luci::get_execution_plan(node);
50 // Check whether the offset for the current BatchMatMul temporary was found.
51 if (execution_plan.offsets().size() > 1)
52 {
53 assert(execution_plan.offsets().size() == 3);
54
55 // If this is true, then we keep this offset in scratchpad.
56 lhs_scratchpad->set_offset(execution_plan.offsets().at(1));
57 rhs_scratchpad->set_offset(execution_plan.offsets().at(2));
58 }
59 }
60 Tensor *lhs_tmp = helper.getRuntimeGraph(node->graph())->addTensor(std::move(lhs_scratchpad));
61 Tensor *rhs_tmp = helper.getRuntimeGraph(node->graph())->addTensor(std::move(rhs_scratchpad));
62
63 BatchMatMulParams params;
64 params.adj_x = node->adj_x();
65 params.adj_y = node->adj_y();
66
67 return std::make_unique<kernels::BatchMatMul>(lhs, rhs, output, lhs_tmp, rhs_tmp, params);
68}
69
70} // namespace luci_interpreter
Tensor * getOutputTensor(const loco::Node *node) const
RuntimeGraph * getRuntimeGraph(const loco::Graph *graph) const
const Tensor * getInputTensor(const loco::Node *node) const
Tensor * addTensor(std::unique_ptr< Tensor > &&tensor)
void set_observable(bool value)
Definition Tensor.h:164
DataType element_type() const
Definition Tensor.h:105
std::unique_ptr< Kernel > build_kernel_CircleBatchMatMul(const luci::CircleNode *circle_node, KernelBuilderHelper &helper)
bool has_execution_plan(const luci::CircleNode *circle_node)
luci::CircleNodeExecutionPlan get_execution_plan(const luci::CircleNode *circle_node)