静态图模式下报错【Should not get item from non-sequence type, obj: CellList】

在MindSpore中,动态图运行正常但静态图编译时出现错误,错误信息指出CellList不能作为非序列类型处理。博客作者在LowRankBilinearDecBlock类中使用CellList创建了一个层列表,但在静态图模式下编译时遇到问题。解决方案包括两种方法:一是避免使用索引直接遍历CellList;二是尝试升级到MindSpore 1.8版本,因为该问题可能已在新版本中得到修复。
摘要由CSDN通过智能技术生成

【功能模块】静态图编译

【操作步骤&问题现象】

平台:昇腾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的包试试。

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值