ONE - On-device Neural Engine
Loading...
Searching...
No Matches
Shape.cpp
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 "Shape.h"
18
19#include <algorithm>
20#include <limits>
21
22using namespace circle_resizer;
23
24Shape::Shape(const std::initializer_list<Dim> &dims) : _dims{dims} {}
25
26Shape::Shape(const std::vector<Dim> &shape_vec) : _dims{shape_vec} {}
27
28Shape::Shape(const std::initializer_list<uint32_t> &shape_vec)
29{
30 for (const auto &dim : shape_vec)
31 {
32 if (dim >= std::numeric_limits<int32_t>::max())
33 {
34 std::out_of_range("Provided dimension: " + std::to_string(dim) + " is out of range");
35 }
36 _dims.emplace_back(Dim{static_cast<int32_t>(dim)});
37 }
38}
39
40Shape Shape::scalar() { return Shape{std::initializer_list<Dim>{}}; }
41
42size_t Shape::rank() const { return _dims.size(); }
43
44Dim Shape::operator[](const size_t &axis) const
45{
46 if (is_scalar())
47 {
48 throw std::invalid_argument("You cannot gather dimension from a scalar");
49 }
50 if (axis > rank() - 1)
51 {
52 throw std::out_of_range("Axis=" + std::to_string(axis) +
53 " is out of range of shape's rank: " + std::to_string(rank()));
54 }
55 return _dims[axis];
56}
57
58bool Shape::is_scalar() const { return _dims.empty(); }
59
60bool Shape::is_dynamic() const
61{
62 if (is_scalar())
63 {
64 return false;
65 }
66 return std::any_of(std::begin(_dims), std::end(_dims),
67 [](const Dim &dim) { return dim.is_dynamic(); });
68}
69
70bool Shape::operator==(const Shape &rhs) const
71{
72 if (rank() != rhs.rank())
73 {
74 return false;
75 }
76 for (size_t axis = 0; axis < rank(); ++axis)
77 {
78 if (_dims[axis].value() != rhs[axis].value())
79 {
80 return false;
81 }
82 }
83 return true;
84}
85
86std::ostream &circle_resizer::operator<<(std::ostream &os, const Shape &shape)
87{
88 if (shape.is_scalar())
89 {
90 os << "[]";
91 return os;
92 }
93 os << "[";
94 for (int i = 0; i < shape.rank() - 1; ++i)
95 {
96 os << shape[i].value() << ", ";
97 }
98 os << shape[shape.rank() - 1].value() << "]";
99 return os;
100}
bool is_dynamic() const
Returns true if the dimension is dynamic. Otherwise, return false.
Definition Dim.cpp:33
bool is_scalar() const
Returns true if the shape is a scalar. Otherwise, return false.
Definition Shape.cpp:58
size_t rank() const
Returns number of dimensions in the shape.
std::ostream & operator<<(std::ostream &os, const Shape &shape)
Print the shape in format [1, 2, 3].
Definition Shape.cpp:86