test_fast_load_pacing.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. """fast_load pacing: never more than the byte budget in flight (8 bytes here, 1 per copy), all complete, sync path untouched."""
  2. import concurrent.futures, os, sys, threading, time, types
  3. os.environ["DSV41_FAST_LOAD"] = "1"; os.environ["DSV41_FAST_LOAD_INFLIGHT_GB"] = repr(8 / 2**30)
  4. sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..", "adapter"))
  5. import fast_load
  6. def orig(*, executor, futures, use_async, func, func_args=(), func_kwargs=None):
  7. if use_async:
  8. futures.append(executor.submit(func, *func_args, **(func_kwargs or {})))
  9. else:
  10. func(*func_args, **(func_kwargs or {}))
  11. mod = types.SimpleNamespace(maybe_executor_submit=orig)
  12. fast_load.install_deepseek_v4(mod)
  13. assert mod.maybe_executor_submit is not orig
  14. inflight = [0]; peak = [0]; done = [0]; lock = threading.Lock()
  15. def work(i):
  16. with lock:
  17. inflight[0] += 1; peak[0] = max(peak[0], inflight[0])
  18. time.sleep(0.001)
  19. with lock:
  20. inflight[0] -= 1; done[0] += 1
  21. futures = []
  22. with concurrent.futures.ThreadPoolExecutor(24) as ex:
  23. t = time.time()
  24. for i in range(2000):
  25. mod.maybe_executor_submit(executor=ex, futures=futures, use_async=True, func=work, func_args=(i,))
  26. for f in concurrent.futures.as_completed(futures):
  27. f.result()
  28. assert done[0] == 2000 and len(futures) == 2000, (done, len(futures))
  29. assert peak[0] <= 8, peak
  30. mod.maybe_executor_submit(executor=None, futures=futures, use_async=False, func=work, func_args=(0,))
  31. assert done[0] == 2001
  32. # an exception in the wrapped submit must not leak a permit
  33. def boom(**kw): raise RuntimeError("x")
  34. mod2 = types.SimpleNamespace(maybe_executor_submit=boom); fast_load.install_deepseek_v4(mod2)
  35. for _ in range(20):
  36. try: mod2.maybe_executor_submit(executor=None, futures=[], use_async=True, func=work)
  37. except RuntimeError: pass
  38. print(f"pacing OK: peak in flight {peak[0]} <= 8, 2000 copies in {time.time()-t:.2f}s")