// Copyright (c) OpenMMLab. All rights reserved. #ifndef CORE_TENSOR_H #define CORE_TENSOR_H #include #include #include "device.h" #include "types.h" namespace mmdeploy { using TensorShape = std::vector; struct TensorDesc { Device device; DataType data_type{DataType::kFLOAT}; TensorShape shape; std::string name; }; class Tensor final { public: Tensor() = default; Tensor(const TensorDesc& desc, Allocator allocator = {}); // NOLINT Tensor(const TensorDesc& desc, Buffer buffer); Tensor(const TensorDesc& desc, std::shared_ptr data); ~Tensor() = default; const TensorDesc& desc() const; const TensorShape& shape() const; TensorShape::value_type shape(int dim) const; DataType data_type() const; const char* name() const; int64_t size() const; int64_t byte_size() const; const Buffer& buffer() const; Buffer& buffer(); Device device() const; void Reshape(const TensorShape& shape); Tensor Slice(int index); Result CopyFrom(const Tensor& tensor, Stream stream = {}); Result CopyTo(Tensor& tensor, Stream stream = {}) const; Result CopyFrom(void* host_ptr, Stream stream = {}); Result CopyTo(void* host_ptr, Stream stream = {}) const; template T* data() { return GetNative(buffer()); } template > U* data() const { return GetNative(buffer()); } private: void Allocate(); TensorDesc desc_; Allocator allocator_; Buffer buffer_; }; // static_assert(sizeof(Tensor) == 80); } // namespace mmdeploy #endif // !CORE_TENSOR_H