Skip to content

Bug in keras.ops.quantile when running with tensorflow graph execution (tf.function) #21781

@SamKnightGit

Description

@SamKnightGit

OS: Ubuntu 24.04.2 LTS
Python Version: 3.12.12
Keras Version: 3.11.3
TF Version: 2.20.0

Description

When calling keras.ops.quantile with arguments that results in output dimension > 1, the call results in a ValueError instead of completing successfully.

Reproduction

import os
os.environ["KERAS_BACKEND"] = "tensorflow"

import numpy as np
import tensorflow as tf
from keras import ops

@tf.function
def run_quantile():
    x = np.array([[1, 2, 3], [4, 5, 6]])
    q = [0.5]
    return ops.quantile(x, q, axis=1)

result = run_quantile()
expected_output = np.array([[2, 5]])
assert(np.allclose(expected_output, result))

Expected Result

Assert passes, program terminates without exceptions.

Actual Result

Exception is raised in the call to ops.quantile:
ValueError: Tried to convert 'perm' to a tensor and failed. Error: Expected values deque([1, 0]) to be a dense tensor with shape [], but got shape [2].

Root Cause

When in graph mode, python arguments are automatically converted to Tensorflow tensors. The final transpose call in the internal _quantile function in keras.ops passes a deque to the perm argument. This fails to be converted to a tensor causing the crash.

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions