send_recv_op_test.cc 6.9 KB
Newer Older
L
Luo Tao 已提交
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.
武毅 已提交
2

L
Luo Tao 已提交
3 4 5
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
武毅 已提交
6

L
Luo Tao 已提交
7
    http://www.apache.org/licenses/LICENSE-2.0
武毅 已提交
8

L
Luo Tao 已提交
9 10 11 12 13
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. */
武毅 已提交
14 15

#include <unistd.h>
Y
Yancey1989 已提交
16
#include <string>
武毅 已提交
17 18 19 20 21 22
#include <thread>

#include "gtest/gtest.h"
#include "paddle/framework/op_registry.h"
#include "paddle/framework/operator.h"
#include "paddle/framework/program_desc.h"
Y
Yancey 已提交
23 24
#include "paddle/operators/math/math_function.h"
#include "paddle/operators/math/selected_rows_functor.h"
Y
Yancey1989 已提交
25
#include "paddle/string/printf.h"
武毅 已提交
26 27 28 29 30

USE_NO_KERNEL_OP(send);
USE_NO_KERNEL_OP(recv);
USE_OP(sum);

Y
Yancey 已提交
31 32 33 34
namespace f = paddle::framework;
namespace p = paddle::platform;
namespace m = paddle::operators::math;

武毅 已提交
35
// global for simplicity.
Y
Yancey 已提交
36
std::unique_ptr<f::OperatorBase> recv_op;
武毅 已提交
37

Y
Yancey 已提交
38 39
void InitTensorsInScope(f::Scope &scope, p::CPUPlace &place) {
  p::CPUDeviceContext ctx(place);
Y
Yancey1989 已提交
40 41 42
  for (int i = 0; i < 2; ++i) {
    auto var_name = paddle::string::Sprintf("x%d", i);
    auto var = scope.Var(var_name);
Y
Yancey 已提交
43
    auto tensor = var->GetMutable<f::LoDTensor>();
Y
Yancey1989 已提交
44 45 46 47 48
    tensor->Resize({10, 10});
    float *expect = tensor->mutable_data<float>(place);
    for (int64_t i = 0; i < tensor->numel(); ++i) {
      expect[i] = static_cast<float>(i);
    }
武毅 已提交
49 50 51
  }

  auto out_var = scope.Var("Out");
Y
Yancey 已提交
52
  auto out_tensor = out_var->GetMutable<f::LoDTensor>();
武毅 已提交
53
  out_tensor->Resize({10, 10});
Y
Yancey1989 已提交
54
  out_tensor->mutable_data<float>(place);  // allocate
武毅 已提交
55 56
}

Y
Yancey 已提交
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
void InitSelectedRowsInScope(f::Scope &scope, p::CPUPlace &place) {
  p::CPUDeviceContext ctx(place);
  int64_t height = 10;
  int64_t row_numel = 10;
  m::SetConstant<p::CPUDeviceContext, float> set_one;
  // init x0
  std::vector<int64_t> rows0{0, 4, 7};
  auto x0_var = scope.Var("x0");
  auto x0 = x0_var->GetMutable<f::SelectedRows>();
  x0->set_rows(rows0);
  x0->set_height(height);
  auto x0_value = x0->mutable_value();
  x0_value->mutable_data<float>(
      f::make_ddim({static_cast<int64_t>(rows0.size()), row_numel}), place);
  set_one(ctx, x0_value, 1.0);

  // init x1
  std::vector<int64_t> rows1{2, 9};
  auto x1_var = scope.Var("x1");
  auto x1 = x1_var->GetMutable<f::SelectedRows>();
  x1->set_rows(rows1);
  x1->set_height(height);
  auto x1_value = x1->mutable_value();
  x1_value->mutable_data<float>(
      f::make_ddim({static_cast<int64_t>(rows1.size()), row_numel}), place);
  set_one(ctx, x1_value, 1.0);

  auto out_var = scope.Var("Out");
  auto out = out_var->GetMutable<f::SelectedRows>();
  auto out_value = out->mutable_value();
  out->set_height(height);
  out_value->mutable_data<float>(f::make_ddim({5, 10}), place);
}

void AddOp(const std::string &type, const f::VariableNameMap &inputs,
           const f::VariableNameMap &outputs, f::AttributeMap attrs,
           f::BlockDesc *block) {
武毅 已提交
94 95 96 97
  // insert output
  for (auto kv : outputs) {
    for (auto v : kv.second) {
      auto var = block->Var(v);
Y
Yancey 已提交
98
      var->SetDataType(f::proto::DataType::FP32);
武毅 已提交
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
    }
  }

  // insert op
  auto op = block->AppendOp();
  op->SetType(type);
  for (auto &kv : inputs) {
    op->SetInput(kv.first, kv.second);
  }
  for (auto &kv : outputs) {
    op->SetOutput(kv.first, kv.second);
  }
  op->SetAttrMap(attrs);
}

Y
Yancey 已提交
114 115 116 117 118 119 120 121
void StartServerNet(bool is_sparse) {
  f::Scope scope;
  p::CPUPlace place;
  if (is_sparse) {
    InitSelectedRowsInScope(scope, place);
  } else {
    InitTensorsInScope(scope, place);
  }
武毅 已提交
122 123

  // sub program run in recv_op, for simple test we use sum
Y
Yancey 已提交
124 125
  f::ProgramDesc program;
  f::BlockDesc *block = program.MutableBlock(0);
武毅 已提交
126
  // X for server side tensors, RX for received tensers, must be of same shape.
Y
Yancey 已提交
127
  AddOp("sum", {{"X", {"x0", "x1"}}}, {{"Out", {"Out"}}}, {}, block);
武毅 已提交
128

Y
Yancey 已提交
129
  f::AttributeMap attrs;
武毅 已提交
130
  attrs.insert({"endpoint", std::string("127.0.0.1:6174")});
Y
Yancey 已提交
131
  attrs.insert({"ParamList", std::vector<std::string>({"Out"})});
T
typhoonzero 已提交
132
  attrs.insert({"GradList", std::vector<std::string>({"x1"})});
T
typhoonzero 已提交
133 134 135 136
  std::string program_proto;
  PADDLE_ENFORCE(program.Proto()->SerializeToString(&program_proto));

  attrs.insert({"OptimizeProgram", program_proto});
Y
Yancey 已提交
137
  recv_op = f::OpRegistry::CreateOp("recv", {{"RX", {"x1"}}}, {}, attrs);
T
typhoonzero 已提交
138
  recv_op->Run(scope, place);
武毅 已提交
139 140
}

Y
Yancey 已提交
141 142
TEST(SendRecvOp, CPUDense) {
  std::thread server_thread(StartServerNet, false);
G
gongweibao 已提交
143
  sleep(10);  // wait server to start
武毅 已提交
144
  // local net
Y
Yancey 已提交
145 146
  f::Scope scope;
  p::CPUPlace place;
武毅 已提交
147 148
  InitTensorsInScope(scope, place);

Y
Yancey 已提交
149
  f::AttributeMap attrs;
T
typhoonzero 已提交
150 151
  attrs.insert({"endpoints", std::vector<std::string>({"127.0.0.1:6174"})});
  attrs.insert({"epmap", std::vector<std::string>({"127.0.0.1:6174"})});
Y
Yancey 已提交
152 153
  auto send_op = f::OpRegistry::CreateOp("send", {{"X", {"x1"}}},
                                         {{"Out", {"Out"}}}, attrs);
T
typhoonzero 已提交
154
  send_op->Run(scope, place);
武毅 已提交
155

T
typhoonzero 已提交
156
  auto in_var = scope.Var("x1");
Y
Yancey 已提交
157
  auto tensor = in_var->GetMutable<f::LoDTensor>();
武毅 已提交
158
  float *expected = tensor->data<float>();
Y
Yancey 已提交
159 160
  auto out_var = scope.Var("Out");
  auto target = out_var->GetMutable<f::LoDTensor>();
T
typhoonzero 已提交
161
  // x1 * 2 == x0
武毅 已提交
162 163 164 165 166
  EXPECT_NE(target->memory_size(), size_t(0));
  float *actual = target->data<float>();
  for (int64_t i = 0; i < target->numel(); ++i) {
    EXPECT_EQ(expected[i] * 2, actual[i]);
  }
Y
Yancey 已提交
167 168 169 170
  recv_op->Stop();
  server_thread.join();
  recv_op.reset(nullptr);
}
T
typhoonzero 已提交
171

Y
Yancey 已提交
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
TEST(SendRecvOp, CPUSparse) {
  std::thread server_thread(StartServerNet, true);
  sleep(3);  // wait server to start
  // local net
  f::Scope scope;
  p::CPUPlace place;
  p::CPUDeviceContext ctx(place);
  InitSelectedRowsInScope(scope, place);
  f::AttributeMap attrs;
  attrs.insert({"endpoints", std::vector<std::string>({"127.0.0.1:6174"})});
  attrs.insert({"epmap", std::vector<std::string>({"127.0.0.1:6174"})});
  auto send_op = f::OpRegistry::CreateOp("send", {{"X", {"x1"}}},
                                         {{"Out", {"Out"}}}, attrs);
  send_op->Run(scope, place);

  auto x0 = scope.Var("x0")->GetMutable<f::SelectedRows>();
  auto x1 = scope.Var("x1")->GetMutable<f::SelectedRows>();
  auto out = scope.Var("Out")->GetMutable<f::SelectedRows>();
  auto actual = out->mutable_value();

  std::unique_ptr<f::SelectedRows> expect{new f::SelectedRows()};
  auto expect_value = expect->mutable_value();
  expect_value->mutable_data<float>(f::make_ddim({5, 10}), place);

  m::SelectedRowsAdd<p::CPUDeviceContext, float> add_functor;
  add_functor(ctx, *x0, *x1, expect.get());

  EXPECT_EQ(actual->numel(), expect_value->numel());
  EXPECT_EQ(out->rows().size(), x0->rows().size() + x1->rows().size());

  for (int64_t i = 0; i < expect_value->numel(); ++i) {
    EXPECT_EQ(expect_value->mutable_data<float>(place)[i],
              actual->mutable_data<float>(place)[i]);
  }
T
typhoonzero 已提交
206
  recv_op->Stop();
武毅 已提交
207
  server_thread.join();
Y
Yancey 已提交
208
  recv_op.reset();
武毅 已提交
209
}