pytorch - 💡(How to fix) Fix `torch.trace` raises `NotImplementedError` for `float16` input on CPU — no kernel registered for `Half`, no documented exclusion, NumPy handles it fine [1 pull requests]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…

Error Message

import torch import numpy as np

x = torch.tensor([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]], dtype=torch.float16)

print(f"shape={tuple(x.shape)} dtype={x.dtype} device={x.device}")

try: result = torch.trace(x) print(f"result: {result}") except Exception as e: print(f"{type(e).name}: {e}")

Fix Action

Fixed

Code Example

import torch
import numpy as np

x = torch.tensor([[1.0, 2.0, 3.0],
                  [4.0, 5.0, 6.0]], dtype=torch.float16)

print(f"shape={tuple(x.shape)}  dtype={x.dtype}  device={x.device}")

try:
    result = torch.trace(x)
    print(f"result: {result}")
except Exception as e:
    print(f"{type(e).__name__}: {e}")

---

shape=(2, 3)  dtype=torch.float16  device=cpu
NotImplementedError: "trace" not implemented for 'Half'

---

shape=(2, 3)  dtype=torch.float16  device=cpu
result: tensor(6., dtype=torch.float16)

---

x32  = x.to(torch.float32)
x64  = x.to(torch.float64)
xbf  = x.to(torch.bfloat16)
xi32 = x.to(torch.int32)

print(torch.trace(x32).item())   # 6.0print(torch.trace(x64).item())   # 6.0print(torch.trace(xbf).item())   # 6.0print(torch.trace(xi32).item())  # 6torch.trace(x)                   # NotImplementedError
---

print(np.trace(x.numpy()))   # 6.0  (no error)

---

PyTorch version: 2.13.0.dev20260512+cu130
Is debug build: False
CUDA used to build PyTorch: 13.0
ROCM used to build PyTorch: N/A

OS: Ubuntu 24.04.4 LTS (x86_64)
GCC version: (Ubuntu 13.3.0-6ubuntu2~24.04.1) 13.3.0
Clang version: 18.1.3 (1ubuntu1)
CMake version: version 3.28.3
Libc version: glibc-2.39

Python version: 3.12.3 (main, Mar 23 2026, 19:04:32) [GCC 13.3.0] (64-bit runtime)
Python platform: Linux-6.14.0-37-generic-x86_64-with-glibc2.39
Is CUDA available: True
GPU models and configuration: GPU 0: NVIDIA GeForce RTX 5090
Nvidia driver version: 590.48.01

[pip3] numpy==2.4.4
[pip3] torch==2.13.0.dev20260512+cu130
[pip3] triton==3.7.0+git88b227e2
RAW_BUFFERClick to expand / collapse

🐛 Describe the bug

torch.trace raises NotImplementedError: "trace" not implemented for 'Half' when called on a 2D float16 tensor on CPU. The same operation works for float32, float64, bfloat16, int32, and int64. There is no documented exclusion of float16 for torch.trace, and NumPy computes float16 trace without error.

Minimal reproducer

import torch
import numpy as np

x = torch.tensor([[1.0, 2.0, 3.0],
                  [4.0, 5.0, 6.0]], dtype=torch.float16)

print(f"shape={tuple(x.shape)}  dtype={x.dtype}  device={x.device}")

try:
    result = torch.trace(x)
    print(f"result: {result}")
except Exception as e:
    print(f"{type(e).__name__}: {e}")

Observed output

shape=(2, 3)  dtype=torch.float16  device=cpu
NotImplementedError: "trace" not implemented for 'Half'

Expected output

shape=(2, 3)  dtype=torch.float16  device=cpu
result: tensor(6., dtype=torch.float16)

Why this is a bug

float16 is a first-class PyTorch dtype. The torch.trace docs list no dtype restrictions. All other numeric dtypes work on the same CPU backend:

x32  = x.to(torch.float32)
x64  = x.to(torch.float64)
xbf  = x.to(torch.bfloat16)
xi32 = x.to(torch.int32)

print(torch.trace(x32).item())   # 6.0    ✓
print(torch.trace(x64).item())   # 6.0    ✓
print(torch.trace(xbf).item())   # 6.0    ✓
print(torch.trace(xi32).item())  # 6      ✓
torch.trace(x)                   # NotImplementedError  ✗

NumPy confirms this is computable for float16:

print(np.trace(x.numpy()))   # 6.0  (no error)

The float16 (Half) kernel is simply not registered for torch.trace on CPU. There is no fallback (e.g. upcast to float32) and no documentation warning the user. MPS and XPU are likely also affected.

Versions

PyTorch version: 2.13.0.dev20260512+cu130
Is debug build: False
CUDA used to build PyTorch: 13.0
ROCM used to build PyTorch: N/A

OS: Ubuntu 24.04.4 LTS (x86_64)
GCC version: (Ubuntu 13.3.0-6ubuntu2~24.04.1) 13.3.0
Clang version: 18.1.3 (1ubuntu1)
CMake version: version 3.28.3
Libc version: glibc-2.39

Python version: 3.12.3 (main, Mar 23 2026, 19:04:32) [GCC 13.3.0] (64-bit runtime)
Python platform: Linux-6.14.0-37-generic-x86_64-with-glibc2.39
Is CUDA available: True
GPU models and configuration: GPU 0: NVIDIA GeForce RTX 5090
Nvidia driver version: 590.48.01

[pip3] numpy==2.4.4
[pip3] torch==2.13.0.dev20260512+cu130
[pip3] triton==3.7.0+git88b227e2

cc @jgong5 @mingfeima @XiaobingSuper @sanchitintel @ashokei @jingxu10 @jerryzh168 @aditew01 @jianyuh @nikitaved @mruberry @walterddr @xwang233 @Lezcano

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING

pytorch - 💡(How to fix) Fix `torch.trace` raises `NotImplementedError` for `float16` input on CPU — no kernel registered for `Half`, no documented exclusion, NumPy handles it fine [1 pull requests]