-
Notifications
You must be signed in to change notification settings - Fork 751
[ET-VK][ez] Explicitly skip marking output nodes that are mutable buffers #11983
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
…fers
## Changes
* Move the logic skipping output nodes that are mutable buffers from runtime to AOT
## Context
A `fx.Graph` may return nodes that are mutable buffers:
```
class GraphModule(torch.nn.Module):
def forward(self, p_wrapped_module_wq_weight: "f32[2048, 2048]", p_wrapped_module_wk_weight: "f32[512, 2048]", p_wrapped_module_wv_weight: "f32[512, 2048]", p_wrapped_module_wo_weight: "f32[2048, 2048]", b_wrapped_module_kv_cache_k_cache: "f32[1, 2048, 8, 64]", b_wrapped_module_kv_cache_v_cache: "f32[1, 2048, 8, 64]", x: "f32[1, s27, 2048]", freqs_cos: "f32[s27, 32]", freqs_sin: "f32[s27, 32]", input_pos: "i64[1]"):
sym_size: "Sym(s27)" = torch.ops.aten.sym_size.int(x, 1)
...
# b_wrapped_module_kv_cache_*_cache are mutable buffers
# getitem_2 and getitem_3 are derived from mutable buffers, hence they are
# themselves mutable buffers
auto_functionalized = torch.ops.higher_order.auto_functionalized(torch.ops.llama.update_cache.default, value = getitem_1, cache = b_wrapped_module_kv_cache_k_cache, start_pos = _local_scalar_dense_1); getitem_1 = b_wrapped_module_kv_cache_k_cache = None
getitem_2: "f32[1, 2048, 8, 64]" = auto_functionalized[1]; auto_functionalized = None
auto_functionalized_1 = torch.ops.higher_order.auto_functionalized(torch.ops.llama.update_cache.default, value = aten_view_copy_default_8, cache = b_wrapped_module_kv_cache_v_cache, start_pos = _local_scalar_dense_1); aten_view_copy_default_8 = b_wrapped_module_kv_cache_v_cache = _local_scalar_dense_1 = None
getitem_3: "f32[1, 2048, 8, 64]" = auto_functionalized_1[1]; auto_functionalized_1 = None
...
aten_permute_copy_default_3: "f32[2048, 2048]" = executorch_exir_dialects_edge__ops_aten_permute_copy_default(p_wrapped_module_wo_weight, [1, 0]); p_wrapped_module_wo_weight = None
aten_view_copy_default_10: "f32[s27, 2048]" = executorch_exir_dialects_edge__ops_aten_view_copy_default(aten_view_copy_default_9, [sym_size, 2048]); aten_view_copy_default_9 = None
aten_mm_default_3: "f32[s27, 2048]" = executorch_exir_dialects_edge__ops_aten_mm_default(aten_view_copy_default_10, aten_permute_copy_default_3); aten_view_copy_default_10 = aten_permute_copy_default_3 = None
aten_view_copy_default_11: "f32[1, s27, 2048]" = executorch_exir_dialects_edge__ops_aten_view_copy_default(aten_mm_default_3, [1, sym_size, 2048]); aten_mm_default_3 = sym_size = None
# getitem_2 and getitem_3 are returned as outputs, presumably to prevent the
# update_cache calls from being removed due to dead code elimination
return (getitem_2, getitem_3, aten_view_copy_default_11, None)
```
In the graph signature of the `ExportedProgram` these show up as `BUFFER_MUTATION` outputs
```
Graph signature:
# inputs
p_wrapped_module_wq_weight: PARAMETER target='wrapped_module.wq.weight'
p_wrapped_module_wk_weight: PARAMETER target='wrapped_module.wk.weight'
p_wrapped_module_wv_weight: PARAMETER target='wrapped_module.wv.weight'
p_wrapped_module_wo_weight: PARAMETER target='wrapped_module.wo.weight'
b_wrapped_module_kv_cache_k_cache: BUFFER target='wrapped_module.kv_cache.k_cache' persistent=True
b_wrapped_module_kv_cache_v_cache: BUFFER target='wrapped_module.kv_cache.v_cache' persistent=True
x: USER_INPUT
freqs_cos: USER_INPUT
freqs_sin: USER_INPUT
input_pos: USER_INPUT
# outputs
getitem_2: BUFFER_MUTATION target='wrapped_module.kv_cache.k_cache'
getitem_3: BUFFER_MUTATION target='wrapped_module.kv_cache.v_cache'
aten_view_copy_default_11: USER_OUTPUT
: USER_OUTPUT
```
Although these nodes are technically returned by the `fx.Graph`, `BUFFER_MUTATION` outputs are not included in the delegate call schema. Since the Vulkan delegate serialization uses the output node to mark which values are returned as outputs, this could result in a mismatch betwen the outputs of the Vulkan delegate and the outputs expected by the ExecuTorch runtime.
## Motivation
Previously, this mismatch was addressed in the runtime, by skipping the processing of non-tensor outputs. However, this solution does not account for the fact that in some models, paramters of the model may be returned as outputs. In this case, those parameter outputs would be skipped but the ExecuTorch runtime would still expect to receive them as outputs.
To solve the problem properly, this diff changes the serialization logic to check if an output node is a mutable buffer, and skip marking it as an output if so. In the runtime, all output nodes are processed instead of only processing tensor outputs.
Differential Revision: [D77281491](https://our.internmc.facebook.com/intern/diff/D77281491/)
[ghstack-poisoned]
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/executorch/11983
Note: Links to docs will display an error until the docs builds have been completed. ✅ No FailuresAs of commit ef5e1d5 with merge base ecb85ce ( This comment was automatically generated by Dr. CI and updates every 15 minutes. |
|
This pull request was exported from Phabricator. Differential Revision: D77281491 |
This PR needs a
|
…mutable buffers"
## Changes
* Move the logic skipping output nodes that are mutable buffers from runtime to AOT
## Context
A `fx.Graph` may return nodes that are mutable buffers:
```
class GraphModule(torch.nn.Module):
def forward(self, p_wrapped_module_wq_weight: "f32[2048, 2048]", p_wrapped_module_wk_weight: "f32[512, 2048]", p_wrapped_module_wv_weight: "f32[512, 2048]", p_wrapped_module_wo_weight: "f32[2048, 2048]", b_wrapped_module_kv_cache_k_cache: "f32[1, 2048, 8, 64]", b_wrapped_module_kv_cache_v_cache: "f32[1, 2048, 8, 64]", x: "f32[1, s27, 2048]", freqs_cos: "f32[s27, 32]", freqs_sin: "f32[s27, 32]", input_pos: "i64[1]"):
sym_size: "Sym(s27)" = torch.ops.aten.sym_size.int(x, 1)
...
# b_wrapped_module_kv_cache_*_cache are mutable buffers
# getitem_2 and getitem_3 are derived from mutable buffers, hence they are
# themselves mutable buffers
auto_functionalized = torch.ops.higher_order.auto_functionalized(torch.ops.llama.update_cache.default, value = getitem_1, cache = b_wrapped_module_kv_cache_k_cache, start_pos = _local_scalar_dense_1); getitem_1 = b_wrapped_module_kv_cache_k_cache = None
getitem_2: "f32[1, 2048, 8, 64]" = auto_functionalized[1]; auto_functionalized = None
auto_functionalized_1 = torch.ops.higher_order.auto_functionalized(torch.ops.llama.update_cache.default, value = aten_view_copy_default_8, cache = b_wrapped_module_kv_cache_v_cache, start_pos = _local_scalar_dense_1); aten_view_copy_default_8 = b_wrapped_module_kv_cache_v_cache = _local_scalar_dense_1 = None
getitem_3: "f32[1, 2048, 8, 64]" = auto_functionalized_1[1]; auto_functionalized_1 = None
...
aten_permute_copy_default_3: "f32[2048, 2048]" = executorch_exir_dialects_edge__ops_aten_permute_copy_default(p_wrapped_module_wo_weight, [1, 0]); p_wrapped_module_wo_weight = None
aten_view_copy_default_10: "f32[s27, 2048]" = executorch_exir_dialects_edge__ops_aten_view_copy_default(aten_view_copy_default_9, [sym_size, 2048]); aten_view_copy_default_9 = None
aten_mm_default_3: "f32[s27, 2048]" = executorch_exir_dialects_edge__ops_aten_mm_default(aten_view_copy_default_10, aten_permute_copy_default_3); aten_view_copy_default_10 = aten_permute_copy_default_3 = None
aten_view_copy_default_11: "f32[1, s27, 2048]" = executorch_exir_dialects_edge__ops_aten_view_copy_default(aten_mm_default_3, [1, sym_size, 2048]); aten_mm_default_3 = sym_size = None
# getitem_2 and getitem_3 are returned as outputs, presumably to prevent the
# update_cache calls from being removed due to dead code elimination
return (getitem_2, getitem_3, aten_view_copy_default_11, None)
```
In the graph signature of the `ExportedProgram` these show up as `BUFFER_MUTATION` outputs
```
Graph signature:
# inputs
p_wrapped_module_wq_weight: PARAMETER target='wrapped_module.wq.weight'
p_wrapped_module_wk_weight: PARAMETER target='wrapped_module.wk.weight'
p_wrapped_module_wv_weight: PARAMETER target='wrapped_module.wv.weight'
p_wrapped_module_wo_weight: PARAMETER target='wrapped_module.wo.weight'
b_wrapped_module_kv_cache_k_cache: BUFFER target='wrapped_module.kv_cache.k_cache' persistent=True
b_wrapped_module_kv_cache_v_cache: BUFFER target='wrapped_module.kv_cache.v_cache' persistent=True
x: USER_INPUT
freqs_cos: USER_INPUT
freqs_sin: USER_INPUT
input_pos: USER_INPUT
# outputs
getitem_2: BUFFER_MUTATION target='wrapped_module.kv_cache.k_cache'
getitem_3: BUFFER_MUTATION target='wrapped_module.kv_cache.v_cache'
aten_view_copy_default_11: USER_OUTPUT
: USER_OUTPUT
```
Although these nodes are technically returned by the `fx.Graph`, `BUFFER_MUTATION` outputs are not included in the delegate call schema. Since the Vulkan delegate serialization uses the output node to mark which values are returned as outputs, this could result in a mismatch betwen the outputs of the Vulkan delegate and the outputs expected by the ExecuTorch runtime.
## Motivation
Previously, this mismatch was addressed in the runtime, by skipping the processing of non-tensor outputs. However, this solution does not account for the fact that in some models, paramters of the model may be returned as outputs. In this case, those parameter outputs would be skipped but the ExecuTorch runtime would still expect to receive them as outputs.
To solve the problem properly, this diff changes the serialization logic to check if an output node is a mutable buffer, and skip marking it as an output if so. In the runtime, all output nodes are processed instead of only processing tensor outputs.
Differential Revision: [D77281491](https://our.internmc.facebook.com/intern/diff/D77281491/)
[ghstack-poisoned]
|
This pull request was exported from Phabricator. Differential Revision: D77281491 |
…mutable buffers"
## Changes
* Move the logic skipping output nodes that are mutable buffers from runtime to AOT
## Context
A `fx.Graph` may return nodes that are mutable buffers:
```
class GraphModule(torch.nn.Module):
def forward(self, p_wrapped_module_wq_weight: "f32[2048, 2048]", p_wrapped_module_wk_weight: "f32[512, 2048]", p_wrapped_module_wv_weight: "f32[512, 2048]", p_wrapped_module_wo_weight: "f32[2048, 2048]", b_wrapped_module_kv_cache_k_cache: "f32[1, 2048, 8, 64]", b_wrapped_module_kv_cache_v_cache: "f32[1, 2048, 8, 64]", x: "f32[1, s27, 2048]", freqs_cos: "f32[s27, 32]", freqs_sin: "f32[s27, 32]", input_pos: "i64[1]"):
sym_size: "Sym(s27)" = torch.ops.aten.sym_size.int(x, 1)
...
# b_wrapped_module_kv_cache_*_cache are mutable buffers
# getitem_2 and getitem_3 are derived from mutable buffers, hence they are
# themselves mutable buffers
auto_functionalized = torch.ops.higher_order.auto_functionalized(torch.ops.llama.update_cache.default, value = getitem_1, cache = b_wrapped_module_kv_cache_k_cache, start_pos = _local_scalar_dense_1); getitem_1 = b_wrapped_module_kv_cache_k_cache = None
getitem_2: "f32[1, 2048, 8, 64]" = auto_functionalized[1]; auto_functionalized = None
auto_functionalized_1 = torch.ops.higher_order.auto_functionalized(torch.ops.llama.update_cache.default, value = aten_view_copy_default_8, cache = b_wrapped_module_kv_cache_v_cache, start_pos = _local_scalar_dense_1); aten_view_copy_default_8 = b_wrapped_module_kv_cache_v_cache = _local_scalar_dense_1 = None
getitem_3: "f32[1, 2048, 8, 64]" = auto_functionalized_1[1]; auto_functionalized_1 = None
...
aten_permute_copy_default_3: "f32[2048, 2048]" = executorch_exir_dialects_edge__ops_aten_permute_copy_default(p_wrapped_module_wo_weight, [1, 0]); p_wrapped_module_wo_weight = None
aten_view_copy_default_10: "f32[s27, 2048]" = executorch_exir_dialects_edge__ops_aten_view_copy_default(aten_view_copy_default_9, [sym_size, 2048]); aten_view_copy_default_9 = None
aten_mm_default_3: "f32[s27, 2048]" = executorch_exir_dialects_edge__ops_aten_mm_default(aten_view_copy_default_10, aten_permute_copy_default_3); aten_view_copy_default_10 = aten_permute_copy_default_3 = None
aten_view_copy_default_11: "f32[1, s27, 2048]" = executorch_exir_dialects_edge__ops_aten_view_copy_default(aten_mm_default_3, [1, sym_size, 2048]); aten_mm_default_3 = sym_size = None
# getitem_2 and getitem_3 are returned as outputs, presumably to prevent the
# update_cache calls from being removed due to dead code elimination
return (getitem_2, getitem_3, aten_view_copy_default_11, None)
```
In the graph signature of the `ExportedProgram` these show up as `BUFFER_MUTATION` outputs
```
Graph signature:
# inputs
p_wrapped_module_wq_weight: PARAMETER target='wrapped_module.wq.weight'
p_wrapped_module_wk_weight: PARAMETER target='wrapped_module.wk.weight'
p_wrapped_module_wv_weight: PARAMETER target='wrapped_module.wv.weight'
p_wrapped_module_wo_weight: PARAMETER target='wrapped_module.wo.weight'
b_wrapped_module_kv_cache_k_cache: BUFFER target='wrapped_module.kv_cache.k_cache' persistent=True
b_wrapped_module_kv_cache_v_cache: BUFFER target='wrapped_module.kv_cache.v_cache' persistent=True
x: USER_INPUT
freqs_cos: USER_INPUT
freqs_sin: USER_INPUT
input_pos: USER_INPUT
# outputs
getitem_2: BUFFER_MUTATION target='wrapped_module.kv_cache.k_cache'
getitem_3: BUFFER_MUTATION target='wrapped_module.kv_cache.v_cache'
aten_view_copy_default_11: USER_OUTPUT
: USER_OUTPUT
```
Although these nodes are technically returned by the `fx.Graph`, `BUFFER_MUTATION` outputs are not included in the delegate call schema. Since the Vulkan delegate serialization uses the output node to mark which values are returned as outputs, this could result in a mismatch betwen the outputs of the Vulkan delegate and the outputs expected by the ExecuTorch runtime.
## Motivation
Previously, this mismatch was addressed in the runtime, by skipping the processing of non-tensor outputs. However, this solution does not account for the fact that in some models, paramters of the model may be returned as outputs. In this case, those parameter outputs would be skipped but the ExecuTorch runtime would still expect to receive them as outputs.
To solve the problem properly, this diff changes the serialization logic to check if an output node is a mutable buffer, and skip marking it as an output if so. In the runtime, all output nodes are processed instead of only processing tensor outputs.
Differential Revision: [D77281491](https://our.internmc.facebook.com/intern/diff/D77281491/)
[ghstack-poisoned]
|
This pull request was exported from Phabricator. Differential Revision: D77281491 |
…mutable buffers"
## Changes
* Move the logic skipping output nodes that are mutable buffers from runtime to AOT
## Context
A `fx.Graph` may return nodes that are mutable buffers:
```
class GraphModule(torch.nn.Module):
def forward(self, p_wrapped_module_wq_weight: "f32[2048, 2048]", p_wrapped_module_wk_weight: "f32[512, 2048]", p_wrapped_module_wv_weight: "f32[512, 2048]", p_wrapped_module_wo_weight: "f32[2048, 2048]", b_wrapped_module_kv_cache_k_cache: "f32[1, 2048, 8, 64]", b_wrapped_module_kv_cache_v_cache: "f32[1, 2048, 8, 64]", x: "f32[1, s27, 2048]", freqs_cos: "f32[s27, 32]", freqs_sin: "f32[s27, 32]", input_pos: "i64[1]"):
sym_size: "Sym(s27)" = torch.ops.aten.sym_size.int(x, 1)
...
# b_wrapped_module_kv_cache_*_cache are mutable buffers
# getitem_2 and getitem_3 are derived from mutable buffers, hence they are
# themselves mutable buffers
auto_functionalized = torch.ops.higher_order.auto_functionalized(torch.ops.llama.update_cache.default, value = getitem_1, cache = b_wrapped_module_kv_cache_k_cache, start_pos = _local_scalar_dense_1); getitem_1 = b_wrapped_module_kv_cache_k_cache = None
getitem_2: "f32[1, 2048, 8, 64]" = auto_functionalized[1]; auto_functionalized = None
auto_functionalized_1 = torch.ops.higher_order.auto_functionalized(torch.ops.llama.update_cache.default, value = aten_view_copy_default_8, cache = b_wrapped_module_kv_cache_v_cache, start_pos = _local_scalar_dense_1); aten_view_copy_default_8 = b_wrapped_module_kv_cache_v_cache = _local_scalar_dense_1 = None
getitem_3: "f32[1, 2048, 8, 64]" = auto_functionalized_1[1]; auto_functionalized_1 = None
...
aten_permute_copy_default_3: "f32[2048, 2048]" = executorch_exir_dialects_edge__ops_aten_permute_copy_default(p_wrapped_module_wo_weight, [1, 0]); p_wrapped_module_wo_weight = None
aten_view_copy_default_10: "f32[s27, 2048]" = executorch_exir_dialects_edge__ops_aten_view_copy_default(aten_view_copy_default_9, [sym_size, 2048]); aten_view_copy_default_9 = None
aten_mm_default_3: "f32[s27, 2048]" = executorch_exir_dialects_edge__ops_aten_mm_default(aten_view_copy_default_10, aten_permute_copy_default_3); aten_view_copy_default_10 = aten_permute_copy_default_3 = None
aten_view_copy_default_11: "f32[1, s27, 2048]" = executorch_exir_dialects_edge__ops_aten_view_copy_default(aten_mm_default_3, [1, sym_size, 2048]); aten_mm_default_3 = sym_size = None
# getitem_2 and getitem_3 are returned as outputs, presumably to prevent the
# update_cache calls from being removed due to dead code elimination
return (getitem_2, getitem_3, aten_view_copy_default_11, None)
```
In the graph signature of the `ExportedProgram` these show up as `BUFFER_MUTATION` outputs
```
Graph signature:
# inputs
p_wrapped_module_wq_weight: PARAMETER target='wrapped_module.wq.weight'
p_wrapped_module_wk_weight: PARAMETER target='wrapped_module.wk.weight'
p_wrapped_module_wv_weight: PARAMETER target='wrapped_module.wv.weight'
p_wrapped_module_wo_weight: PARAMETER target='wrapped_module.wo.weight'
b_wrapped_module_kv_cache_k_cache: BUFFER target='wrapped_module.kv_cache.k_cache' persistent=True
b_wrapped_module_kv_cache_v_cache: BUFFER target='wrapped_module.kv_cache.v_cache' persistent=True
x: USER_INPUT
freqs_cos: USER_INPUT
freqs_sin: USER_INPUT
input_pos: USER_INPUT
# outputs
getitem_2: BUFFER_MUTATION target='wrapped_module.kv_cache.k_cache'
getitem_3: BUFFER_MUTATION target='wrapped_module.kv_cache.v_cache'
aten_view_copy_default_11: USER_OUTPUT
: USER_OUTPUT
```
Although these nodes are technically returned by the `fx.Graph`, `BUFFER_MUTATION` outputs are not included in the delegate call schema. Since the Vulkan delegate serialization uses the output node to mark which values are returned as outputs, this could result in a mismatch betwen the outputs of the Vulkan delegate and the outputs expected by the ExecuTorch runtime.
## Motivation
Previously, this mismatch was addressed in the runtime, by skipping the processing of non-tensor outputs. However, this solution does not account for the fact that in some models, paramters of the model may be returned as outputs. In this case, those parameter outputs would be skipped but the ExecuTorch runtime would still expect to receive them as outputs.
To solve the problem properly, this diff changes the serialization logic to check if an output node is a mutable buffer, and skip marking it as an output if so. In the runtime, all output nodes are processed instead of only processing tensor outputs.
Differential Revision: [D77281491](https://our.internmc.facebook.com/intern/diff/D77281491/)
[ghstack-poisoned]
|
This pull request was exported from Phabricator. Differential Revision: D77281491 |
968688d
into
gh/SS-JIA/249/base
…fers (#12020) This PR was created by the merge bot to help merge the original PR into the main branch. ghstack PR number: #11983 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/249/base ghstack PR head: https://github.com/pytorch/executorch/tree/gh/SS-JIA/249/head Merge bot PR base: https://github.com/pytorch/executorch/tree/main Merge bot PR head: https://github.com/pytorch/executorch/tree/gh/SS-JIA/249/orig @diff-train-skip-merge Co-authored-by: Stephen Jia <[email protected]>
Stack from ghstack (oldest at bottom):
Changes
Context
A
fx.Graphmay return nodes that are mutable buffers:In the graph signature of the
ExportedProgramthese show up asBUFFER_MUTATIONoutputsAlthough these nodes are technically returned by the
fx.Graph,BUFFER_MUTATIONoutputs are not included in the delegate call schema. Since the Vulkan delegate serialization uses the output node to mark which values are returned as outputs, this could result in a mismatch betwen the outputs of the Vulkan delegate and the outputs expected by the ExecuTorch runtime.Motivation
Previously, this mismatch was addressed in the runtime, by skipping the processing of non-tensor outputs. However, this solution does not account for the fact that in some models, paramters of the model may be returned as outputs. In this case, those parameter outputs would be skipped but the ExecuTorch runtime would still expect to receive them as outputs.
To solve the problem properly, this diff changes the serialization logic to check if an output node is a mutable buffer, and skip marking it as an output if so. In the runtime, all output nodes are processed instead of only processing tensor outputs.
Differential Revision: D77281491