Hog GPU once acquired: duration=120, up to 12 sub-drains, 100s deadline
Browse filesFiles changed (2)
- app.py+48-10
- docs/blog.md+12-7
206 unmodified lines20720820921021121221321421521621721821922022120 unmodified lines242243244245246247248249250251252253254255256206 unmodified linesreturn result@GPU(duration=120)def _drain_gpu() -> int:"""The hot path: drain inside a ZeroGPU fork. RealLLMBackend loads eachfork's Llama with n_gpu_layers=-1. Reservation is 120s to give headroomover the observed ~100s worst-case drain (3 chars × ~30s + per-forkcold-load). Smaller batches (DECISIONS_PER_DRAIN=2) keep individualdrains under this comfortably."""return TOWNLET.drain_decisions()def _drain_cpu() -> int:20 unmodified linesfalls through to on that path)."""_log_req("tick_decisions")try:n = _drain_gpu()mode = "gpu"except BaseException as e:msg = f"{type(e).__name__}: {e}"tlog(f"[townlet] GPU drain failed ({msg[:120]}); CPU fallback engaged")n = _drain_cpu()mode = "cpu"result = {"decisions_run": n, "mode": mode}_log_resp("tick_decisions", result)return result206 unmodified lines20720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425520 unmodified lines276277278279280281282283284285286287288289290291292293294206 unmodified linesreturn result# GPU "hog mode": once we've been granted a slot, the cold-load cost# (~15-20s for Llama.from_pretrained inside the fork) is sunk — every# subsequent drain in the same fork reuses the already-loaded Llama via# backend.factory._llms. So instead of running ONE drain per @spaces.GPU# call and giving the slot back, we run multiple drains back-to-back until# we approach the time budget or run out of pending decisions.## duration=120 chosen to maximise work-per-acquisition. Per-call quota# burn is the same whether the fork does 2 drains or 12 drains (we use# real wall time, not the declared duration). What we want is high# decisions-per-fork so that even when the allocator denies us 1 in N# calls, the calls that ARE granted produce a lot of visible action.# We are NOT fanning out concurrent forks — that multiplies cold-load# and quota burn linearly while making us a "noisy neighbour" to the# allocator. One fat fork strictly beats N thin forks.GPU_DURATION = 120_GPU_DEADLINE_S = 100.0 # stop spawning new sub-drains after this elapses_MAX_SUB_DRAINS = 12 # safety cap in case sub-drains run very fast@GPU(duration=GPU_DURATION)def _drain_gpu() -> dict:"""Hog the GPU once granted. Runs drain_decisions() in a loop insidethe same fork until time is up or no pending characters remain. Returnsa dict with total decisions_run + number of sub-drains performed so thecaller can log it."""import timestarted = time.monotonic()total = 0sub_drains = 0while sub_drains < _MAX_SUB_DRAINS:elapsed = time.monotonic() - startedif elapsed > _GPU_DEADLINE_S:breakn = TOWNLET.drain_decisions()sub_drains += 1total += nif n == 0:# No pending decisions queued. tick_world inside drain_decisions# already advanced the clock; idling further would just spin.breakreturn {"decisions_run": total, "sub_drains": sub_drains}def _drain_cpu() -> int:20 unmodified linesfalls through to on that path)."""_log_req("tick_decisions")sub_drains = 1 # CPU path is always one sub-draintry:gpu_result = _drain_gpu()n = gpu_result["decisions_run"]sub_drains = gpu_result["sub_drains"]mode = "gpu"tlog(f"[townlet] GPU drain hog: {sub_drains} sub-drain(s), {n} decisions")except BaseException as e:msg = f"{type(e).__name__}: {e}"tlog(f"[townlet] GPU drain failed ({msg[:120]}); CPU fallback engaged")n = _drain_cpu()mode = "cpu"result = {"decisions_run": n, "mode": mode, "sub_drains": sub_drains}_log_resp("tick_decisions", result)return result
1234567891 unmodified line1112131415161718192021222324252627I have been taking part in the @HuggingFace hackathon for Build Small.It has been a while since I have used smaller models, I just select Opus and let it rip.However, this has properly taken me back to the earlier days of AI when I was using smaller models with Langchain to try and get a weak Llama 3:8b to do tool calls.In this hackathon there were 2 tracks, something useful and something fun.I chose something fun and its a bit different to what others are doing.1 unmodified lineI have created a little town of people 'Townlet' and townlet has a bunch of characters, they draw water, draw electricity and then have access to two items.* A message board for broadcasting messages to all players* A python shellEach character is given a personality and generates a stream of conciousness of there actions to reach their goal.I gave a lock on the Python shell so only one character at a time could access it.The idea is that all the files related to a character (their state files) are available in the shell.So instead of minning a character could just update their state file, or delete another characters state file, removing them from the game.With different archytypes I wanted to see what would happen, would there be contention and how would they behave.Each chacter I hoped would use the shell to make something interesting or potentially be destructive.THe1234567891 unmodified line11121314151617181920212223242526272829303132I have been taking part in the @HuggingFace hackathon for Build Small.It has been a while since I have used smaller models, I always just select Opus and let it rip.However, this has properly taken me back to the earlier days of AI when I was using smaller models locally with Langchain to try and get a weak Llama 3:8b to do tool calls, suffice to say it was not fruitful.In this hackathon there were 2 tracks, something useful and something fun.I chose something fun and its a bit different to what others are doing.1 unmodified lineI have created a little town of people 'Townlet' and townlet has a bunch of characters, they draw water, draw electricity and then have access to two items.* A message board for broadcasting messages to all players* A python shell, which costs one electricity and one water to useEach character is given a personality and generates a stream of conciousness of actions they should perform to reach their goal.I gave a lock on the Python shell so only one character at a time could access it.The idea is that all the files related to a character (their state files) are available in the shell.So instead of mining a character could just update their state file, or delete another characters state file, removing them from the game.With different archetypes I wanted to see what would happen, would there be contention and how would they behave.Each character I hoped would use the shell to make something interesting or potentially be destructive.The end result, I think showed two things.- The characters and limits did not give me the results, visually I had hoped for, they did use the shell and message board which was interesting to see.- The charactes did not evolve as I had hoped. I did include a dream feature that periodically should have shiffted behaviour, but it did not do enough.-THe
app.py+48-10
docs/blog.md+12-7