NVIDIA DeepStream SDK API Reference

8.0 Release
service-maker/includes/tensor.hpp
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: Copyright (c) 2024-2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
3  * SPDX-License-Identifier: MIT
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in
13  * all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21  * DEALINGS IN THE SOFTWARE.
22  */
23 
32 #ifndef DEEPSTREAM_TENSOR_HPP
33 #define DEEPSTREAM_TENSOR_HPP
34 
35 #include <vector>
36 
37 #include "buffer.hpp"
38 
39 namespace deepstream {
40 
41 typedef std::vector<uint64_t> TensorShape;
42 class TensorImplementation;
43 
49 class Tensor {
50  public:
54  typedef enum {
55  INVALID = -1,
60  } DataType;
61 
62  typedef enum {
63  NONE = -1,
64  CPU,
66  } DeviceType;
67 
75  class Context {
76  public:
77  virtual ~Context() {}
78  };
82  Tensor(unsigned int rank,
84  unsigned int bits,
85  const int64_t shape[],
86  const int64_t strides[],
87  void* data,
88  std::string format,
89  unsigned int device_id,
90  DeviceType device,
91  Context* context);
92 
94  Tensor(const Tensor&) = delete;
95 
99  virtual ~Tensor();
100 
104  unsigned int rank() const;
105 
109  TensorShape shape() const;
110 
114  DataType dtype() const;
115 
119  unsigned int bits() const;
120 
124  uint64_t stride(unsigned int d) const;
125 
129  void* data() const;
130 
134  unsigned int deviceId() const;
135 
139  DeviceType deviceType() const;
140 
145 
149  Tensor* clone() const;
150 
154  Tensor* toGPU(unsigned int device_id) const;
155 
159  uint64_t size() const;
160 
161  protected:
162  TensorImplementation* impl_;
163  std::string format_;
164  std::unique_ptr<Context> context_;
165 };
166 
167 }
168 
169 #endif
deepstream::Tensor::NONE
@ NONE
Definition: service-maker/includes/tensor.hpp:63
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:62
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::GPU
@ GPU
Definition: service-maker/includes/tensor.hpp:65
deepstream::Tensor::data
void * data() const
Retrieve the allocated data pointer.
deepstream::Tensor::impl_
TensorImplementation * impl_
Definition: service-maker/includes/tensor.hpp:162
deepstream::Tensor::UNSIGNED
@ UNSIGNED
Definition: service-maker/includes/tensor.hpp:56
deepstream::Tensor::DataType
DataType
Code for tensor data types.
Definition: service-maker/includes/tensor.hpp:54
deepstream::TensorShape
std::vector< uint64_t > TensorShape
Definition: service-maker/includes/tensor.hpp:41
deepstream::Tensor::context_
std::unique_ptr< Context > context_
Definition: service-maker/includes/tensor.hpp:164
deepstream
Definition: buffer.hpp:33
deepstream::Tensor::INVALID
@ INVALID
Definition: service-maker/includes/tensor.hpp:55
deepstream::Tensor::Context::~Context
virtual ~Context()
Definition: service-maker/includes/tensor.hpp:77
deepstream::Buffer
Base class of a buffer.
Definition: buffer.hpp:46
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::COMPLEX
@ COMPLEX
Definition: service-maker/includes/tensor.hpp:59
deepstream::Tensor::format_
std::string format_
Definition: service-maker/includes/tensor.hpp:163
deepstream::Tensor::clone
Tensor * clone() const
Clone a tensor.
deepstream::Tensor
Definition: service-maker/includes/tensor.hpp:49
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.
buffer.hpp
deepstream::Tensor::SIGNED
@ SIGNED
Definition: service-maker/includes/tensor.hpp:57
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:75
deepstream::Tensor::FLOAT
@ FLOAT
Definition: service-maker/includes/tensor.hpp:58
deepstream::Tensor::CPU
@ CPU
Definition: service-maker/includes/tensor.hpp:64
NvBufSurfaceColorFormat
NvBufSurfaceColorFormat
Defines color formats for NvBufSurface.
Definition: 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:26
deepstream::Tensor::dtype
DataType dtype() const
Data type of the tensor elements.