ONE - On-device Neural Engine
Loading...
Searching...
No Matches
Object.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
23#ifndef __NNFW_MISC_TENSOR_OBJECT_H__
24#define __NNFW_MISC_TENSOR_OBJECT_H__
25
26#include "misc/tensor/Shape.h"
27#include "misc/tensor/Index.h"
30#include "misc/tensor/Reader.h"
31
32#include <vector>
33
34namespace nnfw
35{
36namespace misc
37{
38namespace tensor
39{
40
46template <typename T> class Object final : public Reader<T>
47{
48public:
52 using Generator = std::function<T(const Shape &shape, const Index &index)>;
53
54public:
60 Object(const Shape &shape, const Generator &fn) : _shape{shape}
61 {
62 // Set 'stride'
63 _stride.init(shape);
64
65 // Handle scalar object
66 if (shape.rank() == 0)
67 {
68 _values.resize(1);
69 _values.at(0) = fn(_shape, 0);
70 }
71 else
72 {
73 // Pre-allocate buffer
74 _values.resize(_shape.dim(0) * _stride.at(0));
75
76 // Set 'value'
77 iterate(_shape) <<
78 [this, &fn](const Index &index) { _values.at(_stride.offset(index)) = fn(_shape, index); };
79 }
80 }
81
82public:
87 const Shape &shape(void) const { return _shape; }
88
89public:
95 T at(const Index &index) const override { return _values.at(_stride.offset(index)); }
96
97private:
98 Shape _shape;
99 NonIncreasingStride _stride;
100
101private:
102 std::vector<T> _values;
103};
104
105} // namespace tensor
106} // namespace misc
107} // namespace nnfw
108
109#endif // __NNFW_MISC_FEATURE_OBJECT_H__
This file contains nnfw::misc::tensor::NonIncreasingStride class.
Class to represent strides where stride[N-1] >= stride[N] holds for all N < rank.
uint32_t at(uint32_t axis) const
Get an stride value for specific axis.
void init(const Shape &shape)
Initialize the stride data using Shape.
uint32_t offset(const Index &index) const
Get the 1-D offset of specified index for n-D tensor.
Class to build a tensor using specific generator.
Definition Object.h:47
std::function< T(const Shape &shape, const Index &index)> Generator
Function to generate tensor element.
Definition Object.h:52
const Shape & shape(void) const
Get reference of shape.
Definition Object.h:87
Object(const Shape &shape, const Generator &fn)
Construct a new Object object.
Definition Object.h:60
T at(const Index &index) const override
Get and element of tensor.
Definition Object.h:95
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
uint32_t rank(void) const
Get the rank of this shape.
Definition Shape.h:92
This file contains nnfw::misc::tensor::IndexIterator class and helper function and operator.
IndexIterator iterate(const Shape &shape)
Get an IndexItator object.
Definition topk_v2.h:30
This file contains nnfw::misc::tensor::Index struct.
This file contains nnfw::misc::tensor::Reader struct.
This file contains nnfw::misc::tensor::Shape class.
Struct to represent index of each dimension of a tensor.
Definition Index.h:42
Struct to read element of tensor.
Definition Reader.h:40