ONE - On-device Neural Engine
Loading...
Searching...
No Matches
Tensor.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_TENSOR_H_
18#define _MIR_TENSOR_H_
19
20#include "mir/ExternalRegion.h"
21#include "mir/TensorVariant.h"
22
23namespace mir
24{
25
26template <typename T> class Tensor final
27{
28public:
29 explicit Tensor(const TensorVariant &t) : _proxy(t) {}
30
31 T at(const Index &id) const { return *reinterpret_cast<T *>(this->_proxy.at(id)); }
32
33 T &at(const Index &id) { return *reinterpret_cast<T *>(this->_proxy.at(id)); }
34
35 T atOffset(int32_t offset) const { return *reinterpret_cast<T *>(this->_proxy.atOffset(offset)); }
36
37 T &atOffset(int32_t offset) { return *reinterpret_cast<T *>(this->_proxy.atOffset(offset)); }
38
40 {
41 // Only last dimension is safe to process continiously
42 auto last_dim = getShape().rank() - 1;
43 auto base = reinterpret_cast<T *>(_proxy.at(idx));
44 auto length = getShape().dim(last_dim) - idx.at(last_dim);
45 return ExternalRegion<T>(base, length);
46 }
47
48 const Shape &getShape() const { return _proxy.getShape(); };
49
50private:
51 const TensorVariant &_proxy;
52};
53
54extern template class Tensor<float>;
55
56extern template class Tensor<double>;
57
58extern template class Tensor<int>;
59
60} // namespace mir
61
62#endif //_MIR_TENSOR_H_
int32_t & at(int32_t axis)
return position on given axis
Definition Index.h:64
int32_t & dim(int32_t axis) noexcept
Definition Shape.h:47
int32_t rank() const
Definition Shape.h:43
ExternalRegion< T > getRegion(const Index &idx)
Definition Tensor.h:39
T atOffset(int32_t offset) const
Definition Tensor.h:35
Tensor(const TensorVariant &t)
Definition Tensor.h:29
const Shape & getShape() const
Definition Tensor.h:48
T & at(const Index &id)
Definition Tensor.h:33
T at(const Index &id) const
Definition Tensor.h:31
T & atOffset(int32_t offset)
Definition Tensor.h:37
char * atOffset(int32_t offset) const
const Shape & getShape() const
char * at(const Index &idx) const
__global uchar * offset(const Image *img, int x, int y)
Definition helpers.h:540