ONE - On-device Neural Engine
Loading...
Searching...
No Matches
Shape.h
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#ifndef _MIR_SHAPE_H_
18#define _MIR_SHAPE_H_
19
20#include <initializer_list>
21#include <vector>
22#include <cstdint>
23
24#include "adtidas/SmallVector.h"
25#include "mir/Common.h"
26
27namespace mir
28{
29
30class Shape
31{
32public:
33 static constexpr int32_t autoDim = -1;
34
35 Shape() = default;
36
37 explicit Shape(int32_t rank) : _dims(rank) {}
38
39 Shape(std::initializer_list<int32_t> &&dims) : _dims(std::move(dims)) {}
40
41 explicit Shape(const std::vector<int32_t> &dims) : _dims(std::begin(dims), std::end(dims)) {}
42
43 int32_t rank() const { return static_cast<int32_t>(_dims.size()); }
44
45 void resize(int32_t size);
46
47 int32_t &dim(int32_t axis) noexcept
48 {
49 auto dim = wrap_index(axis, _dims.size());
50 return _dims[dim];
51 };
52
53 int32_t dim(int32_t axis) const noexcept
54 {
55 auto dim = wrap_index(axis, _dims.size());
56 return _dims[dim];
57 }
58
59 int32_t numElements() const;
60
61 bool operator==(const Shape &rhs) const { return _dims == rhs._dims; }
62
63 bool operator!=(const Shape &rhs) const { return !(*this == rhs); }
64
65private:
67};
68
69Shape broadcastShapes(const Shape &lhs_shape, const Shape &rhs_shape);
70
71std::string toString(const Shape &shape);
72
73} // namespace mir
74
75#endif //_MIR_SHAPE_H_
vector with cheap memory allocation
Definition SmallVector.h:34
size_t size() const noexcept
Definition SmallVector.h:67
Shape(const std::vector< int32_t > &dims)
Definition Shape.h:41
int32_t & dim(int32_t axis) noexcept
Definition Shape.h:47
int32_t numElements() const
Definition Shape.cpp:30
bool operator==(const Shape &rhs) const
Definition Shape.h:61
Shape()=default
Shape(int32_t rank)
Definition Shape.h:37
void resize(int32_t size)
Definition Shape.cpp:28
bool operator!=(const Shape &rhs) const
Definition Shape.h:63
int32_t rank() const
Definition Shape.h:43
static constexpr int32_t autoDim
Definition Shape.h:33
int32_t dim(int32_t axis) const noexcept
Definition Shape.h:53
Shape(std::initializer_list< int32_t > &&dims)
Definition Shape.h:39
std::string toString(DataFormat data_format)
Definition DataFormat.h:74
Shape broadcastShapes(const Shape &lhs_shape, const Shape &rhs_shape)
Definition Shape.cpp:43
constexpr std::size_t wrap_index(std::int32_t index, std::size_t limit) noexcept
Definition Common.h:30
int32_t size[5]
Definition Slice.cpp:35
int32_t begin[5]
Definition Slice.cpp:33
Definition Shape.h:28