Skip to content

Conversation

@copybara-service
Copy link
Contributor

@copybara-service copybara-service bot commented Oct 21, 2025

Subgraph rewrites for trivial arithmetic operations:

  • Replace add(a, neg(b)), add(neg(a), b), or sub(a, neg(b)) with sub(a, b), sub(b, a), or add(a, b), respectively,
  • Replace mul(neg(a), neg(b)) or div(neg(a), neg(b)) with mul(a, b) or div(a, b), respectively,
  • Replace mul(x, 1.0), div(x, 1.0), add(x, 0.0), or sub(x, 0.0) with x,
  • Replace mul(x, 0.0f), div(0.0f, x) with a static zero.
  • Replace div(x, x) or sub(x, x) with a static one or zero, respectively.

To make identifying and propagating these cases easier (e.g. propagating the dynamic shapes of the output), this change introduces two new xnn_value flags:

  • XNN_VALUE_FLAG_IS_ZERO, and
  • XNN_VALUE_FLAG_IS_ONE.

These flags are initially attached to static scalar values that are zero and one, respectively, and then propagated through the nodes of the subgraph.

@copybara-service copybara-service bot force-pushed the test_822147090 branch 11 times, most recently from 88bc5ee to 42a2c0a Compare October 26, 2025 11:28
@copybara-service copybara-service bot changed the title Elide spurious arithmetic ops such as mul(x, 0.0), div(0.0, x), mul(x, 1.0), div(x, 1.0), add(x, 0.0), and sub(x, 0.0). Subgraph rewrites for trivial arithmetic operations: Oct 26, 2025
* Replace `add(a, neg(b))`, `add(neg(a), b)`, or `sub(a, neg(b))` with `sub(a, b)`, `sub(b, a)`, or `add(a, b)`, respectively,
* Replace `mul(neg(a), neg(b))` or `div(neg(a), neg(b))` with `mul(a, b)` or `div(a, b)`, respectively,
* Replace `mul(x, 1.0)`, `div(x, 1.0)`, `add(x, 0.0)`, or `sub(x, 0.0)` with `x`,
* Replace `mul(x, 0.0f)`, `div(0.0f, x)` with a static zero.
* Replace `div(x, x)` or `sub(x, x)` with a static one or zero, respectively.

To make identifying and propagating these cases easier (e.g. propagating the dynamic shapes of the output), this change introduces two new `xnn_value` flags:
* `XNN_VALUE_FLAG_IS_ZERO`, and
* `XNN_VALUE_FLAG_IS_ONE`.

These flags are initially attached to static scalar values that are zero and one, respectively, and then propagated through the nodes of the subgraph.

PiperOrigin-RevId: 822147090
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants