I am browsing some code in the ATen lib and came across this 3:
  if (scale_grad_by_freq) {
    counts.reset(new int64_t[num_weights]);
    for (int i = 0; i < numel; i++) {
      counts[indices_data[i]] = 0;
    }
    for (int i = 0; i < numel; i++) {
      counts[indices_data[i]]++;
    }
  }

Perhaps I’m missing something, but it looks strange to loop over the indices twice here.