-
Notifications
You must be signed in to change notification settings - Fork 15
/
streaming.py
213 lines (185 loc) · 6.47 KB
/
streaming.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
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import os
import logging
import fnmatch
import random
import numpy as np
import tensorflow as tf
import utils
def get_files(dirname, filename_pat="*", recursive=False):
if not tf.io.gfile.exists(dirname):
return None
files = []
for x in tf.io.gfile.listdir(dirname):
path = os.path.join(dirname, x)
if tf.io.gfile.isdir(path):
if recursive:
files.extend(get_files(path, filename_pat))
elif fnmatch.fnmatch(x, filename_pat):
files.append(path)
return files
def get_worker_files(dirname,
worker_rank,
world_size,
filename_pat="*",
shuffle=False,
seed=0):
"""Get file paths belong to one worker."""
all_files = get_files(dirname, filename_pat)
all_files.sort()
if shuffle:
# g_mutex = threading.Lock()
# g_mutex.acquire()
random.seed(seed)
random.shuffle(all_files)
# g_mutex.release()
files = []
for i in range(worker_rank, len(all_files), world_size):
files.append(all_files[i])
logging.info(
f"worker_rank:{worker_rank}, world_size:{world_size}, shuffle:{shuffle}, seed:{seed}, directory:{dirname}, files:{files}"
)
return files
class StreamReader:
def __init__(self, data_paths, batch_size, shuffle=False, shuffle_buffer_size=1000):
tf.config.experimental.set_visible_devices([], device_type="GPU")
# logging.info(f"visible_devices:{tf.config.experimental.get_visible_devices()}")
path_len = len(data_paths)
# logging.info(f"[StreamReader] path_len:{path_len}, paths: {data_paths}")
dataset = tf.data.Dataset.list_files(data_paths).interleave(
lambda x: tf.data.TextLineDataset(x),
cycle_length=path_len,
block_length=128,
num_parallel_calls=min(path_len, 64),
)
dataset = dataset.interleave(
lambda x: tf.data.Dataset.from_tensor_slices(
self._process_record(x)),
cycle_length=path_len,
block_length=1,
num_parallel_calls=tf.data.experimental.AUTOTUNE)
if shuffle:
dataset = dataset.shuffle(shuffle_buffer_size, reshuffle_each_iteration=True)
dataset = dataset.batch(batch_size)
dataset = dataset.prefetch(1)
self.next_batch = dataset.make_one_shot_iterator().get_next()
self.session = None
def _process_record(self, record):
# iid, uid, time, his, impr
records = tf.strings.split([record], '\t').values
sess = tf.strings.split([records[4]], ' ').values # (num)
sess_label = tf.strings.split(sess, '-').values
sess_poss = tf.gather(sess_label, tf.where(tf.equal(sess_label, '1'))-1)
record = tf.expand_dims(record, axis=0)
poss_num = tf.size(sess_poss)
return sess_poss[:, 0], tf.repeat(record, poss_num, axis=0)
def reset(self):
# print(f"StreamReader reset(), {self.session}, pid:{threading.currentThread()}")
if self.session:
self.session.close()
self.session = tf.Session()
self.endofstream = False
def get_next(self):
try:
ret = self.session.run(self.next_batch)
except tf.errors.OutOfRangeError:
self.endofstream = True
return None
return ret
def reach_end(self):
# print(f"StreamReader reach_end(), {self.endofstream}")
return self.endofstream
class StreamSampler:
def __init__(
self,
data_dir,
filename_pat,
batch_size,
worker_rank,
world_size,
enable_shuffle=False,
shuffle_buffer_size=1000,
shuffle_seed=0,
):
data_paths = get_worker_files(
data_dir,
worker_rank,
world_size,
filename_pat,
shuffle=enable_shuffle,
seed=shuffle_seed,
)
print('fuck',data_paths)
self.stream_reader = StreamReader(
data_paths,
batch_size,
enable_shuffle,
shuffle_buffer_size
)
def __iter__(self):
self.stream_reader.reset()
return self
def __next__(self):
"""Implement iterator interface."""
# logging.info(f"[StreamSampler] __next__")
next_batch = self.stream_reader.get_next()
if not isinstance(next_batch, np.ndarray) and not isinstance(
next_batch, tuple):
raise StopIteration
# print(next_batch.shape)
return next_batch
def reach_end(self):
return self.stream_reader.reach_end()
class StreamReaderTest(StreamReader):
def __init__(self, data_paths, batch_size, shuffle=False, shuffle_buffer_size=1000):
tf.config.experimental.set_visible_devices([], device_type="GPU")
# logging.info(f"visible_devices:{tf.config.experimental.get_visible_devices()}")
path_len = len(data_paths)
# logging.info(f"[StreamReader] path_len:{path_len}, paths: {data_paths}")
dataset = tf.data.Dataset.list_files(data_paths).interleave(
lambda x: tf.data.TextLineDataset(x),
cycle_length=path_len,
block_length=128,
num_parallel_calls=min(path_len, 64),
)
if shuffle:
dataset = dataset.shuffle(shuffle_buffer_size, reshuffle_each_iteration=True)
dataset = dataset.batch(batch_size)
dataset = dataset.prefetch(1)
self.next_batch = dataset.make_one_shot_iterator().get_next()
self.session = None
class StreamSamplerTest(StreamSampler):
def __init__(
self,
data_dir,
filename_pat,
batch_size,
worker_rank,
world_size,
enable_shuffle=False,
shuffle_buffer_size=1000,
shuffle_seed=0,
):
data_paths = get_worker_files(
data_dir,
worker_rank,
world_size,
filename_pat,
shuffle=enable_shuffle,
seed=shuffle_seed,
)
self.stream_reader = StreamReaderTest(
data_paths,
batch_size,
enable_shuffle,
shuffle_buffer_size)
if __name__ == "__main__":
utils.setuplogger()
print("start")
sampler = StreamSampler(
"../MIND/test",
"behaviors_*.tsv", 4, 0, 1)
import time
for i in sampler:
logging.info("sampler")
logging.info(i)
time.sleep(5)