Skip to content

Refactor SamplerLoop class into functional API#550

Open
KumarADITHYA123 wants to merge 1 commit intogoogle-deepmind:mainfrom
KumarADITHYA123:refactor/sampler-loop-functional-api
Open

Refactor SamplerLoop class into functional API#550
KumarADITHYA123 wants to merge 1 commit intogoogle-deepmind:mainfrom
KumarADITHYA123:refactor/sampler-loop-functional-api

Conversation

@KumarADITHYA123
Copy link

closes issue #549
This addresses the TODO in _sampler_loop.py by converting the SamplerLoop class into standalone functions that better fit JAX's functional programming style.

The SamplerLoop class was basically just a container for static config with methods attached. I've split this into:

SamplerConfig: frozen dataclass holding the configuration
autoregressive_sample(): JIT-compiled function for regular sampling
autoregressive_stream_sample(): generator function for streaming
_sample_step(): helper function (was a method, now standalone)
Updated _sampler.py to use the new API. The caller now creates a SamplerConfig and calls the appropriate function instead of instantiating SamplerLoop and calling methods on it.

No functional changes. All the sampling logic is identical, just reorganized to be more functional. The public API (Sampler, ChatSampler, ToolSampler) is completely unchanged.

I've verified the syntax and logic preservation locally, but can't run the full test suite due to Python 3.14 environment issues. The CI should handle that when it runs in Python 3.11.

Convert SamplerLoop class to standalone functions following JAX functional programming patterns. This addresses the TODO comment in _sampler_loop.py.

Changes:
- Replace SamplerLoop class with SamplerConfig frozen dataclass
- Implement autoregressive_sample() for non-streaming sampling
- Implement autoregressive_stream_sample() for streaming sampling
- Refactor _sample_step() to module-level function
- Update _sampler.py to use new functional API

The refactoring improves code maintainability by separating configuration from behavior while preserving all existing functionality and maintaining full backwards compatibility with the public API.
@KumarADITHYA123 KumarADITHYA123 force-pushed the refactor/sampler-loop-functional-api branch from 424ffc4 to a5326ac Compare February 14, 2026 20:11
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.

1 participant