wasiahmad/NeuralCodeSum

an error with the index_select in beam.py

ramin2200 opened this issue · 5 comments

Hi,

when I run your model, I run into the following error. I would really appreciate it if you could let me know how I can fix this error.

Traceback (most recent call last):
File "../../main/test.py", line 474, in
main(args)
File "../../main/test.py", line 436, in main
validate_official(args, dev_loader, model)
File "../../main/test.py", line 280, in validate_official
ret = translator.translate_batch(batch_inputs)
File "/scratch/ex-fhendija-1/ramin/Baseline/NeuralCodeSum/c2nl/translator/translator.py", line 241, in translate_batch
b.advance(out[:, j],
File "/scratch/ex-fhendija-1/ramin/Baseline/NeuralCodeSum/c2nl/translator/beam.py", line 134, in advance
self.attn.append(attn_out.index_select(0, prev_k))
RuntimeError: index_select(): Expected dtype int64 for index

The code is error-free to the best of my knowledge, I don't know why did you get this error. Did you make any changes? By the way, which PyTorch version are you using?

Also, the error message is pretty straight forward. The parameter to index_select, prev_k must be of type torch.long. You can check the data type and see why this is happening.

To help you, I am pasting the following 3 lines that are associated with prev_k. [please check them]

num_words = word_probs.size(1)
best_scores, best_scores_id = flat_beam_scores.topk(self.size, 0, True, True)
prev_k = best_scores_id / num_words

You should check out the type of best_scores_id. As you can see topk method returns (Tensor, LongTensor). So, best_scores_id should be a LongTensor which is correct.

Thank you very much for your quick reply.
I was thinking it might be because of pytorch version, I am using pytorch 1.7, do you think it might be the reason?

Sorry, I can't say but I guess it shouldn't be because of PyTorch. You can debug the code and see why it happens.

Not sure if still relevant but I found that using the Python 3.6 and Torch 1.5.0 it worked correctly while older version (<=1.4.0) or newer version (>= 1.7.1) of Torch this is not supported. In particular the issue is due to try to index based on non int indexes (e.g. prev_k = tensor([0.9965, 0.5468, 0.0970, 0.8939]) instead of being tensor([0, 2, 1, 10] for example).

Also noticed the error code change based on different version of python

errors on 3.7 to 3.10

File ".\main\test.py", line 474, in
main(args)
File ".\main\test.py", line 436, in main
validate_official(args, dev_loader, model)
File ".\main\test.py", line 280, in validate_official
ret = translator.translate_batch(batch_inputs)
File "c:\synologydrive\mortice\dev\code\neuralcodesum\c2nl\translator\translator.py", line 241, in translate_batch
b.advance(out[:, j],
File "c:\synologydrive\mortice\dev\code\neuralcodesum\c2nl\translator\beam.py", line 134, in advance
self.attn.append(attn_out.index_select(0, prev_k))
RuntimeError: index_select(): Expected dtype int64 for index

errors on 3.6

Traceback (most recent call last):
File "../main/test.py", line 474, in
main(args)
File "../main/test.py", line 436, in main
validate_official(args, dev_loader, model)
File "../main/test.py", line 280, in validate_official
ret = translator.translate_batch(batch_inputs)
File "..\c2nl\translator\translator.py", line 242, in translate_batch
beam_attn.data[:, j, :memory_lengths[j]])
File "..\c2nl\translator\beam.py", line 134, in advance
self.attn.append(attn_out.index_select(0, prev_k))
RuntimeError: index_select(): Expected dtype int32 or int64 for index)