Skip to content

Don't Fold In Negative Scalar Multiplies Into Quant/Dequant (#23261) - #23261

Closed
RahulC7 wants to merge 1 commit into
pytorch:mainfrom
RahulC7:export-D120871768
Closed

RahulC7 wants to merge 1 commit into
pytorch:mainfrom
RahulC7:export-D120871768

Conversation

@RahulC7

@RahulC7 RahulC7 commented Sep 29, 2026 •

Copy link
Copy Markdown
Contributor

Summary:

What this diff does

  • Make all three multiply fusion passes stop when the derived scale is not positive.
  • Leave the original multiply in the graph when fusion is not valid.
  • Add focused tests for tensor-to-quant, tensor-from-dequant, and scalar-from-dequant with multipliers 0.0 and -1.0.

Result

The full chunk-32 graph keeps one explicit aten.mul.Tensor with aten.full(-1.0) for N_c = -A_c. All quantize, dequantize, requantize, and packed prep scales in the dumped graph are finite and positive.

Reviewed By: ThomasJannaud

Differential Revision: D120871768

@pytorch-bot

pytorch-bot Bot commented Sep 29, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

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

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

❌ 1 Cancelled Job

As of commit 7eb92a3 with merge base d46a886 (image):

CANCELLED JOB - The following job was cancelled. Please retry:

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 Sep 29, 2026
@linux-foundation-easycla

linux-foundation-easycla Bot commented Sep 29, 2026 •

Copy link
Copy Markdown

CLA Signed
The committers listed above are authorized under a signed CLA.

  • ✅ login: RahulC7 / name: Rahul Chandra (4616348)

@meta-codesync

meta-codesync Bot commented Sep 29, 2026

Copy link
Copy Markdown
Contributor

@RahulC7 has exported this pull request. If you are a Meta employee, you can view the originating Diff in D120871768.

@github-actions

Copy link
Copy Markdown

This PR needs a release notes: label

If your change should be included in the release notes (i.e. would users of this library care about this change?), please use a label starting with release notes:. This helps us keep track and include your important work in the next release notes.

To add a label, you can comment to pytorchbot, for example
@pytorchbot label "release notes: none"

For more information, see
https://github.com/pytorch/pytorch/wiki/PyTorch-AutoLabel-Bot#why-categorize-for-release-notes-and-how-does-it-work.

@meta-codesync meta-codesync Bot changed the title Don't Fold In Negative Scalar Multiplies Into Quant/Dequant Don't Fold In Negative Scalar Multiplies Into Quant/Dequant (#23261) Oct 20, 2026
RahulC7 added a commit to RahulC7/executorch that referenced this pull request Oct 20, 2026
…23261)

Summary:

## Context
The way we lowered the GDN model is we looked at the traced model, and then recreated "stages" of it(i.e created a graph for that stage), and lowered each one of those stages, making them go through the entire compiler pipeline. 

That’s usually fine for the ops/nodes in the middle, because they still have the surrounding nodes(producer consumer chain) and those get collapsed/changed in the same way, but in the boundaries, where there’s connections to other other nodes not in the subgraph, the lowering might end up producing a different result than what we expect in the actual graph. The quant/dequant insertion, calibration and FuseMul adjacency depend on the global context so it’ll look different. 

That's what happens here. 

## Problem

In D117100989, we do `N_c = -A_c` as `aten.full(-1.0) + aten.mul.Tensor`(elementwise kernel). However, before the multiply, there's a quantize, and so multiply gets combined with the quantize. The issue is that this is a negative, and so we're going to have a negative scale in the quantize/dequantize scales, because of `FuseMulTensorIntoDequantPass` and `FuseMulScalarIntoDequantPass` folding it in. 





## What this diff does

- Make all three multiply fusion passes stop when the derived scale is not positive.
- Leave the original multiply in the graph when fusion is not valid.
- Add focused tests for tensor-to-quant, tensor-from-dequant, and scalar-from-dequant with multipliers `0.0` and `-1.0`.
- Give the chunk-32 GDN test a test-only gate offset of `-5.0`. This keeps the 32-step cumulative `Gamma_c` values in the useful int16 calibration range. Chunk size 2 stays unchanged.
- Use measured tolerances for the three equivalent chunk-32 pre-trace transforms.

## Result

The full chunk-32 graph keeps one explicit `aten.mul.Tensor` with `aten.full(-1.0)` for `N_c = -A_c`. All quantize, dequantize, requantize, and packed prep scales in the dumped graph are finite and positive.

Reviewed By: ThomasJannaud

Differential Revision: D120871768
…23261)

Summary:
Pull Request resolved: pytorch#23261

## Context
The way we lowered the GDN model is we looked at the traced model, and then recreated "stages" of it(i.e created a graph for that stage), and lowered each one of those stages, making them go through the entire compiler pipeline.

That’s usually fine for the ops/nodes in the middle, because they still have the surrounding nodes(producer consumer chain) and those get collapsed/changed in the same way, but in the boundaries, where there’s connections to other other nodes not in the subgraph, the lowering might end up producing a different result than what we expect in the actual graph. The quant/dequant insertion, calibration and FuseMul adjacency depend on the global context so it’ll look different.

That's what happens here.

## Problem

In D117100989, we do `N_c = -A_c` as `aten.full(-1.0) + aten.mul.Tensor`(elementwise kernel). However, before the multiply, there's a quantize, and so multiply gets combined with the quantize. The issue is that this is a negative, and so we're going to have a negative scale in the quantize/dequantize scales, because of `FuseMulTensorIntoDequantPass` and `FuseMulScalarIntoDequantPass` folding it in.

## What this diff does

- Make all three multiply fusion passes stop when the derived scale is not positive.
- Leave the original multiply in the graph when fusion is not valid.
- Add focused tests for tensor-to-quant, tensor-from-dequant, and scalar-from-dequant with multipliers `0.0` and `-1.0`.
- Give the chunk-32 GDN test a test-only gate offset of `-5.0`. This keeps the 32-step cumulative `Gamma_c` values in the useful int16 calibration range. Chunk size 2 stays unchanged.
- Use measured tolerances for the three equivalent chunk-32 pre-trace transforms.

## Result

The full chunk-32 graph keeps one explicit `aten.mul.Tensor` with `aten.full(-1.0)` for `N_c = -A_c`. All quantize, dequantize, requantize, and packed prep scales in the dumped graph are finite and positive.

Reviewed By: ThomasJannaud

Differential Revision: D120871768

This branch was previously deployed

1 inactive deployment
cadence — 7eb92a3a Deployed Oct 20, 2026 by RahulC7 via hifi-op-test / hifi4 #34209
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. meta-exported

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants