ONE - On-device Neural Engine
Loading...
Searching...
No Matches
OneHot.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#include "../Validator.h"
19
20#include <AclKernelGen.h>
21
23{
24
25void Validator::visit(const ir::operation::OneHot &) { _supported = true; }
26
27void KernelGenerator::visit(const ir::operation::OneHot &node)
28{
29 const auto output_idx{node.getOutputs().at(0)};
30 const auto indices_idx{node.getInputs().at(ir::operation::OneHot::Input::INDICES)};
31 const auto depth_idx{node.getInputs().at(ir::operation::OneHot::Input::DEPTH)};
32 const auto onvalue_idx{node.getInputs().at(ir::operation::OneHot::Input::ON_VALUE)};
33 const auto offvalue_idx{node.getInputs().at(ir::operation::OneHot::Input::OFF_VALUE)};
34 const auto depth = _ctx.at(depth_idx).asScalar<int32_t>();
35 assert(depth > 0);
36
37 auto output_tensor = _tensor_reg->getAclTensor(output_idx);
38 auto indices_tensor = _tensor_reg->getAclTensor(indices_idx);
39 auto onvalue_tensor = _tensor_reg->getAclTensor(onvalue_idx);
40
41 const size_t output_rank = _ctx.at(output_idx).shape().rank();
42 int32_t axis = node.param().axis == -1 ? output_rank - 1 : node.param().axis;
43 axis = acl_common::ToARMComputeAxis(output_rank, axis).value();
44
45 if (output_tensor->num_dimensions() != output_tensor->info()->num_dimensions())
46 {
47 // This means that high dimension's value is 1 and output_tensor is applied dim_correction
49 }
50
51 std::unique_ptr<::arm_compute::IFunction> fn;
52 const auto &offvalue = _ctx.at(offvalue_idx);
53 if (offvalue.isConstant())
54 {
55 fn = acl_common::generateLayer<arm_compute::CLOneHot>(
56 indices_tensor->handle(), onvalue_tensor->handle(), output_tensor->handle(),
57 acl_common::asPixelValue(offvalue), static_cast<uint32_t>(depth), axis);
58 }
59 else
60 {
61 auto offvalue_tensor = _tensor_reg->getAclTensor(offvalue_idx);
62 fn = acl_common::generateLayer<arm_compute::CLOneHot>(
63 indices_tensor->handle(), onvalue_tensor->handle(), offvalue_tensor->handle(),
64 output_tensor->handle(), static_cast<uint32_t>(depth), axis);
65 }
66
67 if (output_tensor->dimension(0) == 1)
68 {
70 }
71
73}
74
75} // namespace onert::backend::acl_cl
std::unique_ptr< exec::IFunction > _return_fn
const Object & at(const Index &index) const
Get the object that is associated with the given index.
ARMComputeAxis ToARMComputeAxis(uint32_t rank, uint32_t axis)
Definition Swizzle.h:45
void enableDimCorrection(IACLTensor *tensor)
arm_compute::PixelValue asPixelValue(const ir::Operand &operand)
Definition Convert.cc:311
std::unique_ptr< AclFunction > asAclFunction(std::unique_ptr<::arm_compute::IFunction > &&layer)
Definition Convert.cc:246
void disableDimCorrection(IACLTensor *tensor)