Guidance for AI agents working in the torchrecurrent repository.
- Package:
torchrecurrent - Purpose: PyTorch-compatible recurrent neural network cells and layers from research literature, primarily for academic research.
- Python:
>=3.9; CI covers Python 3.9-3.14 on Linux, Windows, and macOS. - Runtime dependency policy:
torchis the only runtime dependency. - Style: Black, line length 92.
Run the narrowest useful command first, then broaden when the change touches shared behavior.
pip install -e .[test]
pytest
coverage run -m pytest
black .
flake8
pre-commit run --all-filespytestruns the test suite.coverage run -m pytestmatches the CI test command.black .formats with the configured 92-character line length.flake8excludesdocs/,benchmarks/, andtests/.pre-commit run --all-filesruns Black and Ruff fixes before committing.
torchrecurrent/base.py: abstract base classes for cells and layers.torchrecurrent/cells/: each*_cell.pydefines both a cell and its layer.torchrecurrent/benchmarks/: packaged task generators.benchmarks/: standalone training scripts and saved runs, not packaged.tests/test_cells.py: per-cell shape, dtype, and state checks.tests/test_layers.py: per-layer stacking andbatch_firstchecks.docs/: Sphinx docs and the model catalog indocs/models.rst.
There is no layers/ directory. Keep cell and layer implementations together in
the relevant torchrecurrent/cells/<name>_cell.py file.
BaseSingleRecurrentCelluses one hidden stateh.BaseDoubleRecurrentCelluses LSTM-style(h, c)state.BaseSingleRecurrentLayerandBaseDoubleRecurrentLayeriterate cell stacks over the time dimension.- Weights are concatenated per gate into
weight_ihandweight_hh, then split with.chunk(n, 0)inforward. - Cells support input shaped
(input_size,)or(N, input_size)via the base_preprocess_*helpers. - Bias controls are separate:
biasfor input-side terms andrecurrent_biasfor recurrent-side terms. - Initializers are configurable through
kernel_init,recurrent_kernel_init,bias_init, andrecurrent_bias_init; defaults arexavier_uniform_for weights andzeros_for biases.
- Create
torchrecurrent/cells/<name>_cell.py. - Define
<Name>Cellfrom the matching single-state or double-state base cell. - Define
<Name>from the matching layer base and callself.initialize_cells(<Name>Cell, **kwargs). - Use
torchrecurrent/cells/mgu_cell.pyas the implementation and docstring template. - Re-export both classes from
torchrecurrent/cells/__init__.pyandtorchrecurrent/__init__.py, including each__all__. - Add the cell to
CELL_CASESintests/test_cells.py. - Add the layer to
tests/test_layers.py. - Add docs under
docs/api/, generated autosummary coverage, anddocs/models.rst.
- Format Python with Black before finishing changes that touch code.
- Keep comments sparse. Add comments only when they explain non-obvious math, paper-specific behavior, numerical stability choices, or API compatibility.
- Do not add comments that merely restate the code.
- Match the existing Google/NumPy-style docstrings with a math block, arXiv link, Args, Inputs, Outputs, and Variables sections.
- Keep tests table-driven and update the relevant parametrized cases when adding or renaming public models.
- Preserve native PyTorch-style interfaces that mirror
torch.nn.RNNandtorch.nn.RNNCellwhere applicable. - Keep the three export sites synchronized:
torchrecurrent/cells/__init__.py,torchrecurrent/__init__.py, and each__all__. - Respect third-party licenses.
NASCellis an Apache-2.0 reimplementation in an MIT-licensed project.
- Adding, removing, or changing runtime dependencies. Do not add dependencies just to simplify an implementation.
- Exporting or otherwise wiring up
rhn_cell.py; it exists but is intentionally not part of the public API. - Large rewrites, API breaks, renamed public classes, or changes to package metadata and release configuration.
- Broad documentation regeneration if it would create large generated diffs.
- Do not create a separate
layers/package. - Do not commit or edit saved artifacts under
benchmarks/.../runs/. - Do not add unnecessary comments.
- Do not skip tests silently; report any tests that could not be run.
- Do not introduce non-
torchruntime dependencies without explicit approval.
Before finishing, check the work against the scope of the change:
- Code is formatted with Black when Python files changed.
- Relevant tests were run, or the reason they were not run is stated.
- New or renamed public models are exported from both package entry points.
- Tests and docs are updated when public behavior changes.
- The final response summarizes changed files, verification, and any remaining risk or follow-up.