THUDM/KBRD

RuntimeError: a view of a leaf Variable that requires grad is being used in an in-place operation.

albertomancino opened this issue · 3 comments

When training the dialog part, executing the command ' bash scripts/t2t_rec_rgcn.sh 1 0 ' the following error rises: "RuntimeError: a view of a leaf Variable that requires grad is being used in an in-place operation."

The error raises because of the following statement (in "KBRD/parlai/agents/transformer_rec/modules.py", line 188)

out[:, 0::2] = torch.FloatTensor(np.sin(position_enc)).type_as(out)

Could you please help me face it?

Here I copied the entire error description and its traceback:

Traceback (most recent call last):
File "parlai/tasks/redial/train_transformer_rec.py", line 36, in
TrainLoop(opt).train()
File "/volume/KBRD2/parlai/scripts/train_model.py", line 232, in init
self.agent = create_agent(opt)
File "/volume/KBRD2/parlai/core/agents.py", line 581, in create_agent
model = model_class(opt)
File "/volume/KBRD2/parlai/agents/transformer_rec/transformer_rec.py", line 212, in init
super().init(opt, shared)
File "/volume/KBRD2/parlai/core/torch_generator_agent.py", line 352, in init
self.build_model()
File "/volume/KBRD2/parlai/agents/transformer_rec/transformer_rec.py", line 219, in build_model
self.model = TransformerGeneratorModel(self.opt, self.dict)
File "/volume/KBRD2/parlai/agents/transformer_rec/modules.py", line 555, in init
n_positions=n_positions,
File "/volume/KBRD2/parlai/agents/transformer_rec/modules.py", line 51, in _build_encoder
n_positions=n_positions,
File "/volume/KBRD2/parlai/agents/transformer_rec/modules.py", line 290, in init
n_positions, embedding_size, out=self.position_embeddings.weight
File "/volume/KBRD2/parlai/agents/transformer_rec/modules.py", line 188, in create_position_codes
out[:, 0::2] = torch.FloatTensor(np.sin(position_enc)).type_as(out)
RuntimeError: a view of a leaf Variable that requires grad is being used in an in-place operation.

I'm also getting the same error. Any fixes?

Added with torch.no_grad(): before updating the variables and now seems to be fine :D

Also,
You can fix the error out[:, 0::2] -> out[:, 0::2].data

out[:, 0::2].data = torch.FloatTensor(np.sin(position_enc)).type_as(out)

reference