@@ -557,9 +557,21 @@ def hamming(x):
557557
558558
559559def heaviside (x1 , x2 ):
560- raise NotImplementedError (
561- "`heaviside` is not supported with openvino backend"
562- )
560+ x1 = get_ov_output (x1 )
561+ x_type = x1 .get_element_type ()
562+ x2 = get_ov_output (x2 , x_type )
563+
564+ zero_scalar = ov_opset .constant (0 , x_type ).output (0 )
565+ one_scalar = ov_opset .constant (1 , x_type ).output (0 )
566+
567+ neg = ov_opset .less (x1 , zero_scalar ).output (0 )
568+ pos = ov_opset .greater (x1 , zero_scalar ).output (0 )
569+ eq = ov_opset .equal (x1 , zero_scalar ).output (0 )
570+
571+ x = ov_opset .select (neg , zero_scalar , x1 ).output (0 )
572+ x = ov_opset .select (pos , one_scalar , x ).output (0 )
573+ x = ov_opset .select (eq , x2 , x ).output (0 )
574+ return OpenVINOKerasTensor (x )
563575
564576
565577def kaiser (x , beta ):
@@ -699,15 +711,9 @@ def count_nonzero(x, axis=None):
699711 zero_constant = ov_opset .convert_like (zero_constant , x )
700712 x = ov_opset .not_equal (x , zero_constant ).output (0 )
701713 x = ov_opset .convert (x , Type .i32 ).output (0 )
702- if axis is None :
703- flatten_shape = ov_opset .constant ([- 1 ], Type .i32 ).output (0 )
704- x = ov_opset .reshape (x , flatten_shape , False ).output (0 )
705- axis = 0
706- if isinstance (axis , tuple ):
707- axis = list (axis )
708- if axis == []:
714+ x , axis = _resolve_axis (x , axis )
715+ if not axis :
709716 return OpenVINOKerasTensor (x )
710- axis = ov_opset .constant (axis , Type .i32 ).output (0 )
711717 return OpenVINOKerasTensor (ov_opset .reduce_sum (x , axis , False ).output (0 ))
712718
713719
@@ -726,11 +732,7 @@ def cumsum(x, axis=None, dtype=None):
726732 if dtype is not None :
727733 ov_type = OPENVINO_DTYPES [standardize_dtype (dtype )]
728734 x = ov_opset .convert (x , ov_type ).output (0 )
729- if axis is None :
730- flatten_shape = ov_opset .constant ([- 1 ], Type .i32 ).output (0 )
731- x = ov_opset .reshape (x , flatten_shape , False ).output (0 )
732- axis = 0
733- axis = ov_opset .constant (axis , Type .i32 ).output (0 )
735+ x , axis = _resolve_axis (x , axis )
734736 if x .get_element_type () == Type .boolean :
735737 x = ov_opset .convert (x , Type .i32 ).output (0 )
736738 return OpenVINOKerasTensor (ov_opset .cumsum (x , axis ).output (0 ))
@@ -1765,7 +1767,9 @@ def pad(x, pad_width, mode="constant", constant_values=None):
17651767 "`pad` operation supports only scalar pad value "
17661768 "in constant mode by openvino backend"
17671769 )
1768- pad_value = constant_values
1770+ pad_value = ov_opset .constant (
1771+ constant_values , x .get_element_type ()
1772+ ).output (0 )
17691773
17701774 # split pad_width into two tensors pads_begin and pads_end
17711775 pads_begin = []
@@ -2061,22 +2065,9 @@ def stack(x, axis=0):
20612065
20622066
20632067def std (x , axis = None , keepdims = False ):
2064- x = get_ov_output (x )
2065- if axis is None :
2066- flatten_shape = ov_opset .constant ([- 1 ], Type .i32 ).output (0 )
2067- x = ov_opset .reshape (x , flatten_shape , False ).output (0 )
2068- axis = 0
2069- axis = ov_opset .constant (axis , Type .i32 ).output (0 )
2070- # The variance is computed using $Var = E[|x|^2] - |E[x]|^2$, It is faster
2071- # but less numerically stable.
2072- mean = ov_opset .reduce_mean (x , axis , keepdims ).output (0 )
2073- const_two = ov_opset .constant (2 , x .get_element_type ()).output (0 )
2074- squared_x = ov_opset .power (x , const_two ).output (0 )
2075- squared_mean = ov_opset .power (mean , const_two ).output (0 )
2076- squared_x_mean = ov_opset .reduce_mean (squared_x , axis , keepdims )
2077- variance = ov_opset .subtract (squared_x_mean , squared_mean ).output (0 )
2078- std_var = OpenVINOKerasTensor (ov_opset .sqrt (variance ).output (0 ))
2079- return std_var
2068+ var_x = var (x , axis , keepdims )
2069+ std_dev = ov_opset .sqrt (var_x ).output (0 )
2070+ return OpenVINOKerasTensor (std_dev )
20802071
20812072
20822073def swapaxes (x , axis1 , axis2 ):
@@ -2441,18 +2432,26 @@ def trapezoid(y, x=None, dx=1.0, axis=-1):
24412432
24422433def var (x , axis = None , keepdims = False ):
24432434 x = get_ov_output (x )
2435+ x_type = x .get_element_type ()
2436+ x , axis = _resolve_axis (x , axis )
2437+
2438+ work_dtype = Type .f64 if x_type .is_integral () else x .get_element_type ()
2439+ if x_type .is_integral ():
2440+ x = ov_opset .convert (x , work_dtype ).output (0 )
24442441 if axis is None :
2445- flatten_shape = ov_opset .constant ([ - 1 ], Type . i32 ).output (0 )
2446- x = ov_opset . reshape ( x , flatten_shape , False ). output ( 0 )
2447- axis = 0
2448- axis = ov_opset . constant ( axis , Type . i32 ). output ( 0 )
2442+ const_zero = ov_opset .constant (0 , dtype = work_dtype ).output (0 )
2443+ return OpenVINOKerasTensor (
2444+ ov_opset . broadcast ( const_zero , ov_opset . shape_of ( x )). output ( 0 )
2445+ )
24492446 # The variance is computed using $Var = E[|x|^2] - |E[x]|^2$, It is faster
24502447 # but less numerically stable.
24512448 mean = ov_opset .reduce_mean (x , axis , keepdims ).output (0 )
2452- const_two = ov_opset .constant (2 , x .get_element_type ()).output (0 )
2449+ const_two = ov_opset .constant (2 , work_dtype ).output (0 )
2450+
24532451 squared_x = ov_opset .power (x , const_two ).output (0 )
24542452 squared_mean = ov_opset .power (mean , const_two ).output (0 )
2455- squared_x_mean = ov_opset .reduce_mean (squared_x , axis , keepdims )
2453+
2454+ squared_x_mean = ov_opset .reduce_mean (squared_x , axis , keepdims ).output (0 )
24562455 variance = OpenVINOKerasTensor (
24572456 ov_opset .subtract (squared_x_mean , squared_mean ).output (0 )
24582457 )
0 commit comments