title: Pytorch giving runtimeerror can't be cast to the desired output type Long
body: <p>The following code is giving runtimeerror &quot;result type Float can't be cast to the desired output type Long&quot;.</p>
  <p>I already tried to do the following:</p>
  <p><strong>FROM:</strong>
  <code>torch.div(self.indices_buf, vocab_size, out=self.beams_buf)</code></p>
  <p><strong>TO:</strong>
  <code>torch.div(self.indices_buf, vocab_size, out=self.beams_buf).type_as(torch.LongTensor)</code></p>
  <p>Problematic code:</p>
  <pre><code>class BeamSearch(Search):
  
      def __init__(self, tgt_dict):
          super().__init__(tgt_dict)
  
      def step(self, step, lprobs, scores):
          super()._init_buffers(lprobs)
          bsz, beam_size, vocab_size = lprobs.size()
  
          if step == 0:
              # at the first step all hypotheses are equally likely, so use
              # only the first beam
              lprobs = lprobs[:, ::beam_size, :].contiguous()
          else:
              # make probs contain cumulative scores for each hypothesis
              lprobs.add_(scores[:, :, step - 1].unsqueeze(-1))
  
          torch.topk(
              lprobs.view(bsz, -1),
              k=min(
                  # Take the best 2 x beam_size predictions. We'll choose the first
                  # beam_size of these which don't predict eos to continue with.
                  beam_size * 2,
                  lprobs.view(bsz, -1).size(1) - 1,  # -1 so we never select pad
              ),
              out=(self.scores_buf, self.indices_buf),
          )
          torch.div(self.indices_buf, vocab_size, out=self.beams_buf).type_as(torch.LongTensor)
          self.indices_buf.fmod_(vocab_size)
          return self.scores_buf, self.indices_buf, self.beams_buf
  </code></pre>
  <p>This code is from fairseq.</p>