Copilot instructions
.github/copilot-instructions.mdCopilot instructions
Quality
100/100
Scores the file, not the repository.Length
594 words
17 headings · 5 code blocksRepository
102k
— · pushed 0 days agoLast changed
3 days ago
First indexed 3 days ago.1# PyTorch Copilot Instructions23This is the PyTorch machine learning framework codebase. These instructions help AI agents navigate and contribute effectively.45## Architecture Overview67### Core Components89- **c10/** - Core library (C++-10 compatible) for essential, binary-size-conscious functionality10- **aten/** - ATen tensor library (C++), PyTorch's foundation without autograd11 - `aten/src/ATen/native/` - Modern operator implementations (CPU/CUDA/MPS/sparse)12 - `aten/src/ATen/native/native_functions.yaml` - **Critical**: Declarative operator registry13- **torch/** - Python bindings and public API14 - `torch/csrc/` - C++ Python bindings (hand-written and generated)15 - `torch/csrc/autograd/` - Reverse-mode automatic differentiation16 - `torch/csrc/jit/` - TorchScript JIT compiler17- **torchgen/** - Code generation tooling that reads `native_functions.yaml`18- **tools/** - Build scripts, autograd derivatives, code generation1920### The Code Generation Workflow2122**Most operator changes require editing `native_functions.yaml`**, not direct C++ files. This YAML file:231. Declares operator signatures, variants (function/method), and dispatch behavior242. Gets processed by `torchgen/` to generate C++/Python bindings253. Produces headers in `build/aten/src/ATen/` during compilation2627Example entry structure:28```yaml29- func: my_op(Tensor self, Scalar alpha=1) -> Tensor30 variants: function, method31 dispatch:32 CPU: my_op_cpu33 CUDA: my_op_cuda34```3536After editing `native_functions.yaml`, implement kernels in `aten/src/ATen/native/` (see `aten/src/ATen/native/README.md`).3738## Development Workflows3940### Building from Source4142**Never run `setup.py` directly** - use pip with editable install:43```bash44python -m pip install --no-build-isolation -v -e .45```4647Speed up builds:48- `DEBUG=1` - Debug symbols with `-g -O0`49- `USE_CUDA=0` - Skip CUDA compilation50- `BUILD_TEST=0` - Skip C++ test binaries51- Install `ninja` (`pip install ninja`) for faster builds52- Use `ccache` for incremental compilation caching5354Rebuild specific targets: `(cd build && ninja <target>)`5556### Testing5758**Critical**: DO NOT run entire test suites. Run specific tests only:59```bash60python test/test_torch.py TestTorch.test_specific_case61```6263**Test structure**: All tests use `torch.testing._internal.common_utils`:64```python65from torch.testing._internal.common_utils import run_tests, TestCase6667class TestFeature(TestCase):68 def test_something(self):69 # Use self.assertEqual for tensor comparisons70 pass7172if __name__ == "__main__":73 run_tests()74```7576**For bug fixes**: Create a standalone reproduction script first, verify it fails, then fix and add to appropriate test file.7778### Linting7980Run linter (not pre-commit): `lintrunner -a` (auto-applies fixes)8182## Project-Specific Conventions8384### Memory and Storage85- **Storage is never nullptr** (but `StorageImpl.data` may be nullptr for unallocated outputs)86- CUDA device info lives in storage objects8788### Python-C++ Integration (`torch/csrc/`)89- Always include `Python.h` **first** to avoid `_XOPEN_SOURCE` redefinition errors90- Use `pybind11::gil_scoped_acquire` before calling Python API or using `THPObjectPtr`91- Wrap entry points with `HANDLE_TH_ERRORS` / `END_HANDLE_TH_ERRORS` for exception conversion9293### Dispatch System94- PyTorch uses operator dispatch to route calls to backend-specific kernels95- Prefer `CompositeExplicitAutograd` dispatch when writing device-agnostic compound ops96- See `aten/src/ATen/native/README.md` for dispatch keyword guidance9798## Git Workflow (AI Agent Specific)99100When preparing PRs from this environment:101```bash102git stash -u103git reset --hard $(cat /tmp/orig_work.txt) # Reset to LOCAL branch104git stash pop105# Resolve conflicts if necessary106```107108## Common Gotchas1091101. **Editing generated files** - If it's in `build/`, don't edit it. Edit the source template or `native_functions.yaml`1112. **NVCC template compilation** - NVCC is stricter about C++ than gcc/clang; code working on Linux may fail Windows CI1123. **Windows symbol visibility** - Use `TORCH_API` macros for exported symbols (required on Windows, optional on Linux)1134. **No internet access** - DO NOT attempt to install dependencies during development114115## Key Files Reference116117- `AGENTS.md` - Instructions specific to AI coding agents118- `CONTRIBUTING.md` - Comprehensive human contributor guide119- `GLOSSARY.md` - Terminology (ATen, kernels, operations, JIT, TorchScript)120- `aten/src/ATen/native/README.md` - Operator implementation guide121- `tools/autograd/derivatives.yaml` - Gradient definitions for autograd122123## Performance Debugging124125Use `TORCH_SHOW_CPP_STACKTRACES=1` for C++ traces in Python errors. For profiling, prefer `py-spy` over manual instrumentation.126
Also in pytorch/pytorch
Diff this repo’s formatsOne repository carrying more than one format is the comparison this product exists for: does anyone actually write different content in each file, or is one a copy of the other?
| Repository | Format | Stack | Covers | Score | Changed |
|---|---|---|---|---|---|
| pytorch/pytorchCLAUDE.md · 102k | CLAUDE.md | setupbuildtestlint-format+5 | 88/100 | 3 days ago | |
| pytorch/pytorchtorch/_dynamo/CLAUDE.md · 102k | CLAUDE.md | buildteststylearch | 84/100 | 3 days ago |
Similar configs
Same format, overlapping stack, ranked by quality.
| Repository | Format | Stack | Covers | Score | Changed |
|---|---|---|---|---|---|
| hiyouga/LlamaFactory.github/copilot-instructions.md · 74k | Copilot instructions | setupbuildtestlint-format+5 | 97/100 | 2 days ago | |
| darkmatter/nixmac.github/copilot-instructions.md · 24 | Copilot instructions | setupbuildtestlint-format+8 | 96/100 | 3 days ago | |
| iloveitaly/llm-ide-rules.github/copilot-instructions.md · 13 | Copilot instructions | teststyledo-notagent-behaviour+1 | 92/100 | 3 days ago | |
| bytedance/deer-flow.github/copilot-instructions.md · 79k | Copilot instructions | setupbuildtestlint-format+6 | 89/100 | 3 days ago | |
| tesseract-ocr/tesseract.github/copilot-instructions.md · 76k | Copilot instructions | setupbuildtestlint-format+5 | 89/100 | 3 days ago | |
| iloveitaly/llm-ide-rules.github/instructions/python.instructions.md · 13 | Copilot instructions | setupstyletypesdo-not | 88/100 | 3 days ago | |
| Significant-Gravitas/AutoGPT.github/copilot-instructions.md · 186k | Copilot instructions | setupbuildtestlint-format+10 | 88/100 | 3 days ago | |
| microsoft/WSL.github/copilot-instructions.md · 33k | Copilot instructions | setupbuildtestlint-format+7 | 88/100 | 3 days ago |
