masahi commented on a change in pull request #10156: URL: https://github.com/apache/tvm/pull/10156#discussion_r799775886
########## File path: src/relay/transforms/infer_layout_utils.cc ########## @@ -0,0 +1,260 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +#include "infer_layout_utils.h" + +#include <tvm/relay/expr.h> +#include <tvm/relay/op_attr_types.h> +#include <tvm/tir/data_layout.h> + +#include <map> +#include <string> +#include <tuple> +#include <utility> + +#include "pattern_utils.h" +#include "tvm/runtime/logging.h" + +namespace tvm { +namespace relay { + +Layout AdjustSubordinateFactors(const Layout& src_layout, const Layout& old_layout, + const Array<tvm::PrimExpr>& old_shape) { + // For each subordinate axis + // 1) Find the corresponding dual axis. + // 2) Find the Index of this dual axis in old_layout. + // 3) Find the shape of the that axis in old_shape. + // 4) a) Adjust factor to 1, if that shape is 1. b) Else retain the factor. + DLOG(INFO) << "AdjustSubordinateFactors" + << "src_layout: " << src_layout << " old_layout: " << old_layout + << " old_shape: " << old_shape << std::endl; + std::string new_layout; + for (auto axis : src_layout->axes) { + if (!LayoutAxis::Get(axis).IsPrimal()) { + bool is_shape_one = false; + // 1) Find the corresponding dual axis + const auto& dual_axis = LayoutAxis::Get(axis).ToPrimal(); + + // 2) Find the index of this dual axis in old_layout + int old_axis = old_layout.IndexOf(dual_axis); + + if (old_axis == -1) { + new_layout += "1"; + is_shape_one = true; + } else { + // 3) Find the shape of this index in old_shape + auto shape_val = old_shape[old_axis]; + + // 4) a) Check if this shape element is 1. + if (auto* shape_int = shape_val.as<IntImmNode>()) { + if (shape_int->value == 1) { + new_layout += "1"; + is_shape_one = true; + } + } + } + + // 4) b) If shape is not 1, retain the factor. + if (!is_shape_one) { + auto new_shape_val = src_layout.FactorOf(dual_axis); + new_layout += std::to_string(new_shape_val); + } + } + new_layout += LayoutAxis::Get(axis).name(); + } + return new_layout != "" ? Layout(new_layout) + : Layout("H").SubLayout(0, 0); // hack to create a scalar layout +} +bool Isomorphic(const Layout& lhs, const Layout& rhs) { + DLOG(INFO) << "Isomorphic: " + << "lhs: " << lhs << " rhs: " << rhs << std::endl; + ICHECK(lhs.defined()); + ICHECK(rhs.defined()); + if (lhs->axes.size() != rhs->axes.size()) return false; + std::map<std::string, std::string> map_to, map_back; + for (size_t i = 0; i < lhs->axes.size(); ++i) { + auto& lhs_axis = LayoutAxis::Get(lhs->axes[i]); + auto& rhs_axis = LayoutAxis::Get(rhs->axes[i]); + std::string name_lhs = lhs_axis.name(); + std::string name_rhs = rhs_axis.name(); + if (lhs_axis.IsPrimal() != rhs_axis.IsPrimal()) return false; + + auto it = map_to.find(name_lhs); + if (it == map_to.end()) + map_to[name_lhs] = name_rhs; + else if (it->second != name_rhs) + return false; + + it = map_back.find(name_rhs); + if (it == map_back.end()) + map_back[name_rhs] = name_lhs; + else if (it->second != name_lhs) + return false; + if (!lhs_axis.IsPrimal() && lhs.FactorOf(lhs_axis) != rhs.FactorOf(rhs_axis)) return false; + } + return true; +} +Layout TryTransformLike(const Layout& old, const Layout& ref_old, const Layout& ref_new) { + DLOG(INFO) << "transform_layout: old = " << old << ", ref_new = " << ref_new + << ", ref_old = " << ref_old << std::endl; + ICHECK(ref_old.defined()); + ICHECK(ref_new.defined()); + ICHECK(old.defined()); + + { // check if old and ref_old are similar enough such that it's + // compatible for the transform ref_old -> ref_new + const Layout& large = ref_old.ndim() > old.ndim() ? ref_old : old; + const Layout& small = large == ref_old ? old : ref_old; + Layout large_sublayout = large.SubLayout(large.ndim() - small.ndim(), small.ndim()), + rest_sublayout = large.SubLayout(0, large.ndim() - small.ndim()); + bool orthorgonal = true; + for (auto i : rest_sublayout->axes) + if (large_sublayout.IndexOf(LayoutAxis::Get(i).ToPrimal()) != -1 || + large_sublayout.IndexOf(LayoutAxis::Get(i).ToSubordinate()) != -1) { + orthorgonal = false; + break; + } + if (!orthorgonal || !Isomorphic(large_sublayout, small)) + return Layout::Undef(); // For now this case is not supported. + } + + // `old` is compatible. Now learn the axis name mapping between `old` and `ref_old` + if (old.ndim() == 0) return old; // an optmization for scalar: no-op + int mapping[26]; + bool used[26]; + memset(mapping, -1, sizeof mapping); + memset(used, 0, sizeof used); + auto find_unused = [&](char preference) -> char { + if (!used[preference - 'A']) return preference; // preference unused + for (int i = 0; i < 26; ++i) + if (!used[i]) return 'A' + i; + LOG(FATAL) << "All letters are used"; + return 0; + }; + for (int j = old->axes.size() - 1, i = ref_old->axes.size() - 1; j >= 0; --i, --j) { + char name_ref = LayoutAxis::Get(ref_old->axes[i]).ToPrimal().name()[0]; + char name = LayoutAxis::Get(old->axes[j]).ToPrimal().name()[0]; + mapping[name_ref - 'A'] = name - 'A'; + used[name - 'A'] = true; + } + for (int i = ref_old->axes.size() - 1; i >= 0; --i) { + char name_ref = LayoutAxis::Get(ref_old->axes[i]).ToPrimal().name()[0]; + int name = mapping[name_ref - 'A']; + if (name == -1) { + mapping[name_ref - 'A'] = find_unused(name_ref) - 'A'; + used[mapping[name_ref - 'A']] = true; + } + } + Review comment: Add new lines above between for loop -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
