8000 Document dynamo by Raymo111 · Pull Request #146736 · pytorch/pytorch · GitHub
[go: up one dir, main page]

Skip to content
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

Document dynamo #146736

Closed
wants to merge 1 commit into from
Closed

Document dynamo #146736

wants to merge 1 commit into from

Conversation

Raymo111
Copy link
Member
@Raymo111 Raymo111 commented Feb 8, 2025

Many files in dynamo are currently lacking file/module-level documentation, which makes it hard to know what they do at a glance and without digging into the code. This fixes that.

Note: documentation was AI-generated and could be incorrect, please review carefully.

cc @voznesenskym @penguinwu @EikanWang @jgong5 @Guobing-Chen @XiaobingSuper @zhuhaozhe @blzheng @wenzhe-nrv @jiayisunx @ipiszy @yf225 @chenyang78 @kadeng @muchulee8 @amjames @chauhang @aakhundov @StrongerXi @xmfan @svekars @brycebortree @sekyondaMeta @AlannaBurke

Copy link
pytorch-bot bot commented Feb 8, 2025

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/146736

Note: Links to docs will display an error until the docs builds have been completed.

❌ 5 New Failures

As of commit 9b5a22c with merge base 8af31e3 (image):

NEW FAILURES - The following jobs have failed:

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@facebook-github-bot
Copy link
Contributor

@Raymo111 has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

@pytorch-bot pytorch-bot bot added the ciflow/trunk Trigger trunk jobs on your pull request label Feb 8, 2025
@Raymo111 Raymo111 added module: docs Related to our documentation, both in docs/ and docblocks better-engineering Relatively self-contained tasks for better engineering contributors topic: not user facing topic category and removed module: docs Related to our documentation, both in docs/ and docblocks labels Feb 8, 2025
@Raymo111 Raymo111 force-pushed the gh/raymo/document-dynamo branch 2 times, most recently from 38cce9e to 2232ecd Compare February 8, 2025 00:33
@facebook-github-bot
Copy link
Contributor

@Raymo111 has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

@Raymo111 Raymo111 force-pushed the gh/raymo/document-dynamo branch from 2232ecd to bef2193 Compare February 8, 2025 02:08
@pytorch-bot pytorch-bot bot added the module: compiled autograd compiled_autograd label Feb 8, 2025
@Raymo111 Raymo111 force-pushed the gh/raymo/document-dynamo branch 3 times, most recently from 4dc32b7 to 8e725f4 Compare February 8, 2025 03:21
@Raymo111 Raymo111 requested a review from zou3519 as a code owner February 8, 2025 03:21
@Raymo111 Raymo111 requested a review from jansel February 8, 2025 03:21
@Raymo111 Raymo111 changed the title [WIP] Document dynamo Document dynamo Feb 8, 2025
@Raymo111
Copy link
Member Author
Raymo111 commented Feb 8, 2025

Ignore the fbsource diff imports, that was for testing purposes and D69337511 is now abandoned.

Copy link
Contributor
@StrongerXi StrongerXi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dropped some comments and deferring approval to others.

Comment on lines 7 to 8
values during compilation, ensuring proper handling of Python literals and
maintaining type safety through the compilation process.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove indentations?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whoops that was a copy-paste mistake when I moved the docstring up, fixed.

Comment on lines 3 to 4
It optimizes memory usage and performance by deferring the creation of VariableTracker
objects until they are actually needed.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

... creation of VariableTracker objects (and thereby guard installation) until ...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added.

The key classes are:
- UserDefinedVariable: Base class for representing custom Python objects
- UserDefinedClassVariable: Handles Python class objects/types
- UserDefinedObjectVariable: Main class for instance objects, with support for method calls,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/Main/Fallback

The idea is that Dynamo would specialize to other VariableTracker subclasses like FrozenDataClassVariable first, and if no subclass qualifies, we fallback to UserDefinedObjectVariable:

else:
return self.wrap_user_defined(value)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated and note added!

