编译流程将Nnet和ComputationRequest作为输入,输出NnetComputation。ComputationRequest包含对请求的输出索引、可用的输入索引的表示。
创建计算图
ComputationGraph的详情
ComputationGraph将Cindexes映射到cindex_ids以及将cindex_ids映射到Cindexes。
ComputationGraph类的定义如下:
struct ComputationGraph { // The mapping of cindex_id to Cindex. std::vector<Cindex> cindexes;
// For each Cindex this tells us whether it was provided as an input to the // computation. std::vector<bool> is_input;
// dependencies[cindex_id] gives you the list of other cindex_ids that this // particular cindex_id directly depends on to compute it. std::vector<std::vector<int32> > dependencies; private: // Maps each Cindex to an integer cindex_id: reverse mapping of "cindexes". // Must be accessed via the GetCindexId() function. unordered_map<Cindex, int32, CindexHasher> cindex_to_cindex_id_; }; |
除了上述映射之外,ComputationGraph还保存了每个cindex_id在计算时需要的依赖。
在计算初期,Descriptor::GetDependencies()返回所有的cindex_ids,即每个cindex_id依赖与其他所有的cindex_ids。
之后这些依赖根据实际计算时的需求进行修剪。
构建ComputationGraph
介绍
ComputationGraphBuilder用于构建ComputationGraph。对于一个最简单的例子,从网络请求的输出开始,并沿着网络向前计算其依赖,并添加到ComputationGraph中,直至计算到输入结点。
判断ComputationRequest中是否包含了所依赖的输入结点,如果不足,则无法进行计算。(在841研究所实习时,为现有DNN添加一输出层后,脚本得出无法计算的结论,就是依赖于此)。
基础算法
本算法不实际使用。
构建计算图时,需要使用如下算法来确定每个Cindex是否可以从提供的输入计算得到:
- 调用Descriptor::GetDependencies()得到输出层处的所有依赖项;
- 调用IsComputable()确定输入中哪些Cindexes可以用于计算,对实际不参与计算的依赖关系进行修剪
- 检查所有请求的输出都是可计算的
- 修脚掉所有不需要参与计算的cindex_ids
将计算组织为多步
根据拓扑顺序对Cindex排序并分组,使同组Cindexes可同时计算。
未完待续