Skip to content

Added numpy.outer function using opset14 #21213

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions keras/src/backend/openvino/excluded_concrete_tests.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ NumpyDtypeTest::test_meshgrid
NumpyDtypeTest::test_minimum_python_types
NumpyDtypeTest::test_moveaxis
NumpyDtypeTest::test_multiply
NumpyDtypeTest::test_outer_
NumpyDtypeTest::test_power
NumpyDtypeTest::test_prod
NumpyDtypeTest::test_quantile
Expand Down Expand Up @@ -144,7 +143,6 @@ NumpyTwoInputOpsCorrectnessTest::test_einsum
NumpyTwoInputOpsCorrectnessTest::test_inner
NumpyTwoInputOpsCorrectnessTest::test_linspace
NumpyTwoInputOpsCorrectnessTest::test_logspace
NumpyTwoInputOpsCorrectnessTest::test_outer
NumpyTwoInputOpsCorrectnessTest::test_quantile
NumpyTwoInputOpsCorrectnessTest::test_take_along_axis
NumpyTwoInputOpsCorrectnessTest::test_tensordot
Expand Down
49 changes: 47 additions & 2 deletions keras/src/backend/openvino/numpy.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import numpy as np
import openvino.runtime.opset14 as ov_opset
from openvino import opset14 as ov_opset
from openvino import Type

from keras.src.backend import config
Expand All @@ -14,6 +14,7 @@
)
from keras.src.backend.openvino.core import get_ov_output
from keras.src.backend.openvino.core import ov_to_keras_type
from keras.src.backend.openvino.core import convert_to_numpy as convert_to_numpy


def add(x1, x2):
Expand Down Expand Up @@ -1163,7 +1164,51 @@ def ones_like(x, dtype=None):


def outer(x1, x2):
raise NotImplementedError("`outer` is not supported with openvino backend")
x1_np=convert_to_numpy(x1)
x2_np=convert_to_numpy(x2)

if x1_np.ndim==0 and x2_np.ndim==0:
#print("0d\n")
res=get_ov_output(np.array([[multiply(x1, x2)]]))
return OpenVINOKerasTensor(res)
elif x1_np.ndim==0:
#print("one is 0d\n")
new_x2=x2_np.flatten()
new_x1=np.broadcast_to(x1_np, new_x2.shape)
elif x2_np.ndim==0:
#print("other is 0d\n")
new_x1=x1_np.flatten()
new_x2=np.broadcast_to(x2_np, new_x1)
else:
#print("flattening both")
new_x1=x1_np.flatten()
new_x2=x2_np.flatten()

x=[]
for elem in new_x1:
#print("adding scaled tensor\n")
x.append(multiply(elem, new_x2))
res = get_ov_output(stack(x))
x1=get_ov_output(x1)
x2=get_ov_output(x2)
x1_type = ov_to_keras_type(x1.get_element_type())
x2_type = ov_to_keras_type(x2.get_element_type())

dtype=dtypes.result_type(x1_type, x2_type)
result_type = OPENVINO_DTYPES[dtype]

res=ov_opset.convert(res, result_type).output(0)
return OpenVINOKerasTensor(res)
# res=stack(x)
# return res
#print("concatenated\n")


"""
Flatten tensors if not already of rank <=1
Broadcast x1 to match x2
Concatenate each scalar product
"""


def pad(x, pad_width, mode="constant", constant_values=None):
Expand Down
3 changes: 3 additions & 0 deletions pytest.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[pytest]
env =
KERAS_BACKEND=openvino
Loading