|
| 1 | +// Copyright (c) 2024 PaddlePaddle Authors. All Rights Reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#include <string> |
| 16 | + |
| 17 | +#include "glog/logging.h" |
| 18 | + |
| 19 | +#include "paddle/fluid/framework/ir/fuse_pass_base.h" |
| 20 | +#include "paddle/fluid/framework/ir/graph_pattern_detector.h" |
| 21 | +#include "paddle/fluid/framework/ir/pass.h" |
| 22 | +#include "paddle/fluid/framework/ir/xpu/pass_utils.h" |
| 23 | +#include "paddle/fluid/framework/ir/xpu/quant_utils.h" |
| 24 | +#include "paddle/fluid/framework/op_version_registry.h" |
| 25 | +#include "paddle/fluid/platform/enforce.h" |
| 26 | + |
| 27 | +namespace phi { |
| 28 | +class DenseTensor; |
| 29 | +} // namespace phi |
| 30 | + |
| 31 | +namespace paddle { |
| 32 | +namespace framework { |
| 33 | +class Scope; |
| 34 | +} // namespace framework |
| 35 | +} // namespace paddle |
| 36 | + |
| 37 | +namespace paddle { |
| 38 | +namespace framework { |
| 39 | +namespace ir { |
| 40 | + |
| 41 | +class BlockMultiHeadAttentionXPUPass : public FusePassBase { |
| 42 | + protected: |
| 43 | + void ApplyImpl(ir::Graph* graph) const override; |
| 44 | + |
| 45 | + private: |
| 46 | + void InplaceBlockMultiHeadAttentionXPU(ir::Graph* graph) const; |
| 47 | + |
| 48 | + const std::string name_scope_{"block_multihead_attention_xpu_pass"}; |
| 49 | +}; |
| 50 | + |
| 51 | +void BlockMultiHeadAttentionXPUPass::ApplyImpl(ir::Graph* graph) const { |
| 52 | + PADDLE_ENFORCE_NOT_NULL( |
| 53 | + graph, platform::errors::PreconditionNotMet("graph should not be null.")); |
| 54 | + Init(name_scope_, graph); |
| 55 | + |
| 56 | + InplaceBlockMultiHeadAttentionXPU(graph); |
| 57 | +} |
| 58 | + |
| 59 | +void BlockMultiHeadAttentionXPUPass::InplaceBlockMultiHeadAttentionXPU( |
| 60 | + ir::Graph* graph) const { |
| 61 | + const int64_t max_batch_size = 10; |
| 62 | + auto* scope = param_scope(); |
| 63 | + for (auto* node : graph->Nodes()) { |
| 64 | + if (node->IsOp() && node->Op()->Type() == "block_multihead_attention") { |
| 65 | + auto* op_desc = node->Op(); |
| 66 | + op_desc->SetType("block_multihead_attention_xpu"); |
| 67 | + phi::DenseTensor cache_k_per_batch_maxs; |
| 68 | + auto base_name = op_desc->Input("qkv")[0]; |
| 69 | + int max_ptr_size = phi::backends::xpu::get_xpu_max_ptr_size(-1); |
| 70 | + std::string cache_k_per_batch_maxs_name = base_name + "_max_cache_k"; |
| 71 | + VarDesc cache_k_per_batch_maxs_desc(cache_k_per_batch_maxs_name); |
| 72 | + cache_k_per_batch_maxs_desc.SetPersistable(true); |
| 73 | + cache_k_per_batch_maxs_desc.SetShape( |
| 74 | + {max_batch_size, static_cast<int64_t>(max_ptr_size)}); |
| 75 | + cache_k_per_batch_maxs_desc.SetDataType( |
| 76 | + proto::VarType::Type::VarType_Type_FP32); |
| 77 | + Node* cache_k_per_batch_maxs_in = |
| 78 | + graph->CreateVarNode(&cache_k_per_batch_maxs_desc); |
| 79 | + phi::DenseTensor cpu_tensor; |
| 80 | + auto* cpu_ctx = static_cast<phi::CPUContext*>( |
| 81 | + platform::DeviceContextPool::Instance().Get(phi::CPUPlace())); |
| 82 | + cpu_tensor.set_type(phi::DataType::FLOAT32); |
| 83 | + cpu_tensor.Resize({max_batch_size, max_ptr_size}); |
| 84 | + std::vector<float> tmp(max_batch_size * max_ptr_size, 0); |
| 85 | + memcpy(cpu_ctx->Alloc<float>(&cpu_tensor), |
| 86 | + tmp.data(), |
| 87 | + max_batch_size * max_ptr_size * sizeof(float)); |
| 88 | + Assign(cpu_tensor, |
| 89 | + scope->Var(cache_k_per_batch_maxs_name) |
| 90 | + ->GetMutable<phi::DenseTensor>()); |
| 91 | + op_desc->SetInput("cache_k_per_batch_maxs", |
| 92 | + {cache_k_per_batch_maxs_name}); |
| 93 | + |
| 94 | + std::string cache_v_per_batch_maxs_name = base_name + "_max_cache_v"; |
| 95 | + VarDesc cache_v_per_batch_maxs_desc(cache_v_per_batch_maxs_name); |
| 96 | + cache_v_per_batch_maxs_desc.SetPersistable(true); |
| 97 | + cache_v_per_batch_maxs_desc.SetShape( |
| 98 | + {max_batch_size, static_cast<int64_t>(max_ptr_size)}); |
| 99 | + cache_v_per_batch_maxs_desc.SetDataType( |
| 100 | + proto::VarType::Type::VarType_Type_FP32); |
| 101 | + Node* cache_v_per_batch_maxs_in = |
| 102 | + graph->CreateVarNode(&cache_v_per_batch_maxs_desc); |
| 103 | + Assign(cpu_tensor, |
| 104 | + scope->Var(cache_v_per_batch_maxs_name) |
| 105 | + ->GetMutable<phi::DenseTensor>()); |
| 106 | + op_desc->SetInput("cache_v_per_batch_maxs", |
| 107 | + {cache_v_per_batch_maxs_name}); |
| 108 | + |
| 109 | + IR_NODE_LINK_TO(cache_k_per_batch_maxs_in, node); |
| 110 | + IR_NODE_LINK_TO(cache_v_per_batch_maxs_in, node); |
| 111 | + } |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +} // namespace ir |
| 116 | +} // namespace framework |
| 117 | +} // namespace paddle |
| 118 | + |
| 119 | +REGISTER_PASS(block_multihead_attention_xpu_pass, |
| 120 | + paddle::framework::ir::BlockMultiHeadAttentionXPUPass); |
| 121 | + |
| 122 | +REGISTER_PASS_CAPABILITY(block_multihead_attention_xpu_pass) |
| 123 | + .AddCombination( |
| 124 | + paddle::framework::compatible::OpVersionComparatorCombination().EQ( |
| 125 | + "block_multihead_attention_xpu", 0)); |
0 commit comments