Add ICL context caching for the PyTorch backend + expose in sklearn API#62
Merged
weihaokong merged 1 commit intoJul 13, 2026
Merged
Conversation
astonishedrobo
requested review from
abhidas,
erzel,
rajatsen91,
siriuz42,
tamannarayan and
weihaokong
as code owners
July 8, 2026 16:37
Contributor
Author
|
Hi everyone, I wanted to kindly follow up on this PR. Is there something you will want me to add or modify something? |
weihaokong
approved these changes
Jul 13, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Scope
This PR adds in-context caching to the PyTorch backend, opt-in via
cache_context=TrueonTabFMClassifier/TabFMRegressor(default:False).When it's on, two options control the cache's memory footprint:
maybe_quantize_kv_cache(default:True): int8-quantize the ICL KV cache toreduce memory; set
Falseto keep the cache full precision.keep_cache_on_device(default:True): caches stay on GPU by default; withFalse, they are stored on CPU between prediction calls.I also ran a benchmark on Adult (OpenML 1590) for throughput and accuracy; results
are in the Benchmark section.
Usage
The following block shows the usage example.
Benchmark
I benchmarked context caching on Adult (OpenML 1590), a binary classification
task: predict whether annual income is > 50K or <= 50K. I used the following
setup:
batch_sizeparameter, which sets the number of ensemble members per forward).run at the same precision, so any difference is the cache alone.
keep_cache_on_device=True, so all members' caches are kept onthe GPU (not offloaded to CPU).
2,000 test, n_estimators=2), where rounding is negligible, to check the cached
and uncached paths produce the same result.
uncached runs in the predicted probability of the > 50K class.
The following table summarizes the bf16 runs.
Observations:
model run, with no observed effect on ROC-AUC.
1e-5 tolerance used for comparing cached vs uncached predictions.
all members' caches are kept on the GPU at once; uncached stays at 4.5 GB.
Changelog
tabfm/src/pytorch/model.pyprefill()/decode()toTabFM: encode the context once and reuseit during prediction.
MultiheadAttention,MultiheadAttentionBlock,Encoder,InducedSelfAttentionBlock,SetTransformer,ColEmbedding,ICLearning) sodecode()reuses thecached KV instead of recomputing it.
QuantizedTensor,_quantize_tensor,ICLearningCache.quantize(),move_cache_to_device. Only the ICL KV isquantized; column representations and training row counts stay full
precision.
tabfm/src/classifier_and_regressor.pycache_context,maybe_quantize_kv_cache,keep_cache_on_devicetoboth estimators.
fit()builds the per-member cache; prediction usesdecode()with the cached context. JAX raisesNotImplementedError.transform_context_only,transform_test_only,prepare_test_tensors.tabfm/src/pytorch/__init__.pytabfm/src/pytorch/model_test.pyprefill+decode) gives the sametest-row output logits as the uncached full forward pass: max absolute
difference <= 1e-5 in fp32; bf16 difference is reported for reference.
within tolerance (2% relative, observed 0.34%).
cache is preserved after a device swap.
tabfm/src/classifier_and_regressor_pytorch_test.pydisabled.