-
Notifications
You must be signed in to change notification settings - Fork 19.6k
[OpenVINO Backend] Support: Implemented numpy.digitize for keras ov backend #21087
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
geeky33
wants to merge
2
commits into
keras-team:master
Choose a base branch
from
geeky33:digitize
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -561,10 +561,48 @@ def diag(x, k=0): | |
raise NotImplementedError("`diag` is not supported with openvino backend") | ||
|
||
|
||
def diagonal(x, offset=0, axis1=0, axis2=1): | ||
raise NotImplementedError( | ||
"`diagonal` is not supported with openvino backend" | ||
) | ||
def diagonal(x, offset=0, axis1=-2, axis2=-1): | ||
x = get_ov_output(x) | ||
x_shape = x.get_partial_shape() | ||
rank = x_shape.rank.get_length() | ||
|
||
if rank < 2: | ||
raise ValueError("diagonal requires at least a 2D input.") | ||
|
||
axis1 = axis1 % rank | ||
axis2 = axis2 % rank | ||
|
||
if axis1 == axis2: | ||
raise ValueError("axis1 and axis2 must be different.") | ||
|
||
dim1 = x_shape[axis1] | ||
dim2 = x_shape[axis2] | ||
|
||
if not dim1.is_static or not dim2.is_static: | ||
raise ValueError("diagonal requires static input shapes.") | ||
|
||
D1 = dim1.get_length() | ||
D2 = dim2.get_length() | ||
|
||
if offset >= 0: | ||
L = np.minimum(D1, D2 - offset) if (D2 - offset) > 0 else 0 | ||
indices = [[i, i + offset] for i in range(L)] | ||
else: | ||
L = np.minimum(D1 + offset, D2) if (D1 + offset) > 0 else 0 | ||
indices = [[i - offset, i] for i in range(L)] | ||
|
||
if L <= 0: | ||
keras_dtype = ov_to_keras_type(x.get_element_type()) | ||
np_dtype = np.dtype(keras_dtype) | ||
empty_np = np.empty((0,), dtype=np_dtype) | ||
empty_const = ov_opset.constant(empty_np, x.get_element_type()).output(0) | ||
return OpenVINOKerasTensor(empty_const) | ||
|
||
indices = np.array(indices, dtype=np.int32) | ||
indices_const = ov_opset.constant(indices, dtype=Type.i32).output(0) | ||
diag_vec = ov_opset.gather_nd(x, indices_const) | ||
|
||
return OpenVINOKerasTensor(diag_vec.output(0)) | ||
Comment on lines
+564
to
+605
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. please revert changes unrelated to ticket |
||
|
||
|
||
def diff(a, n=1, axis=-1): | ||
|
@@ -640,10 +678,30 @@ def diff(a, n=1, axis=-1): | |
return OpenVINOKerasTensor(result) | ||
|
||
|
||
def digitize(x, bins): | ||
raise NotImplementedError( | ||
"`digitize` is not supported with openvino backend" | ||
) | ||
def digitize(x, bins, right=False): | ||
x = get_ov_output(x) | ||
bins = get_ov_output(bins) | ||
bins_shape = bins.get_partial_shape() | ||
|
||
if bins_shape.rank != 1: | ||
raise ValueError("digitize requires bins to be a 1D tensor.") | ||
|
||
if not bins_shape[0].is_static: | ||
raise ValueError("digitize requires a static shape for bins.") | ||
|
||
B = bins_shape[0].get_length() | ||
x_shape = x.get_partial_shape() | ||
x_expanded = ov_opset.unsqueeze(x, axes=[-1]) | ||
bins_broadcast = ov_opset.broadcast(bins, ov_opset.shape_of(x_expanded)) | ||
|
||
if not right: | ||
cmp_tensor = ov_opset.less_equal(bins_broadcast, x_expanded) | ||
else: | ||
cmp_tensor = ov_opset.less(bins_broadcast, x_expanded) | ||
|
||
cmp_int = ov_opset.convert(cmp_tensor, destination_type=Type.i32) | ||
indices = ov_opset.reduce_sum(cmp_int, axes=[-1], keep_dims=False) | ||
return OpenVINOKerasTensor(indices.output(0)) | ||
Comment on lines
+682
to
+704
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. check bucketize operation in OV opset and how it is implemented for other backends |
||
|
||
|
||
def dot(x, y): | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
switch on the corresponding tests