mirror of
https://github.com/open-mmlab/mmdeploy.git
synced 2025-01-14 08:09:43 +08:00
* check in cmake * move backend_ops to csrc/backend_ops * check in preprocess, model, some codebase and their c-apis * check in CMakeLists.txt * check in parts of test_csrc * commit everything else * add readme * update core's BUILD_INTERFACE directory * skip codespell on third_party * update trt_net and ort_net's CMakeLists * ignore clion's build directory * check in pybind11 * add onnx.proto. Remove MMDeploy's dependency on ncnn's source code * export MMDeployTargets only when MMDEPLOY_BUILD_SDK is ON * remove useless message * target include directory is wrong * change target name from mmdeploy_ppl_net to mmdeploy_pplnn_net * skip install directory * update project's cmake * remove useless code * set CMAKE_BUILD_TYPE to Release by force if it isn't set by user * update custom ops CMakeLists * pass object target's source lists * fix lint end-of-file * fix lint: trailing whitespace * fix codespell hook * remove bicubic_interpolate to csrc/backend_ops/ * set MMDEPLOY_BUILD_SDK OFF * change custom ops build command * add spdlog installation command * update docs on how to checkout pybind11 * move bicubic_interpolate to backend_ops/tensorrt directory * remove useless code * correct cmake * fix typo * fix typo * fix install directory * correct sdk's readme * set cub dir when cuda version < 11.0 * change directory where clang-format will apply to * fix build command * add .clang-format * change clang-format style from google to file * reformat csrc/backend_ops * format sdk's code * turn off clang-format for some files * add -Xcompiler=-fno-gnu-unique * fix trt topk initialize * check in config for sdk demo * update cmake script and csrc's readme * correct config's path * add cuda include directory, otherwise compile failed in case of tensorrt8.2 * clang-format onnx2ncnn.cpp Co-authored-by: zhangli <lzhang329@gmail.com> Co-authored-by: grimoire <yaoqian@sensetime.com>
194 lines
5.4 KiB
C++
194 lines
5.4 KiB
C++
// Copyright (c) OpenMMLab. All rights reserved.
|
|
|
|
#include "tensor.h"
|
|
|
|
#include <numeric>
|
|
#include <sstream>
|
|
|
|
#include "core/utils/formatter.h"
|
|
#include "logger.h"
|
|
using std::stringstream;
|
|
|
|
namespace mmdeploy {
|
|
|
|
static inline int64_t element_size(DataType data_type) {
|
|
switch (data_type) {
|
|
case DataType::kFLOAT:
|
|
return 4;
|
|
case DataType::kHALF:
|
|
return 2;
|
|
case DataType::kINT8:
|
|
return 1;
|
|
case DataType::kINT32:
|
|
return 4;
|
|
case DataType::kINT64:
|
|
return 8;
|
|
default:
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
inline static std::string shape_string(const TensorShape& shape) {
|
|
if (shape.empty()) {
|
|
return "0";
|
|
}
|
|
stringstream ss;
|
|
ss << shape[0];
|
|
for (size_t i = 1; i < shape.size(); ++i) ss << "," << shape[i];
|
|
return ss.str();
|
|
}
|
|
|
|
Tensor::Tensor(const TensorDesc& desc, Allocator allocator)
|
|
: desc_(desc), allocator_(std::move(allocator)) {
|
|
if (desc_.shape.empty()) {
|
|
return;
|
|
}
|
|
if (auto _byte_size = byte_size(); _byte_size > 0) {
|
|
buffer_ = Buffer(desc.device, _byte_size, allocator_);
|
|
}
|
|
}
|
|
|
|
Tensor::Tensor(const TensorDesc& desc, Buffer buffer) // NOLINT
|
|
: desc_(desc), buffer_(std::move(buffer)) {}
|
|
|
|
Tensor::Tensor(const TensorDesc& desc, std::shared_ptr<void> data) {
|
|
desc_ = desc;
|
|
buffer_ = Buffer(desc.device, byte_size(), std::move(data));
|
|
}
|
|
|
|
static inline int64_t get_size(const std::vector<int64_t>& shape) {
|
|
if (shape.empty()) {
|
|
return 0;
|
|
}
|
|
auto _size = std::accumulate(begin(shape), end(shape), 1LL, std::multiplies<>());
|
|
return std::max(0LL, _size);
|
|
}
|
|
|
|
int64_t Tensor::size() const { return get_size(shape()); }
|
|
|
|
int64_t Tensor::byte_size() const { return size() * element_size(data_type()); }
|
|
const TensorDesc& Tensor::desc() const { return desc_; }
|
|
const TensorShape& Tensor::shape() const { return desc_.shape; }
|
|
DataType Tensor::data_type() const { return desc_.data_type; }
|
|
const char* Tensor::name() const { return desc_.name.c_str(); }
|
|
const Buffer& Tensor::buffer() const { return buffer_; }
|
|
|
|
Buffer& Tensor::buffer() {
|
|
Allocate();
|
|
return buffer_;
|
|
}
|
|
|
|
Device Tensor::device() const { return desc_.device; }
|
|
|
|
void Tensor::Reshape(const TensorShape& shape) {
|
|
bool is_same_size = size() == get_size(shape);
|
|
desc_.shape = shape;
|
|
if (buffer_ && !is_same_size) {
|
|
// re-allocate buffer
|
|
buffer_ = {};
|
|
Allocate();
|
|
}
|
|
}
|
|
|
|
Result<void> Tensor::CopyFrom(const Tensor& tensor, Stream stream) {
|
|
if (desc_.shape.empty() || tensor.desc().shape.empty()) {
|
|
ERROR("uninitialized tensor");
|
|
return Status(eInvalidArgument);
|
|
}
|
|
if (!(desc_.shape == tensor.desc().shape)) {
|
|
ERROR("mismatched shape {} vs {}", shape_string(desc_.shape),
|
|
shape_string(tensor.desc().shape));
|
|
return Status(eShapeMismatch);
|
|
}
|
|
if (desc_.data_type != tensor.desc().data_type) {
|
|
ERROR("mismatched data type {} vs {}", desc_.data_type, tensor.desc().data_type);
|
|
return Status(eShapeMismatch);
|
|
}
|
|
Allocate();
|
|
if (!stream) {
|
|
auto device = desc_.device.is_device() ? desc_.device : tensor.desc().device;
|
|
auto default_stream = Stream::GetDefault(device);
|
|
OUTCOME_TRY(default_stream.Copy(tensor.buffer(), buffer_));
|
|
} else {
|
|
OUTCOME_TRY(stream.Copy(tensor.buffer(), buffer_));
|
|
}
|
|
return success();
|
|
}
|
|
|
|
Result<void> Tensor::CopyTo(Tensor& tensor, Stream stream) const {
|
|
if (desc_.shape.empty() || tensor.desc().shape.empty()) {
|
|
ERROR("uninitialized tensor");
|
|
return Status(eInvalidArgument);
|
|
}
|
|
|
|
if (!(desc_.shape == tensor.desc().shape)) {
|
|
ERROR("mismatched shape {} vs {}", shape_string(desc_.shape),
|
|
shape_string(tensor.desc().shape));
|
|
return Status(eShapeMismatch);
|
|
}
|
|
if (desc_.data_type != tensor.desc().data_type) {
|
|
ERROR("mismatched data type {} vs {}", desc_.data_type, tensor.desc().data_type);
|
|
return Status(eShapeMismatch);
|
|
}
|
|
tensor.Allocate();
|
|
if (!stream) {
|
|
Device device = desc_.device.is_device() ? desc_.device : tensor.desc().device;
|
|
Stream default_stream = Stream::GetDefault(device);
|
|
return default_stream.Copy(buffer_, tensor.buffer());
|
|
} else {
|
|
return stream.Copy(buffer_, tensor.buffer());
|
|
}
|
|
}
|
|
|
|
Result<void> Tensor::CopyFrom(void* host_ptr, Stream stream) {
|
|
if (nullptr == host_ptr) {
|
|
return Status(eInvalidArgument);
|
|
}
|
|
if (desc_.shape.empty()) {
|
|
ERROR("uninitialized tensor");
|
|
return Status(eInvalidArgument);
|
|
}
|
|
Allocate();
|
|
if (!stream) {
|
|
auto default_stream = Stream::GetDefault(desc_.device);
|
|
return default_stream.Copy(host_ptr, buffer_, buffer_.GetSize());
|
|
} else {
|
|
return stream.Copy(host_ptr, buffer_, buffer_.GetSize());
|
|
}
|
|
}
|
|
|
|
Result<void> Tensor::CopyTo(void* host_ptr, Stream stream) const {
|
|
if (nullptr == host_ptr) {
|
|
return Status(eInvalidArgument);
|
|
}
|
|
if (desc_.shape.empty()) {
|
|
ERROR("uninitialized tensor");
|
|
return Status(eInvalidArgument);
|
|
}
|
|
if (!stream) {
|
|
auto default_stream = Stream::GetDefault(desc_.device);
|
|
return default_stream.Copy(buffer_, host_ptr, buffer_.GetSize());
|
|
} else {
|
|
return stream.Copy(buffer_, host_ptr, buffer_.GetSize());
|
|
}
|
|
}
|
|
|
|
void Tensor::Allocate() {
|
|
if (!buffer_) {
|
|
auto _desc = desc();
|
|
*this = Tensor(_desc, allocator_);
|
|
}
|
|
}
|
|
|
|
Tensor Tensor::Slice(int index) {
|
|
Tensor slice = *this;
|
|
slice.desc_.shape[0] = 1;
|
|
auto bytes = element_size(desc_.data_type) * get_size(slice.desc_.shape);
|
|
slice.buffer_ = Buffer(buffer(), index * bytes, bytes);
|
|
return slice;
|
|
}
|
|
|
|
TensorShape::value_type Tensor::shape(int dim) const { return desc().shape[dim]; }
|
|
|
|
} // namespace mmdeploy
|