Skip to content

[RELAX][LAYOUT] Support multiple axis paching#18869

Open
srkreddy1238 wants to merge 1 commit intoapache:mainfrom
srkreddy1238:layout-packed-rebase
Open

[RELAX][LAYOUT] Support multiple axis paching#18869
srkreddy1238 wants to merge 1 commit intoapache:mainfrom
srkreddy1238:layout-packed-rebase

Conversation

@srkreddy1238
Copy link
Contributor

Like OIHW[4o4i] where we can pack multiple axis.
Helpful while handling complex target layouts.
This PR covers layout representation and transforms for these.

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request significantly enhances the data layout system by introducing support for multi-axis packing, allowing for more flexible and expressive tensor layouts. This capability is crucial for effectively mapping and optimizing computations on diverse hardware architectures. The changes involve fundamental modifications to the C++ Layout class, its parsing and transformation logic, and corresponding updates to the Python API and test suite, ensuring robust handling of these new packed dimensions.

Highlights

  • Multi-Axis Packing Support: Introduced the ability to pack multiple axes into a single logical dimension within data layouts, such as OIHW[4o4i], to support more complex target layouts.
  • Layout Class Enhancements: Added PackIterVar and UnpackIterVar static methods to the Layout class, and updated existing methods like ndim_primal, IndexOf, Contains, and PackedAxisAt to correctly process these new packed axes.
  • Layout String Parsing and Validation: Modified the Layout constructor to parse layout strings containing packed axes (e.g., [4o4i]) and added comprehensive validation checks for their structure.
  • Transformation Rule Refactoring: Refactored the GetStoreRule function to accurately handle index and shape transformations when dealing with layouts that include packed axes, ensuring correct data mapping.
  • Python API Updates and Testing: Updated Python bindings for Layout to reflect the new packed axis behavior and added extensive test cases to validate the functionality, including error handling for invalid packed layout strings.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Changelog
  • include/tvm/s_tir/data_layout.h
    • Added tvm/tir/var.h include for IterVar definitions.
    • Introduced static IterVar PackIterVar(ffi::Array<IterVar> iters) to combine multiple IterVars into a single packed IterVar.
    • Introduced static ffi::Array<IterVar> UnpackIterVar(IterVar packed_iter) to decompose a packed IterVar into its constituent IterVars.
    • Modified ndim_primal() to iterate through unpacked IterVars when counting primal axes.
    • Overloaded IndexOf to accept std::string and tir::IterVar for more flexible axis lookup, and updated the LayoutAxis version to use the string-based one.
    • Updated Contains() to check for axis presence within the unpacked IterVars of each packed axis.
    • Added PackedAxisAt(int32_t i) to retrieve the packed IterVar at a specific index.
  • python/tvm/s_tir/data_layout.py
    • Updated __contains__ method to perform a weaker check for packed axes, assuming layout validity.
    • Modified index_of docstring to indicate support for packed axis names.
  • src/s_tir/data_layout.cc
    • Added necessary includes for tvm/ir/expr.h, tvm/runtime/data_type.h, tvm/runtime/logging.h, tvm/runtime/object.h, tvm/tir/analysis.h, tvm/tir/expr.h, tvm/tir/var.h, and algorithm.
    • Refactored Layout constructor to parse layout strings, including support for [ ] syntax to define packed axes, and added validation for nested packing and variable-sized axes within packed dimensions.
    • Implemented the logic for Layout::UnpackIterVar to parse the name hint of a packed IterVar and reconstruct its constituent IterVars.
    • Implemented the logic for Layout::PackIterVar to concatenate names and multiply extents of multiple IterVars into a single packed IterVar.
    • Modified Layout::FactorOf to correctly calculate the factor of a subordinate axis by iterating through unpacked IterVars.
    • Rewrote GetStoreRule to handle index and shape transformations for layouts with packed axes, introducing norm_indexes and index_divs for complex offset calculations.
    • Updated TransformShape to check if an axis is primal by first unpacking it and then checking its constituent parts.
    • Adjusted FFI bindings for s_tir.LayoutIndexOf and s_tir.LayoutGetItem to align with the new packed axis representation and string-based lookup.
  • tests/python/s_tir/base/test_tir_data_layout.py
    • Updated imports to use tvm.testing and InternalError.
    • Modified test_layout to include assertions for index_of and __contains__ with packed axis names like '16c'.
    • Added new test cases for OIHW[4o4i] layout, verifying factor_of, index_of, __contains__, and __getitem__ behavior.
    • Introduced pytest.raises(InternalError) tests for various invalid packed layout string formats.
    • Added new bijective_layout tests for conversions between OIHW and OIHW[4o4i], including forward_shape, backward_shape, forward_index, and backward_index checks.
    • Changed the test execution block to use tvm.testing.main().
