@@ -120,14 +120,13 @@ validation_dataloader = accelerator.prepare(validation_dataloader)
120120
121121As for your training dataloader, it will mean that (should you run your script on multiple devices) each device will
122122only see part of the evaluation data. This means you will need to group your predictions together. This is very easy to
123- do with the [ ` ~Accelerator.gather ` ] method.
123+ do with the [ ` ~Accelerator.gather_for_metrics ` ] method.
124124
125125``` python
126126for inputs, targets in validation_dataloader:
127127 predictions = model(inputs)
128128 # Gather all predictions and targets
129- all_predictions = accelerator.gather(predictions)
130- all_targets = accelerator.gather(targets)
129+ all_predictions, all_targets = accelerator.gather_for_metrics((predictions, targets))
131130 # Example of use with a *Datasets.Metric*
132131 metric.add_batch(all_predictions, all_targets)
133132```
@@ -141,11 +140,17 @@ As for the training dataloader, passing your validation dataloader through
141140Any instruction using your training dataloader length (for instance if you need the number of total training steps
142141to create a learning rate scheduler) should go after the call to [ ` ~Accelerator.prepare ` ] .
143142
143+ As some data at the end of the dataset may be duplicated so the batch can divide equally to all workers, metrics should be
144+ calculated through the [ ` ~Accelerator.gather_for_metrics ` ] method to automatically remove the duplicated data.
145+
146+ If for some reason you don't wish to have this automatically done, [ ` ~Accelerator.gather ` ] can be used instead to gather
147+ the data across all processes and this can manually be done instead.
148+
144149</Tip >
145150
146151<Tip warning = { true } >
147152
148- The [ ` ~Accelerator.gather ` ] method requires the tensors to be all the same size on each process. If
153+ The [ ` ~Accelerator.gather ` ] and [ ` ~Accelerator.gather_for_metrics ` ] methods require the tensors to be all the same size on each process. If
149154you have tensors of different sizes on each process (for instance when dynamically padding to the maximum length in
150155a batch), you should use the [ ` ~Accelerator.pad_across_processes ` ] method to pad you tensor to the
151156biggest size across processes.
0 commit comments