batch_utils
Small shared helpers for turning backend forward-backward / sampling
outputs into plain Python, used by the SFT / SDFT algorithms' step_metrics
and batch construction.
Kept backend-agnostic: MockBackend emits Python lists; the real
TinkerBackend emits tinker.TensorData (with .to_torch()) and
tinker SamplingResponse objects. These helpers normalize both.
attribute__all__= ['coerce_floats', 'extract_weights', 'extract_completion_tokens_from_response']funccoerce_floats(value) -> list[float] | NoneBest-effort: turn a TensorData / list / torch.Tensor into list[float].
Returns None when the value can't be coerced (so callers can skip it).
paramvalueAnyReturns
list[float] | Nonefunc_flatten_floats(seq) -> list[float] | NoneFlatten an arbitrarily-nested list of numbers to list[float].
Returns None if a leaf isn't coercible to float.
paramseqAnyReturns
list[float] | Nonefuncextract_weights(datum) -> AnyPull the per-position weight mask out of a Datum's loss_fn_inputs.
paramdatumtinker.DatumReturns
typing.Anyfuncextract_completion_tokens_from_response(response) -> list[int]Pull the token-id list out of a tinker SamplingResponse-shape object.
Real tinker exposes .sequences[0].tokens; MockSamplingClient does the
same; either way we get a list of ints back.
paramresponseAnyReturns
list[int]