Skip to content

Fix tf.ragged.constant shape inference for uniform outer dimensions#115237

Open
Khalodddd wants to merge 6 commits intotensorflow:masterfrom
Khalodddd:fix-ragged-factory-stability
Open

Fix tf.ragged.constant shape inference for uniform outer dimensions#115237
Khalodddd wants to merge 6 commits intotensorflow:masterfrom
Khalodddd:fix-ragged-factory-stability

Conversation

@Khalodddd
Copy link
Copy Markdown

@Khalodddd Khalodddd commented Apr 4, 2026

Overview

This PR fixes two bugs in tf.ragged.constant:

  1. Empty lists produce incorrect static shape ((3,0) instead of (3, None)) – discovered while fixing bug 2.
  2. Uniform outer dimensions are not reflected in static shape (issue tf.ragged.constant does not detect dense dimensions #40159).

The Problem

  • Bug 1 (empty list): tf.ragged.constant([[], [], []]) returned shape (3,0) – a dense dimension of size 0, which is wrong because the inner dimension should be ragged (None). This bug was found during testing of the fix for bug 2.
  • Bug 2 (uniform outer dimensions): tf.ragged.constant([[[1],[2,3],[4]],[[5,6],[],[7]]]) returned shape (2, None, None) instead of the expected (2, 3, None). Uniform outer dimensions (2 and 3) were lost.

Key Changes

  • File ragged_factory_ops.py:
    • In _default_inner_shape_for_pylist, added if not flat_values: return () to prevent a zero‑length dense inner shape for empty lists (fixes bug 1).
    • In _constant_value, added static shape inference that:
      • Detects whether the last ragged dimension is uniform.
      • Sets concrete lengths for outer ragged dimensions only when the tensor is truly ragged (last dimension non‑uniform).
      • Leaves the last ragged dimension and all inner_shape dimensions as None, preserving existing behavior for dense tensors (fixes bug 2).
  • File ragged_const_op_test.py:
    • Added new test method testUniformOuterDimensionDetection with five cases covering 3D, 4D, empty inner lists, all‑ragged, and explicit ragged_rank/inner_shape.

Verification

Below is a screenshot of the command line after running the test file:

python ragged_const_op_test.py

Screenshot 2026-04-07 162941

Results Summary

  • Bug 1 fixed: tf.ragged.constant([[], [], []])(3, None)
  • Bug 2 fixed: tf.ragged.constant([[[1],[2,3],[4]],[[5,6],[],[7]]])(2, 3, None)
  • All 81 existing tests pass, including dense tensors (e.g., [[1,2,3],[4,5,6],[7,8,9]](3, None) unchanged).
  • New test passes in graph and eager modes.

@google-ml-butler google-ml-butler bot added the size:M CL Change Size: Medium label Apr 4, 2026
@google-cla
Copy link
Copy Markdown

google-cla bot commented Apr 4, 2026

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@google-ml-butler google-ml-butler bot requested a review from cantonios April 4, 2026 23:52
@google-ml-butler google-ml-butler bot added the awaiting review Pull request awaiting review label Apr 4, 2026
@Khalodddd
Copy link
Copy Markdown
Author

@google-cla recheck

@Khalodddd Khalodddd force-pushed the fix-ragged-factory-stability branch from f157a7a to 06a1067 Compare April 5, 2026 09:39
@Khalodddd Khalodddd changed the title Fix eager crashes and shape inference in ragged.constant for empty/nested structures Fix eager crashes and shape inference in ragged.constant for empty/nested structures#40159 Apr 5, 2026
@Khalodddd Khalodddd changed the title Fix eager crashes and shape inference in ragged.constant for empty/nested structures#40159 Fix eager crashes and shape inference in ragged.constant for empty/nested structures #40159 Apr 5, 2026
@keerthanakadiri keerthanakadiri added the comp:ops OPs related issues label Apr 6, 2026
@github-project-automation github-project-automation bot moved this to Assigned Reviewer in PR Queue Apr 6, 2026
@Khalodddd Khalodddd changed the title Fix eager crashes and shape inference in ragged.constant for empty/nested structures #40159 Fix tf.ragged.constant shape inference for uniform outer dimensions #40159 Apr 6, 2026
@Anthonycy
Copy link
Copy Markdown

following up

@Khalodddd
Copy link
Copy Markdown
Author

@google-ml-butler Could someone please re-run the PyLint check? I've pushed a fix for the line-too-long errors. Thank you!

@Khalodddd Khalodddd changed the title Fix tf.ragged.constant shape inference for uniform outer dimensions #40159 Fix tf.ragged.constant shape inference for uniform outer dimensions Apr 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting review Pull request awaiting review comp:ops OPs related issues size:M CL Change Size: Medium

Projects

Status: Assigned Reviewer

Development

Successfully merging this pull request may close these issues.

4 participants