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 "misc/tensor/Shape.h"
18
19#include <cassert>
20#include <functional>
21#include <numeric>
22
23namespace nnfw
24{
25namespace misc
26{
27namespace tensor
28{
29
30bool operator==(const Shape &lhs, const Shape &rhs)
31{
32 if (lhs.rank() != rhs.rank())
33 {
34 return false;
35 }
36
37 for (uint32_t axis = 0; axis < lhs.rank(); ++axis)
38 {
39 if (lhs.dim(axis) != rhs.dim(axis))
40 {
41 return false;
42 }
43 }
44
45 return true;
46}
47
48Shape Shape::from(const std::string &str)
49{
50 Shape shape(0);
51
52 bool pending = false;
53 int value = 0;
54
55 for (const char *cur = str.c_str(); true; ++cur)
56 {
57 if (*cur == ',' || *cur == '\0')
58 {
59 if (pending)
60 {
61 shape.append(value);
62 }
63
64 if (*cur == '\0')
65 {
66 break;
67 }
68
69 pending = false;
70 value = 0;
71 continue;
72 }
73
74 assert(*cur >= '0' && *cur <= '9');
75
76 pending = true;
77 value *= 10;
78 value += *cur - '0';
79 }
80
81 return shape;
82}
83
84uint64_t Shape::num_elements() const
85{
86 return std::accumulate(_dimensions.cbegin(), _dimensions.cend(), UINT64_C(1),
87 std::multiplies<uint64_t>());
88}
89
90std::ostream &operator<<(std::ostream &os, const Shape &shape)
91{
92 if (shape.rank() > 0)
93 {
94 os << shape.dim(0);
95
96 for (uint32_t axis = 1; axis < shape.rank(); ++axis)
97 {
98 os << "," << shape.dim(axis);
99 }
100 }
101
102 return os;
103}
104
105} // namespace tensor
106} // namespace misc
107} // namespace nnfw
Class to represent shape of a tensor.
Definition Shape.h:45
int32_t dim(uint32_t n) const
Get specific dimension.
Definition Shape.h:100
void append(int32_t d)
Add dimension to the back.
Definition Shape.h:83
uint32_t rank(void) const
Get the rank of this shape.
Definition Shape.h:92
bool operator==(const Shape &, const Shape &)
Check equality of two Shape.
Definition Shape.cpp:30
std::ostream & operator<<(std::ostream &os, const IndexFormatter &fmt)
Send IndexFormatter object to output stream.
std::string str(Args &&...args)
Definition topk_v2.h:30
This file contains nnfw::misc::tensor::Shape class.