-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move MultiprocessIterator to experimental
- Loading branch information
Showing
6 changed files
with
200 additions
and
130 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
# AUTOGENERATED! DO NOT EDIT! File to edit: ../../nbs/experimental/mp.ipynb. | ||
|
||
# %% ../../nbs/experimental/mp.ipynb 1 | ||
from __future__ import print_function, division, annotations | ||
from ..imports import * | ||
from ..datasets import ArrayDataset, JAXDataset | ||
from ..loaders import BaseDataLoader | ||
from ..utils import get_config, asnumpy | ||
from ..tests import * | ||
import jax_dataloader as jdl | ||
from threading import Thread, Event | ||
from queue import Queue, Full | ||
import multiprocessing as mp | ||
import weakref | ||
|
||
# %% auto 0 | ||
__all__ = ['chunk', 'EpochIterator'] | ||
|
||
# %% ../../nbs/experimental/mp.ipynb 2 | ||
def chunk(seq: Sequence, size: int) -> List[Sequence]: | ||
return [seq[pos:pos + size] for pos in range(0, len(seq), size)] | ||
|
||
|
||
# %% ../../nbs/experimental/mp.ipynb 3 | ||
class EpochIterator(Thread): | ||
"""[WIP] Multiprocessing Epoch Iterator""" | ||
|
||
def __init__(self, data, batch_size: int, indices: Sequence[int]): | ||
super().__init__() | ||
self.data = data | ||
batches = chunk(indices, batch_size) | ||
self.iter_idx = iter(batches) | ||
self.output_queue = Queue(5) # TODO: maxsize | ||
self.terminate_event = Event() | ||
self.start() | ||
|
||
def run(self): | ||
try: | ||
while True: | ||
# get data | ||
result = self.get_data() | ||
# put result in queue | ||
while True: | ||
try: | ||
self.output_queue.put(result, block=True, timeout=0.5) | ||
break | ||
except Full: pass | ||
|
||
if self.terminate_event.is_set(): return | ||
|
||
except StopIteration: | ||
self.output_queue.put(None) | ||
|
||
def __next__(self): | ||
result = self.output_queue.get() | ||
if result is None: | ||
self.close() | ||
raise StopIteration() | ||
return result | ||
|
||
def __iter__(self): | ||
return self | ||
|
||
def __del__(self): | ||
self.close() | ||
|
||
def close(self): | ||
self.terminate_event.set() | ||
|
||
def get_data(self): | ||
batch_idx = next(self.iter_idx) | ||
batch = self.data[batch_idx] | ||
return batch | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"#| default_exp experimental.multi_processing" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"#| export\n", | ||
"from __future__ import print_function, division, annotations\n", | ||
"from jax_dataloader.imports import *\n", | ||
"from jax_dataloader.datasets import ArrayDataset, JAXDataset\n", | ||
"from jax_dataloader.loaders import BaseDataLoader\n", | ||
"from jax_dataloader.utils import get_config, asnumpy\n", | ||
"from jax_dataloader.tests import *\n", | ||
"import jax_dataloader as jdl\n", | ||
"from threading import Thread, Event\n", | ||
"from queue import Queue, Full\n", | ||
"import multiprocessing as mp\n", | ||
"import weakref" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"#| export\n", | ||
"def chunk(seq: Sequence, size: int) -> List[Sequence]:\n", | ||
" return [seq[pos:pos + size] for pos in range(0, len(seq), size)] \n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"#| export\n", | ||
"class EpochIterator(Thread):\n", | ||
" \"\"\"[WIP] Multiprocessing Epoch Iterator\"\"\"\n", | ||
" \n", | ||
" def __init__(self, data, batch_size: int, indices: Sequence[int]):\n", | ||
" super().__init__()\n", | ||
" self.data = data\n", | ||
" batches = chunk(indices, batch_size)\n", | ||
" self.iter_idx = iter(batches)\n", | ||
" self.output_queue = Queue(5) # TODO: maxsize\n", | ||
" self.terminate_event = Event()\n", | ||
" self.start()\n", | ||
"\n", | ||
" def run(self):\n", | ||
" try:\n", | ||
" while True:\n", | ||
" # get data\n", | ||
" result = self.get_data()\n", | ||
" # put result in queue\n", | ||
" while True:\n", | ||
" try: \n", | ||
" self.output_queue.put(result, block=True, timeout=0.5)\n", | ||
" break\n", | ||
" except Full: pass\n", | ||
" \n", | ||
" if self.terminate_event.is_set(): return \n", | ||
"\n", | ||
" except StopIteration:\n", | ||
" self.output_queue.put(None)\n", | ||
"\n", | ||
" def __next__(self):\n", | ||
" result = self.output_queue.get()\n", | ||
" if result is None:\n", | ||
" self.close()\n", | ||
" raise StopIteration()\n", | ||
" return result\n", | ||
" \n", | ||
" def __iter__(self):\n", | ||
" return self\n", | ||
" \n", | ||
" def __del__(self):\n", | ||
" self.close()\n", | ||
"\n", | ||
" def close(self):\n", | ||
" self.terminate_event.set()\n", | ||
"\n", | ||
" def get_data(self):\n", | ||
" batch_idx = next(self.iter_idx)\n", | ||
" batch = self.data[batch_idx]\n", | ||
" return batch\n" | ||
] | ||
} | ||
], | ||
"metadata": {}, | ||
"nbformat": 4, | ||
"nbformat_minor": 2 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters