Reuse transformations under a versioned semantic key
Place caches around expensive stable work while preserving random augmentation, artifact identity, and invalidation rules across training experiments.
On this page
The question for today
What you’ll understand
- Define a preprocessing cache key from every influencing input.
- Explain why caching after a random transform can freeze intended variation.
- Compare hit-path cost with capacity and correctness constraints.
A training run becomes faster after caching tokenized examples, then a tokenizer update appears to have no effect. The cache key names only the sample, so the old token array is returned under a new experiment. A preprocessing cache saves work only when the reused result answers the same transformation question. Its identity must include the dataset and transformation semantics, and its placement must preserve whatever randomness the experiment intends to refresh. A fast stale result is a correctness failure.
Key the full deterministic transformation
A useful cache identity includes the dataset snapshot, source record identity, transform code or version, tokenizer or vocabulary artifact, relevant configuration, and output representation. Include normalization rules, truncation length, special-token policy, and dtype when they affect the result. A pathname or experiment label alone is not enough. If source bytes can change behind an identity, either make that identity immutable or bind the actual approved content. Record schema and version information so a reader can reject an incompatible cached entry rather than reinterpret yesterday's bytes under today's assumptions.
Validate writes and publication as well as keys. Concurrent workers must not expose an incomplete entry as a complete result, and a retry should not silently overwrite correct content with a partial result. Verify the stored representation and recover from corrupt entries through an explicit miss or error path. The storage mechanism may provide atomic publication for one object, but the application still defines what that object means. Shared caches need a clear boundary for who can write results consumed by other jobs. Performance reuse is justified by content and transformation identity, not merely by the existence of a file.
Place random work on the intended side of the cache
Caching deterministic decoding or normalization before random cropping can preserve new crops on later visits while avoiding repeated stable work. Caching after a random transform stores that particular realization, so later visits may reuse it instead of drawing a new augmentation. That can be intentional, but it must be declared. Another design keys each augmentation by a stable occurrence seed or index; it trades more entries for explicit reproducibility. Decide whether epoch, view index, and random state belong in the result identity rather than assume one source sample always has one correct transformed output.
TensorFlow 2.16.1 Dataset.cache documents persistence when a filename is reused: changing upstream operations does not automatically refresh an existing file cache. It also requires completing the input iteration to finalize the cache and returns the cached element sequence on subsequent iterations. Shuffle after a stable cache if new order is desired; caching a shuffled or augmented stream can preserve more than intended. Treat these as version-scoped operator semantics, not a guarantee that every framework or custom cache behaves identically. Explicit cache naming, invalidation, and completion checks belong in the experiment workflow.
Measure reuse value within the storage budget
Tokenized and decoded forms can be much larger than compressed sources, so estimate retained bytes and eviction behavior before caching everything. Compare fill cost, subsequent reuse, serialization overhead, and whether the saved work was actually limiting training. A cache with frequent version churn may have little useful reuse even if its raw capacity is large. Separate cold-start measurements from warm-hit measurements and retain the exact keys and source identities used. A high reported hit rate cannot show that randomization remained correct or that a new tokenizer was applied.
The cache lab demonstrates capacity relative to an explicit working set. For each separate run, Reset, set both capacity C and stride s=4, predict, then inspect the initial sixteen requests. Capacity four retains the four-address cycle after four misses, producing twelve hits; capacity two produces no hits. This deterministic LRU model has no tokenizer, source version, random augmentation, or integrity check. It shows why enough capacity can enable reuse, while its missing semantic key fields expose what a real preprocessing cache must additionally verify before returning a result.
Work it through
Calculate a hit-path benefit without approving stale entries
A fictional preprocessing service has a complete hit-path time of 2 ms and complete miss-path time of 10 ms for a fixed sample type. Suppose a measured window has 80% valid hits, with unchanged transformation semantics.
The weighted average is 0.8×2+0.2×10=3.6 ms per request. This arithmetic uses complete path times and the stated hit fraction.
Compared with all requests taking the 10 ms miss path, the mean-time ratio is 10/3.6≈2.78. It is not automatically the end-to-end training speedup.
If a tokenizer changes without a key change, the old hits are not valid hits for the new transformation question. Repair cache identity or invalidate entries before interpreting the faster timing as useful work.
The valid-reuse model gives 3.6 ms average request time and about 2.78-times local mean-time improvement under the stated conditions.
The living notebook
Explore the model
Interactive lab · cache
Capacity enables reuse but does not validate the reused meaning
For each view: Reset, set both C and s=4, predict, then inspect. Initial sixteen requests give 12 hits/4 misses at C=4 and 0 hits/16 misses at C=2. This exact LRU trace has no transform versions, source content, or augmentation state.
- Hits / requests
- 0 / 16
- Hit rate
- 0%
- Address-stream period
- 16 addresses
A fully associative LRU cache starts empty and requests address (i·s) mod 16. Hits move an address to the newest position; misses evict the oldest when full. Playback starts with 16 requests and adds two per second, capped at 4096 explicitly simulated requests. This toy stream is not a processor-cache benchmark. Parameter changes pause playback and recompute the model at the current elapsed time. Reset restores both parameters and returns time to zero. Playback starts paused. Adjust either parameter, then choose Play or Step.
Look forUse an explicit semantic key and a valid publication path. Capacity and hit rate cannot prove that the reused result belongs to the current experiment.
Pause & reason
Check your understanding
Your turn
Try it yourself
A fictional cache has valid hit fraction 0.75, a complete hit path of 1 ms, and a complete miss path of 9 ms. Compute mean request time. What if the result’s normalization version is omitted from its key?
Take a moment to make a prediction or work it out on paper. Open a hint when you need one.
Give me a hint
Weight the complete paths, then separate timing arithmetic from semantic validity.
Compare with the solution
Mean request time is 0.75×1+0.25×9=3 ms under valid reuse. Omitting an influencing normalization version can return stale results after an update; the timing remains an observation, but it no longer establishes correct current preprocessing.
Keep the idea
Take it with you
- Cache identity includes every input that changes the stored transformation.
- Cache placement determines whether random choices and order are reused.
- Evaluate valid reuse, fill cost, memory, and actual bottlenecks together.
Read further
Original sources and deeper explanations to follow at your own pace.
Optional progress, saved in this browser when available. Revisit any lesson at any time.
Lesson not marked complete.