Skip to content

Add structured call_hierarchy metadata to FX nodes - #1

Open
nklshy-aws wants to merge 4 commits into
mainfrom
call-hierarchy-metadata
Open

Add structured call_hierarchy metadata to FX nodes#1
nklshy-aws wants to merge 4 commits into
mainfrom
call-hierarchy-metadata

Conversation

@nklshy-aws

@nklshy-aws nklshy-aws commented Mar 17, 2026

Copy link
Copy Markdown
Owner

Adds an opt-in call_hierarchy metadata field to FX nodes that captures the interleaved module + function call chain as a structured list, built during Dynamo's tx chain walk at proxy creation time.

Today nn_module_stack only tracks modules and stack_trace is an unstructured string; there's no reliable way to produce a unified hierarchy i.e. to know which calls correspond to which module entry's. This field gives compiler backends (profilers, debuggers, visualization tools) the merged view directly.

Gated behind config.record_call_hierarchy (default False). Uses set difference on nn_module_stack keys between consecutive tx frames to distinguish module entries from function entries.

Refs: pytorch#87659

@nklshy-aws
nklshy-aws force-pushed the call-hierarchy-metadata branch 3 times, most recently from 461ada2 to c5d86c1 Compare April 6, 2026 19:27
nklshy-aws pushed a commit that referenced this pull request Apr 6, 2026
## Summary

Add `module_error_inputs_torch_nn_Embedding` function to test error messages for invalid inputs to `nn.Embedding` module.

## Motivation

Currently, `torch.nn.Embedding` does not have `module_error_inputs_func` defined in `common_modules.py`. This PR adds error input tests to enable regression testing for error messages and follow the pattern already established for other modules (BatchNorm, GroupNorm, RNN cells, etc.).

## Test Cases Added

1. **Out of range indices**: Tests IndexError when index exceeds `num_embeddings`
   - Input: `Embedding(10, 3)` with indices `[0, 5, 15]` (15 is out of range)
   - Expected: `IndexError: index out of range in self`
   - **Note**: Only tested on CPU - CUDA triggers kernel assertion instead of Python exception

2. **Float indices**: Tests RuntimeError when float tensor used instead of Long/Int
   - Input: `Embedding(10, 3)` with float tensor `[1.5, 2.5]`
   - Expected: `RuntimeError: Expected tensor for argument #1 'indices' to have one of the following scalar types: Long, Int`

3. **Negative num_embeddings**: Tests RuntimeError when constructor has negative dimension
   - Input: `Embedding(-1, 3)`
   - Expected: `RuntimeError: Trying to create tensor with negative dimension`

## Test Environment

- Tested on H200 GPU with CUDA 12.8
- Verified error messages match on CPU
- CUDA tests pass for dtype and construction errors

Fixes pytorch#174179

Pull Request resolved: pytorch#174180
Approved by: https://github.com/albanD
…pytorch#179511)

Fixes an issue in guard serialization that would cause dataclass fields set to `init=False` to get serialized. Upon loading objects like `DefaultsSource`, it would receive the error

```
    restored = pickle.loads(pickle.dumps(source))
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: DefaultsSource.__init__() takes from 3 to 4 positional arguments but 6 were given
```

Pull Request resolved: pytorch#179511
Approved by: https://github.com/anijain2305
nklshy-aws pushed a commit that referenced this pull request Apr 7, 2026
## Summary

Add `module_error_inputs_torch_nn_Embedding` function to test error messages for invalid inputs to `nn.Embedding` module.

## Motivation

Currently, `torch.nn.Embedding` does not have `module_error_inputs_func` defined in `common_modules.py`. This PR adds error input tests to enable regression testing for error messages and follow the pattern already established for other modules (BatchNorm, GroupNorm, RNN cells, etc.).

## Test Cases Added

1. **Out of range indices**: Tests IndexError when index exceeds `num_embeddings`
   - Input: `Embedding(10, 3)` with indices `[0, 5, 15]` (15 is out of range)
   - Expected: `IndexError: index out of range in self`
   - **Note**: Only tested on CPU - CUDA triggers kernel assertion instead of Python exception

2. **Float indices**: Tests RuntimeError when float tensor used instead of Long/Int
   - Input: `Embedding(10, 3)` with float tensor `[1.5, 2.5]`
   - Expected: `RuntimeError: Expected tensor for argument #1 'indices' to have one of the following scalar types: Long, Int`

3. **Negative num_embeddings**: Tests RuntimeError when constructor has negative dimension
   - Input: `Embedding(-1, 3)`
   - Expected: `RuntimeError: Trying to create tensor with negative dimension`

## Test Environment

- Tested on H200 GPU with CUDA 12.8
- Verified error messages match on CPU
- CUDA tests pass for dtype and construction errors

Fixes pytorch#174179

Pull Request resolved: pytorch#174180
Approved by: https://github.com/albanD
Add a new 'call_hierarchy' metadata field to FX nodes that provides a
unified, ordered list of module and function call entries for each
operation. Built during Dynamo's tx chain walk at proxy creation time,
it uses nn_module_stack depth changes to distinguish module entries
from function entries without relying on function name matching.

Changes:
- torch/_dynamo/config.py: Add record_call_hierarchy flag (default off)
- torch/_dynamo/output_graph.py: Build call_hierarchy in create_proxy
- torch/_dynamo/symbolic_convert.py: Add function_call_counts tracking
- torch/fx/proxy.py: Add call_hierarchy to _COPY_META_FIELDS
- test/dynamo/test_call_hierarchy.py: Tests for the new field
@nklshy-aws
nklshy-aws force-pushed the call-hierarchy-metadata branch from bc25044 to 87948a2 Compare April 7, 2026 19:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants