MCPcopy Index your code
hub / github.com/microsoft/BitNet / bounded_parallel_map

Function bounded_parallel_map

utils/convert.py:1002–1034  ·  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

1000
1001
1002def bounded_parallel_map(func: Callable[[In], Out], iterable: Iterable[In], concurrency: int, max_workers: int | None = None, use_processpool_executor: bool = False) -> Iterable[Out]:
1003 '''Parallel map, but with backpressure. If the caller doesn't call `next`
1004 fast enough, this will stop calling `func` at some point rather than
1005 letting results pile up in memory. Specifically, there is a max of one
1006 output value buffered per thread.'''
1007 if concurrency < 2:
1008 yield from map(func, iterable)
1009 # Not reached.
1010 iterable = iter(iterable)
1011 executor_class: type[ThreadPoolExecutor] | type[ProcessPoolExecutor]
1012 if use_processpool_executor:
1013 executor_class = ProcessPoolExecutor
1014 else:
1015 executor_class = ThreadPoolExecutor
1016 with executor_class(max_workers=max_workers) as executor:
1017 futures: list[concurrent.futures.Future[Out]] = []
1018 done = False
1019 for _ in range(concurrency):
1020 try:
1021 futures.append(executor.submit(func, next(iterable)))
1022 except StopIteration:
1023 done = True
1024 break
1025
1026 while futures:
1027 result = futures.pop(0).result()
1028 while not done and len(futures) < concurrency:
1029 try:
1030 futures.append(executor.submit(func, next(iterable)))
1031 except StopIteration:
1032 done = True
1033 break
1034 yield result
1035
1036
1037def check_vocab_size(params: Params, vocab: BaseVocab, pad_vocab: bool = False) -> None:

Callers 1

write_tensor_dataMethod · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected