Skip to content

Commit

Permalink
[Docs] Add examples for new object-based c10d APIs
Browse files Browse the repository at this point in the history
Pull Request resolved: #43932

Adds some basic examples to the documentation for each of the newly added
object-based collectibves.
ghstack-source-id: 117637342

Differential Revision: [D23441838](https://our.internmc.facebook.com/intern/diff/D23441838/)
  • Loading branch information
rohan-varma committed Dec 2, 2020
1 parent 8e67713 commit 8971b65
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions torch/distributed/distributed_c10d.py
Expand Up @@ -1393,6 +1393,15 @@ def all_gather_object(object_list, obj, group=group.WORLD):
known to be insecure. It is possible to construct malicious pickle data
which will execute arbitrary code during unpickling. Only call this
function with data you trust.
Example::
>>> # Note: Process group initialization omitted on each rank.
>>> import torch.distributed as dist
>>> gather_objects = ["foo", 12, {1: 2}] # any picklable object
>>> output = [None for _ in gather_objects]
>>> dist.all_gather_object(output, gather_objects[dist.get_rank()])
>>> output
['foo', 12, {1: 2}]
"""
if _rank_not_in_group(group):
return
Expand Down Expand Up @@ -1467,6 +1476,20 @@ def gather_object(obj, object_gather_list=None, dst=0, group=group.WORLD):
known to be insecure. It is possible to construct malicious pickle data
which will execute arbitrary code during unpickling. Only call this
function with data you trust.
Example::
>>> # Note: Process group initialization omitted on each rank.
>>> import torch.distributed as dist
>>> gather_objects = ["foo", 12, {1: 2}] # any picklable object
>>> output = [None for _ in gather_objects]
>>> dist.gather_object(
gather_objects[dist.get_rank()],
output if dist.get_rank() == 0 else None,
dst=0
)
>>> # On rank 0
>>> output
['foo', 12, {1: 2}]
"""
if _rank_not_in_group(group):
return
Expand Down Expand Up @@ -1556,6 +1579,17 @@ def broadcast_object_list(object_list, src, group=group.WORLD):
is known to be insecure. It is possible to construct malicious pickle
data which will execute arbitrary code during unpickling. Only call this
function with data you trust.
Example::
>>> # Note: Process group initialization omitted on each rank.
>>> import torch.distributed as dist
>>> if dist.get_rank() == 0:
>>> objects = ["foo", 12, {1: 2}] # any picklable object
>>> else:
>>> objects = [None, None, None]
>>> dist.broadcast_object_list(objects, src=0)
>>> broadcast_objects
['foo', 12, {1: 2}]
"""
if _rank_not_in_group(group):
return
Expand Down Expand Up @@ -1634,6 +1668,19 @@ def scatter_object_list(
is known to be insecure. It is possible to construct malicious pickle
data which will execute arbitrary code during unpickling. Only call this
function with data you trust.
Example::
>>> # Note: Process group initialization omitted on each rank.
>>> import torch.distributed as dist
>>> if dist.get_rank() == 0:
>>> objects = ["foo", 12, {1: 2}] # any picklable object
>>> else:
>>> objects = [None, None, None]
>>> output_list = [None]
>>> dist.scatter_object_list(output_list, objects, src=0)
>>> # Rank i gets objects[i]. For example, on rank 2:
>>> output_list
[{1: 2}]
"""
if _rank_not_in_group(group):
return
Expand Down

0 comments on commit 8971b65

Please sign in to comment.