Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into my_unpool_max_2d
commit
821899ccd4
@ -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,66 @@
|
||||
# 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)
|
||||
IF(APPLE)
|
||||
SET(BUILD_CMD make -n | sed "s/-Werror//g" | sh)
|
||||
ELSE()
|
||||
SET(BUILD_CMD make)
|
||||
ENDIF()
|
||||
|
||||
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
|
||||
# NOTE(yuyang18):
|
||||
# Disable -Werror, otherwise the compile will fail in MacOS.
|
||||
# It seems that we cannot configure that by make command.
|
||||
# Just dry run make command and remove `-Werror`, then use a shell to run make commands
|
||||
BUILD_COMMAND ${BUILD_CMD}
|
||||
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,35 @@
|
||||
/* 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
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in new issue