Skip to content

Better error message when trying to run fp16 weights on CPU #96292

Closed

Description

@patrickvonplaten

🚀 The feature, motivation and pitch

Hey 👋 from the Hugging Face Open-Source team,

We're seeing the following issue over and over again across libraries

RuntimeError: "LayerNormKernelImpl" not implemented for 'Half'

or:

RuntimeError: "addmm_impl_cpu_" not implemented for 'Half'

E.g.: https://github.com/runwayml/stable-diffusion/issues/23

The problem here is that a PyTorch model has been converted to fp16 and the user tried to run it on CPU, e.g. the following:

from torch import nn
import torch

linear = nn.Linear(2,2, dtype=torch.float16)

tensor = torch.ones((2,), dtype=torch.float16)

linear(tensor)

yields:

"addmm_impl_cpu_" not implemented for 'Half'

Could we maybe catch such errors in the forward of https://pytorch.org/docs/stable/_modules/torch/nn/modules/module.html#Module

and return a simpler error message that just says "Float16 cannot be run on CPU"?

Alternatives

No response

Additional context

No response

cc @malfet

Activity

  1. added
    module: error checking
    on Mar 8, 2023
  2. drisspg commented on Mar 8, 2023

    @drisspg
    Contributor

    Yeah I think this is a good idea. We would be happy to accept a PR doing this, and I would open to review it

  3. patrickvonplaten commented on Mar 8, 2023

    @patrickvonplaten
    Author

    Cool! Will open a PR for it

  4. jgong5 commented on Mar 9, 2023

    @jgong5
    Collaborator

    @patrickvonplaten @drisspg Can we make it a warning instead of an error? We are in progress adding FP16 support on CPU. The work would improve the functional coverage of FP16 ops (which can address the problems you raise), improve the performance of CPU FP16 ops (expecting performance speedup with non conv/gemm ops, and conv/gemm will be sped up on next generation Xeon processors), add FP16 autocast (check RFC) and add FP16 support in TorchInductor. cc @mingfeima and @CaoE

  5. patrickvonplaten commented on Mar 9, 2023

    @patrickvonplaten
    Author

    Ah ok in this case maybe not worth it to introduce a new error/warning if Fp16 will work on CPU soon?

  6. vadimkantorov commented on Mar 9, 2023

    @vadimkantorov
    Contributor

    In general, these error messages could be improved to use standardized dtype names: torch.float16 instead of Half. Same for thing called dispatch code, for sparse tensors the name is quite cryptic SparseCpu when it means torch.sparse_coo_tensor :(

  7. added
    triaged
    on Mar 9, 2023
  8. jgong5 commented on Mar 10, 2023

    @jgong5
    Collaborator

    Ah ok in this case maybe not worth it to introduce a new error/warning if Fp16 will work on CPU soon?

    Agreed. We will prioritize the ops to cover according to the popular model families. Models from HuggingFace will definitively be at the top. Thanks for the patience.

  9. moved this from TODO to In Progress in PyTorch Intelon Mar 10, 2023
  10. lileidev commented on Aug 22, 2024

    @lileidev

    file location: aten/src/ATen/native/LinearAlgebra.cpp
    Why addmm_impl_cpu_ can support bfloat16 but dont't support half?

    // Apply BLAS routine
    AT_DISPATCH_ALL_TYPES_AND_COMPLEX_AND(kBFloat16,
    result.scalar_type(), "addmm_impl_cpu_",
    [&]{
    using opmath_t = at::opmath_type<scalar_t>;
    at::native::cpublas::gemm(
    transpose_a ? a.is_conj() ? TransposeType::ConjTranspose : TransposeType::Transpose : TransposeType::NoTranspose,
    transpose_b ? b.is_conj() ? TransposeType::ConjTranspose : TransposeType::Transpose : TransposeType::NoTranspose,
    m, n, k,
    alpha.to<opmath_t>(),
    a.data_ptr<scalar_t>(), lda,
    b.data_ptr<scalar_t>(), ldb,
    beta.to<opmath_t>(),
    c.data_ptr<scalar_t>(), ldc);
    });

  11. ramaseshanms commented on Dec 23, 2025

    @ramaseshanms

    If no one is currently working on this, I’m happy to pick it up and keep the discussion updated.

  12. JonSnow1807 commented on Jan 14, 2026

    @JonSnow1807
    Contributor

    I'm picking this up and will submit a PR shortly.

    After investigating the codebase:

    • The original addmm_impl_cpu_ error is now fixed - the _AT_DISPATCH_ADDMM_TYPES macro in LinearAlgebra.cpp now includes kHalf for non-mobile builds.

    • However, as @vadimkantorov noted, error messages still need improvement - they use internal names like 'Half' instead of user-friendly 'torch.float16'.

    I'll submit a PR to improve the dtype formatting in error messages.

  13. added a commit that references this issue on Jan 14, 2026
    95886c2
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

    module: error checkingtriaged

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions