Lunderberg commented on code in PR #16596: URL: https://github.com/apache/tvm/pull/16596#discussion_r1500745301
########## src/relax/transform/reorder_permute_dims_after_concat.cc: ########## @@ -0,0 +1,173 @@ +/* + * 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. + */ + +/*! + * \file tvm/relax/transform/reorder_permute_dims_after_concat.cc + * \brief Reorder concat(permute_dims(A), permute_dims(B)) into permute_dims(concat(A,B)) + */ + +#include <tvm/relax/analysis.h> +#include <tvm/relax/dataflow_matcher.h> +#include <tvm/relax/expr.h> +#include <tvm/relax/expr_functor.h> +#include <tvm/relax/transform.h> + +#include <optional> +#include <unordered_set> +#include <vector> + +#include "../op/tensor/index.h" +#include "../op/tensor/linear_algebra.h" +#include "../op/tensor/manipulate.h" + +namespace tvm { +namespace relax { + +namespace { +std::tuple<DFPattern, TypedPackedFunc<Expr(Expr, Map<DFPattern, Expr>)>> CreatePatterns() { + // TODO(Lunderberg): Allow pattern-matching to handle a flexible + // number of arguments, each of which matches the same type of + // pattern. + size_t min_concat = 2; + size_t max_concat = 12; Review Comment: Yeah. If the number of concat parameters is exceeded, then the match silently fails. And unlike `CombineParallelMatmul`, which could be run repeatedly if the number of concatenations is exceeded, the validity checking for reordering requires all concatenated tensors before performing any change. Long-term, I think the flexible tuple index will be the way to go. I think it will end up needing explicit tuple extent variables. That way, a pattern containing two flexible tuple extents could distinguish between a single shared extent and two independent extents. I'm picturing something like the following: ```c++ // A normal wildcard argument DFWildcard arg; // Represents the extent of the tuple containing a flexible pattern. DFArbitraryExtent tuple_extent; // A pattern that may have a different value within each element of the tuple. DFRepeatedPattern args(arg, tuple_extent); // The DFRepeatedPattern could be used to build up additional patterns DFCallPattern transposed_args(OpPattern("relax.permute_dims"), args); // Eventually, a pattern match for a tuple of unknown length gets instantiated. DFArbitraryTuplePattern concat_tuple(transposed_args, tuple_extent); // Which could then be used to match operators whose arguments are an // arbitrary tuple length. DFCallPattern concat_pat(OpPattern("relax.concat"), concat_tuple); ``` But that's for brainstorming an implementation that would be much later down the road. -- 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]
