mmdeploy/csrc/core/model.h
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

154 lines
4.0 KiB
C++

// Copyright (c) OpenMMLab. All rights reserved.
#ifndef CORE_SDK_MODEL_H
#define CORE_SDK_MODEL_H
#include <functional>
#include <memory>
#include <string>
#include <vector>
#include "serialization.h"
#include "types.h"
namespace mmdeploy {
struct model_meta_info_t {
std::string name;
std::string net;
std::string weights;
std::string backend;
int batch_size;
std::string precision;
bool dynamic_shape;
MMDEPLOY_ARCHIVE_MEMBERS(name, net, weights, backend, batch_size, precision, dynamic_shape);
};
struct deploy_meta_info_t {
std::string version;
std::vector<model_meta_info_t> models;
MMDEPLOY_ARCHIVE_MEMBERS(version, models);
};
class ModelImpl;
/**
* @class Model
* @brief Read sdk model from file.
* @note there might be more than one models in an sdk model file. For example,
* in case of faster-rcnn model, it splits into two models, one is rpn and the
* other is cnn for roi classification.
*/
class MMDEPLOY_API Model {
public:
Model() = default;
/**
* @brief construct `Model` with an sdk model's path
* @param model_path file path of an sdk model. It can be a file or a
* directory. Refer to `Load`
* @note An exception might be threw. `Try...catch...` is strongly recommended
* when this constructor is used
*/
explicit Model(const std::string& model_path);
Model(const void* buffer, size_t size);
~Model() = default;
/**
* @brief Load an sdk model.
* @param model_path file path of an sdk model. It can be a file or a
* directory.
* @return status with an error code.
*/
Result<void> Init(const std::string& model_path);
Result<void> Init(const void* buffer, size_t size);
/**
* @brief Return a specified model's meta info
* @param name the name of a model in sdk model file
* @return
*/
Result<model_meta_info_t> GetModelConfig(const std::string& name) const;
/**
* @brief Read specified file from an sdk model
* @param file_path path relative to the root directory of an sdk model.
* @return the content of specified file if success, which can be accessed by
* `Result<T>.value()`. Otherwise, error code is returned that can be obtained
* by `Result<T>.error()`
*/
Result<std::string> ReadFile(const std::string& file_path) noexcept;
/**
* @brief get meta information of an sdk model
* @return sdk model's meta information
*/
const deploy_meta_info_t& meta() const { return meta_; }
/**
* @brief Check if an instance of `Model` is valid
* @return the status of an instance of `Model`
*/
explicit operator bool() const { return impl_ != nullptr; }
private:
std::shared_ptr<ModelImpl> impl_;
deploy_meta_info_t meta_;
};
/**
* @class ModelRegistry
* @brief SDK model implementor's factory. The following code shows how to
* register a new implementor to the factory.
* @example
* class ANewModelImpl : public ModelImpl {
* };
* class ANewModelImplRegister {
* public:
* ANewModelImplRegister() {
* ModelRegistry::Get().Register("ANewModelImpl",
* []()->unique_ptr<ModelImpl>{return make_unique<ANewModelImpl>();});
* }
* };
* ANewModelImplRegister a_new_model_impl_register;
*/
class MMDEPLOY_API ModelRegistry {
public:
using Creator = std::function<std::unique_ptr<ModelImpl>()>;
struct Entry {
std::string name;
Creator creator;
};
/**
* @brief Return global instance of `ModelRegistry`
*/
static ModelRegistry& Get();
/**
* @brief Register an sdk model format denoted by an specified `ModelImpl`
* @param name sdk model implementor's name
* @param creator method to create an sdk model implementor
* @return Status of registering result
*/
Result<void> Register(const std::string& name, Creator creator);
/**
* @brief Return the registered sdk model implementors
*/
const std::vector<Entry>& ListEntries() const { return entries_; }
private:
ModelRegistry() = default;
private:
std::vector<Entry> entries_;
};
} // namespace mmdeploy
#endif // !CORE_SDK_MODEL_H