ONE - On-device Neural Engine
Loading...
Searching...
No Matches
Max.cpp
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 "Max.h"
18#include "Common.h"
19
20#include "mir/ShapeRange.h"
21#include "mir/Tensor.h"
22
23#include <algorithm>
24
25namespace mir_interpreter
26{
27
28using namespace mir;
29
30template <typename T> struct MaxImpl
31{
32 static void run(const TensorVariant &lhs, const TensorVariant &rhs, TensorVariant &res);
33};
34
35template <typename T>
36void MaxImpl<T>::run(const TensorVariant &lhs, const TensorVariant &rhs, TensorVariant &res)
37{
38 TensorVariant broadcasted_lhs(lhs, res.getShape());
39 TensorVariant broadcasted_rhs(rhs, res.getShape());
40 Tensor<T> lhs_accessor(broadcasted_lhs);
41 Tensor<T> rhs_accessor(broadcasted_rhs);
42 Tensor<T> res_accessor(res);
43
44 for (const auto &index : ShapeRange(res.getShape()))
45 {
46 res_accessor.at(index) = std::max(lhs_accessor.at(index), rhs_accessor.at(index));
47 }
48}
49template <> struct MaxImpl<uint8_t>
50{
51 static void run(const TensorVariant &lhs, const TensorVariant &rhs, TensorVariant &res)
52 {
53 throw std::runtime_error{"NYI"};
54 };
55};
56
57void Max(const TensorVariant &lhs, const TensorVariant &rhs, TensorVariant &res)
58{
59 if (lhs.getElementType() != rhs.getElementType())
60 {
61 throw std::runtime_error{"Max with different input types is unsupported"};
62 }
63 dispatch<MaxImpl>(lhs.getElementType(), lhs, rhs, res);
64}
65
66} // namespace mir_interpreter
T at(const Index &id) const
Definition Tensor.h:31
const Shape & getShape() const
DataType getElementType() const
static void run(const TensorVariant &lhs, const TensorVariant &rhs, TensorVariant &res)
Definition Max.cpp:51
static void run(const TensorVariant &lhs, const TensorVariant &rhs, TensorVariant &res)
Definition Max.cpp:36