Skip to content

Arm backend: Reuse identical CONST_SHAPE nodes#19770

Merged
martinlsm merged 2 commits into
pytorch:mainfrom
martinlsm:optimize-const-shapes
May 27, 2026
Merged

Arm backend: Reuse identical CONST_SHAPE nodes#19770
martinlsm merged 2 commits into
pytorch:mainfrom
martinlsm:optimize-const-shapes

Conversation

@martinlsm
Copy link
Copy Markdown
Collaborator

@martinlsm martinlsm commented May 26, 2026

Cache CONST_SHAPE nodes created by InsertConstShapesPass and reuse them when a later view/repeat needs the same shape. This removes duplicate shape constants.

This improvement is model dependent. Models with few repeated literal shapes will not see any meaningful change, but some models can benefit from it notably.

The table below shows the results of a local test lowering DeiT Tiny to TOSA-FP. The lowering time reduced in this run, likely because passes following InsertConstShapesPass had fewer nodes to iterate over.

Metric Baseline Optimized Delta
Total ops 2106 1736 -370 (-17.6%)
CONST_SHAPE 466 96 -370 (-79.4%)
TOSA size 23.82 MB 23.75 MB -71.6 KB (-0.3%)
Execution time 118.7 s 78.4 s -40.3 s (-34.0%)

cc @digantdesai @freddan80 @per @zingo @oscarandersson8218 @mansnils @Sebastian-Larsson @robell @rascani

Cache CONST_SHAPE nodes created by InsertConstShapesPass and reuse them
when a later view/repeat needs the same shape. This removes duplicate
shape constants.

This improvement is model dependent. Models with few repeated literal
shapes will not see any meaningful change, but some models can benefit
from it notably.

The table below shows the results of a local test lowering DeiT Tiny to
TOSA-FP. The lowering time reduced in this run, likely because passes
following InsertConstShapesPass had fewer nodes to iterate over.

| Metric         | Baseline | Optimized | Delta            |
| -------------- | -------- | --------- | ---------------- |
| Total ops      | 2106     | 1736      | -370 (-17.6%)    |
| CONST_SHAPE    | 466      | 96        | -370 (-79.4%)    |
| TOSA size      | 23.82 MB | 23.75 MB  | -71.6 KB (-0.3%) |
| Execution time | 118.7 s  | 78.4 s    | -40.3 s (-34.0%) |

Signed-off-by: Martin Lindström <Martin.Lindstroem@arm.com>
Change-Id: I620b04e496996fafebc9c7f7854fd8399b3d6d4b
Copilot AI review requested due to automatic review settings May 26, 2026 11:37
@martinlsm martinlsm requested a review from digantdesai as a code owner May 26, 2026 11:37
@pytorch-bot
Copy link
Copy Markdown

pytorch-bot Bot commented May 26, 2026

🔗 Helpful Links

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

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

❗ 1 Active SEVs

There are 1 currently active SEVs. If your PR is affected, please view them below:

⏳ 38 Pending, 1 Unrelated Failure

As of commit d06f7fd with merge base 77df9b7 (image):

BROKEN TRUNK - The following job failed but were present on the merge base:

👉 Rebase onto the `viable/strict` branch to avoid these failures

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

@meta-cla meta-cla 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 May 26, 2026
@martinlsm
Copy link
Copy Markdown
Collaborator Author

@pytorchbot label ciflow/trunk

@martinlsm
Copy link
Copy Markdown
Collaborator Author

@pytorchbot label "partner: arm"

@pytorch-bot pytorch-bot Bot added the partner: arm For backend delegation, kernels, demo, etc. from the 3rd-party partner, Arm label May 26, 2026
@martinlsm
Copy link
Copy Markdown
Collaborator Author

@pytorchbot label "release notes: none"

@pytorch-bot pytorch-bot Bot added the release notes: none Do not include this in the release notes label May 26, 2026
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR optimizes the Arm backend lowering pipeline by reusing identical tosa.CONST_SHAPE nodes emitted by InsertConstShapesPass, reducing duplicate shape constants in generated TOSA graphs.

Changes:

  • Adds an instance-level cache keyed by shape tuple.
  • Reuses previously created CONST_SHAPE nodes for later view_copy/repeat ops with the same literal shape.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +54 to +55
const_node = self._const_shape_cache.get(shape)
if const_node is None:
Comment on lines +54 to +55
const_node = self._const_shape_cache.get(shape)
if const_node is None:
@github-actions github-actions Bot added the module: arm Issues related to arm backend label May 27, 2026
@martinlsm martinlsm merged commit d83aa08 into pytorch:main May 27, 2026
456 of 465 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/trunk CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. module: arm Issues related to arm backend partner: arm For backend delegation, kernels, demo, etc. from the 3rd-party partner, Arm release notes: none Do not include this in the release notes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants