Paddle icon indicating copy to clipboard operation
Paddle copied to clipboard

fluid dynamic_lstm cause a core dump

Open seanxh opened this issue 8 years ago • 2 comments

# coding: utf-8

# In[1]:


import numpy as np
import time

import paddle
import paddle.fluid as fluid
import paddle.fluid.core as core 
import paddle.fluid.framework as framework
from paddle.fluid.executor import Executor

shape = [10,1]

gate_size = 2

data = fluid.layers.data(name='data', shape=shape, dtype='float32')
input_forward_proj = fluid.layers.fc(name='fc1',input=data,
                                             size=gate_size * 4,
                                             act=None,
                                             bias_attr=False)
forward, _ = fluid.layers.dynamic_lstm(
    input=input_forward_proj, size=gate_size * 4)

place = fluid.CPUPlace()
feeder = fluid.DataFeeder(place=place,feed_list=[data])

exe = fluid.Executor(place)
exe.run(fluid.default_startup_program())

tensor_x = np.random.random((1,10,1))
results = exe.run(fluid.default_main_program(),
                          feed=feeder.feed([tensor_x]),
                          fetch_list=[forward])
print tensor_x
print results[0].shape

this script cause a core dump:

*** Aborted at 1526460974 (unix time) try "date -d @1526460974" if you are using GNU date *** PC: @ 0x0 (unknown) *** SIGSEGV (@0xf8) received by PID 10630 (TID 0x7f2a863c7700) from PID 248; stack trace: *** @ 0x7f2a85b7d160 (unknown) @ 0x7f2a069a6fcc paddle::framework::Vector<>::Vector() @ 0x7f2a072cc545 paddle::operators::LSTMKernel<>::Compute() @ 0x7f2a0744a776 paddle::framework::OperatorWithKernel::RunImpl() @ 0x7f2a06985580 paddle::framework::Executor::RunPreparedContext() @ 0x7f2a06985ce4 paddle::framework::Executor::Run() @ 0x7f2a0690e0cb ZZN8pybind1112cpp_function10initializeIZNS0_C1IvN6paddle9framework8ExecutorEJRKNS4_11ProgramDescEPNS4_5ScopeEibbEJNS_4nameENS_9is_methodENS_7siblingEEEEMT0_FT_DpT1_EDpRKT2_EUlPS5_S8_SA_ibbE_vJSO_S8_SA_ibbEJSB_SC_SD_EEEvOSF_PFSE_SH_ESN_ENUlRNS_6detail13function_callEE1_4_FUNESV @ 0x7f2a06907b24 pybind11::cpp_function::dispatcher() @ 0x7f2a85e9855f PyEval_EvalFrameEx @ 0x7f2a85e9a86d PyEval_EvalCodeEx @ 0x7f2a85e979fc PyEval_EvalFrameEx @ 0x7f2a85e9a86d PyEval_EvalCodeEx @ 0x7f2a85e9a9a2 PyEval_EvalCode @ 0x7f2a85ec3782 PyRun_FileExFlags @ 0x7f2a85ec4af9 PyRun_SimpleFileExFlags @ 0x7f2a85eda82d Py_Main @ 0x7f2a850d7bd5 __libc_start_main @ 0x4007a1 (unknown) @ 0x0 (unknown) Segmentation fault (core dumped)

seanxh avatar May 16 '18 08:05 seanxh

You need to set data by LoDTensor in this way:

import numpy as np
import time

import paddle
import paddle.fluid as fluid
import paddle.fluid.core as core 
import paddle.fluid.framework as framework
from paddle.fluid.executor import Executor

def to_lodtensor(data, place):
    seq_lens = [len(seq) for seq in data]
    cur_len = 0
    lod = [cur_len]
    for l in seq_lens:
        cur_len += l
        lod.append(cur_len)
    flattened_data = np.concatenate(data, axis=0).astype("float32")
    res = fluid.LoDTensor()
    res.set(flattened_data, place)
    res.set_lod([lod])
    return res

shape = [10,1]

gate_size = 2

data = fluid.layers.data(name='data', shape=shape, dtype='float32')
input_forward_proj = fluid.layers.fc(name='fc1',input=data,
                                             size=gate_size * 4,
                                             act=None,
                                             bias_attr=False)
forward, _ = fluid.layers.dynamic_lstm(
    input=input_forward_proj, size=gate_size * 4)

place = fluid.CPUPlace()

exe = fluid.Executor(place)
exe.run(fluid.default_startup_program())

tensor_x = [np.random.random((10,1))]
lod_tensor = to_lodtensor([tensor_x], place)
results = exe.run(
          fluid.default_main_program(),
          feed={'data': lod_tensor},
          fetch_list=[forward], return_numpy=False)
print tensor_x
print np.array(results[0])[0].shape

kuke avatar May 16 '18 10:05 kuke

Thx~ @kuke got it.

Because dynamic_lstm supports variable-time length input sequence。So the input shape = [10,1] is every cell's input dimension and 10 is not the time length.

so the to_lodtensor is a function that contains one batch's data with its length.

[[1, 2, 3], [4,5], [6]]and [3,2,1]

my code is like this:

import numpy as np
import time

import paddle
import paddle.fluid as fluid
import paddle.fluid.core as core
import paddle.fluid.framework as framework
from paddle.fluid.executor import Executor

shape = [2]

gate_size = 2

data = fluid.layers.data(name='data', shape=shape, dtype='float32')
input_forward_proj = fluid.layers.fc(name='fc1',input=data,
                                             size=gate_size * 4,
                                             act=None,
                                             bias_attr=False)
forward, state = fluid.layers.dynamic_lstm(
    name='blstm1',
    input=input_forward_proj, size=gate_size * 4,use_peepholes=False)

def to_lodtensor(data, place):
    seq_lens = [len(seq) for seq in data]
    cur_len = 0
    lod = [cur_len]
    for l in seq_lens:
        cur_len += l
        lod.append(cur_len)
    flattened_data = np.concatenate(data, axis=0).astype("float32")
    res = fluid.LoDTensor()
    res.set(flattened_data, place)
    res.set_lod([lod])
    return res

place = fluid.CPUPlace()
feeder = fluid.DataFeeder(place=place,feed_list=[data])

exe = fluid.Executor(place)
exe.run(fluid.default_startup_program())

tensor_x = [np.random.random((10,2))]
lod_tensor = to_lodtensor(tensor_x, place)

results = exe.run(fluid.default_main_program(),
                          feed={'data':lod_tensor},
                          fetch_list=[forward],return_numpy=False)

print np.array(results[0]).shape

the data's time-length is specified like this tensor_x = [np.random.random((10,2))]。n-dim=2, time-length=10.

seanxh avatar May 16 '18 11:05 seanxh