- 
                Notifications
    You must be signed in to change notification settings 
- Fork 112
shorten mlir dump filename #4197
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
Changes from 6 commits
04762ad
              e78cea4
              f3a0029
              b59fa13
              c431830
              5a02ea9
              4b4e538
              b16cbee
              a486ac6
              File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
|  | @@ -22,6 +22,7 @@ | |
| * THE SOFTWARE. | ||
| */ | ||
| #include <algorithm> | ||
| #include <atomic> | ||
| #include <cstdint> | ||
| #include <migraphx/shape.hpp> | ||
| #include <migraphx/algorithm.hpp> | ||
|  | @@ -163,6 +164,13 @@ using mlir_tuning_space = MIGRAPHX_MANAGE_MLIR_HANDLE(MlirRockTuningSpace, | |
| using mlir_tuning_param = MIGRAPHX_MANAGE_MLIR_HANDLE(MlirRockTuningParam, | ||
| mlirRockTuningParamDestroy); | ||
|  | ||
| static std::atomic<int>& dump_counter() | ||
| { | ||
| // NOLINTNEXTLINE | ||
| static std::atomic<int> c = 0; | ||
| return c; | ||
| } | ||
|  | ||
| static std::string_view to_string_view(MlirStringRef s) { return {s.data, s.length}; } | ||
|  | ||
| static MlirStringRef make_mlir_string_ref(const std::string_view& s) | ||
|  | @@ -1094,6 +1102,20 @@ std::string dump_mlir(module m, const std::vector<shape>& inputs) | |
| return mlir_print(&mlirOperationPrint, mod_op); | ||
| } | ||
|  | ||
| static void abbreviate_symbol_names(std::string& n) | ||
| { | ||
| static const std::map<std::string, std::string> abbrs = { | ||
| {"reduce_max_reshape_sub_exp_reshape_reduce_sum_reshape_div", "softmax"}, | ||
| {"reshape", "rsp"}, | ||
| {"transpose", "trp"}, | ||
| {"slice", "slc"}}; | ||
|  | ||
| for(auto const& [key, val] : abbrs) | ||
| { | ||
| replace_string_inplace(n, key, val); | ||
| } | ||
| } | ||
|  | ||
| static std::string compute_dump_name(const module& m, const std::string& ext) | ||
| { | ||
| std::vector<instruction_ref> sizes; | ||
|  | @@ -1102,11 +1124,30 @@ static std::string compute_dump_name(const module& m, const std::string& ext) | |
| if(contains({"quant_convolution", "quant_dot", "convolution", "dot"}, ins->name())) | ||
| sizes.insert(sizes.end(), ins->inputs().begin(), ins->inputs().end()); | ||
| } | ||
| auto name = | ||
| mlir_program::get_symbol_name(m) + "_" + shape::to_sizes_string(to_shapes(sizes)) + ext; | ||
| replace_string_inplace(name, ", ", "_"); | ||
| replace_string_inplace(name, ":", "s"); | ||
| return name; | ||
| auto shape_str = "_" + shape::to_sizes_string(to_shapes(sizes)); | ||
| auto sym_names = mlir_program::get_symbol_name(m); | ||
| abbreviate_symbol_names(sym_names); | ||
|  | ||
| const int max_file_length = 255; | ||
|         
                  shivadbhavsar marked this conversation as resolved.
              Show resolved
            Hide resolved | ||
| std::string fname; | ||
| if(sym_names.length() + shape_str.length() + ext.length() > max_file_length) | ||
| { | ||
| auto cnt = "_" + std::to_string(dump_counter()++); | ||
| auto cutoff = max_file_length - shape_str.length() - ext.length() - cnt.length(); | ||
|          | ||
|  | ||
| // If the shapes are too big, the filename will be unparsable anyways so simply name it | ||
| // mlir_x to avoid erroring out | ||
| fname = | ||
| cutoff > 0 ? sym_names.substr(0, cutoff) + shape_str + cnt + ext : "mlir" + cnt + ext; | ||
| } | ||
|          | ||
| else | ||
| { | ||
| fname = sym_names + shape_str + ext; | ||
| } | ||
|  | ||
| replace_string_inplace(fname, ", ", "_"); | ||
|          | ||
| replace_string_inplace(fname, ":", "s"); | ||
| return fname; | ||
| } | ||
|  | ||
| void dump_mlir_to_file(module m, const std::vector<shape>& inputs, const fs::path& location) | ||
|  | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is a bug lurking here, should the loop iterate over the string
reshapebefore the longer string (for softmax) which also has the same substring inside it. It is just that the std::map would be in an order which works here. But change it to a another data structure, e.g. a hash, and the renaming scheme could be a surprise for us :-)