wasiahmad / NeuralCodeSum

Official implementation of our work, A Transformer-based Approach for Source Code Summarization [ACL 2020].
MIT License
192 stars 79 forks source link

an error with the index_select in beam.py #16

Closed ramin2200 closed 4 years ago

ramin2200 commented 4 years ago

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

wasiahmad commented 4 years ago

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.

ramin2200 commented 4 years ago

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?

wasiahmad commented 4 years ago

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.

ramin2200 commented 4 years ago

Thank you

On Thu, Oct 29, 2020, 1:19 PM Wasi Ahmad notifications@github.com wrote:

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.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/wasiahmad/NeuralCodeSum/issues/16#issuecomment-718997872, or unsubscribe https://github.com/notifications/unsubscribe-auth/ARNICFXL37FYBT6STIROOADSNHE5FANCNFSM4TDJLCSA .

uetestina commented 2 years ago

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)