Use agent_space/ (git-ignored, at repo root) for temporary scripts, scratch files, and throwaway experiments. Do not commit files from this directory.
When asked to review a PR, always use the /pr-review skill.
If any tool you're trying to use (pip, python, spin, etc) is missing, always stop and ask the user if an environment is needed. Do NOT try to find alternatives or install these tools.
Always ask for build configuration environment variables before running build.
All build (both codegen, C++ and python) is done via pip install -e . -v --no-build-isolation.
You should NEVER run any other command to build PyTorch.
Use our test class and test runner:
from torch.testing._internal.common_utils import run_tests, TestCase
class TestFeature(TestCase):
...
if __name__ == "__main__":
run_tests()
To test Tensor equality, use assertEqual.
For tests over multiple inputs, use the @parametrize decorator.
For any test that checks numerics of the on-device implementation, use instantiate_device_type_tests to write device-generic tests.
Only use commands provided via spin for linting.
Use spin help to list available commands.
Generally, use spin lint as to run the lint and spin fixlint to apply automatic fixes.
Don't commit unless the user explicitly asks you to.
When writing a commit message, don't make a bullet list of the individual changes. Instead, if the PR is large, explain the order to review changes (e.g., the logical progression), or if it's short just omit the bullet list entirely.
Disclose that the PR was authored with Claude.
If a commit message contains ghstack-source-id or Pull-Request trailers,
you MUST preserve them when rewriting or splitting commit messages. ghstack
will update the source id automatically when needed.
ghstack commits follow a different workflow than the conventional GitHub branch and PR workflow. First identify whether you're on a ghstack commit:
- If HEAD is a detached commit, you are almost certainly in a ghstack flow.
- If the commit message contains a
ghstack-source-idtrailer, it is an existing ghstack commit. - If the commit is associated with a remote branch like
origin/gh/USERNAME/N, it is likely a ghstack commit (imperfect signal: local amends without a push can desync this).
Rules for working with ghstack:
- Don't amend unless asked. If the user asks you to work on a ghstack
commit, leave changes uncommitted so the user can review with
git diff. Only amend into the commit if the user explicitly asks you to amend or to submit it directly. - Submitting. Run
ghstackto submit. When only working on a single commit, useghstack --no-stackto avoid updating the rest of the stack and burning unnecessary CI. Use a fullghstackwhen you're intentionally updating CI for the whole stack. - Preserve metadata trailers. When editing a commit message, never delete
Pull-Request:orghstack-source-id:trailers. If you modified the commit message, runghstack -uafterwards to push the updated PR description. - Never push directly. Do not
git pushto branches, and never directly modify thegh/USERNAME/Nbranches — ghstack manages those. - Finding the PR. If the user asks to pull CI results or code review for a
ghstack commit, get the PR URL from the
Pull-Requesttrailer in the commit message. UseghCLI to fetch status/comments from there. - Editing earlier commits / splitting. Treat it like a normal stack of
commits (use
git rebase, etc.). Commits that keep their metadata trailers stay associated with their existing PRs; commits without trailers will get a fresh PR on submit. A fullghstackrun is usually appropriate here.
Follow these rules for all code changes in this repository:
- Minimize comments; be concise; code should be self-explanatory and self-documenting.
- Comments should be useful, for example, comments that remind the reader about some global context that is non-obvious and can't be inferred locally.
- Don't make trivial (1-2 LOC) helper functions that are only used once unless it significantly improves code readability.
- Prefer clear abstractions. State management should be explicit.
For example, if managing state in a Python class: there should be a clear
class definition that has all of the members: don't dynamically
setattra field on an object and then dynamicallygetattrthe field on the object. - Match existing code style and architectural patterns.
- Assume the reader has familiarity with PyTorch. They may not be the expert on the code that is being read, but they should have some experience in the area.
If uncertain, choose the simpler, more concise implementation.
Use torch._dynamo.config.patch for temporarily changing config. It can be used as a decorator on test methods or as a context manager:
# Good - use patch as decorator on test method
@torch._dynamo.config.patch(force_compile_during_fx_trace=True)
def test_my_feature(self):
# test code here
pass
# Good - use patch as context manager
with torch._dynamo.config.patch(force_compile_during_fx_trace=True):
# test code here
pass
# Bad - manual save/restore
orig = torch._dynamo.config.force_compile_during_fx_trace
try:
torch._dynamo.config.force_compile_during_fx_trace = True
# test code here
finally:
torch._dynamo.config.force_compile_during_fx_trace = origIf B950 line too long triggers on a multi-line string block, you cannot fix it by putting # noqa: B950 on that line directly, as that would change the meaning of the string, nor can you fix it by line breaking the string (since you need the string to stay the same). Instead, put # noqa: B950 on the same line as the terminating triple quote.
Example:
self.assertExpectedInline(
foo(),
"""
this line is too long...
""", # noqa: B950
)
When adding debug logging for errors or diagnostic info, consider two user personas:
- Local development: Users run locally and can access files on disk
- Production jobs: Users can only access logs via
tlparsefrom structured traces
For production debugging, use trace_structured to log artifacts:
from torch._logging import trace_structured
# Log an artifact (graph, edge list, etc.)
trace_structured(
"artifact",
metadata_fn=lambda: {
"name": "my_debug_artifact",
"encoding": "string",
},
payload_fn=lambda: my_content_string,
)To check if structured tracing is enabled (for conditional messaging):
from torch._logging._internal import trace_log
if trace_log.handlers:
# Structured tracing is enabled, suggest tlparse in error messages
msg += "[Use tlparse to extract debug artifacts]"Best practices for error diagnostics:
- Always log to
trace_structuredfor production (no runtime cost if disabled) - If you're dumping debug info in the event of a true internal compiler exception, you can also consider writing to local files for local debugging convenience
- In error messages, tell users about both options:
- Local files: "FX graph dump: min_cut_failed_graph.txt"
- Production: "Use tlparse to extract artifacts" (only if tracing enabled)
- Use
_get_unique_path()pattern to avoid overwriting existing debug files