-
Notifications
You must be signed in to change notification settings - Fork 19.6k
[OpenVINO Backend] : add support for numpy.nan_to_num #21186
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
Changes from all commits
9497736
eb04128
4d72bbf
e8ad543
cff09be
1ed1b37
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,8 @@ | |
from keras.src.backend import config | ||
from keras.src.backend.common import dtypes | ||
from keras.src.backend.common.variables import standardize_dtype | ||
from keras.src.backend.openvino.core import DTYPES_MAX | ||
from keras.src.backend.openvino.core import DTYPES_MIN | ||
from keras.src.backend.openvino.core import OPENVINO_DTYPES | ||
from keras.src.backend.openvino.core import OpenVINOKerasTensor | ||
from keras.src.backend.openvino.core import ( | ||
|
@@ -1030,9 +1032,36 @@ def moveaxis(x, source, destination): | |
|
||
|
||
def nan_to_num(x, nan=0.0, posinf=None, neginf=None): | ||
raise NotImplementedError( | ||
"`nan_to_num` is not supported with openvino backend" | ||
) | ||
x = get_ov_output(x) | ||
dtype = x.get_element_type() | ||
if dtype.is_integral(): | ||
return OpenVINOKerasTensor(x) | ||
isfloat64 = True if dtype == Type.f64 else False | ||
if isfloat64: # conversion to f32 due to https://github.com/openvinotoolkit/openvino/issues/30264 | ||
x = ov_opset.convert(x, Type.f32).output(0) | ||
dtype = Type.f32 | ||
nan_val = ov_opset.constant(nan, dtype).output(0) | ||
posinf_val = ov_opset.constant( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @11happy, let us do not call There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I added a check for int type & early retunr & removed the dtype conversion to float ,
removed this There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @11happy, please clarify what cases exactly are failing. For int types, it should pass because there is no inf and nan values. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. so the test case that is failing is previous one only
I tried to debug it , my observations: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have now a reproducible code on f64 dtype for the same error There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Well, can you please do the implementation as follows:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have also added a comment referencing the issue |
||
posinf if posinf is not None else DTYPES_MAX[dtype], dtype | ||
).output(0) | ||
neginf_val = ov_opset.constant( | ||
neginf if neginf is not None else DTYPES_MIN[dtype], dtype | ||
).output(0) | ||
posinf_mask = ov_opset.is_inf( | ||
x, | ||
{"detect_positive": True, "detect_negative": False}, | ||
).output(0) | ||
neginf_mask = ov_opset.is_inf( | ||
x, | ||
{"detect_positive": False, "detect_negative": True}, | ||
).output(0) | ||
nan_mask = ov_opset.is_nan(x).output(0) | ||
x = ov_opset.select(nan_mask, nan_val, x).output(0) | ||
x = ov_opset.select(posinf_mask, posinf_val, x).output(0) | ||
x = ov_opset.select(neginf_mask, neginf_val, x).output(0) | ||
if isfloat64: | ||
x = ov_opset.convert(x, Type.f64).output(0) | ||
return OpenVINOKerasTensor(x) | ||
|
||
|
||
def ndim(x): | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please also remove
NumpyDtypeTest::test_nan
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done