|
| 1 | +# Copyright (c) 2018 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 | +from __future__ import print_function |
| 16 | + |
| 17 | +import os |
| 18 | + |
| 19 | +os.environ["WITH_DISTRIBUTE"] = "ON" |
| 20 | + |
| 21 | +import unittest |
| 22 | +import tempfile |
| 23 | +import shutil |
| 24 | + |
| 25 | +import paddle |
| 26 | +import paddle.fluid as fluid |
| 27 | +import paddle.distributed.fleet.base.role_maker as role_maker |
| 28 | +import paddle.distributed.fleet as fleet |
| 29 | + |
| 30 | +paddle.enable_static() |
| 31 | + |
| 32 | +# For Net |
| 33 | +base_lr = 0.2 |
| 34 | +emb_lr = base_lr * 3 |
| 35 | +dict_dim = 1500 |
| 36 | +emb_dim = 128 |
| 37 | +hid_dim = 128 |
| 38 | +margin = 0.1 |
| 39 | +sample_rate = 1 |
| 40 | +batch_size = 4 |
| 41 | + |
| 42 | + |
| 43 | +# this unittest is tested for SparseSharedAdamSGDRule |
| 44 | +class TestPSPassWithBow(unittest.TestCase): |
| 45 | + |
| 46 | + def net(self): |
| 47 | + |
| 48 | + def get_acc(cos_q_nt, cos_q_pt, batch_size): |
| 49 | + cond = fluid.layers.less_than(cos_q_nt, cos_q_pt) |
| 50 | + cond = fluid.layers.cast(cond, dtype='float64') |
| 51 | + cond_3 = fluid.layers.reduce_sum(cond) |
| 52 | + acc = fluid.layers.elementwise_div(cond_3, |
| 53 | + fluid.layers.fill_constant( |
| 54 | + shape=[1], |
| 55 | + value=batch_size * 1.0, |
| 56 | + dtype='float64'), |
| 57 | + name="simnet_acc") |
| 58 | + return acc |
| 59 | + |
| 60 | + def get_loss(cos_q_pt, cos_q_nt): |
| 61 | + loss_op1 = fluid.layers.elementwise_sub( |
| 62 | + fluid.layers.fill_constant_batch_size_like(input=cos_q_pt, |
| 63 | + shape=[-1, 1], |
| 64 | + value=margin, |
| 65 | + dtype='float32'), |
| 66 | + cos_q_pt) |
| 67 | + loss_op2 = fluid.layers.elementwise_add(loss_op1, cos_q_nt) |
| 68 | + loss_op3 = fluid.layers.elementwise_max( |
| 69 | + fluid.layers.fill_constant_batch_size_like(input=loss_op2, |
| 70 | + shape=[-1, 1], |
| 71 | + value=0.0, |
| 72 | + dtype='float32'), |
| 73 | + loss_op2) |
| 74 | + avg_cost = fluid.layers.mean(loss_op3) |
| 75 | + return avg_cost |
| 76 | + |
| 77 | + is_distributed = False |
| 78 | + is_sparse = True |
| 79 | + |
| 80 | + # query |
| 81 | + q = fluid.layers.data(name="query_ids", |
| 82 | + shape=[1], |
| 83 | + dtype="int64", |
| 84 | + lod_level=1) |
| 85 | + # embedding |
| 86 | + q_emb = fluid.contrib.layers.sparse_embedding( |
| 87 | + input=q, |
| 88 | + size=[dict_dim, emb_dim], |
| 89 | + param_attr=fluid.ParamAttr( |
| 90 | + initializer=fluid.initializer.Constant(value=0.01), |
| 91 | + name="__emb__", |
| 92 | + learning_rate=emb_lr)) |
| 93 | + q_emb = fluid.layers.reshape(q_emb, [-1, emb_dim]) |
| 94 | + # vsum |
| 95 | + q_sum = fluid.layers.sequence_pool(input=q_emb, pool_type='sum') |
| 96 | + q_ss = fluid.layers.softsign(q_sum) |
| 97 | + # fc layer after conv |
| 98 | + q_fc = fluid.layers.fc( |
| 99 | + input=q_ss, |
| 100 | + size=hid_dim, |
| 101 | + param_attr=fluid.ParamAttr( |
| 102 | + initializer=fluid.initializer.Constant(value=0.01), |
| 103 | + name="__q_fc__", |
| 104 | + learning_rate=base_lr)) |
| 105 | + # label data |
| 106 | + label = fluid.layers.data(name="label", shape=[1], dtype="int64") |
| 107 | + # pt |
| 108 | + pt = fluid.layers.data(name="pos_title_ids", |
| 109 | + shape=[1], |
| 110 | + dtype="int64", |
| 111 | + lod_level=1) |
| 112 | + # embedding |
| 113 | + pt_emb = fluid.contrib.layers.sparse_embedding( |
| 114 | + input=pt, |
| 115 | + size=[dict_dim, emb_dim], |
| 116 | + param_attr=fluid.ParamAttr( |
| 117 | + initializer=fluid.initializer.Constant(value=0.01), |
| 118 | + name="__emb__", |
| 119 | + learning_rate=emb_lr)) |
| 120 | + pt_emb = fluid.layers.reshape(pt_emb, [-1, emb_dim]) |
| 121 | + # vsum |
| 122 | + pt_sum = fluid.layers.sequence_pool(input=pt_emb, pool_type='sum') |
| 123 | + pt_ss = fluid.layers.softsign(pt_sum) |
| 124 | + # fc layer |
| 125 | + pt_fc = fluid.layers.fc( |
| 126 | + input=pt_ss, |
| 127 | + size=hid_dim, |
| 128 | + param_attr=fluid.ParamAttr( |
| 129 | + initializer=fluid.initializer.Constant(value=0.01), |
| 130 | + name="__fc__", |
| 131 | + learning_rate=base_lr), |
| 132 | + bias_attr=fluid.ParamAttr(name="__fc_b__")) |
| 133 | + # nt |
| 134 | + nt = fluid.layers.data(name="neg_title_ids", |
| 135 | + shape=[1], |
| 136 | + dtype="int64", |
| 137 | + lod_level=1) |
| 138 | + # embedding |
| 139 | + nt_emb = fluid.contrib.layers.sparse_embedding( |
| 140 | + input=nt, |
| 141 | + size=[dict_dim, emb_dim], |
| 142 | + param_attr=fluid.ParamAttr( |
| 143 | + initializer=fluid.initializer.Constant(value=0.01), |
| 144 | + name="__emb__", |
| 145 | + learning_rate=emb_lr)) |
| 146 | + nt_emb = fluid.layers.reshape(nt_emb, [-1, emb_dim]) |
| 147 | + # vsum |
| 148 | + nt_sum = fluid.layers.sequence_pool(input=nt_emb, pool_type='sum') |
| 149 | + nt_ss = fluid.layers.softsign(nt_sum) |
| 150 | + # fc layer |
| 151 | + nt_fc = fluid.layers.fc( |
| 152 | + input=nt_ss, |
| 153 | + size=hid_dim, |
| 154 | + param_attr=fluid.ParamAttr( |
| 155 | + initializer=fluid.initializer.Constant(value=0.01), |
| 156 | + name="__fc__", |
| 157 | + learning_rate=base_lr), |
| 158 | + bias_attr=fluid.ParamAttr(name="__fc_b__")) |
| 159 | + cos_q_pt = fluid.layers.cos_sim(q_fc, pt_fc) |
| 160 | + cos_q_nt = fluid.layers.cos_sim(q_fc, nt_fc) |
| 161 | + # loss |
| 162 | + avg_cost = get_loss(cos_q_pt, cos_q_nt) |
| 163 | + # acc |
| 164 | + acc = get_acc(cos_q_nt, cos_q_pt, batch_size) |
| 165 | + return [avg_cost, acc, cos_q_pt] |
| 166 | + |
| 167 | + def test(self): |
| 168 | + os.environ["PADDLE_PSERVER_NUMS"] = "2" |
| 169 | + os.environ["PADDLE_TRAINERS_NUM"] = "2" |
| 170 | + os.environ["POD_IP"] = "127.0.0.1" |
| 171 | + os.environ["PADDLE_PORT"] = "36001" |
| 172 | + os.environ["PADDLE_TRAINER_ID"] = "0" |
| 173 | + os.environ["PADDLE_TRAINERS_NUM"] = "2" |
| 174 | + os.environ[ |
| 175 | + "PADDLE_PSERVERS_IP_PORT_LIST"] = "127.0.0.1:36001,127.0.0.2:36001" |
| 176 | + os.environ["TRAINING_ROLE"] = "PSERVER" |
| 177 | + |
| 178 | + role = role_maker.PaddleCloudRoleMaker() |
| 179 | + fleet.init(role) |
| 180 | + loss, acc, _ = self.net() |
| 181 | + |
| 182 | + strategy = paddle.distributed.fleet.DistributedStrategy() |
| 183 | + strategy.a_sync = True |
| 184 | + |
| 185 | + configs = {} |
| 186 | + configs['__emb__'] = { |
| 187 | + "table_parameters.__emb__.accessor.embed_sgd_param.name": |
| 188 | + "SparseSharedAdamSGDRule", |
| 189 | + "table_parameters.__emb__.accessor.embedx_sgd_param.name": |
| 190 | + "SparseSharedAdamSGDRule", |
| 191 | + } |
| 192 | + strategy.sparse_table_configs = configs |
| 193 | + optimizer = paddle.fluid.optimizer.SGD(learning_rate=0.01) |
| 194 | + optimizer = fleet.distributed_optimizer(optimizer, strategy=strategy) |
| 195 | + optimizer.minimize(loss) |
| 196 | + |
| 197 | + fleet.init_server() |
| 198 | + |
| 199 | + |
| 200 | +if __name__ == '__main__': |
| 201 | + unittest.main() |
0 commit comments