/home/docs/checkouts/readthedocs.org/user_builds/advanced-micro-devices-roccv/checkouts/latest/include/core/wrappers/generic_tensor_wrapper.hpp Source File

/home/docs/checkouts/readthedocs.org/user_builds/advanced-micro-devices-roccv/checkouts/latest/include/core/wrappers/generic_tensor_wrapper.hpp Source File#

3 min read time

Applies to Linux

rocCV: /home/docs/checkouts/readthedocs.org/user_builds/advanced-micro-devices-roccv/checkouts/latest/include/core/wrappers/generic_tensor_wrapper.hpp Source File
generic_tensor_wrapper.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2025 Advanced Micro Devices, Inc. All rights reserved.
3  * Permission is hereby granted, free of charge, to any person obtaining a copy
4  * of this software and associated documentation files (the "Software"), to deal
5  * in the Software without restriction, including without limitation the rights
6  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7  * copies of the Software, and to permit persons to whom the Software is
8  * furnished to do so, subject to the following conditions:
9  *
10  * The above copyright notice and this permission notice shall be included in
11  * all copies or substantial portions of the Software.
12  *
13  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19  * THE SOFTWARE.
20  */
21 
22 #pragma once
23 
24 #include "core/tensor.hpp"
25 
26 namespace roccv {
27 template <typename T>
29  public:
35  GenericTensorWrapper(const Tensor& tensor) {
36  auto tensorData = tensor.exportData<TensorDataStrided>();
37 
38  for (int i = 0; i < tensor.rank(); i++) {
39  shape[i] = tensor.shape(i);
40  strides[i] = tensorData.stride(i);
41  }
42 
43  rank = tensor.rank();
44  data = static_cast<unsigned char*>(tensorData.basePtr());
45  }
46 
47  template <typename... ARGS>
48  __device__ __host__ T& at(ARGS... idx) {
49  int64_t coords[] = {idx...};
50  size_t index = 0;
51 
52  for (int i = 0; i < rank; i++) {
53  index += coords[i] * strides[i];
54  }
55 
56  return *(reinterpret_cast<T*>(data + index));
57  }
58 
59  std::array<int64_t, ROCCV_TENSOR_MAX_RANK> shape;
60  std::array<int64_t, ROCCV_TENSOR_MAX_RANK> strides;
61  int rank;
62  unsigned char* data;
63 };
64 } // namespace roccv
Definition: generic_tensor_wrapper.hpp:28
std::array< int64_t, ROCCV_TENSOR_MAX_RANK > strides
Definition: generic_tensor_wrapper.hpp:60
int rank
Definition: generic_tensor_wrapper.hpp:61
std::array< int64_t, ROCCV_TENSOR_MAX_RANK > shape
Definition: generic_tensor_wrapper.hpp:59
__device__ __host__ T & at(ARGS... idx)
Definition: generic_tensor_wrapper.hpp:48
GenericTensorWrapper(const Tensor &tensor)
Constructs a generic tensor descriptor from a roccv::Tensor.
Definition: generic_tensor_wrapper.hpp:35
unsigned char * data
Definition: generic_tensor_wrapper.hpp:62
Holds the underlying tensor data alongside tensor metadata. This particular tensor data type is used ...
Definition: tensor_data.hpp:114
Definition: tensor.hpp:37
TensorData exportData() const
Exports the tensor data of the tensor.
int rank() const
Returns the rank of the tensor (i.e. the number of dimensions)
const TensorShape & shape() const
Returns the shape of the tensor.
Definition: strided_data_wrap.hpp:33