forked from tile-ai/tilelang
-
Notifications
You must be signed in to change notification settings - Fork 6
[Feature] [Layout] Add layout of global buffer #53
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| /*! | ||
| * \file global_layout_utils.h | ||
| * \brief Utility functions to extract global buffer layouts from tensor_meta | ||
| * attributes for Sunmmio target. | ||
| */ | ||
|
|
||
| #ifndef TVM_TL_TRANSFORM_COMMON_GLOBAL_LAYOUT_UTILS_H_ | ||
| #define TVM_TL_TRANSFORM_COMMON_GLOBAL_LAYOUT_UTILS_H_ | ||
|
|
||
| #include <tvm/target/target.h> | ||
| #include <tvm/tir/function.h> | ||
|
|
||
| #include "../../layout/layout.h" | ||
| #include "../../target/utils.h" | ||
|
|
||
| namespace tvm { | ||
| namespace tl { | ||
|
|
||
| using LayoutMap = Map<tir::Buffer, Layout>; | ||
|
|
||
| /*! | ||
| * \brief Populate layout_map with global buffer layouts from tensor_meta | ||
| * attribute. Only applies when target is Sunmmio. | ||
| * | ||
| * \param f The PrimFunc containing tensor_meta attribute | ||
| * \param target The compilation target | ||
| * \param layout_map The layout map to update (in-place) | ||
| * \return true if any layouts were added, false otherwise | ||
| */ | ||
| bool PopulateGlobalBufferLayouts(const tir::PrimFunc &f, Target target, | ||
| LayoutMap *layout_map); | ||
|
|
||
| /*! | ||
| * \brief Parse a single buffer's hierarchical layout from tensor_meta entry | ||
| * | ||
| * \param meta_entry The metadata dict for one buffer | ||
| * \param buffer The buffer to create layout for | ||
| * \return Layout object, or nullopt if parsing fails | ||
| */ | ||
| Optional<Layout> | ||
| ParseGlobalBufferLayout(const Map<String, ObjectRef> &meta_entry, | ||
| const tir::Buffer &buffer); | ||
|
|
||
| } // namespace tl | ||
| } // namespace tvm | ||
|
|
||
| #endif // TVM_TL_TRANSFORM_COMMON_GLOBAL_LAYOUT_UTILS_H_ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,117 @@ | ||
| /*! | ||
| * \file global_layout_utils.cc | ||
| * \brief Implementation of utility functions to extract global buffer layouts | ||
| * from tensor_meta attributes for Sunmmio target. | ||
| */ | ||
|
|
||
| #include "common/global_layout_utils.h" | ||
|
|
||
| #include <tvm/tir/stmt_functor.h> | ||
|
|
||
| #include "../layout/layout.h" | ||
| #include "../target/utils.h" | ||
|
|
||
| namespace tvm { | ||
| namespace tl { | ||
|
|
||
| using namespace tir; | ||
|
|
||
| Optional<Layout> | ||
| ParseGlobalBufferLayout(const Map<String, ObjectRef> &meta_entry, | ||
| const Buffer &buffer) { | ||
| // Extract sharded layout info | ||
| auto hdims_obj = meta_entry.Get("sharded_hdims"); | ||
| auto hstrides_obj = meta_entry.Get("sharded_hstrides"); | ||
| auto hgroups_obj = meta_entry.Get("sharded_hgroups"); | ||
|
|
||
| if (!hdims_obj || !hstrides_obj || !hgroups_obj) { | ||
| return Optional<Layout>(); | ||
| } | ||
|
|
||
| // Convert to arrays for makeHierarchicalLayout | ||
| Array<Integer> hdims_arr, hstrides_arr, logical_shape_arr; | ||
| Array<Array<Integer>> groups_arr; | ||
|
|
||
| // Parse hdims - it's an Array<Integer> from Python | ||
| Array<Integer> hdims = Downcast<Array<Integer>>(hdims_obj.value()); | ||
| for (const auto &dim : hdims) { | ||
| hdims_arr.push_back(dim); | ||
| } | ||
|
|
||
| // Parse hstrides | ||
| Array<Integer> hstrides = Downcast<Array<Integer>>(hstrides_obj.value()); | ||
| for (const auto &stride : hstrides) { | ||
| hstrides_arr.push_back(stride); | ||
| } | ||
|
|
||
| // Parse hgroups - Array<Array<Integer>> | ||
| Array<Array<Integer>> hgroups = | ||
| Downcast<Array<Array<Integer>>>(hgroups_obj.value()); | ||
| for (const auto &group : hgroups) { | ||
| groups_arr.push_back(group); | ||
| } | ||
|
|
||
| // Use buffer shape as logical shape | ||
| for (size_t i = 0; i < buffer->shape.size(); ++i) { | ||
| if (auto *imm = buffer->shape[i].as<IntImmNode>()) { | ||
| logical_shape_arr.push_back(Integer(imm->value)); | ||
| } else { | ||
| return Optional<Layout>(); // Dynamic shape not supported | ||
| } | ||
| } | ||
|
|
||
| // Verify that groups_arr matches logical shape dimensions | ||
| if (groups_arr.size() != logical_shape_arr.size()) { | ||
| return Optional<Layout>(); | ||
| } | ||
|
|
||
| return makeHierarchicalLayout(hdims_arr, hstrides_arr, groups_arr, | ||
| logical_shape_arr); | ||
| } | ||
|
|
||
| bool PopulateGlobalBufferLayouts(const PrimFunc &f, Target target, | ||
| LayoutMap *layout_map) { | ||
| if (!TargetIsSunmmio(target)) { | ||
| return false; | ||
| } | ||
|
|
||
| auto tensor_meta_opt = f->GetAttr<Map<String, ObjectRef>>("tensor_meta"); | ||
| if (!tensor_meta_opt) { | ||
| return false; | ||
| } | ||
|
|
||
| auto tensor_meta = tensor_meta_opt.value(); | ||
| bool any_added = false; | ||
|
|
||
| for (const auto &kv : f->buffer_map) { | ||
| const Var &var = kv.first; | ||
| const Buffer &buffer = kv.second; | ||
|
|
||
| if (buffer.scope() != "global") { | ||
| continue; | ||
| } | ||
|
|
||
| String buffer_name = buffer->name; | ||
| if (!tensor_meta.count(buffer_name)) { | ||
| continue; | ||
| } | ||
|
|
||
| auto meta_entry_obj = tensor_meta[buffer_name]; | ||
| auto meta_entry = meta_entry_obj.as<Map<String, ObjectRef>>(); | ||
| if (!meta_entry.has_value()) { | ||
| continue; | ||
| } | ||
|
|
||
| auto layout_opt = ParseGlobalBufferLayout(meta_entry.value(), buffer); | ||
|
|
||
| if (layout_opt) { | ||
| layout_map->Set(buffer, layout_opt.value()); | ||
| any_added = true; | ||
| } | ||
| } | ||
|
|
||
| return any_added; | ||
| } | ||
|
|
||
| } // namespace tl | ||
| } // namespace tvm | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.