SIGN IN SIGN UP

Introduce `virtual_clone` API to support view of view use cases + fix synchronization hazard with view tensors (#5753)

Summary:
Pull Request resolved: https://github.com/pytorch/executorch/pull/5753

## Context

This diff fixes some hazards (not necessarily) bugs with view tensors.

### `virtual_clone` API

Consider the following sequence of calls which may be common in the view of view use case.

```
// Note the below does not reflect accurate API usage; it is mostly pseudocode.

t1 = graph.add_tensor(...);
// t2 will have the same metadata as t1
t2 = graph.add_tensor_view(t1);
// t3 will also have the same metadata as t2 at this point.
t3 = graph.add_tensor_view(t2);

// t2 metadata will be updated correctly.
t2 = add_transpose_view_node(t1, 0, 1, t2);
// Unfortunately, this node will have an assumption that t3 has the same metadata as t2 to start. However, this is not true.
// As a result, t3 will have incorrect metadata after this node.
t3 = add_transpose_view_node(t2, 1, 2, t3);
```

To address this, the `virtual_clone` API is introduced which will allow view nodes to set the metadata of the output equal to the input before modifying the output.

### WAW synchronization hazards

`vTensorStorage` maintains a `last_access` state which facilitates inserting the correct memory barriers for the underlying `vkImage` or `vkBuffer`. However, when we create a tensor view, `last_access` is not shared between `vTensor` instances that use the same resource.

As as result, writing into a `vTensor` will not update the `last_access` of its views, and vice versa. Therefore, sebsequent accesses of the other tensor that references the same resource will result in a synchronization hazard.

This diff fixes this hazard in a bit of a crude way; if the `vTensor` is a copy, or has copies, then cowardly assume that it has been written to before the current access so that appropriate memory barriers are inserted. This was the selected solution because I thought that adding a map to track last access of tensors that share resources is a bit overkill when the assumption that the underlying resource has been written to before the current access should hold most of the time.
ghstack-source-id: 245517197
exported-using-ghexport

Reviewed By: jorgep31415

Differential Revision: D63642092

fbshipit-source-id: 5ca76347946fa2b2f1b07709cbc7d9afa3aaf00d
S
Stephen Jia committed
a5a76f7b4c5189cc4e8cd08e4847fbed953a9f64
Parent: 6ff52cc
Committed by Facebook GitHub Bot <facebook-github-bot@users.noreply.github.com> on 9/30/2024, 9:48:08 PM