mxnet icon indicating copy to clipboard operation
mxnet copied to clipboard

How sentence data are batched in Model_parallel_lstm?

Open harouwu opened this issue 10 years ago • 9 comments

Hi,

I am reading the model parallel lstm code. I found that in lstm_ptb.py, function load_data/replicate_data, the code loads the contents and splits them using whitespace. However, after that it reshape the data into a ndarray of size [(num_word)/batchsize, batchsize] using Fortan order.

Does it means that

  • sentences are just marked at its end using without spliting them?
  • words in a sentence is distributed in a column instead of a row? How do you read a sentence in the following code?
  • In the previous lstm, a batch is a collection of sentences. What does a batch mean here?

harouwu avatar Apr 12 '16 01:04 harouwu

And in lstm.py, I found that you copy the row of train_data into seq_data, which has size of (20, 1) and words in that row does not come from the same sentence. Are you using each row of them training?

harouwu avatar Apr 12 '16 01:04 harouwu

If you find it a bug, I would love to fix it. @tqchen

harouwu avatar Apr 12 '16 01:04 harouwu

In @tqchen 's particular LSTM model parallel example, it seems it is using a slightly unconventional way to represent the data. Each row is a time-step in a mini-batch. So each column is sentence (marked with eos for new sentences) -- checkout set_rnn_inputs.

In the other LSTM examples, especially the ones using high-level model API, since the data for each index is fed separately, it does not really matter originally it is in row order or column order. In a recent PR #1833 we are refactoring this by having the convention that a mini-batch of sentence is of the shape (batch-size, seq-len).

pluskid avatar Apr 13 '16 02:04 pluskid

Can you help me clarify whether my understanding is right?

The whole data matrix is of size (num_of_words/batch_size, batch_size). Each column is sentence (marked with eos for new sentences).

In set_rnn_inputs, we read a row of data each time because we are doing model parallel. Because ideally, i th row of data represents the i th words of sentences. This row of words can be fed into an unrolled lstm model. A mini-batch is of the shape (seq-len, batch_size) where seq_len is just a proper upper bound of sentence length(a.k.a maximum unrolled lstm length).

If my understanding is correct, I think I found a little bug here. If you look into function replicate_data in model_parallel_lstm/lstm_ptb.py,

   def replicate_data(x, batch_size):
        nbatch = int(x.shape[0] / batch_size)
        x_cut = x[:nbatch * batch_size]
        data = x_cut.reshape((nbatch, batch_size), order='F')
        return data

It does not take end of sentence into account when slicing data, so the first of each column is probably not the first of a sentence. Besides that, because we are using a mini-batch of the shape (seq-len, batch_size). It is probably better to align sentences in a batch with the start of the batch instead of starting from random position within a sentence.

I think I can fix this by adding padding and some rearrange of data into the code. It will probably reduce the training speed a little bit. But it seems a more correct way to train it.

harouwu avatar Apr 13 '16 22:04 harouwu

If we can rearrange the data in an good aligning way, it will be almost the same w.r.t training speed.

harouwu avatar Apr 13 '16 22:04 harouwu

@harouwu Yes, you are absolutely right. The sentence boundary is not padded in our example of fixed-length LSTM unrolling. In the bucketing example, we use unrolling to multiple-length with padding. Here we simply treat the whole corpus as a long "sentence". As long as you forward the states properly between mini-batches, this is also a valid interpretation.

A good idea is to see if the model parallel example here could be easily incorporated with the bucketing example. My guess is that one might need to modify the executor binding code used in model.py to support the memory allocation in this situation.

pluskid avatar Apr 14 '16 16:04 pluskid

@pluskid If the C++ code will deal with <eos> and feed the next level of model initialized input, e.g. all zeros. It is also a valid implementation. When I was doing my research, I found similar idea called sentence splicing they will concat short sentences to fit into a bucket with long sentences to speed up rnn training.

When you say incorporate the model_parallel one with the bucketing one, do you mean that we can design a good API for users so that they will use a flag to choose whether they want to use bucket or model parallel?

harouwu avatar Apr 14 '16 16:04 harouwu

But in my sense(I have not gone through it), the output of an unrolled level of lstm will not detect <eos> and give next level all zeros/ones input. I will check it myself today and update my finding.

harouwu avatar Apr 14 '16 16:04 harouwu

This issue is closed due to lack of activity in the last 90 days. Feel free to reopen if this is still an active issue. Thanks!

phunterlau avatar Sep 28 '17 05:09 phunterlau