mmdeploy/csrc/preprocess/cuda/resize_impl.cpp
lzhangzz 640aa03538
Support Windows (#106)
* minor changes

* support windows

* fix GCC build

* fix lint

* reformat

* fix Windows build

* fix GCC build

* search backend ops for onnxruntime

* fix lint

* fix lint

* code clean-up

* code clean-up

* fix clang build

* fix trt support

* fix cmake for ncnn

* fix cmake for openvino

* fix SDK Python API

* handle ops for other backends (ncnn, trt)

* handle SDK Python API library location

* robustify linkage

* fix cuda

* minor fix for openvino & ncnn

* use CMAKE_CUDA_ARCHITECTURES if set

* fix cuda preprocessor

* fix misc

* fix pplnn & pplcv, drop support for pplcv<0.6.0

* robustify cmake

* update build.md (#2)

* build dynamic modules as module library & fix demo (partially)

* fix candidate path for mmdeploy_python

* move "enable CUDA" to cmake config for demo

* refine demo cmake

* add comment

* fix ubuntu build

* revert docs/en/build.md

* fix C API

* fix lint

* Windows build doc (#3)

* check in docs related to mmdeploy build on windows

* update build guide on windows platform

* update build guide on windows platform

* make path of thirdparty libraries consistent

* make path consistency

* correct build command for custom ops

* correct build command for sdk

* update sdk build instructions

* update doc

* correct build command

* fix lint

* correct build command and fix lint

Co-authored-by: lvhan <lvhan@pjlab.org>

* trailing whitespace (#4)

* minor fix

* fix sr sdk model

* fix type deduction

* fix cudaFree after driver shutting down

* update ppl.cv installation warning (#5)

* fix device allocator threshold & fix lint

* update doc (#6)

* update ppl.cv installation warning

* missing 'git clone'

Co-authored-by: chenxin <chenxin2@sensetime.com>
Co-authored-by: zhangli <zhangli@sensetime.com>
Co-authored-by: lvhan028 <lvhan_028@163.com>
Co-authored-by: lvhan <lvhan@pjlab.org>
2022-02-24 20:08:44 +08:00

104 lines
3.7 KiB
C++

// Copyright (c) OpenMMLab. All rights reserved.
#include "core/utils/device_utils.h"
#include "core/utils/formatter.h"
#include "ppl/cv/cuda/resize.h"
#include "preprocess/transform/resize.h"
using namespace std;
namespace mmdeploy {
namespace cuda {
class ResizeImpl final : public ::mmdeploy::ResizeImpl {
public:
explicit ResizeImpl(const Value& args) : ::mmdeploy::ResizeImpl(args) {
if (arg_.interpolation != "bilinear" && arg_.interpolation != "nearest") {
MMDEPLOY_ERROR("{} interpolation is not supported", arg_.interpolation);
throw_exception(eNotSupported);
}
}
~ResizeImpl() override = default;
protected:
Result<Tensor> ResizeImage(const Tensor& tensor, int dst_h, int dst_w) override {
OUTCOME_TRY(auto src_tensor, MakeAvailableOnDevice(tensor, device_, stream_));
TensorDesc dst_desc{
device_, src_tensor.data_type(), {1, dst_h, dst_w, src_tensor.shape(3)}, src_tensor.name()};
Tensor dst_tensor(dst_desc);
auto stream = GetNative<cudaStream_t>(stream_);
if (tensor.data_type() == DataType::kINT8) {
OUTCOME_TRY(ResizeDispatch<uint8_t>(src_tensor, dst_tensor, stream));
} else if (tensor.data_type() == DataType::kFLOAT) {
OUTCOME_TRY(ResizeDispatch<float>(src_tensor, dst_tensor, stream));
} else {
MMDEPLOY_ERROR("unsupported data type {}", tensor.data_type());
return Status(eNotSupported);
}
return dst_tensor;
}
private:
template <class T, int C, class... Args>
ppl::common::RetCode DispatchImpl(Args&&... args) {
#if PPLCV_VERSION_MAJOR >= 0 && PPLCV_VERSION_MINOR >= 6 && PPLCV_VERSION_PATCH >= 2
if (arg_.interpolation == "bilinear") {
return ppl::cv::cuda::Resize<T, C>(std::forward<Args>(args)...,
ppl::cv::INTERPOLATION_LINEAR);
}
if (arg_.interpolation == "nearest") {
return ppl::cv::cuda::Resize<T, C>(std::forward<Args>(args)...,
ppl::cv::INTERPOLATION_NEAREST_POINT);
}
#else
if (arg_.interpolation == "bilinear") {
return ppl::cv::cuda::Resize<T, C>(std::forward<Args>(args)...,
ppl::cv::INTERPOLATION_TYPE_LINEAR);
}
if (arg_.interpolation == "nearest") {
return ppl::cv::cuda::Resize<T, C>(std::forward<Args>(args)...,
ppl::cv::INTERPOLATION_TYPE_NEAREST_POINT);
}
#endif
return ppl::common::RC_UNSUPPORTED;
}
template <class T>
Result<void> ResizeDispatch(const Tensor& src, Tensor& dst, cudaStream_t stream) {
int h = (int)src.shape(1);
int w = (int)src.shape(2);
int c = (int)src.shape(3);
int dst_h = (int)dst.shape(1);
int dst_w = (int)dst.shape(2);
ppl::common::RetCode ret = 0;
auto input = src.data<T>();
auto output = dst.data<T>();
if (1 == c) {
ret = DispatchImpl<T, 1>(stream, h, w, w * c, input, dst_h, dst_w, dst_w * c, output);
} else if (3 == c) {
ret = DispatchImpl<T, 3>(stream, h, w, w * c, input, dst_h, dst_w, dst_w * c, output);
} else if (4 == c) {
ret = DispatchImpl<T, 4>(stream, h, w, w * c, input, dst_h, dst_w, dst_w * c, output);
} else {
MMDEPLOY_ERROR("unsupported channels {}", c);
return Status(eNotSupported);
}
return ret == 0 ? success() : Result<void>(Status(eFail));
}
};
class ResizeImplCreator : public Creator<::mmdeploy::ResizeImpl> {
public:
const char* GetName() const override { return "cuda"; }
int GetVersion() const override { return 1; }
ReturnType Create(const Value& args) override { return make_unique<ResizeImpl>(args); }
};
} // namespace cuda
} // namespace mmdeploy
using ::mmdeploy::ResizeImpl;
using ::mmdeploy::cuda::ResizeImplCreator;
REGISTER_MODULE(ResizeImpl, ResizeImplCreator);