-
Notifications
You must be signed in to change notification settings - Fork 19.6k
[OpenVINO backend] Support numpy.diagonal issue 29115 #21584
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
arjunverma2004
wants to merge
11
commits into
keras-team:master
Choose a base branch
from
arjunverma2004:master
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 1 commit
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
0f77687
[OpenVINO backend] Support numpy.diagonal issue 29115
arjunverma2004 ea0a40f
gmni comit
arjunverma2004 df028d0
fixxx
arjunverma2004 1635157
convertcde
arjunverma2004 96802c5
Update numpy.py
arjunverma2004 f450869
Run api-gen hook and update API directory
arjunverma2004 5ac523e
Run api-gen and commit generated API directory
arjunverma2004 dfc36cd
Merge branch 'keras-team:master' into master
arjunverma2004 e7ea9b2
Merge branch 'master' into my-fix-branch
arjunverma2004 d43c2f1
Merge pull request #1 from arjunverma2004/my-fix-branch
arjunverma2004 35071fd
Update labeler.js
arjunverma2004 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 hidden or 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
This file contains hidden or 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 |
---|---|---|
|
@@ -15,6 +15,78 @@ | |
from keras.src.backend.openvino.core import convert_to_tensor | ||
from keras.src.backend.openvino.core import get_ov_output | ||
from keras.src.backend.openvino.core import ov_to_keras_type | ||
# --- Chnage for issue 29115 --- | ||
import openvino.runtime.opset14 as ov | ||
|
||
from .core import OpenVINOKerasTensor # already present in file | ||
from .core import _convert_to_node, _wrap_node # adapt if your file names differ | ||
|
||
def diagonal(x, offset=0, axis1=0, axis2=1): | ||
"""OpenVINO backend decomposition for keras.ops.diagonal.""" | ||
x_node = _convert_to_node(x) # -> ov.Node | ||
offset_const = ov.constant(int(offset), dtype="i64") | ||
|
||
# rank & normalize axes | ||
shape = ov.shape_of(x_node) # i64 vector | ||
rank = ov.shape_of(shape) # scalar i64 (len of shape) | ||
rank_val = ov.squeeze(rank) # [] -> scalar | ||
axis1_node = ov.mod(ov.add(ov.constant(int(axis1), dtype="i64"), rank_val), rank_val) | ||
axis2_node = ov.mod(ov.add(ov.constant(int(axis2), dtype="i64"), rank_val), rank_val) | ||
|
||
# If axis1 == axis2, behavior should match numpy error; Keras tests don't hit this, | ||
# so we skip explicit assert to keep graph-friendly. | ||
|
||
# Build permutation to move axis1, axis2 to the end | ||
# perm = [all axes except axis1/axis2 in order] + [axis1, axis2] | ||
arange = ov.range(ov.constant(0, dtype="i64"), rank_val, ov.constant(1, dtype="i64")) | ||
mask1 = ov.equal(arange, axis1_node) | ||
mask2 = ov.equal(arange, axis2_node) | ||
not12 = ov.logical_not(ov.logical_or(mask1, mask2)) | ||
others = ov.squeeze(ov.non_zero(not12), [1]) # gather positions != axis1, axis2 | ||
perm = ov.concat([others, ov.reshape(axis1_node, [1]), ov.reshape(axis2_node, [1])], 0) | ||
|
||
x_perm = ov.transpose(x_node, perm) | ||
permuted_shape = ov.shape_of(x_perm) | ||
# last two dims | ||
last2 = ov.gather(permuted_shape, ov.constant([-2, -1], dtype="i64"), ov.constant(0, dtype="i64")) | ||
d1 = ov.gather(permuted_shape, ov.constant([-2], dtype="i64"), ov.constant(0, dtype="i64")) | ||
d2 = ov.gather(permuted_shape, ov.constant([-1], dtype="i64"), ov.constant(0, dtype="i64")) | ||
d1 = ov.squeeze(d1) # scalar | ||
d2 = ov.squeeze(d2) # scalar | ||
|
||
# start1 = max(0, offset), start2 = max(0, -offset) | ||
zero = ov.constant(0, dtype="i64") | ||
start1 = ov.maximum(zero, offset_const) | ||
start2 = ov.maximum(zero, ov.negative(offset_const)) | ||
|
||
# L = min(d1 - start1, d2 - start2) | ||
l1 = ov.subtract(d1, start1) | ||
l2 = ov.subtract(d2, start2) | ||
L = ov.minimum(l1, l2) | ||
|
||
# r = range(0, L, 1) -> shape [L] | ||
r = ov.range(zero, L, ov.constant(1, dtype="i64")) | ||
idx_row = ov.add(r, start1) | ||
idx_col = ov.add(r, start2) | ||
idx_row = ov.unsqueeze(idx_row, ov.constant(1, dtype="i64")) # [L,1] | ||
idx_col = ov.unsqueeze(idx_col, ov.constant(1, dtype="i64")) # [L,1] | ||
diag_idx = ov.concat([idx_row, idx_col], 1) # [L,2] | ||
|
||
# Broadcast indices to batch dims: target shape = (*batch, L, 2) | ||
# batch_rank = rank(x) - 2 | ||
two = ov.constant(2, dtype="i64") | ||
batch_rank = ov.subtract(rank_val, two) | ||
# build target shape: concat(permuted_shape[:batch_rank], [L, 2]) | ||
batch_shape = ov.slice(permuted_shape, ov.constant([0], dtype="i64"), | ||
ov.reshape(batch_rank, [1]), ov.constant([1], dtype="i64")) | ||
target_shape = ov.concat([batch_shape, ov.reshape(L, [1]), ov.constant([2], dtype="i64")], 0) | ||
bcast_idx = ov.broadcast(diag_idx, target_shape) | ||
|
||
# GatherND with batch_dims = batch_rank | ||
gathered = ov.gather_nd(x_perm, bcast_idx, batch_rank) | ||
|
||
return OpenVINOKerasTensor(gathered) | ||
|
||
|
||
|
||
|
||
def add(x1, x2): | ||
|
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.
If possible, try to avoid broadcast operations, as they tend to increase memory usage.