[PaddlePaddle/Paddle]在导出模型的时候paddle.nn.functional.pad报错

2024-03-22 168 views
7
请提出你的问题 Please ask your question
  • PaddlePaddle 2.4.1
  • Python 3.7
  • Ubuntu or Windows

错误如下:

   File "E:\PyCharm\PPASR\ppasr\model_utils\efficient_conformer\encoder.py", line 341, in forward_chunk
    for i, layer in enumerate(self.encoders):
    File "E:\PyCharm\PPASR\ppasr\model_utils\efficient_conformer\encoder.py", line 347, in forward_chunk
    xs, _, new_att_cache, new_cnn_cache = layer(
    File "E:\PyCharm\PPASR\ppasr\model_utils\conformer\encoder.py", line 394, in forward
    x_att, new_att_cache = self.self_attn(x, x, x, mask, pos_emb, cache=att_cache)
    File "E:\PyCharm\PPASR\ppasr\model_utils\efficient_conformer\attention.py", line 176, in forward
    q, k, v, p, mask, padding_q = self.pad4group(q, k, v, p, mask, self.group_size)
    File "E:\PyCharm\PPASR\ppasr\model_utils\efficient_conformer\attention.py", line 59, in pad4group
        # Input Padding (B, T, D) -> (B, T + P, D)
        print(Q.shape)
        Q = F.pad(Q, (0, 0, 0, padding_Q), mode='constant', value=0.0)
        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ <--- HERE
        K = F.pad(K, (0, 0, 0, padding_KV), mode='constant', value=0.0)
        V = F.pad(V, (0, 0, 0, padding_KV), mode='constant', value=0.0)

    File "D:\Anaconda3\envs\PaddlePaddle\lib\site-packages\paddle\nn\functional\common.py", line 1724, in pad
    helper.append_op(
    File "D:\Anaconda3\envs\PaddlePaddle\lib\site-packages\paddle\fluid\layer_helper.py", line 45, in append_op
    return self.main_program.current_block().append_op(*args, **kwargs)
    File "D:\Anaconda3\envs\PaddlePaddle\lib\site-packages\paddle\fluid\framework.py", line 4017, in append_op
    op = Operator(
    File "D:\Anaconda3\envs\PaddlePaddle\lib\site-packages\paddle\fluid\framework.py", line 2991, in __init__
    self._update_desc_attr(attr_name, attr_val)
    File "D:\Anaconda3\envs\PaddlePaddle\lib\site-packages\paddle\fluid\framework.py", line 3339, in _update_desc_attr
    self._update_desc_plain_attr(name, val)
    File "D:\Anaconda3\envs\PaddlePaddle\lib\site-packages\paddle\fluid\framework.py", line 3363, in _update_desc_plain_attr
    desc._set_int32s_attr(name, val)

    TypeError: _set_int32s_attr(): incompatible function arguments. The following argument types are supported:
    1. (self: paddle.fluid.libpaddle.OpDesc, arg0: str, arg1: List[int]) -> None

Invoked with: <paddle.fluid.libpaddle.OpDesc object at 0x0000028BC900D7B0>, 'paddings', [0, 0, 0, var tmp_36 : LOD_TENSOR.shape(1,).dtype(int32).stop_gradient(False), 0, 0]

回答

2

你好,能提供下最小复现demo吗?

1

你好,请提供下最小复现demo,以便定位paddle问题。

0

这是我想到最简单的方式了。如果要看看代码片段

    def pad4group(self, Q, K, V, P, mask, group_size: int = 3):
        """
        q: (#batch, time1, size) -> (#batch, head, time1, size/head)
        k,v: (#batch, time2, size) -> (#batch, head, time2, size/head)
        p: (#batch, time2, size)
        """
        # Compute Overflows
        overflow_Q = Q.shape[2] % group_size
        overflow_KV = K.shape[2] % group_size

        padding_Q = (group_size - overflow_Q) * int(overflow_Q // (overflow_Q + 0.00000000000000001))
        padding_KV = (group_size - overflow_KV) * int(overflow_KV // (overflow_KV + 0.00000000000000001))

        batch_size, _, seq_len_KV, _ = K.shape

        # Input Padding (B, T, D) -> (B, T + P, D)
        Q = F.pad(Q, (0, 0, 0, padding_Q), mode='constant', value=0.0)
        K = F.pad(K, (0, 0, 0, padding_KV), mode='constant', value=0.0)
        V = F.pad(V, (0, 0, 0, padding_KV), mode='constant', value=0.0)

        if mask is not None and mask.shape[2] > 0:  # time2 > 0:
            mask = mask[:, ::group_size, ::group_size]

        Q = Q.transpose([0, 2, 1, 3]).reshape([batch_size, -1, self.h, self.d_k * group_size]).transpose([0, 2, 1, 3])
        K = K.transpose([0, 2, 1, 3]).reshape([batch_size, -1, self.h, self.d_k * group_size]).transpose([0, 2, 1, 3])
        V = V.transpose([0, 2, 1, 3]).reshape([batch_size, -1, self.h, self.d_k * group_size]).transpose([0, 2, 1, 3])

        # process pos_emb
        P_batch_size = P.shape[0]
        overflow_P = P.shape[1] % group_size
        padding_P = group_size - overflow_P if overflow_P else 0
        P = F.pad(P, (0, 0, 0, padding_P, 0, 0), mode='constant', value=0.0, data_format='NLC')
        P = P.reshape([P_batch_size, -1, self.h, self.d_k * group_size]).transpose([0, 2, 1, 3])

        return Q, K, V, P, mask, padding_Q
5

根据提供的信息,建议通过以下方法排查: 首先,print 打印中间变量,模拟问题代码,比如可能是这样,

p = paddle.rand([...])
F.pad(x)

以便确认是 F.pad 的问题,当然这个过程需要确认参数是否符合预期。

另外,建议使用 api 来使用,比如 paddle.nn.Pad1D, paddle.nn.Pad2D, paddle.nn.Pad3D,尽量不使用 functinoal。

2

@kuizhiqing 好的,谢谢

F.pad(Q, (0, 0, 0, padding_Q), mode='constant', value=0.0)F.pad(P, (0, 0, 0, padding_P, 0, 0), mode='constant', value=0.0, data_format='NLC')如果用 paddle.nn.Pad1D, paddle.nn.Pad2D, paddle.nn.Pad3D,该如何使用呢?

3

@kuizhiqing 我找到问题了,因为我的padding是动态的,所以导出的是会报错,这个如何解决?

9

@yeyupiaoling 您好,请问您解决这个问题了吗,我使用swin transformer做cascade rcnn做主干网络时出现了相同的问题,我也不知道该如何解决,如果您有合适的解决方案,可以指导一下我吗?

1

你可以看看PPASR里面做法,太久了,不记得怎么处理的