Skip to content

Device-generalize more dynamo tests to run on accelerator backends - #87

Open
aws-alexk wants to merge 1 commit into
mainfrom
device_agnostic_main
Open

Device-generalize more dynamo tests to run on accelerator backends#87
aws-alexk wants to merge 1 commit into
mainfrom
device_agnostic_main

Conversation

@aws-alexk

Copy link
Copy Markdown

Follow-up to the earlier device-generalization pass. These test/dynamo tests still hardcode CPU input tensors (no device= argument). torch.compile is device-preserving, but accelerator backends that execute the compiled graph on-device return device tensors, so autograd rejects the device mismatch on backward, e.g.:

RuntimeError: Function CompiledFunctionBackward returned an invalid
gradient at index 0 - expected device cpu but got <accel>:0

or the compiled region raises a plain device mismatch when a CPU input meets an on-device intermediate.

Create inputs on the current accelerator via the module-level device_type (torch.accelerator.current_accelerator, falling back to "cpu"), adding device=device_type to input tensor factories and .to(device_type) to nn.Module instances in the affected tests. test_activation_checkpointing.py and test_wrap_inductor_compiled_regions.py gain the module-level device_type definition (the other files already have it); the latter also generalizes its DTensor device mesh. Because device_type resolves to "cpu" when no accelerator is present, these edits are a no-op on CPU and CUDA CI and only take effect on accelerator backends.

Issue #, if available:

Description of changes:
from this PR: #81

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

Follow-up to the earlier device-generalization pass. These test/dynamo tests
still hardcode CPU input tensors (no device= argument). torch.compile is
device-preserving, but accelerator backends that execute the compiled graph
on-device return device tensors, so autograd rejects the device mismatch on
backward, e.g.:

    RuntimeError: Function CompiledFunctionBackward returned an invalid
    gradient at index 0 - expected device cpu but got <accel>:0

or the compiled region raises a plain device mismatch when a CPU input meets an
on-device intermediate.

Create inputs on the current accelerator via the module-level device_type
(torch.accelerator.current_accelerator, falling back to "cpu"), adding
device=device_type to input tensor factories and .to(device_type) to nn.Module
instances in the affected tests. test_activation_checkpointing.py and
test_wrap_inductor_compiled_regions.py gain the module-level device_type
definition (the other files already have it); the latter also generalizes its
DTensor device mesh. Because device_type resolves to "cpu" when no accelerator
is present, these edits are a no-op on CPU and CUDA CI and only take effect on
accelerator backends.

Test Plan:

On CPU (no accelerator; device_type == "cpu", edits are a no-op):

```
python test/dynamo/test_autograd_function.py AutogradFunctionTests.test_apply_kwargs_old_style
python test/dynamo/test_hooks.py HooksTests.test_input_hooks_same
python test/dynamo/test_repros.py ReproTests.test_intermediate_leaf_requires_grad
python test/dynamo/test_fwd_loss_bwd.py TestForwardLossBackward.test_backward_dict_inputs
```

On an accelerator backend, the same nodeids (and their _nested_graph_breaks
variants) that previously failed with a device mismatch now pass.

Authored with an AI assistant (Claude).
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.

1 participant