allennlp
allennlp copied to clipboard
LstmCellWithProjection can't be wrapped with PytorchSeq2seqWrapper
Steps to Reproduce
>>> import torch
>>> from allennlp.modules.lstm_cell_with_projection import LstmCellWithProjection
>>> from allennlp.modules.seq2seq_encoders import PytorchSeq2SeqWrapper
>>>
>>> input = torch.Tensor([
... [2, 4, 3, 1],
... [2, 3, 1, 0]
... ]).long() # shape: (batch_size=2, max_seq_len=4)
>>> input_mask = (input != 0) # assume padding_index = 0
>>>
>>> embedder = torch.nn.Embedding(5, 10)
>>> lstm_cell = LstmCellWithProjection(input_size=10, hidden_size=4, cell_size=4)
>>> wrapper = PytorchSeq2SeqWrapper(lstm_cell)
>>>
>>> embedded = embed(input)
>>> wrapper(embedded, input_mask)
This is the error I get:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/kd/miniconda3/envs/allennlp/lib/python3.6/site-packages/torch/nn/modules/module.py", line 489, in __call__
result = self.forward(*input, **kwargs)
File "/home/kd/miniconda3/envs/allennlp/lib/python3.6/site-packages/allennlp/modules/seq2seq_encoders/pytorch_seq2seq_wrapper.py", line 83, in forward
self.sort_and_run_forward(self._module, inputs, mask, hidden_state)
File "/home/kd/miniconda3/envs/allennlp/lib/python3.6/site-packages/allennlp/modules/encoder_base.py", line 116, in sort_and_run_forward
module_output, final_states = module(packed_sequence_input, initial_states)
File "/home/kd/miniconda3/envs/allennlp/lib/python3.6/site-packages/torch/nn/modules/module.py", line 489, in __call__
result = self.forward(*input, **kwargs)
File "/home/kd/miniconda3/envs/allennlp/lib/python3.6/site-packages/allennlp/modules/lstm_cell_with_projection.py", line 122, in forward
batch_size = inputs.size()[0]
AttributeError: 'PackedSequence' object has no attribute 'size'
Expected behavior
The inline comments in LstmCellWithProjection.__init__() state that it should be fit to be wrapped by a PytorchSeq2SeqWrapper. I am not sure which one of the two options is feasible/sensible — removing those inline comments, or introducing support as described.
System (please complete the following information):
- OS: Linux (17.10)
- Python version: 3.6.7
- AllenNLP version: 0.8.1
- PyTorch version: 1.0.0
I think introducing support (by enabling forward to take PackedSequence input) would be the right fix here.
I'll be happy to take this up after 23 January if it still remains open. If someone is willing to take it up before that, feel free to.