|
5 | 5 | from keras.src.backend import config
|
6 | 6 | from keras.src.backend.common import dtypes
|
7 | 7 | from keras.src.backend.common.variables import standardize_dtype
|
| 8 | +from keras.src.backend.openvino.core import DTYPES_MAX |
| 9 | +from keras.src.backend.openvino.core import DTYPES_MIN |
8 | 10 | from keras.src.backend.openvino.core import OPENVINO_DTYPES
|
9 | 11 | from keras.src.backend.openvino.core import OpenVINOKerasTensor
|
10 | 12 | from keras.src.backend.openvino.core import (
|
@@ -1079,9 +1081,36 @@ def moveaxis(x, source, destination):
|
1079 | 1081 |
|
1080 | 1082 |
|
1081 | 1083 | def nan_to_num(x, nan=0.0, posinf=None, neginf=None):
|
1082 |
| - raise NotImplementedError( |
1083 |
| - "`nan_to_num` is not supported with openvino backend" |
1084 |
| - ) |
| 1084 | + x = get_ov_output(x) |
| 1085 | + dtype = x.get_element_type() |
| 1086 | + if dtype.is_integral(): |
| 1087 | + return OpenVINOKerasTensor(x) |
| 1088 | + isfloat64 = True if dtype == Type.f64 else False |
| 1089 | + if isfloat64: # conversion to f32 due to https://github.com/openvinotoolkit/openvino/issues/30264 |
| 1090 | + x = ov_opset.convert(x, Type.f32).output(0) |
| 1091 | + dtype = Type.f32 |
| 1092 | + nan_val = ov_opset.constant(nan, dtype).output(0) |
| 1093 | + posinf_val = ov_opset.constant( |
| 1094 | + posinf if posinf is not None else DTYPES_MAX[dtype], dtype |
| 1095 | + ).output(0) |
| 1096 | + neginf_val = ov_opset.constant( |
| 1097 | + neginf if neginf is not None else DTYPES_MIN[dtype], dtype |
| 1098 | + ).output(0) |
| 1099 | + posinf_mask = ov_opset.is_inf( |
| 1100 | + x, |
| 1101 | + {"detect_positive": True, "detect_negative": False}, |
| 1102 | + ).output(0) |
| 1103 | + neginf_mask = ov_opset.is_inf( |
| 1104 | + x, |
| 1105 | + {"detect_positive": False, "detect_negative": True}, |
| 1106 | + ).output(0) |
| 1107 | + nan_mask = ov_opset.is_nan(x).output(0) |
| 1108 | + x = ov_opset.select(nan_mask, nan_val, x).output(0) |
| 1109 | + x = ov_opset.select(posinf_mask, posinf_val, x).output(0) |
| 1110 | + x = ov_opset.select(neginf_mask, neginf_val, x).output(0) |
| 1111 | + if isfloat64: |
| 1112 | + x = ov_opset.convert(x, Type.f64).output(0) |
| 1113 | + return OpenVINOKerasTensor(x) |
1085 | 1114 |
|
1086 | 1115 |
|
1087 | 1116 | def ndim(x):
|
|
0 commit comments