Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content

Polygraphy rounds CPU float16 top-k index 2049 to 2048 #4976

Backlog

Description

@ibondarenko1

Description

Polygraphy reports an exact top-1 match for predictions with different winning classes, 2048 and 2049. This reproduces with FP16 scores on a CPU-only PyTorch installation.

Steps to reproduce

Run with a CPU-only build of PyTorch.

import torch
from polygraphy import util

for winner in (2048, 2049):
    scores = torch.zeros(4096, dtype=torch.float16, device="cpu")
    scores[winner] = 1
    values, indices = util.array.topk(scores, k=1, axis=0)
    print(scores.argmax().item(), values.item(), indices.item(), indices.dtype)

Observed on the release:

2048 1.0 2048.0 torch.float16
2049 1.0 2048.0 torch.float16

The CPU fallback casts the scores to FP32 for sorting, then casts both returned tensors to FP16. That last cast rounds the integer index 2049 to 2048.

Keeping the original torch.int64 indices returns class 2049 in the second case.

CPU fallback

Environment

Windows 11, Python 3.11.15, PyTorch 2.15.0+cpu. Reproduced with Polygraphy 0.53.6 from PyPI and 0.53.9 at 7fe18f0.

Activity

  1. pk954061458 commented on Nov 30, 2026

    @pk954061458

    I’d like to pick this up. As noted above, the CPU fallback appears to cast the indices back to FP16 along with the values. I’ll check that keeping the indices as integers fixes the 2049 case without changing the values’ dtype.

  2. pk954061458 commented on Dec 1, 2026

    @pk954061458

    I confirmed the fix locally: the CPU fallback casts only the scores back to FP16 and leaves the indices as int64. The regression test now returns index 2049 instead of 2048, and the scores are still FP16. I ran the related CPU tests: 252 passed and 4 skipped. If this approach looks good to you, I’m happy to open a PR.

  3. ibondarenko1 commented on Dec 1, 2026

    @ibondarenko1
    Author

    Thanks for checking this. Getting index 2049 back with the scores still in FP16 is exactly what I expected. Please open the PR.

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:Polygraphy

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions