-
Notifications
You must be signed in to change notification settings - Fork 143
Add Copilot instructions file #1643
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+125
−3
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
# PyTensor Copilot Instructions | ||
|
||
## Overview | ||
|
||
**PyTensor**: Python library for defining, optimizing, and evaluating mathematical expressions with multi-dimensional arrays. Focus on hackable graph analysis and manipulation. Supports C, JAX, and Numba compilation backends. ~27MB, 492 Python files, Python support as per numpy NEP 29, uses NumPy, SciPy, pytest. | ||
|
||
## PyTensor Design Principles | ||
|
||
Graph manipulation in Python, graph evaluation out of Python. | ||
Emulate NumPy user-facing API as much as possible. | ||
|
||
### API Differences from NumPy | ||
|
||
1. **Lazy evaluation**: Expressions are symbolic until `pytensor.function()` compiles or `.eval()` evaluates | ||
2. **Pure semantics**: `new_x = x[idx].set(y)` instead of `x[idx] = y` | ||
3. **Immutable/hashable**: PyTensor variables are hashable. `a == b` tests identity (`a is b`), not elementwise equality. | ||
4. **Static shapes**: Broadcasting requires static shape of 1. Valid: `pt.add(pt.vector("x", shape=(1,)), pt.vector("y"))`. Invalid: `pt.add(pt.vector("x", shape=(None,)), pt.vector("y"))` with x.shape=1. | ||
5. **Static rank and type**. PyTensor functions accepts variables with a specific dtype and number of dimensions. Length of each dimension can be static or dynamic. | ||
|
||
## Code Style | ||
|
||
**Uses pre-commit with ruff** | ||
|
||
**Performance** | ||
* Could should be performant | ||
* Avoid expensive work in hot loops | ||
* Avoid redundant checks. Let errors raise naturally | ||
* In contrast, silent errors should be prevented | ||
|
||
**Comments**: Should be used sparingly, only for complex logic | ||
|
||
**Testing**: Should be succinct | ||
- Prefer `tests.unittest_tools.assert_equal_computations` over numerical evaluation | ||
- Test multiple inputs on one compiled function vs multiple compilations | ||
- Minimize test conditions. Be smart, not fearful | ||
- Integrate with similar existing tests | ||
|
||
## Repository Structure | ||
|
||
### Root | ||
- `.github/` (workflows), | ||
- `doc/` (docs) | ||
- `pyproject.toml` (config), | ||
- `setup.py` (Cython build), | ||
- `conftest.py` (pytest config), | ||
- `environment.yml` (conda env) | ||
|
||
### Source (`pytensor/`) | ||
- `configdefaults.py`: Config system (floatX, mode) | ||
- `gradient.py`: Auto-differentiation | ||
- `compile/`: Function compilation | ||
- `graph/`: IR and optimization (`graph/rewriting/`) | ||
- `link/`: Backends (`c/`, `jax/`, `numba/`, `mlx/`, `pytorch/`) | ||
- `tensor/`: Tensor ops (largest module, subdirs: `random/`, `rewriting/`, `conv/`) | ||
- `scalar/`: Scalar ops | ||
- `scan/`: Loop operations (`scan_perform.pyx` Cython) | ||
- `sparse/`: Sparse tensors | ||
- `xtensor/` Tensor Ops with dimensions (lowers to Tensor ops) | ||
|
||
### Tests (`tests/`) | ||
Mirrors source structure. `unittest_tools.py` has testing utilities. | ||
|
||
## Critical: Environment & Commands | ||
|
||
**ALWAYS use micromamba environment**: PyTensor is pre-installed as editable in `.github/workflows/copilot-setup-steps.yml`. | ||
|
||
All commands MUST use: `micromamba run -n pytensor-test <command>` | ||
|
||
Example: `micromamba run -n pytensor-test python -c 'import pytensor; print(pytensor.__version__)'` | ||
|
||
## Testing & Building | ||
|
||
### Running Tests (ALWAYS use micromamba) | ||
|
||
```bash | ||
micromamba run -n pytensor-test python -m pytest tests/ # All tests | ||
micromamba run -n pytensor-test python -m pytest tests/test_updates.py -v # Single file | ||
micromamba run -n pytensor-test python -m pytest tests/ --runslow # Include slow tests | ||
``` | ||
|
||
Tests are run with `config.floatX == "float32"` and `config.mode = "FAST_COMPILE"`. If needed: | ||
- Cast numerical values `test_value.astype(symbolic_var.type.dtype)` | ||
- Use custom function mode `get_default_mode().excluding("fusion")` or skip tests in `FAST_COMPILE` if they are not directly relevant to the mode. | ||
|
||
Alternative backends (JAX, NUMBA, ...) are optional. Use `pytest.importorskip` to fail gracefully. | ||
|
||
### Pre-commit | ||
|
||
```bash | ||
micromamba run -n pytensor-test pre-commit | ||
``` | ||
|
||
### MyPy | ||
|
||
```bash | ||
micromamba run -n pytensor-test python ./scripts/run_mypy.py --verbose | ||
``` | ||
**PyTensor incompatible with strict mypy**. Type-hints are for users/developers not to appease mypy. Liberal `type: ignore[rule]` and file exclusions are acceptable. | ||
|
||
### Documentation | ||
|
||
```bash | ||
micromamba run -n pytensor-test python -m sphinx -b html ./doc ./html # Build docs (2-3 min) | ||
``` | ||
**Never commit `html` directory**. | ||
|
||
|
||
## CI/CD Pipeline | ||
|
||
### Workflows (`.github/workflows/`) | ||
1. **test.yml**: Main suite - Several Python versions, fast-compile (0/1), float32 (0/1), 7 test parts + backend jobs (numba, jax, torch) | ||
2. **mypy.yml**: Type checking | ||
3. **copilot-setup-steps.yml**: Environment setup | ||
|
||
|
||
## Trust These Instructions | ||
These instructions are comprehensive and tested. Only search for additional information if: | ||
1. Instructions are incomplete for your specific task | ||
2. Instructions are found to be incorrect | ||
3. You need deeper understanding of an implementation detail | ||
|
||
For most coding tasks, these instructions provide everything needed to build, test, and validate changes efficiently. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it effective to give it an "out" like this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's the prompt they suggested on the GitHub docs