Skip to content

[OpenVINO backend] support tri, triu, and tril #21408

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
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

Mohamed-Ashraf273
Copy link
Contributor

@Mohamed-Ashraf273 Mohamed-Ashraf273 commented Jun 21, 2025

Hi @rkazants
I've supported tri, triu and tril, and they are ready for review.

@codecov-commenter
Copy link

codecov-commenter commented Jun 21, 2025

Codecov Report

Attention: Patch coverage is 91.83673% with 4 lines in your changes missing coverage. Please review.

Project coverage is 77.20%. Comparing base (be9b002) to head (5de788d).
Report is 25 commits behind head on master.

Files with missing lines Patch % Lines
keras/src/backend/openvino/numpy.py 91.83% 2 Missing and 2 partials ⚠️

❗ There is a different number of reports uploaded between BASE (be9b002) and HEAD (5de788d). Click for more details.

HEAD has 2 uploads less than BASE
Flag BASE (be9b002) HEAD (5de788d)
keras 5 4
keras-torch 1 0
Additional details and impacted files
@@            Coverage Diff             @@
##           master   #21408      +/-   ##
==========================================
- Coverage   82.72%   77.20%   -5.52%     
==========================================
  Files         565      565              
  Lines       55219    55562     +343     
  Branches     8608     8671      +63     
==========================================
- Hits        45682    42899    -2783     
- Misses       7427    10601    +3174     
+ Partials     2110     2062      -48     
Flag Coverage Δ
keras 77.12% <91.83%> (-5.42%) ⬇️
keras-jax 63.33% <0.00%> (-0.01%) ⬇️
keras-numpy 58.54% <0.00%> (+<0.01%) ⬆️
keras-openvino 33.79% <91.83%> (-0.01%) ⬇️
keras-tensorflow 63.78% <0.00%> (+0.03%) ⬆️
keras-torch ?

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@Mohamed-Ashraf273 Mohamed-Ashraf273 force-pushed the support_triu branch 2 times, most recently from f178511 to 3bbbf99 Compare June 21, 2025 20:44
@@ -126,7 +126,6 @@ NumpyOneInputOpsCorrectnessTest::test_trace
NumpyOneInputOpsCorrectnessTest::test_transpose
NumpyOneInputOpsCorrectnessTest::test_tril
NumpyOneInputOpsCorrectnessTest::test_tril_in_layer
NumpyOneInputOpsCorrectnessTest::test_triu
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NumpyDtypeTest::test_tri needs to be removed as well

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, all are supported and passed tests.

@Mohamed-Ashraf273
Copy link
Contributor Author

@rkazants
they are ready.

@Mohamed-Ashraf273 Mohamed-Ashraf273 changed the title [OpenVINO backend] support triu [OpenVINO backend] support tri, triu, and tril Jun 22, 2025
dtype = "float32"

ov_dtype = OPENVINO_DTYPES[dtype]

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please add comments with explanation for each block

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!, comments added.

# Mask for lower triangle (col <= row + k)
k_const = ov_opset.constant(k, Type.i32)
mask = ov_opset.less_equal(col_idx, ov_opset.add(row_idx, k_const))
mask = ov_opset.convert(mask, ov_type)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you use tri for mask computation? This way you can avoid code duplication

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rkazants
Done!

Comment on lines 1532 to 1534
row_idx = ov_opset.broadcast(row_idx, target_shape)
col_idx = ov_opset.broadcast(col_idx, target_shape)
mask = ov_opset.less_equal(col_idx, ov_opset.add(row_idx, k_const))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can create mask without preliminary boradcasting. less_equal supports numpy broadcasting internally.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pls avoid extra operations and extra memory usage.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're absolutely right, apologies for that. That was my old implementation. I've updated it to the more efficient one.

ov_opset.constant(1, Type.i32),
output_type=Type.i32,
)
return ov_opset.gather(shape, indices, axis=0)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it looks to return the same value as shape = ov_opset.shape_of(x, Type.i32). No need in get_shape_dims

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants