[TRANSFORMATIONS][CPU] Share activations scaling between GPU and CPU #38563
Results (LTX-Video, Core Ultra 7 258V, f16):
- T5 encoder: 100% NaN without scaling, 0 NaN with it (cosine 0.99998 vs f32).
- Overhead: about 0.1–0.4% latency on the T5 encoder and the VAE.
- Full 97-frame sample -->
https://github.com/user-attachments/assets/bd357139-3666-4615-b0f4-7548a19f3367
@v-Golubev @CuriousPanCake Please take a look, thanks!
build_jenkins
build_jenkins
build_jenkins
The exported model pass is not idempotent: every invocation rematches the same MatMul/Convolution nodes and inserts another scale pair, so registering or running it twice scales by an additional factor and adjusts normalization epsilon again. Mark already processed linear ops (or recognize the existing scale pair) so a second run is a no-op, and cover this with a double-run transformation test.
Set the explicit flag only after conversion succeeds. Plugin::set_property() applies this directly to the shared engConfig, so an invalid value currently throws after leaving the flag set; subsequent compilations then ignore a valid model rt_info value and silently retain the default factor.
Setting the cache directory on this core mutates the shared PluginCache Core used by other SubgraphBaseTest fixtures. Removing the directory does not clear that property, so later tests remain cache-enabled against this test-specific path and can become order-dependent or leave artifacts. Pass the cache directory through this fixture's per-compile configuration instead.
<!-- ccr-overview-v2 -->
🟡 Changes recommended
The pass is not idempotent, and property parsing plus cache-test setup can leave persistent invalid state.
<details open>
<summary><strong>3 open findings</strong></summary>
</details>
<details>
<summary><strong>What changed in this PR</strong></summary>
Shares activation scaling between GPU and x86-64 CPU through a common transformation pass.
Changes:
ActivationsScaling and extends normalization handling.ShapeOf.| File | Description |
| ---- | ----------- |
| src/plugins/intel_gpu/src/plugin/transformations_pipeline.cpp | Uses the common scaling pass. |
| src/plugins/intel_cpu/tests/functional/sources.cmake | Registers the CPU test. |
| src/plugins/intel_cpu/tests/functional/custom/subgraph_tests/src/x64/activations_scaling.cpp | Tests inference and caching. |
| src/plugins/intel_cpu/tests/functional/custom/behavior/ov_plugin/properties.cpp | Checks plugin property exposure. |
| src/plugins/intel_cpu/tests/functional/custom/behavior/ov_executable_network/properties.cpp | Tests property values and precedence. |
| src/plugins/intel_cpu/src/transformations/transformation_pipeline.cpp | Enables scaling for CPU f16. |
| src/plugins/intel_cpu/src/plugin.cpp | Exposes the CPU property. |
| src/plugins/intel_cpu/src/config.h | Stores scaling configuration. |
| src/plugins/intel_cpu/src/config.cpp | Parses property and runtime metadata. |
| src/plugins/intel_cpu/src/compiled_model.cpp | Reports compiled-model property. |
| src/common/transformations/tests/common_optimizations/shared_ops_optimization.cpp | Tests i32 ShapeOf preservation. |
| src/common/transformations/tests/common_optimizations/activations_scaling_test.cpp | Extends transformation coverage. |
| src/common/transformations/src/transformations/common_optimizations/shared_ops_optimization.cpp | Preserves ShapeOf output type. |
| src/common/transformations/src/transformations/common_optimizations/activations_scaling.cpp | Implements the common pipeline. |
| src/common/transformations/include/transformations/common_optimizations/activations_scaling.hpp | Declares and documents the pass. |
| docs/articles_en/openvino-workflow/running-inference/optimize-inference/precision-control.rst | Documents CPU support. |
| docs/articles_en/openvino-workflow/running-inference/inference-devices-and-modes/cpu-device.rst | Lists the CPU property. |
</details>
🧠 Review effort: Balanced
---
Give feedback about Copilot approvals in this survey to enter a drawing for a $150 gift card.
This is the original ActivationsScaling behavior, we shouldn't change it within this PR
done
done
build_jenkins
Please note: EliminateScalarMul is shared with GPU, so this extension changes GPU graphs as well, not only CPU ones. GPU also keeps some norms in f32 (e.g. DisableFP16CompForGemma3RMSPattern keeps RMS in f32, so ConvertPrecision inserts a Convert in front of it), and now the scale-up Multiply is eliminated there too. So, as far as I see, GPU output is not guaranteed to be bit-identical anymore, as the description states.
I believe the change itself is correct, but since it affects GPU accuracy, could you please validate it on GPU on a model with f32 norms (e.g. Gemma3 with ACTIVATIONS_SCALE_FACTOR) and cover this case in src/plugins/intel_gpu/tests/functional/subgraph_tests/dynamic/activations_scaling.cpp? Please also update the PR description accordingly
Please note: #38680 changes exactly this pattern and adds tests right after EliminateScalarMulTest, so the PRs conflict in activations_scaling.cpp and activations_scaling_test.cpp. When resolving the conflict, the gamma-less branch must take post_convert_m as well, otherwise Multiply -> Convert -> RMS will be handled only for RMS with gamma:
auto rms_with_gamma_m = pattern::wrap_type<ov::op::internal::RMS>({post_convert_m, pattern::any_input()});
auto rms_without_gamma_m = pattern::wrap_type<ov::op::internal::RMS>({post_convert_m});
Also, #38680 adds the
ActivationsScalingFeatureAxisRMS GPU test, which will go through the new ActivationsScaling pass, so GPU activations scaling tests should be re-run by whichever PR is merged second.Minor:
const float m_scale_factor;
const ov::element::Type m_scaled_prec;
We mustn't disable the existing perf optimizations because of activations scaling. Let's better register ActivationsScaling before MLPFusion/QKVProjFusion
Please note: I suppose MLPFusion pattern may need to tolerate a scalar Multiply between the gate MatMul and the activation after scaling, since the scale-up can't be moved through Swish/Gelu. QKVProjFusion should be fine since it matches on the shared input. Could you please double check this?
Activations scaling is needed on all platforms which use f16 inference precision (e.g. f16 is the default precision on ARM), so let's register it for all platforms, not only x64. Please note: RMSFusion and DecomposeRMSNorm are registered only for x64 now, and the RMSNorm node itself requires avx2+, so on other platforms we need RMSFusion -> ActivationsScaling -> DecomposeRMSNorm as well, otherwise the scale-up is not eliminated at the norms. The functional test and the docs ("x86-64 only") should be updated accordingly
build_jenkins
done, registered for all platforms with RMSFusion -> ActivationsScaling -> DecomposeRMSNorm outside x64.
updated tests and docs as well
thanks for the heads up, will take care of this!
Thanks for the review @v-Golubev!
I haven't addressed all the changes yet, will address the remaining ones soon :)
v-Golubev
goyaladitya05
Details:
- This PR is a follow up to #37041, it moves GPU's activations scaling pipeline into a common
ov::pass::ActivationsScalingModelPass. GPU now registers this pass; its output is bit-identical to before.- CPU registers the pass on x86-64 when inference_precision is f16 and activations_scale_factor > 0. The factor comes from the property or the IR's runtime_options, where optimum-intel sets it to 8 for diffusion text encoders and VAEs.
- The pass runs on a copy of the caller's PassConfig, so plugin callbacks apply to its inner passes while its LPT disables stay local.
- On AMX-FP16 CPUs, MLP/QKV fusion is skipped when scaling is on, so the MatMuls stay visible to the pass.
-
EliminateScalarMulnow also matches when a Convert sits between the scale and the norm, which happens because CPU keeps norms in f32.-
SharedOpOptimizationkeeps the output type when it upgradesv0::ShapeOf. It used to hardcode i64, which broke CPU's i32 shape subgraphs and failed T5 compilation.Tickets:
- Closes #37039
AI Assistance:
- AI assistance used: yes
- If yes, summarize how AI was used and what human validation was performed (build/tests/manual checks). Used for occasional code generation and review.