Skip to content

[RFC] CPU float16 performance optimization on eager mode. #97068

Open

Description

@mingfeima

🚀 The feature, motivation and pitch

The RFC is to improve float16 performance as well as Op coverage on PyTorch CPU backend on eager mode.

Float16 and BFloat16 are both commonly used reduced floating point types for performance improvement in neural network inference/training. On the CPU side, previous optimization efforts have been placed more on BFloat16, which leaves float16 at a relatively primitive status.

On the 4th generation Intel® Xeon® Scalable processor (Sapphire Rapids), a new fp16 instruction set architecture for Intel® AVX-512 has been added, e.g. avx512-fp16. The instruction set supports a wide range of general-purpose numeric operations for fp16. One the next generation of Xeon, Intel® Advanced Matrix Extensions (AMX) will have fp16 support, e.g. amx-fp16.

This proposal would help the scenario when the model is pre trained on GPU with mixed precision of float16/float32 and users intend to do deployment on the CPU side without modifying the model weights, for instance, many HuggingFace models belong to this scenario.

This project will be targeting at:

  • Improve float16 Op coverage on PT from 52% to ~80% (for reference, BFloat16 Op coverage ratio is 83%). Torchbench models and HuggingFace models will be prioritized.
  • Improve float16 performance: Since the current fp16 performance on CPU is very low, we can use fp16 v.s. fp32 speedup as the metrics here. Performance speedup will match bf16, on average a 4x-5x performance speedup will be achieved on hardware with amx-fp16 support.
  • Add Automatic Mixed Precision (AMP) support for float16 on CPU.
  • Improve float16 numerical stability: use fp32 as accumulate type in reduction Ops, e.g. mean.

Technically, the optimization will be carried out as below:

Compute intensive Ops (e.g. Convolution, Gemm, and RNN):

  • Rely on oneDNN for optimal performance when the hardware has float16 acceleration.
  • Functional coverage will be added for hardware without float16 acceleration, no performance gain.

Generic ATen kernels:

  • Extend avx256 and avx512 and vectorization utils for dtype Half. Add native convert intrinsics: _mm256_cvtph_ps/_mm256_cvtps_ph (Rounding mode: RNE).
  • Unary and binary Op kernels: map to fp32 for computation.
  • Non arithmetic Ops: do direct memory copy (no dtype conversion), e.g. cat, index_select.
  • Reduction Ops: use fp32 as accumulate type.
  • NN Ops: reuse kernels of BFloat16.

Test Plan:

  • Extend vec_test_all_types_AVX2 and vec_test_all_types_AVX512 for float16.
  • Add OpInfo at torch/testing/_internal/common_methods_invocations.py.
  • Provide specific test cases for reduced floating point.

Alternatives

No response

Additional context

Previous RFC on extending AMP fp16 on CPU:

Float16 support in torch inductor working in parallel (implemented in similar method as BFloat16 support), has dependency on explicit vectorization utils of at::vec::Vectorized<Half>.

Pull Requests related to this feature requests:

cc @jgong5 @XiaobingSuper @sanchitintel @ashokei @jingxu10

