Paddle paddle.fluid.core.EnforceNotMet: holder_ should not be null的问题

pexxcrt2  于 2021-11-29  发布在  Java
关注(0)|答案(10)|浏览(304)
def __padding_lstm_rnn(
            self,
            input_embedding,
            num_layers,
            num_steps,
            hidden_size,
            initializer,
            scope,
            init_hidden,
            init_cell,
            dropout=None):
        weight_1_arr = []
        bias_arr = []
        hidden_array = []
        cell_array = []

        for i in range(num_layers):
            weight_1 = layers.create_parameter([hidden_size + input_embedding.shape[-1], hidden_size * 4], dtype="float32",
                                               name=scope + "/fc_weight1_" + str(i),
                                               default_initializer=initializer)
            weight_1_arr.append(weight_1)
            bias_1 = layers.create_parameter(
                [hidden_size * 4],
                dtype="float32",
                name=scope + "/fc_bias1_" + str(i),
                default_initializer=fluid.initializer.Constant(0.0))
            bias_arr.append(bias_1)

            pre_hidden = layers.slice(
                init_hidden, axes=[0], starts=[i], ends=[i + 1])
            pre_cell = layers.slice(
                init_cell, axes=[0], starts=[i], ends=[i + 1])
            pre_hidden = layers.reshape(pre_hidden, shape=[-1, hidden_size])
            pre_cell = layers.reshape(pre_cell, shape=[-1, hidden_size])
            hidden_array.append(pre_hidden)
            cell_array.append(pre_cell)

        input_embedding = layers.transpose(input_embedding, perm=[1, 0, 2])
        rnn = StaticRNN(name=scope)

        with rnn.step():
            input = rnn.step_input(input_embedding)

            for k in range(num_layers):
                pre_hidden = rnn.memory(init=hidden_array[k])
                pre_cell = rnn.memory(init=cell_array[k])
                weight_1 = weight_1_arr[k]
                bias = bias_arr[k]

                nn = layers.concat([input, pre_hidden], 1)
                gate_input = layers.matmul(x=nn, y=weight_1)

                gate_input = layers.elementwise_add(gate_input, bias)
                # i, j, f, o = layers.split(gate_input, num_or_sections=4, dim=-1)
                i = layers.slice(
                    gate_input, axes=[1], starts=[0], ends=[hidden_size])
                j = layers.slice(
                    gate_input,
                    axes=[1],
                    starts=[hidden_size],
                    ends=[hidden_size * 2])
                f = layers.slice(
                    gate_input,
                    axes=[1],
                    starts=[hidden_size * 2],
                    ends=[hidden_size * 3])
                o = layers.slice(
                    gate_input,
                    axes=[1],
                    starts=[hidden_size * 3],
                    ends=[hidden_size * 4])

                c = pre_cell * layers.sigmoid(f) + layers.sigmoid(
                    i) * layers.tanh(j)
                m = layers.tanh(c) * layers.sigmoid(o)

                rnn.update_memory(pre_hidden, m)
                rnn.update_memory(pre_cell, c)

                rnn.step_output(m)
                rnn.step_output(c)

                input = m

                if dropout != None and dropout > 0.0:
                    input = layers.dropout(
                        input,
                        dropout_prob=dropout,
                        dropout_implementation='upscale_in_train')

            rnn.step_output(input)
        # real_res = layers.concat(res, 0)
        rnnout = rnn()
        last_hidden_array = []
        last_cell_array = []
        real_res = rnnout[-1]
        for i in range(num_layers):
            m = rnnout[i * 2]
            c = rnnout[i * 2 + 1]
            m.stop_gradient = True
            c.stop_gradient = True
            last_h = layers.slice(
                m, axes=[0], starts=[num_steps - 1], ends=[num_steps])
            last_hidden_array.append(last_h)
            last_c = layers.slice(
                c, axes=[0], starts=[num_steps - 1], ends=[num_steps])
            last_cell_array.append(last_c)

        real_res = layers.transpose(x=real_res, perm=[1, 0, 2])
        last_hidden = layers.concat(last_hidden_array, 0)
        last_cell = layers.concat(last_cell_array, 0)
        return real_res, last_hidden, last_cell

我总共调用了这个函数三次,只有第一次可以使用layers.Print()查看最终结果,到了第二次的时候,出现paddle.fluid.core.EnforceNotMet: holder_ should not be null的错误

0sgqnhkj

0sgqnhkj1#

并且当我编辑完问题之后马上又重新启动了一遍程序,这次的报错是

paddle.fluid.core.EnforceNotMet: Enforce failed. Expected out_dims[j] == ins[i][j], but received out_dims[j]:0 != ins[i][j]:128.
Input tensors should have the same elements except the specify axis. at [/home/teamcity/work/10934dbd7f8bff0e/paddle/fluid/operators/concat_op.cc:52]
PaddlePaddle Call Stacks: 
0          0x12f291be9p paddle::platform::EnforceNotMet::EnforceNotMet(std::exception_ptr, char const*, int) + 697
1          0x12f6a6b82p paddle::operators::ConcatOp::InferShape(paddle::framework::InferShapeContext*) const + 3266
2          0x1302b38b8p paddle::framework::OperatorWithKernel::RunImpl(paddle::framework::Scope const&, boost::variant<paddle::platform::CUDAPlace, paddle::platform::CPUPlace, paddle::platform::CUDAPinnedPlace, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_> const&) const + 88
3          0x1302af60cp paddle::framework::OperatorBase::Run(paddle::framework::Scope const&, boost::variant<paddle::platform::CUDAPlace, paddle::platform::CPUPlace, paddle::platform::CUDAPinnedPlace, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_> const&) + 524
4          0x12f35de01p paddle::framework::Executor::RunPreparedContext(paddle::framework::ExecutorPrepareContext*, paddle::framework::Scope*, bool, bool, bool) + 657
5          0x12f35d663p paddle::framework::Executor::Run(paddle::framework::ProgramDesc const&, paddle::framework::Scope*, int, bool, bool) + 163
6          0x12f88450ep paddle::operators::RecurrentOp::RunImpl(paddle::framework::Scope const&, boost::variant<paddle::platform::CUDAPlace, paddle::platform::CPUPlace, paddle::platform::CUDAPinnedPlace, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_> const&) const + 1198
7          0x1302af60cp paddle::framework::OperatorBase::Run(paddle::framework::Scope const&, boost::variant<paddle::platform::CUDAPlace, paddle::platform::CPUPlace, paddle::platform::CUDAPinnedPlace, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_> const&) + 524
8          0x12f35de01p paddle::framework::Executor::RunPreparedContext(paddle::framework::ExecutorPrepareContext*, paddle::framework::Scope*, bool, bool, bool) + 657
9          0x12f35d663p paddle::framework::Executor::Run(paddle::framework::ProgramDesc const&, paddle::framework::Scope*, int, bool, bool) + 163
10         0x12f2c31d4p void pybind11::cpp_function::initialize<paddle::pybind::pybind11_init_core(pybind11::module&)::$_64, void, paddle::framework::Executor&, paddle::framework::ProgramDesc const&, paddle::framework::Scope*, int, bool, bool, pybind11::name, pybind11::is_method, pybind11::sibling>(paddle::pybind::pybind11_init_core(pybind11::module&)::$_64&&, void (*)(paddle::framework::Executor&, paddle::framework::ProgramDesc const&, paddle::framework::Scope*, int, bool, bool), pybind11::name const&, pybind11::is_method const&, pybind11::sibling const&)::'lambda'(pybind11::detail::function_call&)::__invoke(pybind11::detail::function_call&) + 244
11         0x12f284f87p pybind11::cpp_function::dispatcher(_object*, _object*, _object*) + 3399
12         0x1007a3e5cp PyCFunction_Call + 60
13         0x1008115edp PyEval_EvalFrameEx + 23213
14         0x1008150c3p _PyEval_EvalCodeWithName + 1779
15         0x1008159aep fast_function + 334
16         0x100811434p PyEval_EvalFrameEx + 22772
17         0x1008150c3p _PyEval_EvalCodeWithName + 1779
18         0x1008159aep fast_function + 334
19         0x100811434p PyEval_EvalFrameEx + 22772
20         0x1008150c3p _PyEval_EvalCodeWithName + 1779
21         0x10080bac1p PyEval_EvalCode + 81
22         0x10083a937p PyRun_FileExFlags + 215
23         0x10083a0eap PyRun_SimpleFileExFlags + 842
24         0x100850c5bp Py_Main + 3355
25         0x100000dc7p main + 215
26         0x100000ce4p start + 52
27                 0x2p

