Skip to content

Conversation

@zasdfgbnm
Copy link
Collaborator

@zasdfgbnm zasdfgbnm commented Jan 6, 2026

This PR just adds a utility and its test. This utility will be used in #5772, please see #5772 for the context.

@zasdfgbnm
Copy link
Collaborator Author

!test

@github-actions
Copy link

github-actions bot commented Jan 6, 2026

Review updated until commit b7778bd

Description

  • Add new utility function resetContiguityFromTensor in ir_utils namespace

  • Function infers and resets TensorView contiguity based on actual at::Tensor sizes/strides

  • Handles complex dimension types: iteration, broadcast, and reduction domains

  • Includes comprehensive test coverage with 8 test cases for different contiguity patterns

Changes walkthrough

Relevant files
Enhancement
utils.cpp
Implement resetContiguityFromTensor utility function         

csrc/ir/utils.cpp

  • Added #include header
  • Implemented resetContiguityFromTensor function with contiguity
    inference logic
  • Function processes allocation domain right-to-left, handling
    reduction/broadcast dimensions
  • Computes contiguity by comparing consecutive strides for iteration
    dimensions
  • +63/-0   
    utils.h
    Add function declaration and documentation                             

    csrc/ir/utils.h

  • Added function declaration for resetContiguityFromTensor
  • Included documentation explaining function purpose and behavior
  • Documents handling of broadcast and reduction dimensions
  • +6/-0     
    Tests
    test_utils.cpp
    Add comprehensive test for resetContiguityFromTensor         

    tests/cpp/test_utils.cpp

  • Added TEST_F(UtilsTest, ResetContiguityFromTensor) test case
  • Creates TensorView with mixed dimension types: Iteration, Broadcast,
    Iteration, Reduction, Iteration
  • Tests all 8 contiguity combinations using different stride patterns
  • Validates nullopt contiguity for broadcast/reduction dimensions
  • +109/-0 

    PR Reviewer Guide

    Here are some key observations to aid the review process:

    🧪 PR contains tests
    ⚡ No major issues detected

    @zasdfgbnm
    Copy link
    Collaborator Author

    !test

    @zasdfgbnm
    Copy link
    Collaborator Author

    !test

    1 similar comment
    @zasdfgbnm
    Copy link
    Collaborator Author

    !test

    @zasdfgbnm
    Copy link
    Collaborator Author

    !test

    @greptile-apps
    Copy link
    Contributor

    greptile-apps bot commented Jan 15, 2026

    Greptile Summary

    Added utility function ir_utils::resetContiguityFromTensor that infers and sets TensorView contiguity based on actual tensor strides. The implementation uses a two-pointer algorithm traversing right-to-left through the allocation domain, correctly handling broadcast dimensions (set to nullopt, consume sizes/strides entry) and reduction dimensions (set to nullopt, don't consume entry). Contiguity is determined by comparing consecutive strides: innermost dimension is contiguous if stride==1, others are contiguous if stride equals the product of the next dimension's stride and size.

    Key changes:

    • csrc/ir/utils.cpp: Implemented resetContiguityFromTensor with proper index management and validation
    • csrc/ir/utils.h: Added function declaration with clear documentation
    • tests/cpp/test_utils.cpp: Added comprehensive test covering all 8 contiguity combinations for 3 iteration dimensions mixed with broadcast/reduction dimensions

    Testing coverage: The test validates all 2^3=8 combinations of contiguity patterns for a 5-dimension allocation domain [Iteration, Broadcast, Iteration, Reduction, Iteration] mapped to tensor shape [3, 1, 4, 6], ensuring correct handling of non-leftmost broadcast dimensions and reduction dimensions with no sizes/strides entries.

    Confidence Score: 4/5

    • This PR is safe to merge with minimal risk - adds well-tested utility function with no changes to existing code paths
    • Score reflects solid implementation with comprehensive testing. The algorithm correctly handles all tested cases with proper index management and validation. Minor deduction due to potential edge case with size-0 dimensions (rare in practice but worth noting). The utility function is self-contained and will be used in a future PR, so no immediate production impact.
    • No files require special attention - all changes are additive and well-tested

    Important Files Changed

    Filename Overview
    csrc/ir/utils.cpp Added resetContiguityFromTensor utility with two-pointer algorithm for inferring contiguity from tensor strides; handles broadcast/reduction dimensions correctly
    csrc/ir/utils.h Added function declaration with clear documentation explaining contiguity inference behavior
    tests/cpp/test_utils.cpp Comprehensive test covering all 8 combinations of 3-dimensional contiguity patterns with mixed broadcast/reduction dimensions

    Sequence Diagram

    sequenceDiagram
        participant Client
        participant resetContiguityFromTensor
        participant inferAllocationSizesAndStrides
        participant TensorView
        participant TensorDomain
    
        Client->>resetContiguityFromTensor: Call with TensorView & at::Tensor
        
        alt tensor not defined
            resetContiguityFromTensor->>Client: Early return
        end
        
        resetContiguityFromTensor->>inferAllocationSizesAndStrides: Get sizes & strides
        Note over inferAllocationSizesAndStrides: Returns vectors excluding<br/>reduction dimensions
        inferAllocationSizesAndStrides-->>resetContiguityFromTensor: (sizes, strides)
        
        resetContiguityFromTensor->>TensorView: getMaybeAllocationDomain()
        TensorView-->>resetContiguityFromTensor: alloc domain (all dims)
        
        Note over resetContiguityFromTensor: Initialize contiguity vector<br/>(all nullopt)
        
        loop Right-to-left traversal (alloc_idx)
            alt Reduction dimension
                Note over resetContiguityFromTensor: Keep nullopt<br/>Don't consume sizes/strides
            else Broadcast dimension
                Note over resetContiguityFromTensor: Keep nullopt<br/>Consume sizes/strides entry<br/>(decrement sizes_idx)
            else Regular iteration dimension
                alt Rightmost non-skipped dim
                    Note over resetContiguityFromTensor: Check: stride == 1
                else Other dimensions
                    Note over resetContiguityFromTensor: Check: stride ==<br/>prev_stride * prev_size
                end
                Note over resetContiguityFromTensor: Set contiguity[alloc_idx]<br/>Update prev_non_skipped_sizes_idx
            end
        end
        
        Note over resetContiguityFromTensor: Validate all sizes/strides<br/>consumed (sizes_idx == -1)
        
        resetContiguityFromTensor->>TensorView: setContiguity(contiguity)
        TensorView->>TensorDomain: setContiguity(contiguity)
        
        resetContiguityFromTensor->>Client: Return
    
    Loading

    @zasdfgbnm zasdfgbnm requested review from naoyam and wujingyue January 15, 2026 18:51
    Copy link
    Collaborator

    @naoyam naoyam left a comment

    Choose a reason for hiding this comment

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

    LGTM

    @zasdfgbnm
    Copy link
    Collaborator Author

    !test

    @zasdfgbnm
    Copy link
    Collaborator Author

    !test

    Copy link
    Contributor

    @greptile-apps greptile-apps bot left a comment

    Choose a reason for hiding this comment

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

    3 files reviewed, 1 comment

    Edit Code Review Agent Settings | Greptile

    Comment on lines +1827 to +1833
    // A dimension is contiguous if its stride equals the stride of the
    // next dimension multiplied by that dimension's size
    contiguity[alloc_idx] =
    (strides[sizes_idx] ==
    strides[prev_non_skipped_sizes_idx] *
    sizes[prev_non_skipped_sizes_idx]);
    }
    Copy link
    Contributor

    Choose a reason for hiding this comment

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

    style: Consider handling size-0 dimensions explicitly. When sizes[prev_non_skipped_sizes_idx] == 0, the expected stride is 0 regardless of actual stride (which is undefined for size-0 dims in PyTorch). Current logic may incorrectly mark such dimensions as non-contiguous.

    Suggested change
    // A dimension is contiguous if its stride equals the stride of the
    // next dimension multiplied by that dimension's size
    contiguity[alloc_idx] =
    (strides[sizes_idx] ==
    strides[prev_non_skipped_sizes_idx] *
    sizes[prev_non_skipped_sizes_idx]);
    }
    } else {
    // A dimension is contiguous if its stride equals the stride of the
    // next dimension multiplied by that dimension's size
    // Special case: size-0 dimensions have undefined stride semantics
    if (sizes[prev_non_skipped_sizes_idx] == 0) {
    contiguity[alloc_idx] = std::nullopt;
    } else {
    contiguity[alloc_idx] =
    (strides[sizes_idx] ==
    strides[prev_non_skipped_sizes_idx] *
    sizes[prev_non_skipped_sizes_idx]);
    }

    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.

    3 participants