Jit 报错TracingCheckError:ERROR: Graphs differed across invocations!
问题描述
使用Tinynn将Pytorch转化为tflite时报错:
发生异常: TracingCheckError (note: full exception trace is shown but execution is paused at: _run_module_as_main)
Tracing failed sanity checks!
ERROR: Graphs differed across invocations!
Graph diff:
graph(%self.1 : __torch__.model.Musicnn_NPU,
%x.1 : Tensor):
%dense2 : __torch__.torch.nn.modules.linear.Linear = prim::GetAttr[name="dense2"](%self.1)
.............
First diverging operator:
Node diff:
- %dense2 : __torch__.torch.nn.modules.linear.___torch_mangle_64.Linear = prim::GetAttr[name="dense2"](%self.1)
? ^
+ %dense2 : __torch__.torch.nn.modules.linear.___torch_mangle_143.Linear = prim::GetAttr[name="dense2"](%self.1)
?
这个错误是指追踪(Tracing)过程中的健全性检查(sanity checks)失败。
ERROR: Graphs differed across invocations!:错误表明在不同的调用(invocations)中,计算图(Graphs)不一致。
First diverging operator::指出了导致计算图出现差异的第一个操作符(operator)。
原因以及解决方法
模型中包含随机操作(如dropout或随机初始化),每次执行时可能会产生不同的结果,导致计算图不一致,如我使用的模型:
out = self.relu(self.bn(self.dense1(out)))
out = self.dropout(out)
out = self.dense2(out)
# out = nn.Sigmoid()(out)
# [ruihai.jing]module changed cause of quantized error--start
out = nn.functional.sigmoid(out)
第一个报错的层在self.dense2(out),因为上面有dropout操作,把这个操作注释掉就可以。