tnn
TNN_NS::TNN是TNN對外暴露接口的管理類涨椒,如下使用
//api接口的管理對象
TNN_NS::TNN tnn;
//配置MODEL_TYPE_TNN和模型文件(tnnparam和tnnmodel文件)
TNN_NS::ModelConfig model_config;
//讀取tnnparam和tnnmodel文件
auto proto_tnn = fdLoadFile(model_param);
auto model_tnn = fdLoadFile(model);
model_config.model_type = TNN_NS::MODEL_TYPE_TNN;
model_config.params = {proto_tnn, model_tnn};
初始化
tnn.Init(model_config);
TNN這個類的主要目的是為了對外暴露接口摊鸡,主要有四個成員函數(shù),分別是Init蚕冬、DeInit免猾、AddOutput和CreateInst,先來看下include/tnn下的tnn.h,如下
class TNNImpl;
class PUBLIC TNN {
public:
TNN();
~TNN();
// 初始化函數(shù),解析模型文件
Status Init(ModelConfig& config);
// 釋放模型解析器.
Status DeInit();
// 增加輸出節(jié)點伞租,先通過output_name,如果沒有找到名字作喘,就通過索引 查找節(jié)點.
Status AddOutput(const std::string& output_name, int output_index = 0);
// 用于創(chuàng)建網(wǎng)絡(luò)執(zhí)行器Instance
std::shared_ptr<Instance> CreateInst(
NetworkConfig& config, Status& status,
InputShapesMap inputs_shape = InputShapesMap());
private:
//要暴露接口的tnn內(nèi)部類
std::shared_ptr<TNNImpl> impl_ = nullptr;
};
由于這個類的目的是為了暴露接口暇矫,所以它的四個成員函數(shù)其實對應(yīng)了TNNImpl中的四個函數(shù)槽奕,來看看source/tnn/core下的tnn.cc,
Status TNN::Init(ModelConfig& config) {
//tnn內(nèi)部需要暴露接口的類的實例對象
impl_ = TNNImplManager::GetTNNImpl(config.model_type);
if (!impl_) {
LOGE("Error: not support mode type: %d\n", config.model_type);
return Status(TNNERR_NET_ERR, "not support mode type");
}
//對應(yīng)實例對象初始化函數(shù)囱持,解析模型文件
return impl_->Init(config);
}
//對應(yīng)釋放模型解析器
Status TNN::DeInit() {
impl_ = nullptr;
return TNN_OK;
}
Status TNN::AddOutput(const std::string& layer_name, int output_index) {
// todo for output index
if (!impl_) {
LOGE("Error: impl_ is nil\n");
return Status(TNNERR_NET_ERR, "tnn impl_ is nil");
}
//對應(yīng)增加輸出節(jié)點函數(shù)
return impl_->AddOutput(layer_name, output_index);
}
std::shared_ptr<Instance> TNN::CreateInst(NetworkConfig& config, Status& status, InputShapesMap inputs_shape) {
if (!impl_) {
status = Status(TNNERR_NET_ERR, "tnn impl_ is nil");
return nullptr;
}
//對應(yīng)創(chuàng)建網(wǎng)絡(luò)執(zhí)行器Instance掩幢,返回Instance對象
return impl_->CreateInst(config, status, inputs_shape);
}