mmdeploy/csrc/experimental/module_adapter.h
lvhan028 36124f6205
Merge sdk (#251)
* 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>
2021-12-07 10:57:55 +08:00

134 lines
4.0 KiB
C++

// Copyright (c) OpenMMLab. All rights reserved.
#ifndef MMDEPLOY_SRC_EXPERIMENTAL_MODULE_ADAPTER_H_
#define MMDEPLOY_SRC_EXPERIMENTAL_MODULE_ADAPTER_H_
#include "archive/value_archive.h"
#include "core/module.h"
#include "core/mpl/type_traits.h"
namespace mmdeploy {
namespace module_detail {
template <typename T>
struct is_tuple : std::false_type {};
template <typename... Ts>
struct is_tuple<std::tuple<Ts...>> : std::true_type {};
template <typename T>
inline constexpr auto is_tuple_v = is_tuple<T>::value;
template <typename Ret, typename... Args>
struct InvokeImpl {
template <typename F, typename... Ts>
static Result<Value> apply(F&& f, const Value& params, Ts&&... ts) {
std::tuple<uncvref_t<Args>...> args;
try {
from_value(params, args);
auto ret = apply_impl(std::forward<F>(f), std::move(args), std::index_sequence_for<Args...>{},
std::forward<Ts>(ts)...);
return make_ret_val(std::move(ret));
} catch (const std::exception& e) {
ERROR("unhandled exception: {}", e.what());
return Status(eFail);
} catch (...) {
return Status(eFail);
}
}
template <typename F, typename Tuple, size_t... Is, typename... Ts>
static decltype(auto) apply_impl(F&& f, Tuple&& tuple, std::index_sequence<Is...>, Ts&&... ts) {
return std::invoke(std::forward<F>(f), std::forward<Ts>(ts)...,
std::get<Is>(std::forward<Tuple>(tuple))...);
}
template <typename T, typename T0 = uncvref_t<T>>
static Result<Value> make_ret_val(T&& ret) {
if constexpr (module_detail::is_tuple_v<T0>) {
return to_value(std::forward<T>(ret));
} else if constexpr (is_result_v<T0>) {
return ret ? make_ret_val(std::forward<T>(ret).value()) : std::forward<T>(ret).as_failure();
} else {
return make_ret_val(std::forward_as_tuple(std::forward<T>(ret)));
}
}
};
// function pointer
template <typename Ret, typename... Args>
Result<Value> Invoke(Ret (*f)(Args...), const Value& args) {
return InvokeImpl<Ret, Args...>::apply(f, args);
}
// member function pointer
template <typename Ret, typename C, typename... Args>
Result<Value> Invoke(Ret (C::*f)(Args...) const, C* inst, const Value& args) {
return InvokeImpl<Ret, Args...>::apply(f, args, inst);
}
template <typename Ret, typename C, typename... Args>
Result<Value> Invoke(Ret (C::*f)(Args...), C* inst, const Value& args) {
return InvokeImpl<Ret, Args...>::apply(f, args, inst);
}
// function object
template <typename T, typename C = std::remove_reference_t<T>,
typename = std::void_t<decltype(&C::operator())>>
Result<Value> Invoke(T&& t, const Value& args) {
return Invoke(&C::operator(), &t, args);
}
template <typename T>
struct IsPointer : std::false_type {};
template <typename R, typename... Args>
struct IsPointer<R (*)(Args...)> : std::false_type {};
template <typename T>
struct IsPointer<std::shared_ptr<T>> : std::true_type {};
template <typename T, typename D>
struct IsPointer<std::unique_ptr<T, D>> : std::true_type {};
template <typename T>
struct IsPointer<T*> : std::true_type {};
template <typename T, typename SFINAE = void>
struct AccessPolicy {
static constexpr auto apply = [](auto& x) -> decltype(auto) { return x; };
};
template <typename T>
struct AccessPolicy<T, std::enable_if_t<IsPointer<T>::value>> {
static constexpr auto apply = [](auto& x) -> decltype(auto) { return *x; };
};
template <typename T, typename A = AccessPolicy<T>>
class Task : public Module {
public:
explicit Task(T task) : task_(std::move(task)) {}
Result<Value> Process(const Value& arg) override {
return module_detail::Invoke(A::apply(task_), arg);
}
private:
T task_;
};
template <typename T>
std::unique_ptr<Module> CreateTask(T&& x) {
return std::unique_ptr<Module>(new Task{std::forward<T>(x)});
}
template <typename T>
auto MakeTask(T&& x) {
return Task(std::forward<T>(x));
}
} // namespace module_detail
using module_detail::CreateTask;
using module_detail::MakeTask;
} // namespace mmdeploy
#endif // MMDEPLOY_SRC_EXPERIMENTAL_MODULE_ADAPTER_H_