- Tracks mutations to Python objects, lists, and dictionaries
- Manages attribute modifications and deletions
- Handles tensor hooks and backward pass state
- Supports cell variable mutations and global variable changes
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/Supports/Tracks

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Applied.

and other side effects that occur during program execution.

Key responsibilities:
- Tracks mutations to Python objects, lists, and dictionaries
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd change this one, because mutations to list/dict/etc. are actually tracked along the VariableTracker instances (e.g., ConstDictVariable).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tweaked the comment and moved it down to be the class docstring since there's only one class in the file.

@Raymo111 Raymo111 force-pushed the gh/raymo/document-dynamo branch from 8e725f4 to 3c5caf7 Compare February 10, 2025 21:15
Copy link
Contributor
@StrongerXi StrongerXi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

Comment on lines 6 to 11
This module provides the main interface for TorchDynamo's functionality, including:
- Various compilation backends and their registration
- Optimization and export capabilities
- Debugging and profiling utilities
- Graph manipulation and control flow tools
- Decorators for fine-grained control over compilation behavior
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we remove these lines. Its too generic, and seems off.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Edited.

Comment on lines +7 to +9
This module lazily imports and registers the TorchInductor compiler to avoid loading it
into memory when it is not being used. This helps reduce memory overhead when using
other backends.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove these lines.

Comment on lines 13 to 15
Many settings can be overridden via environment variables. The environment variable
name typically matches the setting name in uppercase with a "TORCH_DYNAMO_" or
"TORCH_COMPILE_" prefix.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed.

Comment on lines 7 to 25
Key functionality includes:
- Function marking decorators (disable, skip, allow_in_graph, disallow_in_graph)
- Tensor dimension handling (mark_dynamic, mark_static, mark_unbacked)
- Graph compilation control (run, graph_break)
- Module attribute handling (mark_static for nn.Module)

The decorators and utilities in this module are thread-safe and can be used to:
- Control which functions are included/excluded from graph compilation
- Specify tensor dimension properties for optimization
- Configure compilation behavior for specific functions
- Mark tensors and modules as static/dynamic for better performance

Important decorators:
- disable: Prevents TorchDynamo from operating on a function
- allow_in_graph: Permits direct function inclusion in compiled graphs
- mark_dynamic: Specifies dynamic tensor dimensions
- mark_static: Marks tensor dimensions or nn.Modules as static

Note: Most marking functions should be called before compilation, not during tracing.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this. Mostly because this will be outdated soon. And the docs should live in the decorators itself.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed.

Comment on lines 2 to 15
This module implements lazy evaluation mechanisms for Dynamo's variable tracking system.
It optimizes memory usage and performance by deferring the creation of VariableTracker
objects (and thereby guard installation) until they are actually needed.

The lazy evaluation strategy is particularly beneficial when dealing with large
containers or complex object graphs, where eagerly creating VariableTrackers for
all elements would be expensive and potentially wasteful. Key components include:

- LazyCache: Stores the original value and source until realization is needed
- LazyVariableTracker: Provides a proxy interface that triggers realization on access
- Realization system: Handles the actual creation of VariableTrackers on demand

This approach allows Dynamo to efficiently handle large data structures and complex
object relationships while maintaining good performance characteristics.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is somewhat wrong. Lets delete it for now, I can put the right doc later on.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay.

Comment on lines 18 to 21
Special handling is provided for torch.Size to work with symbolic shapes,
and for named tuples to preserve their field names and type information.
The module also includes restricted list subclass support for user-defined
list subclasses that don't override core list methods.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove

10000
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed.

Copy link
Contributor
@anijain2305 anijain2305 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor suggestions. Approving.

@Raymo111 Raymo111 force-pushed the gh/raymo/document-dynamo branch from 3c5caf7 to 19c2609 Compare February 12, 2025 00:18
@Raymo111 Raymo111 force-pushed the gh/raymo/document-dynamo branch from 19c2609 to 9b5a22c Compare February 12, 2025 01:00
@Raymo111
Copy link
Member Author

