Source providers: multi-epoch out-of-core fits
Chapter 18 of the Development Explained book. Prerequisites: the streaming executor from chapter 7 and the stochastic-gradient family from chapter 17. This chapter explains the why behind the
streaming-source-providerchange, from zero.
1. The problem: a stream cannot rewind
Every out-of-core fit consumed a one-shot source: a fallible iterator that yields batches and then ends. That is exactly right for an exact accumulator — a Gram matrix, a running mean, a category table — because reading the data twice would double-count it.
It is wrong for a stochastic estimator. SKSGDClassifier and SKSGDRegressor learn one
sample at a time; one pass over the stream is one epoch, and one epoch of SGD usually
underfits. The in-memory fit runs up to a thousand epochs; the streamed fit ran one. The
PRD promised out-of-core SGD, not out-of-core one-epoch SGD.
flowchart LR
accTitle: One-shot sources compared with reopenable providers
accDescr: A one-shot source yields batch zero, one and two, then reaches end of file and cannot rewind, so it stops. A reopenable provider opens the source again for each pass, replaying the same batches on pass zero, one and two.
classDef once fill:#fee2e2,stroke:#dc2626,color:#7f1d1d,stroke-width:2px
classDef re fill:#dcfce7,stroke:#16a34a,color:#14532d,stroke-width:2px
classDef io fill:#e0f2fe,stroke:#0284c7,color:#0c4a6e,stroke-width:2px
subgraph OneShot["one-shot source"]
O0["batch 0"]:::io --> O1["batch 1"]:::io --> O2["batch 2"]:::io --> OE["EOF"]:::once
OE -.->|"cannot rewind"| OX["stop"]:::once
end
subgraph Reopen["reopenable provider"]
P0["pass 0: open() → b0 b1 b2 → EOF"]:::re
P1["pass 1: open() → b0 b1 b2 → EOF"]:::re
P2["pass 2: open() → b0 b1 b2 → EOF"]:::re
P0 --> P1 --> P2
end
2. Why not Into<Source>?
The obvious reach — accept “anything convertible into a source” — does not work in Rust:
#![allow(unused)]
fn main() {
fn fit_streaming<S>(&self, source: impl Into<S>)
where S: SKSupervisedLazySource<F> // E0283: cannot infer S from the argument
}
Sappears only in the bound, so the compiler has no target for the conversion.impl<T> From<T> for Talready makes every concrete sourceInto<itself>; a factory conversion would need a per-type impl, and the orphan rule forbids it for foreign types.- A closure has an unnameable type, so nobody can implement
From/Intofor it.
The working seam is a dedicated trait, implemented for a reference to a concrete source
and for a wrapper around a factory. No dyn, no allocation, no new dependency.
3. The provider contract
#![allow(unused)]
fn main() {
pub trait SKSupervisedSourceProvider<F> {
type Source<'pass>: SKSupervisedLazySource<F> + Send where Self: 'pass;
const IS_REOPENABLE: bool;
fn open(&mut self) -> Result<Self::Source<'_>, SKError>;
}
}
The generic associated type is the key: it lets one trait return a borrow for a concrete source and an owned source for a factory, under the same signature.
| Argument | Implementation | Passes |
|---|---|---|
&mut source | residual impl for &mut S | 1 |
| `SKReopenableSource::new( | open_file(path))` |
The residual implementation is what keeps every existing call compiling:
#![allow(unused)]
fn main() {
impl<F, S: SKSupervisedLazySource<F> + Send> SKSupervisedSourceProvider<F> for &mut S {
type Source<'pass> = &'pass mut S where Self: 'pass;
const IS_REOPENABLE: bool = false;
fn open(&mut self) -> Result<Self::Source<'_>, SKError> { Ok(&mut **self) }
}
}
The reopenable wrapper stores a FnMut() -> Result<S, SKError> and calls it once per pass,
so each epoch sees the same data in the same order — the property that makes a streamed fit
reproducible.
classDiagram
accTitle: The source-provider trait and its two implementations
accDescr: SKSupervisedSourceProvider is a trait with an associated source type, a reopenable flag and an open method. ConcreteSource implements it for a mutable source used in one pass, and SKReopenableSource implements it with a factory that yields N passes.
class SKSupervisedSourceProvider {
<<trait>>
+Source~pass~ Source
+bool IS_REOPENABLE
+open() Result
}
class ConcreteSource {
+next_supervised_batch() Option
}
class SKReopenableSource {
-factory FnMut
+IS_REOPENABLE = true
}
SKSupervisedSourceProvider <|.. ConcreteSource : "&mut S, one pass"
SKSupervisedSourceProvider <|.. SKReopenableSource : "factory, N passes"
4. One rule, classified by estimator
The executor does not guess. SKStreamingPassPolicy says how many passes are allowed:
| Class | Estimators | Pass policy |
|---|---|---|
| Exact accumulator | scalers, polynomial features, simple imputer, OLS, ridge, lasso, elastic net, logistic regression | SinglePass — reading twice would double-count $n$, $\Sigma x$, $X^{\mathsf T}X$ |
| Iterative | SKSGDClassifier, SKSGDRegressor (and later mini-batch k-means, incremental PCA) | Iterative { maximum_number_of_passes } — one epoch per pass |
A one-shot provider with a budget above one takes one pass and emits a
tracing::warn!; the model reports epochs_taken() == 1 so the clamp is observable without
logs. Silently pretending the budget was honoured would be worse than the warning.
SKOneHotEncoder is the declared exception: its streaming seam consumes categorical
views (IntoIterator<Item = SKCategoricalView>), not numeric blocks, so it stays outside
the provider trait until an owned categorical block exists. Every numeric estimator in the
library uses the seam.
5. Multi-epoch SGD, pass by pass
Each pass opens a source, runs one epoch over its batches with the same learning state, and merges the epoch objective into the convergence tracker:
sequenceDiagram
accTitle: Epoch loop over a reopenable provider
accDescr: For each pass in the epoch budget the estimator opens the provider, receives a source, and reads batches until exhaustion, applying one update per row. At the end of each pass it merges the epoch objective and updates the convergence tracker.
participant E as SKSGDRegressor
participant P as provider
participant S as source
participant W as learning state
loop pass in 0..budget
E->>P: open()
P-->>E: source
loop each batch
E->>S: next_supervised_batch()
S-->>E: batch
E->>W: one update per row
end
E->>E: merge the epoch objective, update the tracker
end
Note over E: epochs_taken = passes<br/>converged = plateau?
The schedule counter and the averaged iterate live in the state, so they continue across
passes exactly as they do across partial_fit. The convergence policy is unchanged:
StrictraisesNotConvergedonly when the full requested budget executed without a plateau;Permissivereturns the model withconverged() == false;- a provider-limited fit (one-shot, fewer passes than the budget) returns the model with
converged() == falseand never raises.
Early stopping over a stream is still refused with UnsupportedOperation: it needs a
held-out validation slice the provider does not promise to retain. The refusal is explicit,
not a silent no-op.
6. Walking the real tests
sciencekit_common/src/batching/provider/*_tests.rs: a concrete source reports itself as not reopenable and yields once; the factory is invoked once per pass; a factory error aborts; the feature-only and supervised families agree on passes and order.sciencekit_common/src/execution/streaming_driver/provider_entry_points_tests.rs: a one-shot provider runs one pass despite a budget of five; a reopenable provider runs the budget.- Scalers, polynomial features and the imputer: a reopenable provider with a larger budget still takes one pass and the model equals the dense fit.
- OLS, ridge, lasso, elastic net and logistic: the same one-pass rule, with the accumulator compared against the dense fit.
- SGD: a reopenable multi-epoch stream equals the in-memory fit under a fixed seed, with the
schedule counter and the average carried across passes; a one-shot provider takes one pass
and does not raise;
Strictraises only after the full budget.
7. Recap
- A source provider is the seam that lets one method accept a concrete source and a
factory, because
Intocannot express it in Rust. - The GAT returns a borrow or an owned source;
IS_REOPENABLEis a compile-time fact. - The number of passes follows the estimator class: exact accumulators take one, iterative estimators take the epoch budget from a reopenable provider.
- The one-shot call shape survives, the clamp is reported by
epochs_taken(), and the convergence policy decides what an unconverged streamed fit does.
Next: the batch contract itself becomes representation-generic, so sparse data can stream without ever being densified.