MCPcopy Create free account
hub / github.com/Tiiny-AI/PowerInfer / bounded_parallel_map

Function bounded_parallel_map

convert-dense.py:756–788  ·  view source on GitHub ↗

Parallel map, but with backpressure. If the caller doesn't call `next` fast enough, this will stop calling `func` at some point rather than letting results pile up in memory. Specifically, there is a max of one output value buffered per thread.

(func: Callable[[In], Out], iterable: Iterable[In], concurrency: int, max_workers: int | None = None, use_processpool_executor: bool = False)

Source from the content-addressed store, hash-verified

754Out = TypeVar('Out')
755
756def bounded_parallel_map(func: Callable[[In], Out], iterable: Iterable[In], concurrency: int, max_workers: int | None = None, use_processpool_executor: bool = False) -> Iterable[Out]:
757 '''Parallel map, but with backpressure. If the caller doesn't call `next`
758 fast enough, this will stop calling `func` at some point rather than
759 letting results pile up in memory. Specifically, there is a max of one
760 output value buffered per thread.'''
761 if concurrency < 2:
762 yield from map(func, iterable)
763 # Not reached.
764 iterable = iter(iterable)
765 executor_class: type[ThreadPoolExecutor] | type[ProcessPoolExecutor]
766 if use_processpool_executor:
767 executor_class = ProcessPoolExecutor
768 else:
769 executor_class = ThreadPoolExecutor
770 with executor_class(max_workers = max_workers) as executor:
771 futures: list[concurrent.futures.Future[Out]] = []
772 done = False
773 for _ in range(concurrency):
774 try:
775 futures.append(executor.submit(func, next(iterable)))
776 except StopIteration:
777 done = True
778 break
779
780 while futures:
781 result = futures.pop(0).result()
782 while not done and len(futures) < concurrency:
783 try:
784 futures.append(executor.submit(func, next(iterable)))
785 except StopIteration:
786 done = True
787 break
788 yield result
789
790def check_vocab_size(params: Params, vocab: Vocab) -> None:
791 if params.n_vocab != vocab.vocab_size:

Callers 1

write_allMethod · 0.70

Calls 5

mapFunction · 0.85
rangeFunction · 0.85
nextFunction · 0.50
appendMethod · 0.45
popMethod · 0.45

Tested by

no test coverage detected