Is there something wrong with these loops?

I am browsing some code in the ATen lib and came across this:

  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.

The first zeros the counts, the second then counts. If you have the same index multiple times (which is the point of counting when you want to scale by frequency), you cannot combine the two.
My guess for the first loop would be that it is cheaper to only initialize those indices that are actually used rather than all of them en bloc.

Best regards

Thomas