The error is not pointing to a device mismatch, but to invalid indices.
Make sure that all index tensors used in obj_mask[b, best_n, gj, gi] contain valid values for the shape of obj_mask.
You could e.g. print the min. and max. values of them and compare it to the the obj_mask.shape.