MCPcopy Create free account
hub / github.com/pytorch/pytorch / _enqueue_batch

Method _enqueue_batch

caffe2/python/data_workers.py:259–316  ·  view source on GitHub ↗

This pulls data from the python-side queue and collects them into batch-sized pieces, unless dont_rebatch is set to true.

(self, data_input_coordinator)

Source from the content-addressed store, hash-verified

257 self._enqueue(b, q, c)
258
259 def _enqueue_batch(self, data_input_coordinator):
260 '''
261 This pulls data from the python-side queue and collects them
262 into batch-sized pieces, unless dont_rebatch is set to true.
263 '''
264 if self._dont_rebatch:
265 self._enqueue_batch_direct(data_input_coordinator)
266 return
267
268 cur_batch = [np.array([]) for d in self._input_blob_names]
269 first_batch_col = self._batch_columns[0]
270
271 # Collect data until we have a full batch size
272 while (
273 cur_batch[0].shape[0] == 0 or
274 cur_batch[0].shape[first_batch_col] < self._batch_size
275 ) and data_input_coordinator.is_active():
276 chunk = self._get(data_input_coordinator)
277 if chunk is None:
278 continue
279
280 for j, chunk_elem in enumerate(chunk):
281 if cur_batch[j].shape[0] == 0:
282 cur_batch[j] = chunk_elem.copy()
283 else:
284 cur_batch[j] = np.append(
285 cur_batch[j], chunk_elem, axis=self._batch_columns[j]
286 )
287
288 start_time = time.time()
289 try:
290 # Return data over the batch size back to queue
291 if cur_batch[0].shape[0] > 0 and cur_batch[0].shape[
292 first_batch_col
293 ] > self._batch_size:
294 leftover = []
295 trimmed_batch = []
296 for j, b in enumerate(cur_batch):
297 [c, l] = np.split(
298 b, [self._batch_size], axis=self._batch_columns[j]
299 )
300 leftover.append(l)
301 trimmed_batch.append(c)
302 cur_batch = trimmed_batch
303 try:
304 self._internal_queue.put(leftover, block=False)
305 except Queue.Full:
306 pass
307
308 assert cur_batch[0].shape[first_batch_col] == self._batch_size
309
310 if data_input_coordinator.is_active():
311 for b, q, c in zip(
312 self._input_blob_names, self._queues, cur_batch
313 ):
314 self._enqueue(b, q, c)
315 finally:
316 self._metrics.put_metric('enqueue_time', time.time() - start_time)

Callers 1

enqueuerFunction · 0.80

Calls 9

_enqueue_batch_directMethod · 0.95
_getMethod · 0.95
_enqueueMethod · 0.95
put_metricMethod · 0.80
is_activeMethod · 0.45
copyMethod · 0.45
appendMethod · 0.45
splitMethod · 0.45
putMethod · 0.45

Tested by

no test coverage detected