-
Notifications
You must be signed in to change notification settings - Fork 5.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add grpc benchmark #6176
Closed
Closed
Add grpc benchmark #6176
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
/* 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 <iostream> | ||
#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; | ||
int benchmark_count = 1000; | ||
// FIXME(typhoonzero): protobuf message size limits the maximum tensor size | ||
int mat_size = 512; | ||
|
||
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({mat_size, mat_size}); | ||
float *expect = tensor->mutable_data<float>(place); | ||
for (int64_t i = 0; i < tensor->numel(); ++i) { | ||
expect[i] = static_cast<float>(i) / 1000.0f; | ||
} | ||
|
||
auto out_var = scope.Var("Out"); | ||
auto out_tensor = out_var->GetMutable<paddle::framework::LoDTensor>(); | ||
out_tensor->Resize({mat_size, mat_size}); | ||
out_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(SendRecvBenchmark, 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); | ||
|
||
for (int i = 0; i < benchmark_count; ++i) { | ||
send_op->Run(scope, ctx); | ||
} | ||
|
||
recv_op.reset(); // dtor can shutdown and join server thread. | ||
server_thread.join(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As a benchmark, we can run multi times with different tensor size.