Skip to content

Cache (2/5): Cache inference at do_inference - #122

Merged
ErlisLushtaku merged 6 commits into
mainfrom
cache-on-118/02-do-inference
Sep 22, 2026
Merged

ErlisLushtaku merged 6 commits into
mainfrom
cache-on-118/02-do-inference

Conversation

@ErlisLushtaku

@ErlisLushtaku ErlisLushtaku commented Sep 9, 2026 •

Copy link
Copy Markdown
Collaborator

Description

Moves cache lookup and writes to do_inference.

  • PreparedModel defers backend construction until an input misses the cache.
  • Cache identity combines the canonical model input with the validated model descriptor.
  • Mixed batches run only missing inputs and restore the original output order.
  • Completion and judgement cache classes define their own stored rows.
  • Cached judgements restore first-token logprobs. Hits do not record provider usage.

This is stacked on #121.

@ErlisLushtaku
ErlisLushtaku force-pushed the cache-on-118/02-do-inference branch from 97138e9 to 965bacf Compare September 9, 2026 13:01
@ErlisLushtaku ErlisLushtaku changed the title Cache: WIP (2/6) Cache inference at do_inference Cache: WIP (2/5) Cache inference at do_inference Sep 9, 2026
@ErlisLushtaku ErlisLushtaku changed the title Cache: WIP (2/5) Cache inference at do_inference Cache (2/5): Cache inference at do_inference Sep 15, 2026
@ErlisLushtaku
ErlisLushtaku marked this pull request as ready for review September 15, 2026 07:40
@kargibora
kargibora force-pushed the cache-on-118/01-local-store branch from b1156b8 to 52a4620 Compare September 15, 2026 12:54
@kargibora
kargibora force-pushed the cache-on-118/02-do-inference branch from 58f0f59 to 969347c Compare September 15, 2026 12:54
Comment thread judgearena/inference.py Outdated


@dataclass(frozen=True)
class InferenceCache(ABC):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we create a seperate file for storing these cache objects?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, the cache-specific types now live in judgearena/cache/inference.py in #127. The provider-neutral InferenceResult stays in judgearena/inference.py.

Comment thread judgearena/models.py Outdated
cached_rows = store.query(input_hashes).set_index("input_hash")
results: list[InferenceResult | None] = [
(
InferenceResult(**cache.cached_result(cached_rows.loc[key]).__dict__)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why we are converting CachedInferenceResult back to InferenceResult? If so we may not need CachedInferenceResultat all

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, CachedInferenceResult is removed and cache hits now return InferenceResult directly.

Comment thread judgearena/models.py Outdated
)
for index, result in zip(missing_indices, generated, strict=True):
results[index] = result
cache.save_outputs(

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this fail? If it fails for some reason the entire pipeline fails. We can put try except

  try:
       cache.save_outputs(...)
   except CacheWriteError as exc:
       logger.warning("Could not save inference cache: %s", exc)

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, cache write failures now log and preserve the generated results.

Comment thread judgearena/models.py Outdated
return_top_logprobs: bool = False,
*,
stage: str = "unspecified",
cache_metadata: list[dict] | None = None,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think one thing that is confusing for me is this PR introduces some changes like cache_metadata which will be provided in the future PR's in #124. Design-wise it makes sense to first introduce it without wiring however while reading the PR it creates some readability problems. I think if necessary, creating large PR's (~1000-2000LOC) where we introduce the functionality while introducing the changes makes more sense to me.

Not a requirement for this PR (as I will review the entire stack as a single one)

Comment thread judgearena/models.py Outdated


def do_inference(
def _do_inference_uncached(

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can rename this to _call_model or _invoke_model or similar (I am not creative enough). Because we will be using it to do inference for uncached samples, this reads like we will be doing inference without using the cache (although it is true we are doing it for uncached inputs, flow can be better)

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Renamed it to _run_backend_inference.

Comment thread judgearena/models.py Outdated
stage=stage,
)

try:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know we need this for closing the cache but it becomes hard to parse with lots of try: excepts

@ErlisLushtaku ErlisLushtaku Sep 22, 2026 •

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, I moved cache open and close handling into a context manager and read and write failures into small helpers.

@kargibora kargibora left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@ErlisLushtaku
ErlisLushtaku changed the base branch from cache-on-118/01-local-store to main September 22, 2026 22:29
@ErlisLushtaku
ErlisLushtaku merged commit 1ab69ef into main Sep 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants