ONE - On-device Neural Engine
Loading...
Searching...
No Matches
Shape.cpp
Go to the documentation of this file.
1/*
2 * Copyright (c) 2018 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 "mir/Shape.h"
18
19#include <algorithm>
20#include <cassert>
21#include <sstream>
22
23namespace mir
24{
25
26constexpr int32_t mir::Shape::autoDim;
27
28void Shape::resize(int32_t size) { _dims.resize(size); }
29
30int32_t Shape::numElements() const
31{
32 int32_t res = 1;
33
34 for (int32_t axis = 0; axis < rank(); ++axis)
35 {
36 assert(dim(axis) != Shape::autoDim);
37 res *= dim(axis);
38 }
39
40 return res;
41}
42
43Shape broadcastShapes(const Shape &lhs_shape, const Shape &rhs_shape)
44{
45 const int num_dims = std::max(lhs_shape.rank(), rhs_shape.rank());
46 Shape result_shape(num_dims);
47
48 for (int i = 0; i < num_dims; ++i)
49 {
50 const std::int32_t lhs_dim =
51 (i >= num_dims - lhs_shape.rank()) ? lhs_shape.dim(i - (num_dims - lhs_shape.rank())) : 1;
52 const std::int32_t rhs_dim =
53 (i >= num_dims - rhs_shape.rank()) ? rhs_shape.dim(i - (num_dims - rhs_shape.rank())) : 1;
54 if (lhs_dim == 1)
55 {
56 result_shape.dim(i) = rhs_dim;
57 }
58 else
59 {
60 assert(rhs_dim == 1 || rhs_dim == lhs_dim);
61 result_shape.dim(i) = lhs_dim;
62 }
63 }
64
65 return result_shape;
66}
67
68std::string toString(const Shape &shape)
69{
70 std::stringstream ss;
71
72 ss << "[";
73 for (int32_t axis = 0; axis < shape.rank(); ++axis)
74 {
75 if (axis != 0)
76 ss << ", ";
77 if (shape.dim(axis) == Shape::autoDim)
78 ss << "AUTO";
79 else
80 ss << shape.dim(axis);
81 }
82 ss << "]";
83
84 return ss.str();
85}
86
87} // namespace mir
void resize(size_t new_size) noexcept
resize to given new size
Definition SmallVector.h:78
int32_t & dim(int32_t axis) noexcept
Definition Shape.h:47
int32_t rank() const
Definition Shape.h:43
static constexpr int32_t autoDim
Definition Shape.h:33
std::string toString(DataFormat data_format)
Definition DataFormat.h:74
Shape broadcastShapes(const Shape &lhs_shape, const Shape &rhs_shape)
Definition Shape.cpp:43
int32_t size[5]
Definition Slice.cpp:35