Skip to content

Add matrix power operation to linalg API and backend implementations#23227

Draft
buildwithsuhana wants to merge 1 commit into
keras-team:masterfrom
buildwithsuhana:add-matrix-power-op
Draft

Add matrix power operation to linalg API and backend implementations#23227
buildwithsuhana wants to merge 1 commit into
keras-team:masterfrom
buildwithsuhana:add-matrix-power-op

Conversation

@buildwithsuhana

Copy link
Copy Markdown
Collaborator

Description

This PR introduces the matrix_power operation to the keras.ops.linalg module, providing a unified API to raise square matrices to an integer power n across all supported backends.

matrix_power is a fundamental operation in linear algebra with several critical applications in machine learning, physics, and computer science.

The keras.ops.linalg.matrix_power(x, n) operation raises a square matrix (or a batch of square matrices) to the integer power n.

  • If $n > 0$: The matrix is multiplied by itself $n$ times (calculated efficiently via binary exponentiation).
  • If $n = 0$: Returns the identity matrix of the same shape as x.
  • If $n < 0$: Computes the matrix inverse $x^{-1}$ and then raises it to the power $|n|$.

Contributor Agreement

Please review our AI-Assisted Contribution Policy and check all boxes below before submitting your PR for review:

  • I am a human, and not a bot.
  • I will be responsible for responding to review comments in a timely manner.
  • I will work with the maintainers to push this PR forward until submission.

Note: Failing to adhere to this agreement may result in your future PRs no longer being reviewed.

@buildwithsuhana buildwithsuhana marked this pull request as draft July 7, 2026 09:41

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request introduces the matrix_power operation to Keras linear algebra operations (keras.ops.linalg), implementing it across all supported backends (JAX, NumPy, OpenVINO, TensorFlow, and PyTorch) along with corresponding unit tests. The review feedback highlights two important issues: first, the OpenVINO backend implementation passes raw Python integers to opset functions instead of Node or Output objects, which can cause compilation or runtime failures; second, the non-symbolic execution path in _matrix_power lacks validation to ensure n is an integer, which could lead to infinite loops or incorrect behavior in backends like TensorFlow and OpenVINO if a float is provided.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment thread keras/src/backend/openvino/linalg.py
Comment thread keras/src/ops/linalg.py
@codecov-commenter

codecov-commenter commented Jul 7, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 58.62069% with 36 lines in your changes missing coverage. Please review.
✅ Project coverage is 84.01%. Comparing base (2aeecf0) to head (cce3ec7).
⚠️ Report is 1 commits behind head on master.

Files with missing lines Patch % Lines
keras/src/backend/openvino/linalg.py 17.07% 31 Missing and 3 partials ⚠️
keras/src/ops/linalg.py 91.66% 1 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master   #23227      +/-   ##
==========================================
- Coverage   84.80%   84.01%   -0.80%     
==========================================
  Files         465      465              
  Lines       69201    69288      +87     
  Branches    11379    11398      +19     
==========================================
- Hits        58687    58212     -475     
- Misses       7577     8147     +570     
+ Partials     2937     2929       -8     
Flag Coverage Δ
keras 83.83% <58.62%> (-0.78%) ⬇️
keras-cpu 83.83% <58.62%> (-0.04%) ⬇️
keras-gpu ?
keras-jax 57.88% <31.03%> (-0.38%) ⬇️
keras-numpy 53.67% <31.03%> (-0.03%) ⬇️
keras-openvino 59.51% <36.78%> (-0.03%) ⬇️
keras-tensorflow 59.49% <47.12%> (-0.31%) ⬇️
keras-torch 58.62% <31.03%> (-0.44%) ⬇️
keras-tpu ?

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

☔ View full report in Codecov by Harness.
📢 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.

@buildwithsuhana buildwithsuhana force-pushed the add-matrix-power-op branch 5 times, most recently from 26fe0b1 to 295e14a Compare July 7, 2026 15:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants