Tell the runtime which device a Python input lives on - #21705
Open
shoumikhin wants to merge 1 commit into
Open
Conversation
Contributor
Author
|
Stack from ghstack (oldest at bottom): |
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/executorch/21705
Note: Links to docs will display an error until the docs builds have been completed. ❌ 1 New Failure, 1 Cancelled JobAs of commit 403891f with merge base 730b77a ( NEW FAILURE - The following job has failed:
CANCELLED JOB - The following job was cancelled. Please retry:
This comment was automatically generated by Dr. CI and updates every 15 minutes. |
This PR needs a
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The problem
When you pass a tensor to a model from Python, ExecuTorch builds its own description of
that tensor: the shape, the data type, and where the memory lives. That last part was
never filled in, so it always said main memory (the host):
TensorPtr tensor = for_blob(data_ptr, sizes, type) .strides(...).dim_order(...).make_tensor_ptr(); // device silently defaults to CPUFor an ordinary model that is correct, since the tensor really is in main memory. It is
wrong for a model whose activations stay on the GPU, where you hand over memory that is
already on the GPU. The runtime reads this description to decide what to do with your
input, so it was being told the wrong thing, and nothing downstream could tell the
difference between a host tensor and a GPU one.
The change
Pass the tensor's device through instead of letting it default:
TensorPtr tensor = make_tensor_ptr( std::move(sizes), data_ptr, std::move(dim_order), std::move(strides), type, aten::TensorShapeDynamism::STATIC, nullptr, torch_to_executorch_device(at_tensor.device()));The tensor factory already accepts a device, and its documentation describes exactly this
case, so nothing new had to be invented:
The one new piece is a small translator beside the existing scalar-type one, because
PyTorch and the ExecuTorch runtime describe devices with different types:
executorch::runtime::etensor::Device torch_to_executorch_device(c10::Device device);It raises an error for a device the runtime does not model, rather than quietly reporting
host memory. Quietly reporting host memory is what made the original problem invisible.
The size and stride vectors now use the runtime's own types, which the factory takes
directly, so no extra conversion happens at the call.
Test plan
existing caller goes through this code path, so this is the important check: the device
of an ordinary tensor is the host, which is what the code used to assume.
before this change as well, for an unrelated reason, and the remaining 35 pass.
Tested on Linux x86_64. Continuous integration builds and runs these bindings, which is
where the compiled result is exercised.
Note
This describes the input correctly; it does not by itself make a GPU-activation program
run from Python. Such a program also has to be exported so the runtime shares the
caller's memory rather than reserving its own buffer for it.