以下为鄙人读源码时做的一些粗略记录,如有不当望请多多指教(抱拳)
一、源码解读(主要为多线程执行过程)。从C++ 模型声明tflite::Interpreter类型做入口
1、tensorflow\lite\interpreter.cc
void Interpreter::SetNumThreads(int num_threads) {
for (auto& subgraph : subgraphs_) {
subgraph->context()->recommended_num_threads = num_threads;
}
for (int i = 0; i < kTfLiteMaxExternalContexts; ++i) {
auto* c = external_contexts_[i];
if (c && c->Refresh) {
c->Refresh(context_);
}
}
}
(a)、void SetNumThreads(int num_threads) ; 线程数设置 最新github上改了函数名字,改成void cpus(int num_threads);
(b)、顺着函数里面subgraph找到, tensorflow\lite\core\subgraph.h 里面的class Subgraph{} 以及里面的成员TfLiteContext* context()。
(c)、TfLiteContext 在tensorflow\lite\c\c_api_internal.h 里面定义typedef struct TfLiteContext {} 以及成员变量recommended_num_threads 。
2、根据recommended_num_threads 找到tensorflow\lite\kernels\cpu_backend_context.cc
CpuBackendContext* CpuBackendContext::GetFromContext(TfLiteContext* context) {
auto* external_context = static_cast<ExternalCpuBackendContext*>(
context->GetExternalContext(context, kTfLiteCpuBackendContext));
if (external_context == nullptr) {
TF_LITE_FATAL(
"ExternalCpuBackendContext isn't properly initialized during TFLite "
"interpreter initialization.");
}
auto* cpu_backend_context = static_cast<CpuBackendContext*>(
external_context->internal_backend_context());