ONE - On-device Neural Engine
Loading...
Searching...
No Matches
StridedSlice.cpp
Go to the documentation of this file.
1/*
2 * Copyright (c) 2024 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 "OMStatus.h"
18
19#include "core/OMUtils.h"
20#include "core/OMDataType.h"
21
24
25using namespace onert_micro;
26using namespace onert_micro::train;
27
28namespace
29{
30
31constexpr uint32_t inputTensorIdx = 0;
32constexpr uint32_t outputTensorIdx = 0;
33
34} // namespace
35
36/*
37 * - Calculate input gradient - Optional (not required if it is last op)
38 * Note: now support when it is just reshape, number of output tensor is one and flat size of the
39 * output tensor is equal to input
40 */
41// TODO: support more general part
42OMStatus onert_micro::train::train_kernel_CircleStridedSlice(const OMBackpropExecuteArgs &args)
43{
44 // Check is it last layer for training
45 if (args.is_last_layer)
46 return Ok;
47
48 core::OMRuntimeContext &runtime_context = args.backward_context;
49 core::OMRuntimeStorage &runtime_storage = args.backward_storage;
50 uint16_t op_index = args.kernel_index;
51
52 execute::OMRuntimeKernel runtime_kernel;
53 runtime_kernel.readKernel(op_index, runtime_context);
54
55 const circle::Tensor *input = runtime_kernel.inputs[inputTensorIdx];
56 const circle::Tensor *output = runtime_kernel.outputs[outputTensorIdx];
57
58 assert(input != nullptr);
59 assert(output != nullptr);
60
61 // Note: now support when it is just reshape, number of output tensor is one and flat size of the
62 // output tensor is equal to input
63 assert(runtime_kernel.outputs_num == 1);
64 const core::OMRuntimeShape shape(input);
66 assert(shape.flatSize() == output_shape.flatSize());
67 if (runtime_kernel.outputs_num > 1 or shape.flatSize() != output_shape.flatSize())
68 return UnsupportedType;
69
70 OMStatus status = Ok;
71
72 status = runtime_kernel.getDataFromStorage(op_index, runtime_storage, runtime_context);
73 if (status != Ok)
74 return status;
75
76 uint8_t *input_data = runtime_kernel.inputs_data[inputTensorIdx];
77 uint8_t *output_data = runtime_kernel.outputs_data[outputTensorIdx];
78
79 assert(input_data != nullptr);
80 assert(output_data != nullptr);
81
82 // Check is it inplace kernel
83 if (input_data == output_data)
84 return Ok;
85
86 const size_t element_size =
87 static_cast<uint32_t>(getOMDataTypeSize(core::onertMicroDatatype(input->type())));
88 const int32_t num_elements = shape.flatSize();
89 std::memcpy(input_data, output_data, num_elements * element_size);
90
91 return status;
92}
uint8_t * outputs_data[maxOutputSize]
OMStatus getDataFromStorage(uint16_t op_index, core::OMRuntimeStorage &storage, core::OMRuntimeContext &context)
OMStatus readKernel(uint16_t op_index, core::OMRuntimeContext &runtime_context)
const circle::Tensor * outputs[maxOutputSize]
const circle::Tensor * inputs[maxInputSize]
const luci_interpreter::RuntimeShape output_shape
constexpr uint32_t outputTensorIdx
args
Definition infer.py:21
list input_data
Definition infer.py:29
uint32_t num_elements(const Shape &shape)
The number of elements of a feature map of a given shape.
Definition Shape.h:59
OMDataType onertMicroDatatype(const circle::TensorType type)
size_t getOMDataTypeSize(OMDataType data_type)
Definition OMDataType.h:179
@ UnsupportedType
Definition OMStatus.h:26