Activity
  • The pull request introduces significant new functionality for data layout management.
  • It involves changes across C++ headers, implementation files, Python bindings, and test cases.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

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

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 support for packed axes in data layouts, like OIHW[4o4i], which is a significant feature for handling complex tensor layouts. No security vulnerabilities were found. However, there are a few issues that need to be addressed: a potential bug in the Python __contains__ implementation for Layout, a likely bug in the C++ GetStoreRule function due to incorrect vector manipulation, an unused variable in GetStoreRule, and an inconsistency between the documentation and implementation of PackIterVar. Additionally, there's a typo in the PR title: "paching" should be "packing".

Comment on lines +331 to +337
for (size_t j = 0; j < src_unpacked_axes.size(); j++) {
index_divs[j] = value;
const auto* extent = src_unpacked_axes[j]->dom->extent.as<IntImmNode>();
TVM_FFI_ICHECK(extent) << "Expected Integer Extents for Offset Calculation";
index_divs.push_back(value);
value = value * extent->value;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

high

There appears to be a bug in this loop. The vector index_divs is initialized with a specific size at line 330. Inside the loop, both index_divs[j] = value; and index_divs.push_back(value); are called. This will result in a vector of double the intended size with incorrect contents, which will likely cause incorrect behavior in layout transformations. The push_back call should probably be removed.

      for (size_t j = 0; j < src_unpacked_axes.size(); j++) {
        index_divs[j] = value;
        const auto* extent = src_unpacked_axes[j]->dom->extent.as<IntImmNode>();
        TVM_FFI_ICHECK(extent) << "Expected Integer Extents for Offset Calculation";
        value = value * extent->value;
      }

Comment on lines 43 to +45
def __contains__(self, axis):
return len(axis) == 1 and axis[0].isalpha() and axis[0] in self.name
# Note: We do a weaker check for packed axis assuming layout is valid
return not any(bkt in axis for bkt in "[]") and axis in self.name
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

The current implementation of __contains__ using a substring check (axis in self.name) can lead to incorrect results and is inconsistent with index_of. For example, for a layout NCHW16c, __contains__("16") would return True, which is incorrect as "16" is not an axis. Similarly, for OIHW[4o4i], "i" in layout is True while layout.index_of("i") is -1.

A more robust implementation should check against the actual axes of the layout. The current substring check is too broad and gives false positives.

for (size_t j = 0; j < src_unpacked_axes.size(); j++) {
const int extent = src_unpacked_axes[j]->dom->extent.as<IntImmNode>()->value;
const LayoutAxis& store_axis_impl = LayoutAxis::Get(src_unpacked_axes[j]);
const LayoutAxis& sub_axis = store_axis_impl.ToSubordinate(); /* Not Needed */
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

The comment /* Not Needed */ suggests this variable sub_axis is unused. If it's indeed not needed, it should be removed to improve code clarity and avoid confusion.

Comment on lines +164 to +168
* \brief Packs the Given Array of IterVars into a Single IterVar. Each IterVar in the Array
* should represent either a single primal axis or one or more subordinate axis
* \param iters Array of iter vars to be packed
* \return A packed iter var
*/
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

The documentation for PackIterVar states that it can pack a "single primal axis or one or more subordinate axis". However, the implementation in src/s_tir/data_layout.cc only supports packing subordinate axes and includes a check Packed Axis can contain only Subordinate Axes. This discrepancy should be resolved. Please update the documentation to reflect the implementation's behavior.

@srkreddy1238 srkreddy1238 changed the title [RELAX][LAYOUT] Support multiple axis paching. [RELAX][LAYOUT] Support multiple axis paching Mar 4, 2026
Like OIHW[4o4i] where we can pack multiple axis.
Helpful while handling complex target layouts.
This PR covers layout representation and transforms for these.
@srkreddy1238 srkreddy1238 force-pushed the layout-packed-rebase branch from 0b54493 to 84e03e0 Compare March 4, 2026 04:57
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