I was able to piece together single-worker, multi-GPU prediction as follows (consider it a sketch - it uses plumbing code that's not generally applicable, but should give you a template to go off of):
# https://github.com/tensorflow/tensorflow/issues/37686
# https://www.tensorflow.org/tutorials/distribute/custom_training
def compute_and_write_ious_multi_gpu(path: str, filename_csv: str, include_sampled: bool):
strategy = tf.distribute.MirroredStrategy()
util.log('Number of devices: {}'.format(strategy.num_replicas_in_sync))
(ds, s, n) = dataset(path, shuffle=False, repeat=False, mask_as_input=True)
dist_ds = strategy.experimental_distribute_dataset(ds)
def predict_step(inputs):
images, labels = inputs
return model(images, training=False)
@tf.function
def distributed_predict_step(dataset_inputs):
per_replica_losses = strategy.run(predict_step, args=(dataset_inputs,))
return per_replica_losses # unwrap!?
# https://stackoverflow.com/questions/57549448/how-to-convert-perreplica-to-tensor
def unwrap(per_replica): # -> list of numpy arrays
if strategy.num_replicas_in_sync > 1:
out = per_replica.values
else:
out = (per_replica,)
return list(map(lambda x: x.numpy(), out))
with strategy.scope():
model = wrap_model()
util.log(f'Starting distributed prediction for {filename_csv}')
ious = [unwrap(distributed_predict_step(x)) for x in dist_ds]
t = ious
ious = [item for sublist in t for item in
sublist] # https://stackoverflow.com/questions/952914/how-to-make-a-flat-list-out-of-list-of-lists
util.log(f'Distributed prediction done for {filename_csv}')
ious = np.concatenate(ious).ravel().tolist()
ious = round_ious(ious)
ious = list(zip(ious, ds.all_image_paths))
ious.sort()
write_ious(ious, filename_csv, include_sampled)
This does distribute the load across the GPUs, but unfortunately makes very poor use of them - in my particular case the corresponding single-GPU code runs in ~12 hours, and this runs in 7.7 hours, so not even a 2x speedup despite have 8x the number of GPUs.
I think it's mostly a data feeding issue, but I don't know how to fix it. Hopefully someone else can provide some better insights?