If you need different processes on the same node to write to a combined single file, then you can first call gather or gather_objects to collect all the outputs, and then write them to the file. Or you can just let all the processes collectively write to a sharded file, where each shard is a single file output by a process.
If you only want the master to write, you can specify the condition torch.distributed.get_rank() == 0. This is usually good enough for checkpointing the model, since DDP guarantees the model on each process is the same.