Send recv op (#5520)
* WIP send recv op * WIP send recv * put grpc impl in details * put grpc impl in details * update wip * update proto * update proto * update proto * clean cmake * wip on op implementations * wip on op implementations * compile ok adding ut * wip unitest * add extern cares for linking * wip add ut * working version send recv * revert optimizer.py * update test cmake * add libtool to dockerfile * update cmake dependency * update cmake depends * update cmake grpc depends * fix cmake dependency * fix compile error * fix compile * follow comments * update * update copyfromrelease/0.11.0
parent
dc82a30908
commit
0a8a86e0c9
@ -0,0 +1,45 @@
|
||||
# Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.
|
||||
#
|
||||
# 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.
|
||||
#
|
||||
|
||||
IF(MOBILE_INFERENCE)
|
||||
return()
|
||||
ENDIF()
|
||||
|
||||
include (ExternalProject)
|
||||
|
||||
# NOTE: c-ares is needed when linking with grpc.
|
||||
|
||||
SET(CARES_SOURCES_DIR ${THIRD_PARTY_PATH}/cares)
|
||||
SET(CARES_INSTALL_DIR ${THIRD_PARTY_PATH}/install/cares)
|
||||
SET(CARES_INCLUDE_DIR "${CARES_INSTALL_DIR}/include/" CACHE PATH "cares include directory." FORCE)
|
||||
|
||||
ExternalProject_Add(
|
||||
extern_cares
|
||||
GIT_REPOSITORY "https://github.com/c-ares/c-ares.git"
|
||||
GIT_TAG "cares-1_13_0"
|
||||
PREFIX ${CARES_SOURCES_DIR}
|
||||
UPDATE_COMMAND ""
|
||||
CONFIGURE_COMMAND ./buildconf && ./configure --disable-shared --prefix=${CARES_INSTALL_DIR}
|
||||
BUILD_IN_SOURCE 1
|
||||
BUILD_COMMAND make
|
||||
INSTALL_COMMAND make install
|
||||
)
|
||||
|
||||
ADD_LIBRARY(cares STATIC IMPORTED GLOBAL)
|
||||
SET_PROPERTY(TARGET cares PROPERTY IMPORTED_LOCATION
|
||||
"${CARES_INSTALL_DIR}/lib/libcares.a")
|
||||
|
||||
include_directories(${CARES_INCLUDE_DIR})
|
||||
ADD_DEPENDENCIES(cares extern_cares)
|
@ -0,0 +1,58 @@
|
||||
# Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.
|
||||
#
|
||||
# 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.
|
||||
#
|
||||
|
||||
IF(MOBILE_INFERENCE)
|
||||
return()
|
||||
ENDIF()
|
||||
|
||||
include (ExternalProject)
|
||||
|
||||
SET(GRPC_SOURCES_DIR ${THIRD_PARTY_PATH}/grpc)
|
||||
SET(GRPC_INSTALL_DIR ${THIRD_PARTY_PATH}/install/grpc)
|
||||
SET(GRPC_INCLUDE_DIR "${GRPC_INSTALL_DIR}/include/" CACHE PATH "grpc include directory." FORCE)
|
||||
SET(GRPC_CPP_PLUGIN "${GRPC_INSTALL_DIR}/bin/grpc_cpp_plugin" CACHE FILEPATH "GRPC_CPP_PLUGIN" FORCE)
|
||||
|
||||
ExternalProject_Add(
|
||||
extern_grpc
|
||||
DEPENDS protobuf zlib
|
||||
GIT_REPOSITORY "https://github.com/grpc/grpc.git"
|
||||
GIT_TAG "v1.7.x"
|
||||
PREFIX ${GRPC_SOURCES_DIR}
|
||||
UPDATE_COMMAND ""
|
||||
CONFIGURE_COMMAND ""
|
||||
BUILD_IN_SOURCE 1
|
||||
BUILD_COMMAND make
|
||||
INSTALL_COMMAND make prefix=${GRPC_INSTALL_DIR} install
|
||||
)
|
||||
|
||||
# FIXME(typhoonzero): hack to get static lib path, try a better way like merge them.
|
||||
ADD_LIBRARY(grpc++_unsecure STATIC IMPORTED GLOBAL)
|
||||
SET_PROPERTY(TARGET grpc++_unsecure PROPERTY IMPORTED_LOCATION
|
||||
"${GRPC_INSTALL_DIR}/lib/libgrpc++_unsecure.a")
|
||||
|
||||
ADD_LIBRARY(grpc++ STATIC IMPORTED GLOBAL)
|
||||
SET_PROPERTY(TARGET grpc++ PROPERTY IMPORTED_LOCATION
|
||||
"${GRPC_INSTALL_DIR}/lib/libgrpc++.a")
|
||||
ADD_LIBRARY(gpr STATIC IMPORTED GLOBAL)
|
||||
SET_PROPERTY(TARGET gpr PROPERTY IMPORTED_LOCATION
|
||||
"${GRPC_INSTALL_DIR}/lib/libgpr.a")
|
||||
|
||||
ADD_LIBRARY(grpc_unsecure STATIC IMPORTED GLOBAL)
|
||||
SET_PROPERTY(TARGET grpc_unsecure PROPERTY IMPORTED_LOCATION
|
||||
"${GRPC_INSTALL_DIR}/lib/libgrpc_unsecure.a")
|
||||
|
||||
include_directories(${GRPC_INCLUDE_DIR})
|
||||
ADD_DEPENDENCIES(grpc++_unsecure extern_grpc)
|
||||
|
@ -0,0 +1 @@
|
||||
grpc_library(sendrecvop_grpc SRCS recv_impl.cc send_impl.cc PROTO send_recv.proto DEPS lod_tensor selected_rows)
|
@ -0,0 +1,44 @@
|
||||
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.
|
||||
|
||||
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 "send_recv_impl.h"
|
||||
|
||||
namespace paddle {
|
||||
namespace operators {
|
||||
namespace detail {
|
||||
|
||||
Status SendRecvServerImpl::SendVariable(ServerContext *context,
|
||||
const VariableMessage *in_var,
|
||||
VariableMessage *out_var) {
|
||||
framework::LoDTensor t;
|
||||
// TODO(typhoonzero): desirealize in_tensor and run pserver network.
|
||||
std::istringstream iss(in_var->serialized());
|
||||
framework::DeserializeFromStream(iss, &t);
|
||||
lodtensor_queue_.Push(std::move(t));
|
||||
// Block util the sub graph is done.
|
||||
t = lodtensor_return_queue_.Pop();
|
||||
std::ostringstream oss;
|
||||
// FIXME(typhoonzero): get context from op.
|
||||
framework::SerializeToStream(oss, t, platform::CPUDeviceContext());
|
||||
std::string *varname = out_var->mutable_varname();
|
||||
*varname = in_var->varname();
|
||||
std::string *serialized = out_var->mutable_serialized();
|
||||
*serialized = oss.str();
|
||||
|
||||
return Status::OK;
|
||||
}
|
||||
|
||||
} // namespace detail
|
||||
} // namespace operators
|
||||
} // namespace paddle
|
@ -0,0 +1,54 @@
|
||||
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.
|
||||
|
||||
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 "send_recv_impl.h"
|
||||
|
||||
namespace paddle {
|
||||
namespace operators {
|
||||
namespace detail {
|
||||
|
||||
bool RPCClient::SendVariable(const framework::Scope& scope,
|
||||
const std::string& inname,
|
||||
const std::string& outname) {
|
||||
ClientContext context;
|
||||
VariableMessage msg, out_msg;
|
||||
// FIXME(typhoonzero): pass device context to here.
|
||||
auto ctx = platform::CPUDeviceContext();
|
||||
auto* var = scope.FindVar(inname);
|
||||
PADDLE_ENFORCE(var);
|
||||
// TODO(typhoonzero): support SelectedRows
|
||||
PADDLE_ENFORCE(var->IsType<framework::LoDTensor>(),
|
||||
"Only support LoDTensor, %s has wrong type", inname);
|
||||
const framework::LoDTensor& tensor = var->Get<framework::LoDTensor>();
|
||||
std::ostringstream oss;
|
||||
framework::SerializeToStream(oss, tensor, ctx);
|
||||
msg.set_varname(inname);
|
||||
msg.set_serialized(oss.str());
|
||||
Status status = stub_->SendVariable(&context, msg, &out_msg);
|
||||
if (!status.ok()) {
|
||||
return false;
|
||||
}
|
||||
std::istringstream iss(out_msg.serialized());
|
||||
framework::LoDTensor ret_tensor;
|
||||
framework::DeserializeFromStream(iss, &ret_tensor);
|
||||
auto* outvar = scope.FindVar(outname);
|
||||
framework::LoDTensor* out_tensor = outvar->GetMutable<framework::LoDTensor>();
|
||||
// FIXME(typhoonzero): do not copy.
|
||||
framework::CopyFrom(ret_tensor, ctx.GetPlace(), ctx, out_tensor);
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace detail
|
||||
} // namespace operators
|
||||
} // namespace paddle
|
@ -0,0 +1,37 @@
|
||||
/* 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. */
|
||||
|
||||
syntax = "proto3";
|
||||
|
||||
package sendrecv;
|
||||
|
||||
service SendRecvService {
|
||||
// For parameter server round-robin like hashing, do not split tensors.
|
||||
// Send and recv only one tensor
|
||||
rpc SendVariable(VariableMessage) returns (VariableMessage) {}
|
||||
}
|
||||
|
||||
// VariableMessage is serialized paddle variable message.
|
||||
// It can be:
|
||||
// Tensor
|
||||
// LoDTensor
|
||||
// SelectedRows
|
||||
message VariableMessage {
|
||||
string varname = 1;
|
||||
bytes serialized = 2;
|
||||
}
|
||||
|
||||
message VoidMessage {
|
||||
|
||||
}
|
@ -0,0 +1,87 @@
|
||||
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.
|
||||
|
||||
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. */
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "paddle/framework/data_type.h"
|
||||
#include "paddle/framework/lod_tensor.h"
|
||||
#include "paddle/framework/scope.h"
|
||||
#include "paddle/framework/selected_rows.h"
|
||||
#include "paddle/operators/detail/simple_block_queue.h"
|
||||
|
||||
// #include <grpc++/channel.h>
|
||||
// #include <grpc++/client_context.h>
|
||||
// #include <grpc++/create_channel.h>
|
||||
// #include <grpc++/security/credentials.h>
|
||||
#include "paddle/operators/detail/send_recv.grpc.pb.h"
|
||||
#include "paddle/operators/detail/send_recv.pb.h"
|
||||
|
||||
#include <grpc++/grpc++.h>
|
||||
|
||||
using grpc::Channel;
|
||||
using grpc::Server;
|
||||
using grpc::ServerContext;
|
||||
using grpc::ServerReader;
|
||||
using grpc::ServerBuilder;
|
||||
|
||||
using grpc::ClientContext;
|
||||
using grpc::ClientReader;
|
||||
using grpc::ClientReaderWriter;
|
||||
using grpc::ClientWriter;
|
||||
using grpc::Status;
|
||||
using sendrecv::SendRecvService;
|
||||
using sendrecv::VariableMessage;
|
||||
using sendrecv::VoidMessage;
|
||||
|
||||
namespace paddle {
|
||||
namespace operators {
|
||||
namespace detail {
|
||||
|
||||
class SendRecvServerImpl final : public SendRecvService::Service {
|
||||
public:
|
||||
explicit SendRecvServerImpl() {}
|
||||
|
||||
Status SendVariable(ServerContext *context, const VariableMessage *in_var,
|
||||
VariableMessage *out_var) override;
|
||||
|
||||
const framework::LoDTensor Get() { return this->lodtensor_queue_.Pop(); }
|
||||
|
||||
void Push(const framework::LoDTensor &tensor) {
|
||||
this->lodtensor_return_queue_.Push(tensor);
|
||||
}
|
||||
|
||||
private:
|
||||
SimpleBlockQueue<framework::LoDTensor> lodtensor_queue_;
|
||||
SimpleBlockQueue<framework::LoDTensor> lodtensor_return_queue_;
|
||||
SimpleBlockQueue<framework::SelectedRows> selected_rows_queue_;
|
||||
SimpleBlockQueue<framework::SelectedRows> selected_rows_return_queue_;
|
||||
};
|
||||
|
||||
// RPCClient is a class to send tensors to pserver sub-network
|
||||
// using different hashing methods.
|
||||
class RPCClient {
|
||||
public:
|
||||
RPCClient(std::shared_ptr<Channel> channel)
|
||||
: stub_(SendRecvService::NewStub(channel)) {}
|
||||
|
||||
bool SendVariable(const framework::Scope &scope, const std::string &inname,
|
||||
const std::string &outname);
|
||||
|
||||
private:
|
||||
std::unique_ptr<SendRecvService::Stub> stub_;
|
||||
};
|
||||
|
||||
} // namespace detail
|
||||
} // namespace operators
|
||||
} // namespace paddle
|
@ -0,0 +1,52 @@
|
||||
/* 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. */
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <condition_variable>
|
||||
#include <deque>
|
||||
#include <mutex>
|
||||
|
||||
namespace paddle {
|
||||
namespace operators {
|
||||
namespace detail {
|
||||
|
||||
template <typename T>
|
||||
class SimpleBlockQueue {
|
||||
private:
|
||||
std::mutex mutex_;
|
||||
std::condition_variable condition_;
|
||||
std::deque<T> queue_;
|
||||
|
||||
public:
|
||||
void Push(T const& value) {
|
||||
{
|
||||
std::unique_lock<std::mutex> lock(this->mutex_);
|
||||
queue_.push_front(value);
|
||||
}
|
||||
this->condition_.notify_one();
|
||||
}
|
||||
|
||||
T Pop() {
|
||||
std::unique_lock<std::mutex> lock(this->mutex_);
|
||||
this->condition_.wait(lock, [=] { return !this->queue_.empty(); });
|
||||
T rc(std::move(this->queue_.back()));
|
||||
this->queue_.pop_back();
|
||||
return rc;
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace detail
|
||||
} // namespace operators
|
||||
} // namespace paddle
|
@ -0,0 +1,121 @@
|
||||
/* 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 <stdint.h>
|
||||
#include <sys/stat.h>
|
||||
#include <ostream>
|
||||
#include <thread>
|
||||
|
||||
#include <unistd.h>
|
||||
|
||||
#include "paddle/framework/data_type.h"
|
||||
#include "paddle/framework/executor.h"
|
||||
#include "paddle/framework/framework.pb.h"
|
||||
#include "paddle/framework/lod_tensor.h"
|
||||
#include "paddle/framework/op_registry.h"
|
||||
#include "paddle/operators/detail/send_recv_impl.h"
|
||||
#include "paddle/operators/detail/simple_block_queue.h"
|
||||
|
||||
namespace paddle {
|
||||
namespace operators {
|
||||
|
||||
void RunServer(Server **rpc_server,
|
||||
std::shared_ptr<detail::SendRecvServerImpl> service,
|
||||
const std::string &server_address) {
|
||||
ServerBuilder builder;
|
||||
builder.AddListeningPort(server_address, grpc::InsecureServerCredentials());
|
||||
builder.RegisterService(service.get());
|
||||
std::unique_ptr<Server> server(builder.BuildAndStart());
|
||||
*rpc_server = server.get();
|
||||
LOG(INFO) << "Server listening on " << server_address << std::endl;
|
||||
server->Wait();
|
||||
}
|
||||
|
||||
class RecvOp : public framework::OperatorBase {
|
||||
public:
|
||||
RecvOp(const std::string &type, const framework::VariableNameMap &inputs,
|
||||
const framework::VariableNameMap &outputs,
|
||||
const framework::AttributeMap &attrs)
|
||||
: OperatorBase(type, inputs, outputs, attrs) {
|
||||
if (!rpc_service_) {
|
||||
rpc_service_.reset(new detail::SendRecvServerImpl());
|
||||
std::string endpoint = Attr<std::string>("endpoint");
|
||||
server_thread_.reset(
|
||||
new std::thread(RunServer, &rpc_server_, rpc_service_, endpoint));
|
||||
}
|
||||
}
|
||||
|
||||
virtual ~RecvOp() {
|
||||
rpc_server_->Shutdown();
|
||||
server_thread_->join();
|
||||
}
|
||||
|
||||
void Run(const framework::Scope &scope,
|
||||
const platform::DeviceContext &dev_ctx) const override {
|
||||
// blocking get one var from client.
|
||||
const framework::LoDTensor &t = rpc_service_->Get();
|
||||
framework::Scope &recv_scope = scope.NewScope();
|
||||
// set graph input var
|
||||
auto *var = recv_scope.Var(Input("RX"));
|
||||
auto *tensor = var->GetMutable<framework::LoDTensor>();
|
||||
// FIXME(typhoonzero): do not copy
|
||||
framework::CopyFrom(t, dev_ctx.GetPlace(), dev_ctx, tensor);
|
||||
|
||||
auto *block = Attr<framework::BlockDescBind *>("OptimizeBlock");
|
||||
auto *program = block->Program();
|
||||
framework::Executor executor(dev_ctx);
|
||||
// Run sub graph to get optimized tensor
|
||||
executor.Run(*program, &recv_scope, block->ID(),
|
||||
false /*create_local_scope*/);
|
||||
|
||||
auto *out_var = recv_scope.FindVar("Out");
|
||||
// push back
|
||||
rpc_service_->Push(out_var->Get<framework::LoDTensor>());
|
||||
}
|
||||
|
||||
protected:
|
||||
// grpc server instance to track status and gracefully shutdown.
|
||||
// borrow an pointer from server thread.
|
||||
Server *rpc_server_{nullptr};
|
||||
// grpc send/recv service implement to register.
|
||||
std::shared_ptr<detail::SendRecvServerImpl> rpc_service_;
|
||||
std::shared_ptr<std::thread> server_thread_;
|
||||
};
|
||||
|
||||
class RecvOpMaker : public framework::OpProtoAndCheckerMaker {
|
||||
public:
|
||||
RecvOpMaker(framework::OpProto *proto, framework::OpAttrChecker *op_checker)
|
||||
: OpProtoAndCheckerMaker(proto, op_checker) {
|
||||
AddInput("RX", "(Tensor) Input tensor to be saved");
|
||||
AddComment(R"DOC(
|
||||
Recv operator
|
||||
|
||||
This operator will recv tensor from send_op
|
||||
)DOC");
|
||||
AddAttr<std::string>("endpoint",
|
||||
"(string, default 127.0.0.1:6164)"
|
||||
"IP address to listen on.")
|
||||
.SetDefault("127.0.0.1:6164")
|
||||
.AddCustomChecker([](const std::string &ip) { return !ip.empty(); });
|
||||
AddAttr<framework::BlockDescBind *>("OptimizeBlock", "type BlockDescBind*",
|
||||
"optimize network run in server");
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace operators
|
||||
} // namespace paddle
|
||||
|
||||
namespace ops = paddle::operators;
|
||||
|
||||
REGISTER_OPERATOR(recv, ops::RecvOp, ops::RecvOpMaker);
|
@ -0,0 +1,84 @@
|
||||
/* 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 <ostream>
|
||||
|
||||
#include "paddle/framework/data_type.h"
|
||||
#include "paddle/framework/framework.pb.h"
|
||||
#include "paddle/framework/lod_tensor.h"
|
||||
#include "paddle/framework/op_registry.h"
|
||||
|
||||
#include "paddle/operators/detail/send_recv_impl.h"
|
||||
#include "paddle/operators/detail/simple_block_queue.h"
|
||||
|
||||
namespace paddle {
|
||||
namespace operators {
|
||||
|
||||
// TODO(typhoonzero): this is a simple implementation which only send
|
||||
// one tensor
|
||||
class SendOp : public framework::OperatorBase {
|
||||
public:
|
||||
SendOp(const std::string &type, const framework::VariableNameMap &inputs,
|
||||
const framework::VariableNameMap &outputs,
|
||||
const framework::AttributeMap &attrs)
|
||||
: OperatorBase(type, inputs, outputs, attrs) {
|
||||
// init client when the operator is created at runtime.
|
||||
if (!client_) {
|
||||
std::string endpoint = Attr<std::string>("endpoint");
|
||||
client_.reset(new detail::RPCClient(
|
||||
grpc::CreateChannel(endpoint, grpc::InsecureChannelCredentials())));
|
||||
// TODO(typhoonzero): how to call InitVariables
|
||||
}
|
||||
}
|
||||
void Run(const framework::Scope &scope,
|
||||
const platform::DeviceContext &dev_ctx) const override {
|
||||
auto iname = Input("X");
|
||||
auto oname = Output("Out");
|
||||
// TODO(typhoonzero): currently it's non-blocking,
|
||||
// should block until server responds.
|
||||
bool ret = client_->SendVariable(scope, iname, oname);
|
||||
if (!ret) {
|
||||
LOG(ERROR) << "send variable error";
|
||||
}
|
||||
}
|
||||
|
||||
protected:
|
||||
std::shared_ptr<detail::RPCClient> client_{nullptr};
|
||||
};
|
||||
|
||||
class SendOpMaker : public framework::OpProtoAndCheckerMaker {
|
||||
public:
|
||||
SendOpMaker(framework::OpProto *proto, framework::OpAttrChecker *op_checker)
|
||||
: OpProtoAndCheckerMaker(proto, op_checker) {
|
||||
AddInput("X", "(Tensor) Input tensor to be saved");
|
||||
AddOutput("Out", "(Tensor) Output fetched from server");
|
||||
AddComment(R"DOC(
|
||||
Recv operator
|
||||
|
||||
This operator will recv tensor from send_op
|
||||
)DOC");
|
||||
AddAttr<std::string>("endpoint",
|
||||
"(string, default 127.0.0.1:6164)"
|
||||
"IP address to listen on.")
|
||||
.SetDefault("127.0.0.1:6164")
|
||||
.AddCustomChecker([](const std::string &ip) { return !ip.empty(); });
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace operators
|
||||
} // namespace paddle
|
||||
|
||||
namespace ops = paddle::operators;
|
||||
|
||||
REGISTER_OPERATOR(send, ops::SendOp, ops::SendOpMaker);
|
@ -0,0 +1,125 @@
|
||||
/* 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. */
|
||||
|
||||
// TODO(typhoonzero): add python bindings for this test as
|
||||
// a RemoteOptimizer.
|
||||
|
||||
#include <unistd.h>
|
||||
#include <thread>
|
||||
|
||||
#include "gtest/gtest.h"
|
||||
#include "paddle/framework/op_registry.h"
|
||||
#include "paddle/framework/operator.h"
|
||||
#include "paddle/framework/program_desc.h"
|
||||
|
||||
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);
|
||||
auto var = scope.Var("X");
|
||||
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);
|
||||
}
|
||||
|
||||
auto out_var = scope.Var("Out");
|
||||
auto out_tensor = out_var->GetMutable<paddle::framework::LoDTensor>();
|
||||
out_tensor->Resize({10, 10});
|
||||
tensor->mutable_data<float>(place); // allocate
|
||||
}
|
||||
|
||||
void AddOp(const std::string &type,
|
||||
const paddle::framework::VariableNameMap &inputs,
|
||||
const paddle::framework::VariableNameMap &outputs,
|
||||
paddle::framework::AttributeMap attrs,
|
||||
paddle::framework::BlockDescBind *block) {
|
||||
// insert output
|
||||
for (auto kv : outputs) {
|
||||
for (auto v : kv.second) {
|
||||
auto var = block->Var(v);
|
||||
var->SetDataType(paddle::framework::DataType::FP32);
|
||||
}
|
||||
}
|
||||
|
||||
// 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
|
||||
paddle::framework::ProgramDescBind program;
|
||||
paddle::framework::BlockDescBind *block = program.MutableBlock(0);
|
||||
// X for server side tensors, RX for received tensers, must be of same shape.
|
||||
AddOp("sum", {{"X", {"X", "RX"}}}, {{"Out", {"Out"}}}, {}, block);
|
||||
|
||||
paddle::framework::AttributeMap attrs;
|
||||
attrs.insert({"endpoint", std::string("127.0.0.1:6174")});
|
||||
attrs.insert({"OptimizeBlock", block});
|
||||
recv_op = paddle::framework::OpRegistry::CreateOp("recv", {{"RX", {"RX"}}},
|
||||
{{"Out", {"Out"}}}, attrs);
|
||||
paddle::platform::CPUDeviceContext ctx(place);
|
||||
recv_op->Run(scope, ctx);
|
||||
}
|
||||
|
||||
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;
|
||||
attrs.insert({"endpoint", std::string("127.0.0.1:6174")});
|
||||
|
||||
auto send_op = paddle::framework::OpRegistry::CreateOp(
|
||||
"send", {{"X", {"X"}}}, {{"Out", {"Out"}}}, attrs);
|
||||
paddle::platform::CPUDeviceContext ctx(place);
|
||||
send_op->Run(scope, ctx);
|
||||
|
||||
auto in_var = scope.Var("X");
|
||||
auto tensor = in_var->GetMutable<paddle::framework::LoDTensor>();
|
||||
float *expected = tensor->data<float>();
|
||||
|
||||
auto out_var = scope.Var("Out");
|
||||
auto target = out_var->GetMutable<paddle::framework::LoDTensor>();
|
||||
// send fail cause output is none.
|
||||
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]);
|
||||
}
|
||||
recv_op.reset(); // dtor can shutdown and join server thread.
|
||||
server_thread.join();
|
||||
}
|
Loading…
Reference in new issue