@pytorchbot merge -f "Errors are unrelated to my docstring changes"

@pytorchmergebot
Copy link
Collaborator

Merge started

Your change will be merged immediately since you used the force (-f) flag, bypassing any CI checks (ETA: 1-5 minutes). Please use -f as last resort and instead consider -i/--ignore-current to continue the merge ignoring current failures. This will allow currently pending tests to finish and report signal before the merge.

Learn more about merging in the wiki.

Questions? Feedback? Please reach out to the PyTorch DevX Team

Advanced Debugging
Check the merge workflow status
here

@Raymo111 Raymo111 deleted the gh/raymo/document-dynamo branch February 13, 2025 00:25
facebook-github-bot pushed a commit to pytorch/benchmark that referenced this pull request Feb 13, 2025
Summary:
Many files in dynamo are currently lacking file/module-level documentation, which makes it hard to know what they do at a glance and without digging into the code. This fixes that.

Note: documentation was AI-generated and could be incorrect, please review carefully.

X-link: pytorch/pytorch#146736
Approved by: https://github.com/jansel, https://github.com/StrongerXi, https://github.com/anijain2305, https://github.com/zou3519

Reviewed By: huydhn

Differential Revision: D69565828

fbshipit-source-id: 093b4575224b914a263f3e8e8994a63015a5b66d
Copy link
Member
@williamwen42 williamwen42 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't have a chance to review this before it got merged - I put in my comments for modules that I'm more familiar with.

Can we also add a disclaimer for each of the generated docstrings stating that they are AI-generated and partially audited? This will help to highlight that the generated docstrings may not necessarily be correct or precise.

You can make these changes in a followup PR.


Key Debugging Backends:
- eager: Simple pass-through backend that runs models in eager mode
- eager_noexcept: Similar to eager but with additional exception handling
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- eager_noexcept: Similar to eager but with additional exception handling
- eager_noexcept: Similar to `eager` backend but ensures that the given graph doesn't raise any errors.

- eager_debug: Adds schema validation checks for custom operators
- aot_eager: Uses AOT Autograd with nop compiler for debugging
- aot_eager_decomp_partition: Uses TorchInductor decompositions for debugging
- torchscript: Compiles using TorchScript for debugging JIT-related issues
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not a relevant backend.


Testing and Development Tools:
- Backends for inducing specific errors (compile/runtime/accuracy)
- ExplainOutput class for detailed graph compilation analysis
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not really used anymore

1241

These backends are primarily used for:
1. Debugging graph breaks and compilation failures
2. Testing error handling and recovery mechanisms
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not really

These backends are primarily used for:
1. Debugging graph breaks and compilation failures
2. Testing error handling and recovery mechanisms
3. Analyzing performance bottlenecks
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not really

@@ -1,5 +1,27 @@
# This module contains functions that *will be allowed* by dynamo