Activity

  1. added
    module: cpu
    module: half
    feature
    on Mar 18, 2023
  2. moved this to In Progress in PyTorch Intelon Mar 18, 2023
  3. changed the title [-][Feature Proposal] CPU float16 performance optimization on eager mode.[/-] [+][RFC] CPU float16 performance optimization on eager mode.[/+] on Mar 20, 2023
  4. jgong5 commented on Mar 20, 2023

    @jgong5
    Collaborator

    More clarification on avx512-fp16: we are not going to leverage avx512-fp16 to optimize non-conv/gemm ATen ops (primarily pointwise and reductions) but plan to follow the similar "fused type cast" approaches as how we optimize those ops for the bf16 data type, i.e. fp16 data are converted to/from fp32, fused with computation which happens with fp32. The type cast will rely on the f16c instruction set, as noted by @mingfeima in the RFC description. This is due to the following considerations:

    1. avx512-fp16 support needs GCC12 which introduces extra compiler dependency to PyTorch while f16c is well supported by older compilers.
    2. Reductions have to use fp32 accumulate data type anyway.
    3. Pointwise ops might benefit a bit from avx512-fp16 vs. fused type cast approach but we don't expect much, since they are memory bound in most cases.
    4. Similar fused type cast approach will be adopted in the Inductor CPP codegen as well. We expect this is good enough thanks to the fusion of more ops.

    amx-fp16 will be leveraged to optimize conv and gemm ops via the oneDNN library.

  5. added
    triaged
    on Mar 21, 2023
  6. ternaus commented on Oct 11, 2026

    @ternaus

    Consolidated Albucore CPU float16 performance evidence

    These consumer measurements use PyTorch 2.14.1 on Apple M4 Max with one CPU thread and compare complete public routes on the same half-quantized data.

    • Pointwise routes remain slower in some regions: mixed torch.mul(float16, float32, out=float16) took 1.82–1.89× float32 public time; direct C9 half controls took 1.055–1.221× the float32 bridge. addcmul normalization candidates took 1.98–3.17× float32 on the tested CHW C3/C9 cases, and a composed add_weighted half candidate missed one ULP under cancellation.
    • Exact NumPy float16→uint8 conversion took 1.29–1.31× float32. An exhaustive 65,536-entry LUT was exact but 1.39–2.46× slower than the current half route.
    • Half accumulation is not a universal substitute: float32 accumulation misses subnormal contributions in per-channel means, while Torch CPU std cannot write a wider output buffer. Exact float64 reduction paths are needed for those cases.
    • Native half Conv3d separable filtering took 1.11–1.30× the float32 working route and rounded between passes. Direct half Tensor C1 bilinear resize took 1.19–5.21× float32.

    These results identify CPU workloads where a fused or wider-accumulator kernel could remove Albucore's conversion overhead while preserving the existing output precision.

  7. egeboy35 commented on Nov 21, 2026

    @egeboy35
    Contributor

    @mingfeima I checked the current state of this RFC on my laptop's CPU, which doesn't support AVX512_FP16. Most of it looks good so far. There are still a few gaps in group_norm and argmax that I'd be happy to work on, if you're okay with that.

    Drafted by Claude (AI) for @egeboy35.

    A status check against the goals in this RFC, measured on an i7-11800H (AVX512, no AVX512-FP16 or AMX).

    Coverage. Counting OpInfos on a build of main (87f51fb), 548 of the 678 OpInfos that support float32 on CPU also support float16 (80.8%), against 552 for bfloat16 (81.4%). By that measure the coverage goal is met. The 7 OpInfos with bfloat16 but no float16 on CPU are nn.functional.rrelu, polygamma (n=1..4) and round with decimals=3 / decimals=-3.

    Speed. Ops where float16 is clearly slower than bfloat16, on the 2026-11-17 nightly (median of 3 runs, ms):

    op threads fp32 bf16 fp16 fp16 / bf16
    matmul 512x512 1 2.17 9.35 121.7 13.0x
    matmul 512x512 4 0.95 3.08 105.8 34.3x
    conv2d 3x3, (8, 64, 32, 32) 4 2.22 6.38 126.1 19.8x
    linear 256x1024 -> 1024 4 2.19 5.92 12.7 2.1x
    vector_norm, (1024, 1024), dim=-1 1 0.147 0.144 1.49 10.3x
    group_norm, (32, 64, 32, 32), 8 groups 4 0.285 0.324 0.883 2.7x
    gelu, 2^20 elements 4 0.192 0.184 0.359 2.0x
    argmax, (1024, 1024), dim=-1 4 0.413 0.764 1.086 1.4x

    I have prototypes for group_norm (contiguous forward) and argmax/argmin that close the float16 gap on this CPU, and the argmax change also speeds up bfloat16. I'll open PRs once the tests are in place. For gelu, #189234 rewrites the same CPU kernel and drops the oneDNN dispatch on x86, so I'm measuring that PR for float16 rather than sending a competing change. Is anyone already working on group_norm or argmax for float16?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    featuremodule: cpumodule: halftriaged

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions