-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmp_benchmark_xla.py
More file actions
170 lines (143 loc) · 5.5 KB
/
mp_benchmark_xla.py
File metadata and controls
170 lines (143 loc) · 5.5 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import ray
path = "gs://mlperf-dataset/data/2021_Brats_np/11_3d"
import gcsfs
import glob
import os
import tensorflow.io as io
from ray.data.block import BlockMetadata
from ray.data.datasource import FileMetadataProvider,FastFileMetadataProvider
import time
import numpy as np
def _rand_crop(image, label):
low_x=low_y=low_z=0
high_x=high_y=high_z=128
image = image[:, low_x:high_x, low_y:high_y, low_z:high_z]
label = label[:, low_x:high_x, low_y:high_y, low_z:high_z]
return image, label, [low_x, high_x, low_y, high_y, low_z, high_z]
def load_data(path, files_pattern):
data = sorted(io.gfile.glob(os.path.join(path, files_pattern)))
assert len(data) > 0, f"Found no data at {path}"
return data
from torch.utils.data import Dataset,DataLoader
import torch
import torch_xla.core.xla_model as xm
import torch_xla.distributed.parallel_loader as pl
class PytTrain(Dataset):
def __init__(self, images, labels, dataset, **kwargs):
self.dataset = dataset
self.images, self.labels = images, labels
def __len__(self):
return len(self.images)
def __getitem__(self, idx):
with io.gfile.GFile(os.path.join(self.dataset, self.images[idx]), 'rb') as f, io.gfile.GFile(os.path.join(self.dataset, self.labels[idx]), 'rb') as g:
data = {"image": np.load(f), "label": np.load(g)}
#data = self.rand_crop(data)
#data = self.train_transforms(data)
return data["image"], data["label"]
paths_x = load_data(path, "*_x.npy")
paths_y = load_data(path, "*_y.npy")
def rayloader():
provider=FastFileMetadataProvider()
ds = ray.data.read_numpy(paths_x,filesystem=gcsfs.GCSFileSystem(), meta_provider=provider)
ds.to_torch()
start = time.time()
for j in range(10):
for i, batch in enumerate(ds.iter_batches(batch_size=1)):
batch = torch.as_tensor(batch[0])
batch = xm.send_cpu_data_to_device(batch, device)
batch.to(device)
pass
training_time = (time.time() - start)/10
print(f"Training time for ray : {training_time:.2f} seconds")
def rayddploader():
path = "gs://mlperf-dataset/data/2021_Brats_np/11_3d"
paths_x = load_data(path, "*_x.npy")
paths_y = load_data(path, "*_y.npy")
@ray.remote
def data_loading(paths_x, paths_y, idx):
device = xm.xla_device()
if 1:
paths_x = [name.split('/')[-1] for name in paths_x]
paths_y = [name.split('/')[-1] for name in paths_y]
train_dataset = PytTrain(paths_x, paths_y, path)
train_loader = DataLoader(
train_dataset,
batch_size=1,
shuffle=False,
sampler=None,
num_workers=4,
pin_memory=False,
drop_last=True,
persistent_workers=True
)
for i, batch in enumerate(train_loader):
batch[0].to(device)
pass
else:
provider=FastFileMetadataProvider()
ds = ray.data.read_numpy(paths_x,filesystem=gcsfs.GCSFileSystem(), meta_provider=provider)
ds.to_torch()
start = time.time()
for j in range(10):
for i, batch in enumerate(ds.iter_batches(batch_size=1)):
batch = torch.as_tensor(batch[0])
batch = xm.send_cpu_data_to_device(batch, device)
batch.to(device)
pass
features_ref = ray.put(paths_x)
label_ref = ray.put(paths_y)
task_ref = []
for i in range(4):
task_ref.append(data_loading.remote(features_ref,label_ref, i))
result = ray.get(task_ref)
import torch_xla.distributed.xla_multiprocessing as xmp
def xla_main(local_rank):
device = xm.xla_device()
path = "gs://mlperf-dataset/data/2021_Brats_np/11_3d"
paths_x = load_data(path, "*_x.npy")
paths_y = load_data(path, "*_y.npy")
if 0 :
paths_x = [name.split('/')[-1] for name in paths_x]
paths_y = [name.split('/')[-1] for name in paths_y]
train_dataset = PytTrain(paths_x, paths_y, path)
train_loader = DataLoader(
train_dataset,
batch_size=1,
shuffle=False,
sampler=None,
num_workers=4,
pin_memory=False,
drop_last=True,
persistent_workers=True
)
train_loader = pl.MpDeviceLoader(train_loader, device)
start = time.time()
for j in range(10):
for i, batch in enumerate(train_loader):
batch[0].to(device)
pass
training_time = (time.time() - start)/10
print(f"Training time for pytorch: {training_time:.2f} seconds")
else:
provider=FastFileMetadataProvider()
ds = ray.data.read_numpy(paths_x,filesystem=gcsfs.GCSFileSystem(), meta_provider=provider)
ds.to_torch()
start = time.time()
for j in range(2):
for i, batch in enumerate(ds.iter_batches(batch_size=1)):
batch = torch.as_tensor(batch[0])
batch = xm.send_cpu_data_to_device(batch, device)
batch.to(device)
pass
training_time = (time.time() - start)/2
print(f"Training time for pytorch: {training_time:.2f} seconds")
xm.rendezvous("exit")
if __name__ == '__main__':
mode = 2
if mode == 0:
ray.init(ignore_reinit_error=True)
rayddploader()
elif mode == 1:
pass
else:
xmp.spawn(xla_main)