|
| 1 | +// Copyright (c) 2021 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 | +#pragma once |
| 15 | + |
| 16 | +#include "paddle/fluid/framework/ir/fc_gru_fuse_pass.h" |
| 17 | + |
| 18 | +#include <gtest/gtest.h> |
| 19 | +#include "paddle/fluid/framework/ir/pass_tester_helper.h" |
| 20 | + |
| 21 | +namespace paddle { |
| 22 | +namespace framework { |
| 23 | +namespace ir { |
| 24 | + |
| 25 | +namespace fc_gru_test { |
| 26 | +void AddVarToScope(Scope* param_scope, const std::string& name, |
| 27 | + const DDim& dims) { |
| 28 | + auto* tensor = param_scope->Var(name)->GetMutable<LoDTensor>(); |
| 29 | + tensor->Resize(dims); |
| 30 | + tensor->mutable_data<float>(platform::CPUPlace()); |
| 31 | +} |
| 32 | + |
| 33 | +Scope* CreateParamScope() { |
| 34 | + auto param_scope = new Scope(); |
| 35 | + AddVarToScope(param_scope, "gru_fc_w", {}); |
| 36 | + AddVarToScope(param_scope, "gru_fc_b", {}); |
| 37 | + AddVarToScope(param_scope, "gru_w", {}); |
| 38 | + AddVarToScope(param_scope, "gru_b", {}); |
| 39 | + AddVarToScope(param_scope, "gru_batch_gate_0", {}); |
| 40 | + AddVarToScope(param_scope, "gru_batch_reset_hidden_prev_0", {}); |
| 41 | + AddVarToScope(param_scope, "gru_batch_hidden_0", {}); |
| 42 | + AddVarToScope(param_scope, "gru_hidden_0", {}); |
| 43 | + AddVarToScope(param_scope, "gru_batch_gate_1", {}); |
| 44 | + AddVarToScope(param_scope, "gru_batch_reset_hidden_prev_1", {}); |
| 45 | + AddVarToScope(param_scope, "gru_batch_hidden_1", {}); |
| 46 | + AddVarToScope(param_scope, "gru_hidden_1", {}); |
| 47 | + return param_scope; |
| 48 | +} |
| 49 | + |
| 50 | +std::unique_ptr<ir::Graph> PrepareGraph( |
| 51 | + std::string activation = "tanh", std::string gate_activation = "sigmoid") { |
| 52 | + // inputs operator output |
| 53 | + // -------------------------------------------------------- |
| 54 | + // (a, gru_fc_w) mul -> fc_0_tmp_0 |
| 55 | + // (fc_0_tmp_0, gru_fc_b) elementwise_add -> fc_0_tmp_1 |
| 56 | + // (fc_0_tmp_1,gru_w,gru_b gru -> gru_out_0 |
| 57 | + |
| 58 | + // (b, gru_fc_w) mul -> fc_1_tmp_0 |
| 59 | + // (fc_1_tmp_0, gru_fc_b) elementwise_add -> fc_1_tmp_1 |
| 60 | + // (fc_1_tmp_1,gru_w,gru_b) gru -> gru_out_1 |
| 61 | + Layers layers; |
| 62 | + auto* a = layers.data("a"); |
| 63 | + auto* b = layers.data("b"); |
| 64 | + auto* fc_w = layers.data("gru_fc_w", {}, true); |
| 65 | + auto* fc_b = layers.data("gru_fc_b", {}, true); |
| 66 | + auto* gru_w = layers.data("gru_w", {}, true); |
| 67 | + auto* gru_b = layers.data("gru_b", {}, true); |
| 68 | + auto* fc_0_tmp0 = layers.mul(a, fc_w); |
| 69 | + auto* fc_0_tmp1 = layers.elementwise_add(fc_0_tmp0, fc_b); |
| 70 | + auto* gru_batch_gate_0 = layers.data("gru_batch_gate_0", {}, false); |
| 71 | + auto* gru_batch_reset_hidden_prev_0 = |
| 72 | + layers.data("gru_batch_reset_hidden_prev_0", {}, false); |
| 73 | + auto* gru_batch_hidden_0 = layers.data("gru_batch_hidden_0", {}, false); |
| 74 | + auto* gru_hidden_0 = layers.data("gru_hidden_0", {}, false); |
| 75 | + layers.gru(fc_0_tmp1, gru_w, gru_b, gru_batch_gate_0, |
| 76 | + gru_batch_reset_hidden_prev_0, gru_batch_hidden_0, gru_hidden_0, |
| 77 | + nullptr, false, false, activation, gate_activation); |
| 78 | + |
| 79 | + auto* fc_1_tmp0 = layers.mul(b, fc_w); |
| 80 | + auto* fc_1_tmp1 = layers.elementwise_add(fc_1_tmp0, fc_b); |
| 81 | + auto* gru_batch_gate_1 = layers.data("gru_batch_gate_1", {}, false); |
| 82 | + auto* gru_batch_reset_hidden_prev_1 = |
| 83 | + layers.data("gru_batch_reset_hidden_prev_1", {}, false); |
| 84 | + auto* gru_batch_hidden_1 = layers.data("gru_batch_hidden_1", {}, false); |
| 85 | + auto* gru_hidden_1 = layers.data("gru_hidden_1", {}, false); |
| 86 | + layers.gru(fc_1_tmp1, gru_w, gru_b, gru_batch_gate_1, |
| 87 | + gru_batch_reset_hidden_prev_1, gru_batch_hidden_1, gru_hidden_1, |
| 88 | + nullptr, false, false, activation, gate_activation); |
| 89 | + |
| 90 | + std::unique_ptr<ir::Graph> graph(new ir::Graph(layers.main_program())); |
| 91 | + return std::move(graph); |
| 92 | +} |
| 93 | +} // namespace fc_gru_test |
| 94 | +} // namespace ir |
| 95 | +} // namespace framework |
| 96 | +} // namespace paddle |
0 commit comments