agrabows commented on code in PR #21115: URL: https://github.com/apache/incubator-mxnet/pull/21115#discussion_r951289581
########## src/operator/subgraph/dnnl/dnnl_transformer_qk_common.h: ########## @@ -0,0 +1,230 @@ +/* + * 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. + */ + +#ifndef MXNET_OPERATOR_SUBGRAPH_DNNL_DNNL_TRANSFORMER_QK_COMMON_H_ +#define MXNET_OPERATOR_SUBGRAPH_DNNL_DNNL_TRANSFORMER_QK_COMMON_H_ + +#if MXNET_USE_ONEDNN == 1 + +#include <string> +#include <vector> + +#include "operator/contrib/transformer-inl.h" +#include "operator/numpy/np_matrix_op-inl.h" +#include "operator/tensor/matrix_op-inl.h" +#include "operator/subgraph/common.h" +#include "dnnl_common.h" +#include "dnnl_subgraph_base-inl.h" +#include "dnnl_transformer-inl.h" + +namespace mxnet { +namespace op { +namespace qk_common { + +enum SelectStatusTransformerQK { + kFail = 0, + kStart, + kFirstSwapAx, + kSecondSwapAx, + kFirstReshape, + kSecondReshape, + kSuccess +}; + +// /* +// kStart ---> kFirstSwapAx ---> kSecondSwapAx ---> kFirstReshape ---> kSecondReshape ---> kSuccess +// OR +// kStart ---> kFirstSwapAx ---> kSecondSwapAx ---> kFirstReshape ---> kSuccess +// each status except kStart is connected with kFail +// */ + +inline bool CheckSwapAxisConditionsQK(const BiDirectedNode& input_node) { + if (input_node.outputs.size() != 1) + return false; + return CheckSwapAxisConditions(*input_node.node); +} + +inline bool CheckReshapeConditionsQK(const BiDirectedNode& input_node, const index_t out_index) { + if (input_node.outputs.size() != 1) + return false; + return CheckReshapeConditions(*input_node.node, out_index); +} + +inline bool CheckSplitConditions(const std::vector<const BiDirectedNode*>& matched_list, + const BiDirectedNode& node) { + const SplitParam& param = dmlc::get<SplitParam>(node.node->attrs.parsed); + + if (param.axis != -1 || param.sections != 3 || param.squeeze_axis) + return false; + + const auto first_reshape = (*(matched_list.end() - 2))->node; + const auto second_reshape = (*(matched_list.end() - 1))->node; + if (first_reshape->op() != Op::Get("_npx_reshape") || + second_reshape->op() != Op::Get("_npx_reshape")) { + return false; + } + // 3 sections - ensure that every output is used only once + if (node.outputs.size() == 3 && node.outputs.count(first_reshape) && + node.outputs.count(second_reshape)) { + return true; + } + + return false; +} + +inline bool Select(SelectStatusTransformerQK* status, + std::vector<const BiDirectedNode*>* matched_list, + const BiDirectedNode& seed_node, + const std::shared_ptr<NodeAttr>& node_attr) { + if (seed_node.node->op() == Op::Get("batch_dot")) { + *status = kStart; + matched_list->clear(); + matched_list->push_back(&seed_node); + return true; + } + return false; +} + +template <bool with_split> +bool SelectInput(SelectStatusTransformerQK* status, + std::vector<const BiDirectedNode*>* matched_list, + const BiDirectedNode& n, + const BiDirectedNode& input_node) { + if (*status == kFail || *status == kSuccess || input_node.node->is_variable()) + return false; + const auto& raw_input_node = *input_node.node; + switch (*status) { + case kStart: + if (raw_input_node.op() == Op::Get("SwapAxis")) { + if (CheckSwapAxisConditionsQK(input_node)) { + *status = kFirstSwapAx; + matched_list->push_back(&input_node); + } + return true; Review Comment: done -- 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]
