I have run your code and it actually works on my cluster. It allows to perfectly balance between the two classes.
Output:
target train 0/1: 990/10
batch index 0, 0/1: 48/52
batch index 1, 0/1: 49/51
batch index 2, 0/1: 48/52
batch index 3, 0/1: 54/46
batch index 4, 0/1: 52/48
batch index 5, 0/1: 51/49
batch index 6, 0/1: 47/53
batch index 7, 0/1: 53/47
batch index 8, 0/1: 48/52
batch index 9, 0/1: 55/45

I don’t see any particular difference between the two pieces of code. The only curious difference was the number of workers. But even setting it to one I don’t get a correct distribution.