Skip to content

tf.experimental.numpy.trace overflows for int8/uint8 and fails for bool on the default (offset=0) path #129233

Closed

Description

@bodapatisaikrishna

Issue type

Bug

Have you reproduced the bug with TensorFlow Nightly?

Yes

Source

binary

TensorFlow version

tf-nightly 2.23.0-dev20261014 (git v1.12.1-146947-gfcb676979cd)

Custom code

Yes

OS platform and distribution

macOS 15.7.2 (arm64)

Mobile device

No response

Python version

3.11.7

Bazel version

No response

GCC/compiler version

No response

CUDA/cuDNN version

No response

GPU model and memory

No response

Current behavior?

With the default offset=0 and axes covering the last two dimensions, tnp.trace takes a fast path that calls math_ops.trace(a) directly (np_math_ops.py#L1821). That path skips the integer/bool promotion tnp.sum applies, which the non-fast path (diagonal + sum) does use. As a result:

  • small integer dtypes accumulate in their own width and silently overflow: the trace of a 200x200 int8 ones matrix comes out as -56 (int8) where NumPy gives 200 (int64)
  • the same matrix with offset=1 takes the slow path and is correct (199, int64), so the result depends on offset
  • bool input raises InvalidArgumentError (NumPy returns an int count)

Expected: same dtype promotion as np.trace / tnp.sum (int8/int16/bool to int64, uint8/uint16 to uint64), whichever path runs.

Standalone code to reproduce the issue

import numpy as np
import tensorflow.experimental.numpy as tnp

a = np.ones((200, 200), np.int8)
print(tnp.trace(a), np.trace(a))                    # offset=0 fast path: int8 -56 vs 200
print(tnp.trace(a, offset=1), np.trace(a, offset=1))  # slow path promotes: 199 vs 199
print(tnp.trace(np.full((300, 300), 1, np.uint8)), np.trace(np.full((300, 300), 1, np.uint8)))
print(np.trace(np.eye(3, dtype=bool)))
print(tnp.trace(np.eye(3, dtype=bool)))             # raises InvalidArgumentError

Relevant log output

tf.Tensor(-56, shape=(), dtype=int8) 200
tf.Tensor(199, shape=(), dtype=int64) 199
tf.Tensor(44, shape=(), dtype=uint8) 300
3
...
tensorflow.python.framework.errors_impl.InvalidArgumentError: Value for attr 'T' of bool is not in the list of allowed values: float, double, int32, uint8, int16, int8, complex64, int64, ... [Op:Sum]

Activity

  1. added
    comp:ops
    2.21.0
    on Oct 19, 2026
  2. Kayyuri commented on Oct 19, 2026

    @Kayyuri

    Hi @bodapatisaikrishna,
    Thanks for reporting this issue and submitting a fix for TensorFlow. Once the PR is merged, the issue will be resolved. We appreciate your contribution.

  3. added a commit that references this issue on Oct 23, 2026
    adefc9a
  4. google-ml-butler commented on Oct 23, 2026

    @google-ml-butler

    Are you satisfied with the resolution of your issue?
    Yes
    No

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

Metadata

Metadata

Assignees

Labels

2.21.0awaiting PR mergecomp:opstype:bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions