Skip to content

Make is_contiguous check common #3083

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

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft

Make is_contiguous check common #3083

wants to merge 3 commits into from

Conversation

laggui
Copy link
Member

@laggui laggui commented Apr 25, 2025

Checklist

  • Confirmed that run-checks all script has been executed.

Related Issues/PRs

Following this comment in #3077

Changes

Migrated is_contiguous to common (including minor fix).

Testing

Also added a unit test for shape [3, 1] and strides [1, 1] which would previously be marked as not contiguous.

Comment on lines +52 to +54
if prev_stride > stride {
return false;
}
Copy link
Member Author

Choose a reason for hiding this comment

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

Previously checked for prev_stride >= stride but equal adjacent strides are still valid.

See added test case for tensor shape [3, 1] with strides [1, 1], which would previously fail.

/// such that the strides are in non-increasing order, and the stride at position
/// `k` is equal to the product of the shapes of all dimensions greater than `k`.
/// Axes with a shape of 1 are ignored.
pub fn is_contiguous(shape: &[usize], strides: &[usize]) -> bool {
Copy link
Member Author

Choose a reason for hiding this comment

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

I also think the check could be simplified to

pub fn is_contiguous(shape: &[usize], strides: &[usize]) -> bool {
    if shape.is_empty() {
        return true;
    }

    let mut expected_stride = 1;

    for (&shape, &stride) in shape.iter().zip(strides).rev() {
        if shape == 1 {
            continue;
        }

        if stride != expected_stride {
            return false;
        }

        expected_stride *= shape;
    }

    true
}

But leaving this here as a note and not a required change.

Copy link

codecov bot commented Apr 25, 2025

Codecov Report

Attention: Patch coverage is 94.28571% with 2 lines in your changes missing coverage. Please review.

Project coverage is 81.14%. Comparing base (e096b0c) to head (f04fa77).
Report is 3 commits behind head on main.

Files with missing lines Patch % Lines
crates/burn-common/src/lib.rs 92.59% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3083      +/-   ##
==========================================
- Coverage   81.16%   81.14%   -0.02%     
==========================================
  Files         815      817       +2     
  Lines      117201   117314     +113     
==========================================
+ Hits        95121    95196      +75     
- Misses      22080    22118      +38     

☔ 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.

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

Successfully merging this pull request may close these issues.

1 participant