"""
This module contains utility functions that are explicitly allowed to be called during
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
This module contains utility functions that are explicitly allowed to be called during
This module contains utility functions that are explicitly allowed to be traced/inlined during

Comment on lines +4 to +15
This module provides functionality for resuming Python execution at specific points in code,
primarily used by PyTorch Dynamo for control flow handling and optimization. It implements
bytecode transformation and execution state management to enable:

- Resuming execution at arbitrary points in Python bytecode
- Managing context managers and their state across execution boundaries
- Transforming and generating new code objects with preserved execution state
- Supporting Python 3.11+ exception handling and block management
- Restoring torch function mode stacks and other execution context

The module is critical for PyTorch Dynamo's ability to optimize code while preserving
Python semantics and execution state.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
This module provides functionality for resuming Python execution at specific points in code,
primarily used by PyTorch Dynamo for control flow handling and optimization. It implements
bytecode transformation and execution state management to enable:
- Resuming execution at arbitrary points in Python bytecode
- Managing context managers and their state across execution boundaries
- Transforming and generating new code objects with preserved execution state
- Supporting Python 3.11+ exception handling and block management
- Restoring torch function mode stacks and other execution context
The module is critical for PyTorch Dynamo's ability to optimize code while preserving
Python semantics and execution state.
This module is responsible for constructing the bytecode for graph break continuation functions.
Graph break continuation functions allow Dynamo to handle unsupported bytecode instructions by
breaking the operation graph, compiling the traced partial graph, and resuming execution after
the unsupported instruction.
The continuation function:
- Restores contexts at the time of the graph break (e.g. supported context managers and torch function mode stacks)
- Jumps to the instruction after the unsupported bytecode

Comment on lines +4 to +24
Core module responsible for converting Python bytecode into TorchDynamo's symbolic execution format.

This module implements the bytecode-level tracing system that allows TorchDynamo to analyze
and transform Python code. It converts Python bytecode instructions into a symbolic format
that tracks the flow of tensors and other values through the program.

Key components:
- InstructionTranslatorBase: Base class for converting bytecode to symbolic execution
- InstructionTranslator: Main translator for function bytecode
- InliningInstructionTranslator: Handles inlining of called functions
- SpeculationLog: Manages state for speculative execution and rollback

The symbolic conversion process handles:
- Control flow (loops, conditionals, etc.)
- Function inlining and call stack management
- Tracking of program values and side effects
- Graph breaks and resumption points
- Exception handling and stack frame management

This is a core part of TorchDynamo's tracing system that enables ahead-of-time
optimization of PyTorch programs.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Core module responsible for symbolically tracing Python bytecode.

This module implements the bytecode-level tracing system that allows TorchDynamo to analyze
and transform Python code.

Key components:

  • InstructionTranslatorBase: Base class for converting bytecode to symbolic execution
  • InstructionTranslator: Translator for the top-level function bytecode
  • InliningInstructionTranslator: Translator for any nested function bytecode
  • SpeculationLog: Logs bytecode locations that Dynamo can graph break on. Used to restore state
    if a bytecode instruction is determined to graph break. Dynamo will restart symbolic analysis and
    trace until the final speculation log entry.

The symbolic conversion process handles things like:

  • Control flow (loops, conditionals, etc.)
  • Nested function calls
  • Closures
  • Side effects
  • Basic exception handling

If tracing fails for some reason (including unsupported bytecode operations), we attempt to graph break.

This is a core part of TorchDynamo's tracing system that enables ahead-of-time
optimization of PyTorch programs.


"""Common utilities for testing Dynamo's minifier functionality.

This module provides the base infrastructure for running minification tests in Dynamo.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
This module provides the base infrastructure for running minification tests in Dynamo.
This module provides the base infrastructure for running minifier tests in Dynamo.

- Running tests in isolated environments
- Managing temporary directories and configurations
- Executing minifier launcher scripts
- Running and validating reproduction scripts
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- Running and validating reproduction scripts
- Running and validating minifier reproduction scripts

Tytskiy pushed a commit to Tytskiy/pytorch that referenced this pull request Feb 18, 2025
Many files in dynamo are currently lacking file/module-level documentation, which makes it hard to know what they do at a glance and without digging into the code. This fixes that.

Note: documentation was AI-generated and could be incorrect, please review carefully.

Pull Request resolved: pytorch#146736
Approved by: https://github.com/jansel, https://github.com/StrongerXi, https://github.com/anijain2305, https://github.com/zou3519
Raymo111 added a commit that referenced this pull request Feb 20, 2025
Many files in dynamo are currently lacking file/module-level documentation, which makes it hard to know what they do at a glance and without digging into the code. This fixes that.

Note: documentation was AI-generated and could be incorrect, please review carefully.

Pull Request resolved: #146736
Approved by: https://github.com/jansel, https://github.com/StrongerXi, https://github.com/anijain2305, https://github.com/zou3519
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
better-engineering Relatively self-contained tasks for better engineering contributors ciflow/inductor ciflow/trunk Trigger trunk jobs on your pull request Merged module: compiled autograd compiled_autograd module: dynamo module: inductor topic: not user facing topic category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants
0