感觉有点奇怪,是什么原因造成的呢

n53p2ov0

n53p2ov02#

@dianhuasici 看log Input tensors should have the same elements except the specify axis 说的是concat时输入的维度不不匹配,你的代码中有三处concat,能先定位到是哪一个concat有问题么?谢谢。

7fhtutme

7fhtutme3#

@hjchen2 我是这么调用的

init_hidden = fluid.layers.reshape(self.init_hidden, shape=[1, -1, num_units])
            init_cell = fluid.layers.reshape(self.init_cell, shape=[1, -1, num_units])

            cell_fw, h_fw, c_fw = self.__padding_lstm_rnn(
                input_embedding=self.encode_emb,
                num_layers=1,
                hidden_size=num_units,
                num_steps=self.max_length_sen,
                initializer=self.initializer,
                scope="encode_fw",
                init_hidden=init_hidden,
                init_cell=init_cell
            )

            fluid.layers.Print(input=cell_fw, message="cell_fw", summarize=10)

            cell_bw, h_bw, c_bw = self.__padding_lstm_rnn(
                input_embedding=layers.reverse(x=self.encode_emb, axis=1),
                num_layers=1,
                hidden_size=num_units,
                num_steps=self.max_length_sen,
                initializer=self.initializer,
                scope="encode_bw",
                init_hidden=init_hidden,
                init_cell=init_cell
            )
            cell_bw = Ops.reverse(cell_bw, axis=1)
            fluid.layers.Print(input=cell_bw, message="cell_bw", summarize=10)

其中第一处layers.Print("cell_fw")是可以输出结果的,但是相同的操作和相同的参数(除了第二个rnn的input我做了一下reverse),第二个layers.Print("cell_bw")就无法输出,并且报错了

gijlo24d

gijlo24d4#

并且我是根据官方的modelhttps://github.com/PaddlePaddle/models/blob/develop/fluid/PaddleNLP/language_model/lstm/lm_model.py这里面的代码做了一些小改动,基本上没有很大的变化,所以我感觉这个函数的内部应该concat不会出错,是不是我是用的时候哪里不太对呢 @hjchen2

wvmv3b1j

wvmv3b1j5#

问题找到了,当我使用图中被注解的layers.Print(input, message=scope + "/input", summarize=0)时,会报paddle.fluid.core.EnforceNotMet: holder_ should not be null,如果注解掉这句话,那在nn = layers.concat([input, pre_hidden], 1)这句就会concat出错,所以我是不是应该在reverse之后同样把lodlevel设置为1呢 @hjchen2

sf6xfgos

sf6xfgos6#

reverse应该是不会用到lod的,你对input的self.encode_emb做reverse,axis=1是不是有问题,应该是axis=0?

dphi5xsq

dphi5xsq7#

所以现在的问题就是,我把同样的输入做一个reverse之后,

with rnn.step():
            input = rnn.step_input(input_embedding)

我打印了一下这个其中的input,然后发现其实是空的(这时调用layers.Print报错),所以下面的concat中使用了input,会报维度的错误,因为这时候input的维度是0 @hjchen2

a1o7rhls

a1o7rhls8#

事实上,我现在只是把原来的输入(input_embedding)没做改变又重新输入再次调用函数,效果仍然是一样的。。这个input还是空的

nzkunb0c

nzkunb0c9#

@dianhuasici 能把第一次调用后的self.encode_emb shape打印一下么,并跟调用之前比较一下。我比较怀疑是调用后self.encode_emb发生了改变。

0yg35tkg

0yg35tkg10#

搞清楚了,是由于https://github.com/PaddlePaddle/Paddle/issues/15446这个问题导致的 @hjchen2 ,我猜是由于变量名字相同,在step_input的时候无法create_param

相关问题