Skip to content

[XLA:tf2xla] Support half, bfloat16, and integer dtypes in RangeOp - #126449

Merged
copybara-service[bot] merged 3 commits into
tensorflow:masterfrom
adi-IL:fix/xla-range-dtypes
Sep 2, 2026
Merged

copybara-service[bot] merged 3 commits into
tensorflow:masterfrom
adi-IL:fix/xla-range-dtypes

Conversation

@adi-IL

@adi-IL adi-IL commented Aug 30, 2026

Copy link
Copy Markdown
Contributor

The TensorFlow Range op accepts bfloat16, half, float, double, int8, int16, int32, int64, uint16, uint32, and uint64.

In tensorflow/compiler/tf2xla/kernels/sequence_ops.cc, RangeOp::Compile only handled DT_INT32, DT_INT64, DT_FLOAT, and DT_DOUBLE. Compiling tf.range with jit_compile=True on float16, bfloat16, or other integer dtypes failed with an InvalidArgumentError.

This adds DT_HALF, DT_BFLOAT16, DT_INT8, DT_INT16, DT_UINT16, DT_UINT32, and DT_UINT64 to RangeOp::Compile, updates dynamic dimension calculation to check DataTypeIsInteger, and adds test coverage in ternary_ops_test.py.

Fixes #113143.

@google-ml-butler google-ml-butler Bot added the size:M CL Change Size: Medium label Aug 30, 2026
@keerthanakadiri
keerthanakadiri requested a review from a team August 31, 2026 06:44
@google-ml-butler google-ml-butler Bot added the awaiting review Pull request awaiting review label Aug 31, 2026
@keerthanakadiri keerthanakadiri added the prtype:bugfix PR to fix a bug label Aug 31, 2026
@github-project-automation github-project-automation Bot moved this to Assigned Reviewer in PR Queue Aug 31, 2026
@dmiltr3
dmiltr3 self-requested a review August 31, 2026 21:53

@dmiltr3 dmiltr3 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The updated testRange test in tensorflow/compiler/tests/ternary_ops_test.py fails with an AssertionError when testing int8, int16, uint16, uint32, and uint64:

  File "tensorflow/python/ops/math_ops.py", line 2107, in range
    assert all(arg.dtype in dtype_hierarchy for arg in [start, limit, delta])
AssertionError

Cause

When dtype is not explicitly specified, math_ops.range (tensorflow/python/ops/math_ops.py:2099-2107) uses a fixed dtype_hierarchy to infer the output type:

      dtype_hierarchy = [
          dtypes.int32,
          dtypes.int64,
          dtypes.float16,
          dtypes.bfloat16,
          dtypes.float32,
          dtypes.float64,
      ]
      assert all(arg.dtype in dtype_hierarchy for arg in [start, limit, delta])

Because int8, int16, uint16, uint32, and uint64 are not included in dtype_hierarchy, calling math_ops.range(start, limit, delta) with these dtypes fails the assertion.

Suggested Fix

  1. Update dtype_hierarchy in tensorflow/python/ops/math_ops.py to support all integer types accepted by Range, or
  2. Pass dtype=dtype explicitly when calling math_ops.range in ternary_ops_test.py.

@github-project-automation github-project-automation Bot moved this from Assigned Reviewer to Reviewer Requested Changes in PR Queue Aug 31, 2026
@adi-IL

adi-IL commented Sep 1, 2026

Copy link
Copy Markdown
Contributor Author

Updated dtype_hierarchy in tensorflow/python/ops/math_ops.py to include dtypes.int8, dtypes.int16, dtypes.uint16, dtypes.uint32, and dtypes.uint64. math_ops.range now resolves the types without requiring an explicit dtype parameter.

@google-ml-butler google-ml-butler Bot added kokoro:force-run Tests on submitted change ready to pull PR ready for merge process labels Sep 1, 2026
@github-project-automation github-project-automation Bot moved this from Reviewer Requested Changes to Approved by Reviewer in PR Queue Sep 1, 2026
@nithyak0204 nithyak0204 removed awaiting review Pull request awaiting review kokoro:force-run Tests on submitted change labels Sep 1, 2026
@copybara-service
copybara-service Bot merged commit b95e0be into tensorflow:master Sep 2, 2026
16 checks passed
@github-project-automation github-project-automation Bot moved this from Approved by Reviewer to Merged in PR Queue Sep 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

prtype:bugfix PR to fix a bug ready to pull PR ready for merge process size:M CL Change Size: Medium

Projects

Status: Merged

Development

Successfully merging this pull request may close these issues.

[XLA] Unsupported dtype float16 for tf.range when jit_compile=True

5 participants