SWCompiler开发笔记

日志

关于导入模型时命名的问题

对于lenet mnist的例子,网络比较小,简单地以每个op的output作为node的name,以及name+”_out”作为其输出Tensor没有出问题。
然而,对于比价复杂的Resnet50网络,则带来了如下的问题。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
op {
input: "gpu_0/res3_2_branch2c"
input: "gpu_0/res3_2_branch2c_bn_s"
input: "gpu_0/res3_2_branch2c_bn_b"
input: "gpu_0/res3_2_branch2c_bn_rm"
input: "gpu_0/res3_2_branch2c_bn_riv"
output: "gpu_0/res3_2_branch2c_bn"
name: ""
type: "SpatialBN"
arg {
name: "epsilon"
f: 1.00000006569e-05
}
arg {
name: "is_test"
i: 1
}
arg {
name: "order"
s: "NCHW"
}
device_option {
}
engine: ""
}
op {
input: "gpu_0/res3_2_branch2c_bn"
input: "gpu_0/res3_1_branch2c_bn"
output: "gpu_0/res3_2_branch2c_bn"
name: ""
type: "Sum"
device_option {
}
engine: ""
}
op {
input: "gpu_0/res3_2_branch2c_bn"
output: "gpu_0/res3_2_branch2c_bn"
name: ""
type: "Relu"
device_option {
}
engine: ""
}

  1. 以Sum节点为例,其输出是输入之一。input(0)和output(0)对应的不同的TensorNode(且名字也应该不同,因为dotGen时要用到);但这俩个TensorNode最好指向同一个Tensor(否则还要做优化,比如这里Sum操作的原义是将B加到A上,而最好不要C=A+B)。
  2. 以Relu节点为例,其输入gpu_0/res3_2_branch2c_bn是Sum的输出,也是BN的输出。但是其在拓扑上应该与Relu节点连接。
  3. 为了避免极可能的数组访问越界,摒弃直接index访问元素,转而封装成函数,内部可以报错或者是创建不存在的节点。

去除tempalte时一系列duplicate symbol错误

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
[100%] Linking CXX executable testMLP
duplicate symbol __ZNK3swc10TensorNode8toStringEv in:
CMakeFiles/testMLP.dir/test/testMLP.cpp.o
libgraphcore.a(IRGraph.cpp.o)
duplicate symbol __ZNK3swc6OpNode8toStringEv in:
CMakeFiles/testMLP.dir/test/testMLP.cpp.o
libgraphcore.a(IRGraph.cpp.o)
duplicate symbol __ZNK3swc10TensorNode5cloneEv in:
CMakeFiles/testMLP.dir/test/testMLP.cpp.o
libgraphcore.a(IRGraph.cpp.o)
duplicate symbol __ZNK3swc6OpNode5cloneEv in:
CMakeFiles/testMLP.dir/test/testMLP.cpp.o
libgraphcore.a(IRGraph.cpp.o)
duplicate symbol __ZTVN3swc10TensorNodeE in:
CMakeFiles/testMLP.dir/test/testMLP.cpp.o
libgraphcore.a(IRGraph.cpp.o)
duplicate symbol __ZTSN3swc10TensorNodeE in:
CMakeFiles/testMLP.dir/test/testMLP.cpp.o
libgraphcore.a(IRGraph.cpp.o)

开始莫名其妙,细看后意识到时特定的函数,它们定义在类之外。即
std::string TensorNode::toStringEv() const {}

  1. 在使用模板类时,这些成员函数定义在.h文件是没有问题的,因为在.cpp中才是真正的实例化。
  2. 去掉模板后,这些定义在类结构外的成员函数,在头文件被多个其他文件包含时,函数定义也会include进去,从而造成duplica问题。
    解决办法:要么将所有成员函数定义在类结构体中,要么定义在.cpp中。