Skip to content

Conversation

@pytorchbot
Copy link
Collaborator

This PR was created by the merge bot to help merge the original PR into the main branch.
ghstack PR number: #11508 by @SS-JIA
^ Please use this as the source of truth for the PR details, comments, and reviews
ghstack PR base: https://github.com/pytorch/executorch/tree/gh/SS-JIA/239/base
ghstack PR head: https://github.com/pytorch/executorch/tree/gh/SS-JIA/239/head
Merge bot PR base: https://github.com/pytorch/executorch/tree/gh/SS-JIA/241/orig
Merge bot PR head: https://github.com/pytorch/executorch/tree/gh/SS-JIA/239/orig
@diff-train-skip-merge

SS-JIA added 3 commits June 12, 2025 12:34
…O and push constant

Pull Request resolved: #11599

## Changes

* Add dim order to the list of tensor metadata that can be ingested by compute shaders
* Do not persistently store derivative metadata (i.e. padded sizes, padded numel, unsqueezed strides, etc.) as members of vTensor; instead store these in `uniform_data_` and use `uniform_data_` as the source of truth

## Motivation

> Add dim order to the list of tensor metadata that can be ingested by compute shaders

Knowing the dim order is necessary to convert between a linear buffer index to N-dimensional tensor index using a tensor's strides. Technically, the dim order can be inferred from the strides by performing an index sort on the strides array; however to prevent compute shaders from having to do this operation frequently, it is more efficient to pass in the dim order directly to the compute shader.

Currently, ET-VK compute shaders make strong assumptions about the dim order of buffer backed tensors so as to avoid having to dynamically generate the dim order from the strides array. However, these assumptions are not enforced and it is more correct to just account for the dim order rather than make assumptions. This will be addressed in the next diff.

> Do not persistently store derivative metadata (i.e. padded sizes, padded numel, unsqueezed strides, etc.) as members of vTensor; instead store these in `uniform_data_` and use `uniform_data_` as the source of truth

I realized that the purpose of these "derived metadata" is to simply convert default tensor metadata such sizes, strides, etc. to a form where they can be used in a compute shader. There is no need to store these derived metadata persistently, since they are pretty much only useful in the final `ivec4` form they exist as inside `UniformData`. So to simplify `vTensor` and to reduce the size of the class, I elected to remove these superfluous data members.

## Performance Impact

* Potential memory footprint improvement from reducing the size of `vTensor`.
ghstack-source-id: 290022826
@exported-using-ghexport

Differential Revision: [D76393427](https://our.internmc.facebook.com/intern/diff/D76393427/)
Pull Request resolved: #11600

## Changes

* Update callsites to `bufi_to_tidx` to account for the tensor dim order
* Remove existing functions which do not accept dim order as argument.

## Motivation

> Update callsites to `bufi_to_tidx` to account for the tensor dim order
> Remove existing functions which do not accept dim order as argument.


As mentioned in the below diff, dim order is required to properly convert from a linear buffer index to N-dimension tensor index using a tensor's strides. Technically the dim order can be inferred from the strides array by performing an index sort. However, for the sake of efficiency it is better to just pass the dim order directly into the compute shader.

Currently the `bufi_to_tidx` function which performs the conversion between buffer index and tensor index assumes that the dim order follows a specific pattern using the packed dim as an input. However, it is not guaranteed that the dim order is the same as what is assumed.

Furthermore, there is an existing bug when calling `bufi_to_tidx` without providing `packed_dim` as an input. In this case, the function will infer the packed dim by finding the first dim with a stride of 1. However, this causes issues when multiple dims may have a stride of 1, which may occur when there are dims with a size of 1. In this case the wrong packed dim may be inferred and therefore the assumed dim order is completely wrong.

To address these issues, make it standard to either account for the packed dim when converting bufi to tidx, or to explicitly call out an assumption about the tensor's dim order.

## Performance Impact

* None expected
ghstack-source-id: 290022827
@exported-using-ghexport

Differential Revision: [D76393428](https://our.internmc.facebook.com/intern/diff/D76393428/)
Pull Request resolved: #11508

## Changes

* Introduce `concat_texture.glsl` and `concat_buffer.glsl` to implement the `torch.cat` operator
* Introduce `Concat.cpp` to replace `Cat.cpp`
* Fix a bug with channels-packed buffer tensors where input data would be copied incorrectly with multiple dims have a stride of 1

## Motivation

> * Introduce `concat_texture.glsl` and `concat_buffer.glsl` to implement the `torch.cat` operator
> * Introduce `Concat.cpp` to replace `Cat.cpp`

The existing implementation of `torch.cat` uses the copy_channel_offset` shaders. However, these shaders have a critical bug where the output tensor is passed in separately with difference access types, i.e.

```
graph.execute_nodes().emplace_back(new DispatchNode(
        graph,
        VK_KERNEL_FROM_STR(kernel_name),
        global_size,
        local_size,
        // Inputs and Outputs
        {
            {out, vkapi::kWrite},
            {out, vkapi::kRead},
            {in, vkapi::kRead},
        },
```

This creates many validation layer errors because the memory barriers for the resource cannot be formed properly. The shader essentially relies on undefined behaviour to work correctly. The result is that the `cat` operator produces incorrect result on many platforms.

Rather than fix the `copy_offset` shaders, I decided to just introduce new shaders to perform the concat operation. The new implementation handles both buffer and texture inputs and is agnostic to memory layout.
ghstack-source-id: 290022825

Differential Revision: [D76305343](https://our.internmc.facebook.com/intern/diff/D76305343/)
@pytorchbot pytorchbot requested a review from SS-JIA as a code owner June 12, 2025 22:29
@pytorch-bot
Copy link

pytorch-bot bot commented Jun 12, 2025

🔗 Helpful Links

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

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

❌ 2 New Failures, 5 Pending

As of commit 0995d76 with merge base 4a14fdd (image):

NEW FAILURES - The following jobs have failed:

  • pull / unittest / linux / linux-job (gh)
    /pytorch/executorch/backends/vulkan/runtime/api/containers/Tensor.cpp:651:17: error: no matching constructor for initialization of 'vkcompute::api::vTensor::TextureLimits'
  • pull / unittest-editable / linux / linux-job (gh)
    /pytorch/executorch/backends/vulkan/runtime/api/containers/Tensor.cpp:651:17: error: no matching constructor for initialization of 'vkcompute::api::vTensor::TextureLimits'

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

@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Jun 12, 2025
Base automatically changed from gh/SS-JIA/241/orig to main June 12, 2025 23:21
@SS-JIA SS-JIA merged commit b59f5cc into main Jun 12, 2025
93 of 95 checks passed
@SS-JIA SS-JIA deleted the gh/SS-JIA/239/orig branch June 12, 2025 23:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants