grpc_serde_test.cc 7.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/* Copyright (c) 2016 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. */

#include <unistd.h>
#include <string>
Y
Yi Wang 已提交
17
#include <thread>  // NOLINT
18

Y
Yi Wang 已提交
19
#include "google/protobuf/text_format.h"
20 21 22 23
#include "gtest/gtest.h"
#include "paddle/fluid/framework/lod_tensor.h"
#include "paddle/fluid/framework/tensor_util.h"
#include "paddle/fluid/framework/variable.h"
24 25 26
#include "paddle/fluid/operators/detail/macros.h"
#include "paddle/fluid/operators/distributed/grpc_serde.h"
#include "paddle/fluid/operators/distributed/grpc_variable_response.h"
27
#include "paddle/fluid/operators/distributed/sendrecvop_utils.h"
28 29 30 31 32 33 34 35 36 37
#include "paddle/fluid/operators/math/math_function.h"
#include "paddle/fluid/platform/place.h"
#include "paddle/fluid/string/printf.h"

namespace framework = paddle::framework;
namespace platform = paddle::platform;
namespace operators = paddle::operators;
namespace math = paddle::operators::math;
namespace memory = paddle::memory;

38
void RunSerdeTestSelectedRows(platform::Place place) {
39 40
  platform::DeviceContextPool& pool = platform::DeviceContextPool::Instance();
  auto& ctx = *pool.Get(place);
41 42 43 44

  // serialize var to ByteBuffer
  framework::Variable var;
  auto* slr = var.GetMutable<framework::SelectedRows>();
45
  slr->set_height(1000);
46 47
  auto* tensor = slr->mutable_value();
  auto* rows = slr->mutable_rows();
T
typhoonzero 已提交
48
  tensor->Resize(framework::make_ddim({564, 128}));
49
  tensor->mutable_data<float>(place);
T
typhoonzero 已提交
50
  int tensor_numel = 564 * 128;
51
  math::set_constant(ctx, tensor, 32.7);
T
typhoonzero 已提交
52
  for (int i = 0; i < 564; ++i) rows->push_back(i);
53 54

  ::grpc::ByteBuffer msg;
55
  operators::distributed::SerializeToByteBuffer("myvar", &var, ctx, &msg);
56
  EXPECT_GT(msg.Length(), static_cast<size_t>(0));
57 58 59 60 61 62 63 64

  // deserialize
  std::vector<::grpc::Slice> slices;
  (void)msg.Dump(&slices);
  std::string tmp;
  for (const auto& s : slices) {
    tmp.append(reinterpret_cast<const char*>(s.begin()), s.size());
  }
65

66 67
  sendrecv::VariableMessage varmsg;
  EXPECT_TRUE(varmsg.ParseFromString(tmp));
68

T
typhoonzero 已提交
69
  // deserialize bytebuffer
70
  EXPECT_EQ(varmsg.varname(), "myvar");
71
  EXPECT_EQ(varmsg.type(), 1);
72 73 74

  const float* tensor_data =
      reinterpret_cast<const float*>(varmsg.serialized().data());
75 76
  const int64_t* rows_data =
      reinterpret_cast<const int64_t*>(varmsg.rows().data());
77
  for (int i = 0; i < tensor_numel; ++i) {
78
    EXPECT_FLOAT_EQ(tensor_data[i], 32.7);
79
  }
T
typhoonzero 已提交
80 81 82 83
  for (int i = 0; i < 564; ++i) {
    EXPECT_EQ(rows_data[i], i);
  }

84
  // deserialize zero-copy
85
  // framework::Variable var2;
86
  // operators::distributed::DeserializeFromByteBuffer(msg, ctx, &var2);
87 88
  framework::Scope scope;
  scope.Var("myvar");
89
  operators::distributed::GRPCVariableResponse resp(&scope, &ctx);
90 91 92 93 94 95 96
  EXPECT_EQ(resp.Parse(msg), 0);

  framework::Variable* var2 = resp.GetVar();

  auto* slr2 = var2->GetMutable<framework::SelectedRows>();
  auto* tensor2 = slr2->mutable_value();
  auto* rows2 = slr2->mutable_rows();
97 98 99 100 101
  float* tensor_data2 = nullptr;
  framework::Tensor tmp_tensor;

  if (platform::is_gpu_place(ctx.GetPlace())) {
    platform::CPUPlace cpu;
102
    framework::TensorCopy(*tensor2, cpu, &tmp_tensor);
103 104
    tensor_data2 = tmp_tensor.data<float>();
  } else {
105
    tensor_data2 = const_cast<float*>(tensor2->data<float>());
106
  }
Y
Yi Wang 已提交
107
  const int64_t* rows_data2 = rows2->data();
108

109 110 111
  for (int i = 0; i < tensor_numel; ++i) {
    EXPECT_FLOAT_EQ(tensor_data2[i], 32.7);
  }
T
typhoonzero 已提交
112
  for (size_t i = 0; i < rows2->size(); ++i) {
Y
Yancey1989 已提交
113
    EXPECT_EQ(rows_data2[i], static_cast<int64_t>(i));
T
typhoonzero 已提交
114
  }
115
  EXPECT_EQ(slr2->height(), 1000);
116 117
}

118
void RunTestLodTensor(platform::Place place, int from_type = 0) {
119 120
  // serialize var to ByteBuffer
  framework::Variable var;
121
  auto* tensor = var.GetMutable<framework::LoDTensor>();
T
typhoonzero 已提交
122
  tensor->Resize(framework::make_ddim({512, 8, 4, 2}));
123 124 125
  framework::LoD lod;
  lod.push_back(framework::Vector<size_t>({1, 3, 8}));
  tensor->set_lod(lod);
T
typhoonzero 已提交
126
  int tensor_numel = 512 * 8 * 4 * 2;
127 128
  platform::DeviceContextPool& pool = platform::DeviceContextPool::Instance();
  auto& ctx = *pool.Get(place);
129
  tensor->mutable_data<float>(place);
130
  math::set_constant(ctx, tensor, 31.9);
131 132

  ::grpc::ByteBuffer msg;
133
  operators::distributed::SerializeToByteBuffer("myvar", &var, ctx, &msg);
134
  EXPECT_GT(msg.Length(), static_cast<size_t>(0));
135 136 137 138 139 140 141 142 143 144 145

  // deserialize
  std::vector<::grpc::Slice> slices;
  (void)msg.Dump(&slices);
  std::string tmp;
  for (const auto& s : slices) {
    tmp.append(reinterpret_cast<const char*>(s.begin()), s.size());
  }
  sendrecv::VariableMessage varmsg;
  EXPECT_TRUE(varmsg.ParseFromString(tmp));
  EXPECT_EQ(varmsg.varname(), "myvar");
146
  EXPECT_EQ(varmsg.type(), 0);
T
typhoonzero 已提交
147
  EXPECT_EQ(varmsg.dims()[0], 512);
148 149 150 151 152 153 154
  EXPECT_EQ(varmsg.dims()[1], 8);
  EXPECT_EQ(varmsg.dims()[2], 4);
  EXPECT_EQ(varmsg.dims()[3], 2);
  EXPECT_EQ(varmsg.lod_level(), 1);
  EXPECT_EQ(varmsg.lod(0).lod_data(0), 1);
  EXPECT_EQ(varmsg.lod(0).lod_data(1), 3);
  EXPECT_EQ(varmsg.lod(0).lod_data(2), 8);
155 156 157 158

  const float* tensor_data =
      reinterpret_cast<const float*>(varmsg.serialized().data());
  for (int i = 0; i < tensor_numel; ++i) {
159
    EXPECT_FLOAT_EQ(tensor_data[i], 31.9);
160
  }
161 162 163 164 165 166 167 168 169 170 171 172

  // message binary
  std::string str;
  varmsg.SerializeToString(&str);

  // message bytebuffer
  ::grpc::Slice slices_2[1];
  int num_slices = 1;
  slices_2[0] = ::grpc::Slice(str.length());
  memcpy(const_cast<uint8_t*>(slices_2[0].begin()), str.c_str(), str.length());
  ::grpc::ByteBuffer bytebuffer2(&slices_2[0], num_slices);

173
  // deserialize zero-copy
174 175
  framework::Scope scope;
  scope.Var("myvar");
176
  operators::distributed::GRPCVariableResponse resp(&scope, &ctx);
177 178 179 180 181
  if (from_type == 0) {
    EXPECT_EQ(resp.Parse(msg), 0);
  } else {
    EXPECT_EQ(resp.Parse(bytebuffer2), 0);
  }
182

183 184 185
  framework::Variable* var2 = resp.GetVar();

  auto tensor2 = var2->Get<framework::LoDTensor>();
186 187 188 189 190
  float* tensor_data2 = nullptr;
  framework::Tensor tmp_tensor;

  if (platform::is_gpu_place(ctx.GetPlace())) {
    platform::CPUPlace cpu;
191
    framework::TensorCopy(tensor2, cpu, &tmp_tensor);
192 193
    tensor_data2 = tmp_tensor.data<float>();
  } else {
194
    tensor_data2 = const_cast<float*>(tensor2.data<float>());
195 196
  }

197 198 199 200 201 202 203
  EXPECT_EQ(varmsg.lod_level(), 1);
  EXPECT_EQ(varmsg.lod(0).lod_data(0), 1);
  EXPECT_EQ(varmsg.lod(0).lod_data(1), 3);
  EXPECT_EQ(varmsg.lod(0).lod_data(2), 8);
  for (int i = 0; i < tensor_numel; ++i) EXPECT_FLOAT_EQ(tensor_data2[i], 31.9);
}

Y
Yancey 已提交
204 205
TEST(LodTensor, Run) {
  platform::CPUPlace place;
206 207
  RunTestLodTensor(place);
  RunTestLodTensor(place, 1);
Y
Yancey 已提交
208
#ifdef PADDLE_WITH_CUDA
Y
yi.wu 已提交
209 210 211
  platform::CUDAPlace gpu(0);
  RunTestLodTensor(gpu);
  RunTestLodTensor(gpu, 1);
Y
Yancey 已提交
212
#endif
213 214
}

Y
Yancey 已提交
215
TEST(SelectedRows, Run) {
216 217
  platform::CPUPlace place;
  RunSerdeTestSelectedRows(place);
218

Y
Yancey 已提交
219
#ifdef PADDLE_WITH_CUDA
Y
yi.wu 已提交
220 221
  platform::CUDAPlace gpu;
  RunSerdeTestSelectedRows(gpu);
Y
Yancey 已提交
222
#endif
223
}