keras icon indicating copy to clipboard operation
keras copied to clipboard

OpenVINOKerasTensor does not implement the full expected API of KerasTensor

Open RyanSaxe opened this issue 4 months ago • 6 comments

A variety of keras ops have paths that return objects of type OpenVINOKerasTensor. For example

import keras.ops as ops
from typing import reveal_type

result = keras.ops.concatenate(...)
reveal_type(result) # OpenVINOKerasTensor

This is because the paths of this op route to backend.numpy.concatenate(...)

And backend.numpy.concatenate has the following implementation:

def concatenate(xs, axis=0):
    assert isinstance(xs, list), "`concatenate` is supported only for `x` list"
    elems = []
    for elem in xs:
        elem = get_ov_output(elem)
        elems.append(elem)
    res = ov_opset.concat(elems, axis).output(0)
    return OpenVINOKerasTensor(res)

However, when inspecting the definition of OpenVINOKerasTensor here, it doesn't implement a variety of the __dunder__ methods expected (we see KerasTensor and frameworks like PyTorch, Jax, Numpy, and TFs objects all implement a much larger superset). Furthermore, it is missing an __array__ implementation, which is surprising of the return of any op coming from backend.numpy.some_operation.

When working with framework code that is supposed to cleanly be interoperable with other frameworks, which is a large selling point of Keras 3.x, the minimum expectation should be to abide by the same interface defined by KerasTensor, including overloads that are expected from non-symbolic tensors like __array__ with errors just like KerasTensor has. The reason for this is that it makes it much easier to write properly typed downstream code with Protocols for things like ArrayLike and TensorLike. Right now, all such code will get typing errors for any ops that have implementations through backend.numpy, which is not ideal.

RyanSaxe avatar Aug 02 '25 08:08 RyanSaxe

@rkazants for open vino support

mattdangerw avatar Aug 20 '25 22:08 mattdangerw

Hi @RyanSaxe,
Thanks for your efforts!
The OpenVINO backend is not fully supported yet, as many ops are still unimplemented. However, we’ve recently added support for several __dunder__ operations, so please try it again using Keras nightly or the main branch from source.

Mohamed-Ashraf273 avatar Aug 21 '25 12:08 Mohamed-Ashraf273

Hi @RyanSaxe, Thanks for your efforts! The OpenVINO backend is not fully supported yet, as many ops are still unimplemented.

I see. And I think my issue above is that go-to definition in my editor brings backend.numpy.concatenate to the OpenVino backend weirdly. My inlay hints also show Pytree[Any] | OpenVinoKerasTensor for the return type of ops.concatenate, even though I don't use this backend (I realize that the way the backend is set makes proper typing very hard).

I'll check out nightly related to the __dunder__ methods. Thanks

RyanSaxe avatar Aug 21 '25 14:08 RyanSaxe

Hi @RyanSaxe --

The Keras team would like to learn more your Keras usage. Please thumbs up this comment if you are open to speaking with us and we'll reach out to you over LinkedIn.

Thanks you so much! Monica, Keras Product Manager

monicadsong avatar Sep 09 '25 23:09 monicadsong

Hi @RyanSaxe,

We have a list of GFIs to contribute for OpenVINO backend: https://github.com/keras-team/keras/issues/20910. We are covering more Keras operations every release. If you have concrete request to support some scenario/model, please let us know to fix the required gap.

Best regards, Roman

rkazants avatar Sep 11 '25 04:09 rkazants

Hi @RyanSaxe -- sent an invite to connect and message over LinkedIn, thanks!

monicadsong avatar Sep 17 '25 18:09 monicadsong