NVIDIA DeepStream SDK API Reference

9.1 Release
service-maker/includes/tensor.hpp
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: Copyright (c) 2024-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
3  * SPDX-License-Identifier: Apache-2.0
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
26 #ifndef DEEPSTREAM_TENSOR_HPP
27 #define DEEPSTREAM_TENSOR_HPP
28 
29 #include <vector>
30 
31 #include "buffer.hpp"
32 
33 namespace deepstream {
34 
35 typedef std::vector<uint64_t> TensorShape;
36 class TensorImplementation;
37 
43 class Tensor {
44  public:
48  typedef enum {
49  INVALID = -1,
54  } DataType;
55 
56  typedef enum {
57  NONE = -1,
58  CPU,
60  } DeviceType;
61 
69  class Context {
70  public:
71  virtual ~Context() {}
72  };
76  Tensor(unsigned int rank,
78  unsigned int bits,
79  const int64_t shape[],
80  const int64_t strides[],
81  void* data,
82  std::string format,
83  unsigned int device_id,
84  DeviceType device,
85  Context* context);
86 
88  Tensor(const Tensor&) = delete;
89 
93  virtual ~Tensor();
94 
98  unsigned int rank() const;
99 
103  TensorShape shape() const;
104 
108  DataType dtype() const;
109 
113  unsigned int bits() const;
114 
118  uint64_t stride(unsigned int d) const;
119 
123  void* data() const;
124 
128  unsigned int deviceId() const;
129 
133  DeviceType deviceType() const;
134 
139 
143  Tensor* clone() const;
144 
148  Tensor* toGPU(unsigned int device_id) const;
149 
153  uint64_t size() const;
154 
155  protected:
156  TensorImplementation* impl_;
157  std::string format_;
158  std::unique_ptr<Context> context_;
159 };
160 
161 }
162 
163 #endif
deepstream::TensorShape
std::vector< uint64_t > TensorShape
Definition: service-maker/includes/tensor.hpp:35
deepstream::Tensor::wrap
Buffer wrap(NvBufSurfaceColorFormat format)
Wrap the tensor to a buffer.
deepstream::Tensor::size
uint64_t size() const
total size of the tensor in bytes
deepstream::Tensor::DeviceType
DeviceType
Definition: service-maker/includes/tensor.hpp:56
deepstream::Tensor::FLOAT
@ FLOAT
Definition: service-maker/includes/tensor.hpp:52
deepstream::Tensor::context_
std::unique_ptr< Context > context_
Definition: service-maker/includes/tensor.hpp:158
deepstream::Tensor::Tensor
Tensor(unsigned int rank, DataType dtype, unsigned int bits, const int64_t shape[], const int64_t strides[], void *data, std::string format, unsigned int device_id, DeviceType device, Context *context)
Construct from common tensor configurations.
deepstream::Tensor::data
void * data() const
Retrieve the allocated data pointer.
deepstream::Tensor::CPU
@ CPU
Definition: service-maker/includes/tensor.hpp:58
buffer.hpp
deepstream::Tensor::DataType
DataType
Code for tensor data types.
Definition: service-maker/includes/tensor.hpp:48
deepstream::Tensor::INVALID
@ INVALID
Definition: service-maker/includes/tensor.hpp:49
deepstream
Definition: service-maker/includes/buffer.hpp:38
deepstream::Tensor::GPU
@ GPU
Definition: service-maker/includes/tensor.hpp:59
deepstream::Tensor::NONE
@ NONE
Definition: service-maker/includes/tensor.hpp:57
deepstream::Tensor::Context::~Context
virtual ~Context()
Definition: service-maker/includes/tensor.hpp:71
deepstream::Buffer
Base class of a buffer.
Definition: service-maker/includes/buffer.hpp:51
deepstream::Tensor::deviceType
DeviceType deviceType() const
Retrieve the device type.
deepstream::Tensor::rank
unsigned int rank() const
Number of dimenstions of the tensor.
deepstream::Tensor::toGPU
Tensor * toGPU(unsigned int device_id) const
Copy the tensor to GPU.
deepstream::Tensor::SIGNED
@ SIGNED
Definition: service-maker/includes/tensor.hpp:51
deepstream::Tensor::format_
std::string format_
Definition: service-maker/includes/tensor.hpp:157
deepstream::Tensor::clone
Tensor * clone() const
Clone a tensor.
deepstream::Tensor
Definition: service-maker/includes/tensor.hpp:43
deepstream::Tensor::impl_
TensorImplementation * impl_
Definition: service-maker/includes/tensor.hpp:156
deepstream::Tensor::~Tensor
virtual ~Tensor()
Destructor.
deepstream::Tensor::shape
TensorShape shape() const
Shape of the tensor in vector.
deepstream::Tensor::stride
uint64_t stride(unsigned int d) const
Retrieve the stride of a specific dimension.
deepstream::Tensor::UNSIGNED
@ UNSIGNED
Definition: service-maker/includes/tensor.hpp:50
deepstream::Tensor::deviceId
unsigned int deviceId() const
Retrieve the device id.
deepstream::Tensor::Context
Context object of the tensor, which associates the tensor with what creates it.
Definition: service-maker/includes/tensor.hpp:69
deepstream::Tensor::COMPLEX
@ COMPLEX
Definition: service-maker/includes/tensor.hpp:53
NvBufSurfaceColorFormat
NvBufSurfaceColorFormat
Defines color formats for NvBufSurface.
Definition: sources/includes/nvbufsurface.h:105
deepstream::Tensor::bits
unsigned int bits() const
Number of bits for each data.
ds3d::v2xinfer::format
static std::string format(const char *fmt,...)
Definition: sources/libs/ds3d/inference_custom_lib/ds3d_v2x_infer_custom_preprocess/tensor.hpp:31
deepstream::Tensor::dtype
DataType dtype() const
Data type of the tensor elements.