Skip to content

Fix EditDistance crash with dense tensor inputs - #2973

Open
buildwithsuhana wants to merge 2 commits into
keras-team:masterfrom
buildwithsuhana:fix-edit-distance-dense-input
Open

Fix EditDistance crash with dense tensor inputs#2973
buildwithsuhana wants to merge 2 commits into
keras-team:masterfrom
buildwithsuhana:fix-edit-distance-dense-input

Conversation

@buildwithsuhana

@buildwithsuhana buildwithsuhana commented Aug 20, 2026

Copy link
Copy Markdown
Contributor

Description of the change

This PR fixes a bug where EditDistance(normalize=True) would crash when provided with a dense 2D Tensor as input. The crash occurred because the metric attempted to access the flat_values attribute, which is only available on RaggedTensor.

I have replaced the y_true.flat_values access with tf.size(y_true), which works correctly for both dense and ragged tensors in TensorFlow. A new test case has been added to edit_distance_test.py to cover this scenario.

Colab Notebook

https://colab.research.google.com/drive/1qkcPI-JIlDcaHKVTO6yx8u-gbAjUp_Hs?usp=sharing

Checklist

  • I have added all the necessary unit tests for my change.
  • I have verified that my change does not break existing code and works with all backends (TensorFlow, JAX, and PyTorch).
  • My PR is based on the latest changes of the main branch (if unsure, rebase the code).
  • I have followed the Keras Hub Model contribution guidelines in making these changes.
  • I have followed the Keras Hub API design guidelines in making these changes.
  • I have signed the Contributor License Agreement.

@gemini-code-assist gemini-code-assist Bot 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.

Code Review

This pull request modifies the EditDistance metric to support dense tensor inputs and adds a corresponding unit test. However, the reviewer noted that using tf.size(y_true) directly will cause a crash when y_true is a tf.RaggedTensor, which is the primary input format. A code suggestion was provided to robustly handle both dense and ragged tensors.

Comment thread keras_hub/src/metrics/edit_distance.py
@buildwithsuhana
buildwithsuhana marked this pull request as draft August 20, 2026 16:15
@buildwithsuhana
buildwithsuhana marked this pull request as ready for review August 27, 2026 07:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant