test_dist_fleet_ps3.py 6.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#   Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed 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.

import unittest
P
pangyoki 已提交
16

T
tangwei12 已提交
17
import paddle
18

P
pangyoki 已提交
19
paddle.enable_static()
20

T
tangwei12 已提交
21
import paddle.distributed.fleet as fleet
22 23
import paddle.distributed.fleet.base.role_maker as role_maker
import paddle.fluid as fluid
T
tangwei12 已提交
24

25 26 27 28 29 30 31 32 33 34 35 36 37 38
# For Net
base_lr = 0.2
emb_lr = base_lr * 3
dict_dim = 1500
emb_dim = 128
hid_dim = 128
margin = 0.1
sample_rate = 1
batch_size = 4


class TestPSPassWithBow(unittest.TestCase):
    def net(self):
        def get_acc(cos_q_nt, cos_q_pt, batch_size):
L
LiYuRio 已提交
39
            cond = paddle.less_than(cos_q_nt, cos_q_pt)
40
            cond = fluid.layers.cast(cond, dtype='float64')
41
            cond_3 = paddle.sum(cond)
42
            acc = paddle.divide(
43 44 45 46 47 48
                cond_3,
                fluid.layers.fill_constant(
                    shape=[1], value=batch_size * 1.0, dtype='float64'
                ),
                name="simnet_acc",
            )
49 50 51
            return acc

        def get_loss(cos_q_pt, cos_q_nt):
52
            loss_op1 = paddle.subtract(
53 54 55 56 57
                fluid.layers.fill_constant_batch_size_like(
                    input=cos_q_pt, shape=[-1, 1], value=margin, dtype='float32'
                ),
                cos_q_pt,
            )
58
            loss_op2 = paddle.add(loss_op1, cos_q_nt)
H
HongyuJia 已提交
59
            loss_op3 = paddle.maximum(
60 61 62 63 64
                fluid.layers.fill_constant_batch_size_like(
                    input=loss_op2, shape=[-1, 1], value=0.0, dtype='float32'
                ),
                loss_op2,
            )
65
            avg_cost = paddle.mean(loss_op3)
66 67 68
            return avg_cost

        is_distributed = False
69
        is_sparse = False
70 71

        # query
G
GGBond8488 已提交
72 73
        q = paddle.static.data(
            name="query_ids", shape=[-1, 1], dtype="int64", lod_level=1
74
        )
75 76 77 78 79 80 81 82
        # embedding
        q_emb = fluid.layers.embedding(
            input=q,
            is_distributed=is_distributed,
            size=[dict_dim, emb_dim],
            param_attr=fluid.ParamAttr(
                initializer=fluid.initializer.Constant(value=0.01),
                name="__emb__",
83 84 85 86
                learning_rate=emb_lr,
            ),
            is_sparse=is_sparse,
        )
87
        q_emb = paddle.reshape(q_emb, [-1, emb_dim])
88 89
        # vsum
        q_sum = fluid.layers.sequence_pool(input=q_emb, pool_type='sum')
90
        q_ss = paddle.nn.functional.softsign(q_sum)
91
        # fc layer after conv
C
Charles-hit 已提交
92 93
        q_fc = paddle.static.nn.fc(
            x=q_ss,
94
            size=hid_dim,
C
Charles-hit 已提交
95
            weight_attr=fluid.ParamAttr(
96 97
                initializer=fluid.initializer.Constant(value=0.01),
                name="__q_fc__",
98 99 100
                learning_rate=base_lr,
            ),
        )
101
        # label data
G
GGBond8488 已提交
102
        label = paddle.static.data(name="label", shape=[-1, 1], dtype="int64")
103
        # pt
G
GGBond8488 已提交
104 105
        pt = paddle.static.data(
            name="pos_title_ids", shape=[-1, 1], dtype="int64", lod_level=1
106
        )
