Skip to content

Implement C code for ExtractDiagonal and ARange #1392

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 1 commit into
base: main
Choose a base branch
from

Conversation

ricardoV94
Copy link
Member

@ricardoV94 ricardoV94 commented May 7, 2025

Closes #1360.

It also allows ExtractDiagonal to be a view (it's on by default). Under the hood numpy has returned non-writeable views since 1.9.0, and we have a lower pin of 1.17.0. It's as simple as swapping the flag [citation needed].


📚 Documentation preview 📚: https://pytensor--1392.org.readthedocs.build/en/1392/

Set view flag of ExtractDiagonal to True and respect by default
Copy link

codecov bot commented May 7, 2025

Codecov Report

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

Project coverage is 82.04%. Comparing base (afe934b) to head (68bd67a).

Files with missing lines Patch % Lines
pytensor/tensor/basic.py 89.18% 3 Missing and 1 partial ⚠️

❌ Your patch check has failed because the patch coverage (89.18%) is below the target coverage (100.00%). You can increase the patch coverage or adjust the target coverage.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #1392      +/-   ##
==========================================
+ Coverage   82.02%   82.04%   +0.01%     
==========================================
  Files         207      207              
  Lines       49294    49305      +11     
  Branches     8746     8745       -1     
==========================================
+ Hits        40433    40450      +17     
+ Misses       6695     6689       -6     
  Partials     2166     2166              
Files with missing lines Coverage Δ
pytensor/tensor/basic.py 91.67% <89.18%> (+0.48%) ⬆️

... and 1 file with indirect coverage changes

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

Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR implements C code for two tensor operations: ARange and ExtractDiag, addressing numerical consistency and performance. Key changes include subclassing both operations from COp (instead of Op) for C-level performance improvements, updating ARange’s perform and c_code methods, and modifying ExtractDiag to enable non‐writeable views by default with safe fallbacks.

"""Create an array containing evenly spaced values within a given interval.

Parameters and behaviour are the same as numpy.arange().

"""

# TODO: Arange should work with scalars as inputs, not arrays
Copy link
Preview

Copilot AI May 7, 2025

Choose a reason for hiding this comment

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

[nitpick] Consider addressing the TODO by supporting scalar inputs for ARange, which would simplify the interface and align behavior with numpy.arange.

Copilot uses AI. Check for mistakes.

try:
out.flags.writeable = True
except ValueError:
# We can't make this array writable
Copy link
Preview

Copilot AI May 7, 2025

Choose a reason for hiding this comment

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

[nitpick] Consider logging a warning or providing additional context here when the array cannot be made writeable and a copy is made, to aid in diagnosing potential performance implications.

Suggested change
# We can't make this array writable
# We can't make this array writable
warnings.warn(
"Unable to make the array writeable. A copy of the array is being created instead. "
"This may have performance implications if the array is large.",
RuntimeWarning,
)

Copilot uses AI. Check for mistakes.

@ricardoV94 ricardoV94 added the enhancement New feature or request label May 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-backend enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Use numpy C-API arange implementation
1 participant