send_recv_op_test.cc 4.4 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
Yancey1989 已提交
23
#include "paddle/string/printf.h"
武毅 已提交
24 25 26 27 28 29 30 31 32 33 34

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

// global for simplicity.
std::unique_ptr<paddle::framework::OperatorBase> recv_op;

void InitTensorsInScope(paddle::framework::Scope &scope,
                        paddle::platform::CPUPlace &place) {
  paddle::platform::CPUDeviceContext ctx(place);
Y
Yancey1989 已提交
35 36 37 38 39 40 41 42 43
  for (int i = 0; i < 2; ++i) {
    auto var_name = paddle::string::Sprintf("x%d", i);
    auto var = scope.Var(var_name);
    auto tensor = var->GetMutable<paddle::framework::LoDTensor>();
    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);
    }
武毅 已提交
44 45 46 47 48
  }

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

void AddOp(const std::string &type,
           const paddle::framework::VariableNameMap &inputs,
           const paddle::framework::VariableNameMap &outputs,
           paddle::framework::AttributeMap attrs,
T
typhoonzero 已提交
56
           paddle::framework::BlockDesc *block) {
武毅 已提交
57 58 59 60
  // insert output
  for (auto kv : outputs) {
    for (auto v : kv.second) {
      auto var = block->Var(v);
T
typhoonzero 已提交
61
      var->SetDataType(paddle::framework::proto::DataType::FP32);
武毅 已提交
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
    }
  }

  // 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);
}

void StartServerNet() {
  paddle::framework::Scope scope;
  paddle::platform::CPUPlace place;
  InitTensorsInScope(scope, place);

  // sub program run in recv_op, for simple test we use sum
T
typhoonzero 已提交
83 84
  paddle::framework::ProgramDesc program;
  paddle::framework::BlockDesc *block = program.MutableBlock(0);
武毅 已提交
85
  // X for server side tensors, RX for received tensers, must be of same shape.
T
typhoonzero 已提交
86
  AddOp("sum", {{"X", {"x0", "x1"}}}, {{"Out", {"x0"}}}, {}, block);
武毅 已提交
87 88 89

  paddle::framework::AttributeMap attrs;
  attrs.insert({"endpoint", std::string("127.0.0.1:6174")});
T
typhoonzero 已提交
90 91
  attrs.insert({"ParamList", std::vector<std::string>({"x0"})});
  attrs.insert({"GradList", std::vector<std::string>({"x1"})});
T
typhoonzero 已提交
92 93 94 95
  std::string program_proto;
  PADDLE_ENFORCE(program.Proto()->SerializeToString(&program_proto));

  attrs.insert({"OptimizeProgram", program_proto});
T
typhoonzero 已提交
96 97 98
  recv_op = paddle::framework::OpRegistry::CreateOp("recv", {{"RX", {"x1"}}},
                                                    {}, attrs);
  recv_op->Run(scope, place);
武毅 已提交
99 100 101 102 103 104 105 106 107 108 109
}

TEST(SendRecvOp, CPU) {
  std::thread server_thread(StartServerNet);
  sleep(5);  // wait server to start
  // local net
  paddle::framework::Scope scope;
  paddle::platform::CPUPlace place;
  InitTensorsInScope(scope, place);

  paddle::framework::AttributeMap attrs;
T
typhoonzero 已提交
110 111
  attrs.insert({"endpoints", std::vector<std::string>({"127.0.0.1:6174"})});
  attrs.insert({"epmap", std::vector<std::string>({"127.0.0.1:6174"})});
武毅 已提交
112
  auto send_op = paddle::framework::OpRegistry::CreateOp(
T
typhoonzero 已提交
113 114
      "send", {{"X", {"x1"}}}, {{"Out", {"x0"}}}, attrs);
  send_op->Run(scope, place);
武毅 已提交
115

T
typhoonzero 已提交
116
  auto in_var = scope.Var("x1");
武毅 已提交
117 118
  auto tensor = in_var->GetMutable<paddle::framework::LoDTensor>();
  float *expected = tensor->data<float>();
T
typhoonzero 已提交
119
  auto out_var = scope.Var("x0");
武毅 已提交
120
  auto target = out_var->GetMutable<paddle::framework::LoDTensor>();
T
typhoonzero 已提交
121
  // x1 * 2 == x0
武毅 已提交
122 123 124 125 126
  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]);
  }
T
typhoonzero 已提交
127 128

  recv_op->Stop();
武毅 已提交
129
  server_thread.join();
T
typhoonzero 已提交
130
  // recv_op.reset();
武毅 已提交
131
}