【功能模块】静态图编译
【操作步骤&问题现象】
平台:昇腾Modelarts
镜像:mindspore1.7.0-cann5.1.0-py3.7-euler2.8.3
动态图运行正常,静态图报错。使用CellList,报错为其是非序列类型。可是文档中说,CellList可以像普通Python列表一样使用。
class LowRankBilinearDecBlock(nn.Cell):
def __init__(
self,
embed_dim,
att_type,
att_heads,
att_mid_dim,
att_mid_drop,
dropout,
layer_num
):
super(LowRankBilinearDecBlock, self).__init__()
self.layer_num = layer_num
self.layers = nn.CellList([])
for _ in range(layer_num):
sublayer = LowRankBilinearLayer(
embed_dim = embed_dim,
att_type = att_type,
att_heads = att_heads,
att_mid_dim = att_mid_dim,
att_mid_drop = att_mid_drop,
dropout = dropout
)
self.layers.append(sublayer)
self.proj = nn.Dense(embed_dim * (layer_num + 1), embed_dim)
self.layer_norm = nn.LayerNorm([1024])
self.concat_last = ops.Concat(-1)
self.reducesum = ops.ReduceSum()
self.reducemean = ops.ReduceMean()
def precompute(self, key, value2):
keys = []
value2s = []
# for layer in self.layers:
for i in range(self.layer_num):
layer = self.layers[i]
k, v = layer.precompute(key, value2)
keys.append(k)
value2s.append(v)
return self.concat_last(keys), self.concat_last(value2s)
以下是具体报错信息:
Traceback (most recent call last):
File "main2.py", line 119, in
model.train(epoch=args.epochs, train_dataset=dataset_train, callbacks=cbs, dataset_sink_mode=True)
File "/home/ma-user/anaconda3/envs/MindSpore/lib/python3.7/site-packages/mindspore/train/model.py", line 906, in train
sink_size=sink_size)
File "/home/ma-user/anaconda3/envs/MindSpore/lib/python3.7/site-packages/mindspore/train/model.py", line 87, in wrapper
func(self, *args, **kwargs)
File "/home/ma-user/anaconda3/envs/MindSpore/lib/python3.7/site-packages/mindspore/train/model.py", line 548, in _train
self._train_dataset_sink_process(epoch, train_dataset, list_callback, cb_params, sink_size)
File "/home/ma-user/anaconda3/envs/MindSpore/lib/python3.7/site-packages/mindspore/train/model.py", line 628, in _train_dataset_sink_process
outputs = train_network(*inputs)
File "/home/ma-user/anaconda3/envs/MindSpore/lib/python3.7/site-packages/mindspore/nn/cell.py", line 586, in __call__
out = self.compile_and_run(*args)
File "/home/ma-user/anaconda3/envs/MindSpore/lib/python3.7/site-packages/mindspore/nn/cell.py", line 964, in compile_and_run
self.compile(*inputs)
File "/home/ma-user/anaconda3/envs/MindSpore/lib/python3.7/site-packages/mindspore/nn/cell.py", line 937, in compile
_cell_graph_executor.compile(self, *inputs, phase=self.phase, auto_parallel_mode=self._auto_parallel_mode)
File "/home/ma-user/anaconda3/envs/MindSpore/lib/python3.7/site-packages/mindspore/common/api.py", line 1006, in compile
result = self._graph_executor.compile(obj, args_list, phase, self._use_vm_mode())
RuntimeError: mindspore/ccsrc/pipeline/jit/parse/resolve.cc:367 GetObjectFromSequence] Should not get item from non-sequence type, obj: CellList<
(0): LowRankBilinearLayer<
(encoder_attn): LowRank<
(in_proj_q): SequentialCell<
(0): Dense
(1): ReLU<>
>
(in_proj_k): SequentialCell<
(0): Dense
(1): ReLU<>
>
(in_proj_v1): SequentialCell<
(0): Dense
(1): ReLU<>
>
(in_proj_v2): SequentialCell<
(0): Dense
(1): ReLU<>
>
(attn_net): SCAtt<
(attention_basic): SequentialCell<
(0): Dense
(1): ReLU<>
(2): Dropout
>
(attention_last): Dense
(attention_last2): Dense
>
>
(dropout): Dropout
>
(1): LowRankBilinearLayer<
(encoder_attn): LowRank<
(in_proj_q): SequentialCell<
(0): Dense
(1): ReLU<>
>
(in_proj_k): SequentialCell<
(0): Dense
(1): ReLU<>
>
(in_proj_v1): SequentialCell<
(0): Dense
(1): ReLU<>
>
(in_proj_v2): SequentialCell<
(0): Dense
(1): ReLU<>
>
(attn_net): SCAtt<
(attention_basic): SequentialCell<
(0): Dense
(1): ReLU<>
(2): Dropout
>
(attention_last): Dense
(attention_last2): Dense
>
>
(dropout): Dropout
>
(2): LowRankBilinearLayer<
(encoder_attn): LowRank<
(in_proj_q): SequentialCell<
(0): Dense
(1): ReLU<>
>
(in_proj_k): SequentialCell<
(0): Dense
(1): ReLU<>
>
(in_proj_v1): SequentialCell<
(0): Dense
(1): ReLU<>
>
(in_proj_v2): SequentialCell<
(0): Dense
(1): ReLU<>
>
(attn_net): SCAtt<
(attention_basic): SequentialCell<
(0): Dense
(1): ReLU<>
(2): Dropout
>
(attention_last): Dense
(attention_last2): Dense
>
>
(dropout): Dropout
>
>
# In file /home/ma-user/work/acn_mindspore/models_ms/capsule_lowrank_bilinear_block.py(252)
layer = self.layers[i]
****************************************************解答*****************************************************
可以尝试两种方法:
方法一:不用index的方式,如下:
方法二:CellList问题在1.8上被修复了,可以用mindspore1.8的包试试。