107 108 109 110 111 112 113 114
        # embedding
        pt_emb = fluid.layers.embedding(
            input=pt,
            is_distributed=is_distributed,
            size=[dict_dim, emb_dim],
            param_attr=fluid.ParamAttr(
                initializer=fluid.initializer.Constant(value=0.01),
                name="__emb__",
115 116 117 118
                learning_rate=emb_lr,
            ),
            is_sparse=is_sparse,
        )
119
        pt_emb = paddle.reshape(pt_emb, [-1, emb_dim])
120 121
        # vsum
        pt_sum = fluid.layers.sequence_pool(input=pt_emb, pool_type='sum')
122
        pt_ss = paddle.nn.functional.softsign(pt_sum)
123
        # fc layer
C
Charles-hit 已提交
124 125
        pt_fc = paddle.static.nn.fc(
            x=pt_ss,
126
            size=hid_dim,
C
Charles-hit 已提交
127
            weight_attr=fluid.ParamAttr(
128 129
                initializer=fluid.initializer.Constant(value=0.01),
                name="__fc__",
130 131 132 133
                learning_rate=base_lr,
            ),
            bias_attr=fluid.ParamAttr(name="__fc_b__"),
        )
134
        # nt
G
GGBond8488 已提交
135 136
        nt = paddle.static.data(
            name="neg_title_ids", shape=[-1, 1], dtype="int64", lod_level=1
137
        )
138 139 140 141 142 143 144 145
        # embedding
        nt_emb = fluid.layers.embedding(
            input=nt,
            is_distributed=is_distributed,
            size=[dict_dim, emb_dim],
            param_attr=fluid.ParamAttr(
                initializer=fluid.initializer.Constant(value=0.01),
                name="__emb__",
146 147 148 149
                learning_rate=emb_lr,
            ),
            is_sparse=is_sparse,
        )
150
        nt_emb = paddle.reshape(nt_emb, [-1, emb_dim])
151 152
        # vsum
        nt_sum = fluid.layers.sequence_pool(input=nt_emb, pool_type='sum')
153
        nt_ss = paddle.nn.functional.softsign(nt_sum)
154
        # fc layer
C
Charles-hit 已提交
155 156
        nt_fc = paddle.static.nn.fc(
            x=nt_ss,
157
            size=hid_dim,
C
Charles-hit 已提交
158
            weight_attr=fluid.ParamAttr(
159 160
                initializer=fluid.initializer.Constant(value=0.01),
                name="__fc__",
161 162 163 164
                learning_rate=base_lr,
            ),
            bias_attr=fluid.ParamAttr(name="__fc_b__"),
        )
C
ccrrong 已提交
165 166
        cos_q_pt = paddle.nn.functional.cosine_similarity(q_fc, pt_fc)
        cos_q_nt = paddle.nn.functional.cosine_similarity(q_fc, nt_fc)
167 168 169 170 171 172 173 174
        # loss
        avg_cost = get_loss(cos_q_pt, cos_q_nt)
        # acc
        acc = get_acc(cos_q_nt, cos_q_pt, batch_size)
        return [avg_cost, acc, cos_q_pt]

    def test(self):
        endpoints = [
175 176 177 178
            "127.0.0.1:36004",
            "127.0.0.1:36005",
            "127.0.0.1:36006",
            "127.0.0.1:36007",
179 180
        ]

181 182 183 184 185 186
        role = fleet.UserDefinedRoleMaker(
            current_id=0,
            role=role_maker.Role.WORKER,
            worker_num=2,
            server_endpoints=endpoints,
        )
187 188 189 190

        fleet.init(role)
        loss, acc, _ = self.net()
        optimizer = fluid.optimizer.SGD(base_lr)
T
tangwei12 已提交
191 192 193

        strategy = paddle.distributed.fleet.DistributedStrategy()
        strategy.a_sync = True
194
        strategy.a_sync_configs = {"launch_barrier": False}
T
tangwei12 已提交
195

196 197 198
        optimizer = fleet.distributed_optimizer(optimizer, strategy)
        optimizer.minimize(loss)

199 200
        fleet.shrink(10)

201 202 203

if __name__ == '__main__':
    unittest.main()