-
Notifications
You must be signed in to change notification settings - Fork 326
/
memmap_speed_distributed.py
95 lines (76 loc) · 2.42 KB
/
memmap_speed_distributed.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.
import os
import time
import configargparse
import torch
import torch.distributed.rpc as rpc
from tensordict import MemoryMappedTensor
parser = configargparse.ArgumentParser()
parser.add_argument("--rank", default=-1, type=int)
parser.add_argument("--world_size", default=2, type=int)
parser.add_argument("--tensortype", default="memmap", type=str)
AGENT_NAME = "main"
OBSERVER_NAME = "worker{}"
str_init_method = "tcp://localhost:10000"
options = rpc.TensorPipeRpcBackendOptions(
_transports=["uv"], num_worker_threads=16, init_method=str_init_method
)
global tensor
def send_tensor(t):
global tensor
tensor = t
print(tensor)
def op_on_tensor(idx):
tensor[idx] += 1
if isinstance(tensor, torch.Tensor):
return tensor
if __name__ == "__main__":
args = parser.parse_args()
rank = args.rank
world_size = args.world_size
tensortype = args.tensortype
os.environ["MASTER_ADDR"] = "localhost"
os.environ["MASTER_PORT"] = "29500"
if rank == 0:
rpc.init_rpc(
AGENT_NAME,
rank=rank,
world_size=world_size,
backend=rpc.BackendType.TENSORPIPE,
rpc_backend_options=options,
)
# create tensor
tensor = torch.zeros(10000, 10000)
if tensortype == "memmap":
tensor = MemoryMappedTensor.from_tensor(tensor)
elif tensortype == "tensor":
pass
else:
raise NotImplementedError
# send tensor
w = 1
fut0 = rpc.remote(f"worker{w}", send_tensor, args=(tensor,))
fut0.to_here()
# execute
t0 = time.time()
idx = 10
for i in range(100):
fut1 = rpc.remote(f"worker{w}", op_on_tensor, args=(idx,))
tensor_out = fut1.to_here()
if tensortype == "memmap":
assert (tensor[idx] == i + 1).all()
else:
assert (tensor_out[idx] == i + 1).all()
print(f"{tensortype}, time spent: {time.time() - t0: 4.4f}")
else:
rpc.init_rpc(
OBSERVER_NAME.format(rank),
rank=rank,
world_size=world_size,
backend=rpc.BackendType.TENSORPIPE,
rpc_backend_options=options,
)
rpc.shutdown()