Skip to content

Commit 4aa803e

Browse files
committed
Improve agents.md
1 parent 5d084fc commit 4aa803e

1 file changed

Lines changed: 185 additions & 17 deletions

File tree

AGENTS.md

Lines changed: 185 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,185 @@
1-
- Only generate docstrings for public functions or functions that contain more than 4 lines of code.
2-
- Use the Sphinx style for Python docstrings (e.g. :param my_param: Does something) and never
3-
include a :returns: key.
4-
- The code you generate should always contain type hints in the function prototypes (including
5-
return type hints of None), but type hints are not needed when initializing variables.
6-
- We use uv for everything (e.g. we do `uv run python ...` to run some python code, and
7-
`uv run pytest tests/unit` to run unit tests). Please prefer `uv run python -c ...` over
8-
`python3 -c ...`
9-
- When you create or modify a code example in a public docstring, always update the corresponding
10-
doc test in the appropriate file of `tests/doc`. This also applies to any change in an example of
11-
a `.rst` file, that must be updated in the corresponding test in `tests/doc/test_rst.py`.
12-
- After generating code, please run `uv run ty check`, `uv run ruff check` and `uv run ruff format`.
13-
Fix any error.
14-
- After changing anything in `src` or in `tests/unit` or `tests/doc`, please identify the affected
15-
test files in `tests/` and run them with e.g.
16-
`uv run pytest tests/unit/aggregation/test_upgrad.py -W error`. Fix any error, either in the
17-
changes you've made or by adapting the tests to the new specifications.
1+
# AGENTS.md - TorchJD Development Guide
2+
3+
This file contains guidelines for agentic coding agents working in this repository.
4+
5+
## Running Commands
6+
7+
### Using uv
8+
We use [uv](https://docs.astral.sh/uv/) for all Python operations. Always use `uv run` prefix:
9+
```bash
10+
uv run python ... # Run Python code
11+
uv run pytest ... # Run tests
12+
uv run ty check ... # Type checking
13+
uv run ruff check ... # Linting
14+
uv run ruff format ... # Formatting
15+
```
16+
17+
### Running Tests
18+
19+
Run all unit tests:
20+
```bash
21+
uv run pytest tests/unit
22+
```
23+
24+
Run a single test file:
25+
```bash
26+
uv run pytest tests/unit/aggregation/test_upgrad.py -W error
27+
```
28+
29+
Run a single test function:
30+
```bash
31+
uv run pytest tests/unit/aggregation/test_upgrad.py::test_function_name -W error
32+
```
33+
34+
Run tests with slow tests included:
35+
```bash
36+
uv run pytest tests/unit --runslow
37+
```
38+
39+
Run doc tests (examples in docstrings and .rst files):
40+
```bash
41+
uv run pytest tests/doc
42+
```
43+
44+
Run tests on CUDA (requires GPU):
45+
```bash
46+
CUBLAS_WORKSPACE_CONFIG=:4096:8 PYTEST_TORCH_DEVICE=cuda:0 uv run pytest tests/unit
47+
```
48+
49+
Run tests with coverage:
50+
```bash
51+
uv run pytest tests/unit tests/doc --cov=src
52+
```
53+
54+
### Linting and Type Checking
55+
56+
Run type checking:
57+
```bash
58+
uv run ty check
59+
```
60+
61+
Run linting:
62+
```bash
63+
uv run ruff check
64+
```
65+
66+
Run formatting:
67+
```bash
68+
uv run ruff format
69+
```
70+
71+
Run all checks together:
72+
```bash
73+
uv run ty check && uv run ruff check && uv run ruff format
74+
```
75+
76+
### Building Documentation
77+
78+
From the `docs` folder:
79+
```bash
80+
uv run make html
81+
```
82+
83+
Clean build:
84+
```bash
85+
uv run make clean
86+
```
87+
88+
## Code Style Guidelines
89+
90+
### Docstrings
91+
92+
- Only generate docstrings for public functions or functions with more than 4 lines of code
93+
- Use Sphinx style (`:param my_param: Does something`) - never use `:returns:` key
94+
- Include usage examples in docstrings for public classes
95+
- Always update corresponding doc tests in `tests/doc/` when modifying examples
96+
97+
### Type Hints
98+
99+
- Always include type hints in function prototypes (including `-> None`)
100+
- Do not include type hints when initializing local variables
101+
- Use `ty` for type checking
102+
103+
### Imports
104+
105+
- Use `combine-as-imports = true` (configured in pyproject.toml)
106+
- Order imports: standard library, third-party, local
107+
- Use absolute imports (e.g., `from torchjd.aggregation import UPGrad`)
108+
109+
### Formatting
110+
111+
- Line length: 100 characters (enforced by ruff)
112+
- Quote style: double quotes
113+
- Run `uv run ruff format` before committing
114+
115+
### Naming Conventions
116+
117+
- Classes: `PascalCase` (e.g., `class UPGrad`)
118+
- Functions/methods: `snake_case` (e.g., `def jac_to_grad`)
119+
- Constants: `UPPER_SNAKE_CASE`
120+
- Private functions: prefix with `_` (e.g., `_internal_func`)
121+
- Private modules: prefix with `_` (e.g., `_utils`)
122+
123+
### Error Handling
124+
125+
- Use appropriate exception types from Python standard library or PyTorch
126+
- Provide clear error messages with context
127+
- Validate inputs at function boundaries
128+
- Use assertions for internal invariants
129+
130+
### Testing
131+
132+
- Use partial tensor functions from `tests/utils/tensors.py` (e.g., `ones_()`, `randn_()`)
133+
- This ensures tensors are on correct device and dtype automatically
134+
- Manually move models/rng to DEVICE (defined in `settings.py`)
135+
- Use `ModuleFactory` for creating modules on correct device
136+
- Mark slow tests with `@pytest.mark.slow`
137+
- Run affected tests after changes: `uv run pytest tests/unit/<module> -W error`
138+
139+
### Project Structure
140+
141+
```
142+
src/torchjd/
143+
├── __init__.py # Public API exports
144+
├── autojac/ # Jacobian computation engine
145+
│ ├── _backward.py
146+
│ ├── _jac.py
147+
│ ├── _jac_to_grad.py
148+
│ ├── _mtl_backward.py
149+
│ └── _transform/ # Internal transform implementations
150+
├── autogram/ # Gramian-based engine
151+
│ ├── _engine.py
152+
│ └── ...
153+
├── aggregation/ # Aggregators and weightings
154+
│ ├── _upgrad.py
155+
│ ├── _mean.py
156+
│ └── _utils/ # Internal utilities
157+
└── _linalg/ # Linear algebra utilities
158+
159+
tests/
160+
├── unit/ # Unit tests (mirrors src structure)
161+
├── doc/ # Docstring and rst example tests
162+
└── utils/ # Test utilities (tensors.py, settings.py)
163+
```
164+
165+
### Adding New Aggregators
166+
167+
- Subclass `Aggregator` or `Weighting` base classes
168+
- Aggregators must be **immutable** (no stateful changes)
169+
- Implement the mathematical mapping as documented
170+
- Add corresponding weighting if applicable
171+
- Update `__init__.py` to export the new class
172+
173+
### Deprecation
174+
175+
- Raise `DeprecationWarning` for deprecated public functionality
176+
- Add test in `tests/unit/test_deprecations.py` to verify warning
177+
178+
### Code Quality
179+
180+
- Follow SOLID principles
181+
- Keep code simple and readable
182+
- Prefer explicit over implicit
183+
- Add type hints to all public interfaces
184+
- Document complex algorithms with comments
185+
- Run all checks before submitting: `uv run ty check && uv run ruff check && uv run ruff format`

0 commit comments

Comments
 (0)