Allow Tensor/View ops outside XLADispatchMode by delegating to env.dispatch()#98
Open
lk-chen wants to merge 1 commit into
Open
Allow Tensor/View ops outside XLADispatchMode by delegating to env.dispatch()#98lk-chen wants to merge 1 commit into
lk-chen wants to merge 1 commit into
Conversation
…spatch() Tensor.__torch_dispatch__ and View.__torch_dispatch__ previously raised AssertionError unconditionally for any op other than wait_tensor/prim.device. This caused TypeError when torchax tensors were used in arithmetic outside an active XLADispatchMode context — for example during vLLM AOT lower, where XLADispatchMode is not installed but torchax Tensors and Views appear as arguments to aten ops (e.g. Tensor + View from deepstack vision features). Fix: before raising, scan args/kwargs for the first Tensor or View and delegate to its _env.dispatch(). env.dispatch() calls v2t_iso() which materialises any View arguments to Tensors, then t2j_iso() converts to JAX arrays and runs the op. This matches the behaviour already provided by XLADispatchMode when that mode is active. The View.__torch_dispatch__ change handles the View-as-left-operand case. The Tensor.__torch_dispatch__ change is the primary fix: for Tensor + View, Python dispatches to Tensor first (left operand), so View.__torch_dispatch__ is never reached unless Tensor's dispatch succeeds or returns NotImplemented.
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.
Tensor.__torch_dispatch__andView.__torch_dispatch__previously raisedAssertionErrorunconditionally for any op other thanwait_tensor/prim.device. This causedTypeErrorwhen torchax tensors were used in arithmetic outside an activeXLADispatchModecontext — for example during vLLM AOT lower, whereXLADispatchModeis not installed but torchax Tensors and Views appear as arguments to aten ops (e.g. Tensor + View from deepstack vision features).Fix: before raising, scan args/kwargs for the first Tensor or View and delegate to its
_env.dispatch().env.dispatch()callsv2t_iso()which materializes any View arguments to Tensors, thent2j_iso()converts to JAX arrays and runs the op. This matches the behaviour already provided byXLADispatchModewhen that mode is active.The
View.__torch_dispatch__change handles the View-as-left-operand case. TheTensor.__torch_dispatch__change is the primary fix: for Tensor + View, Python dispatches to Tensor first (left operand), soView.__torch_dispatch__is never reached unless Tensor's dispatch succeeds or returns NotImplemented.