Skip to content
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

[DO NOT REVIEW]Feature/cpp benchmark #10624

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ BasedOnStyle: Google
IndentWidth: 2
TabWidth: 2
ContinuationIndentWidth: 4
AccessModifierOffset: -2 # The private/protected/public has no indent in class
AccessModifierOffset: -1 # The private/protected/public has 1 indent in class
Standard: Cpp11
AllowAllParametersOfDeclarationOnNextLine: true
BinPackParameters: false
Expand Down
4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,10 @@ if(WITH_MKLDNN)
list(APPEND EXTERNAL_LIBS ${MKLDNN_LIB})
endif()

if(WITH_TESTING)
add_subdirectory(tools)
endif(WITH_TESTING)

if(USE_NNPACK)
include(external/nnpack)
list(APPEND EXTERNAL_LIBS ${NNPACK_LIBS})
Expand Down
2 changes: 2 additions & 0 deletions tools/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
cc_library(benchmark SRCS benchmark.cc DEPS lod_tensor op_registry device_context init)
cc_test(benchmark_cpu SRCS benchmark_cpu.cc DEPS benchmark sum_op)
38 changes: 38 additions & 0 deletions tools/benchmark.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright (c) 2018 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 <sys/time.h>
#include <time.h>

#include "tools/benchmark.h"

namespace paddle {
namespace framework {

inline uint64_t NanoTime() {
struct timeval tv;
gettimeofday(&tv, nullptr);
return 1000 * (static_cast<uint64_t>(tv.tv_sec) * 1000000 + tv.tv_usec);
}

template <typename DeviceContext>
void Benchmark<DeviceContext>::Register(const char* op) {
auto& op_info = OpInfoMap::Instance().Get(op);
}

template <typename DeviceContext>
void Benchmark<DeviceContext>::Run(int iters) const {}

} // namespace framework
} // namespace paddle
54 changes: 54 additions & 0 deletions tools/benchmark.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Copyright (c) 2018 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 <functional>
#include <memory>
#include <string>
#include <unordered_map>
#include <vector>

#include "paddle/fluid/framework/op_registry.h"
#include "paddle/fluid/framework/operator.h"

namespace paddle {
namespace framework {
/**
* @brief benchmark the Kernel performance on single thread single GPU.
*/

// Get current time point in ns.
inline uint64_t NanoTime();

template <typename DeviceContext>
class Benchmark {
public:
explicit Benchmark(const char* name) : name_(name) {}
void Register(const char* op);
void Run(int iters) const;
// void RunRepeats() const;

private:
std::string name_;
std::vector<std::unique_ptr<OperatorBase>> ops_;
};

// NOTE(dzhwinter): benchmark only support OpWithkernel
#define TEST_OP_CPU(op_name, iters) \
USE_OP(#op_name); \
PADDLE_ENFORCE(OpInfoMap::Instance().Has(#op_name)); \
Benchmark<platform::CPUDeviceContext> bench(#op_name);

} // namespace framework
} // namespace paddle
69 changes: 69 additions & 0 deletions tools/benchmark_cpu.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// Copyright (c) 2018 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 "tools/benchmark.h"
#include "paddle/fluid/framework/init.h"

#include "gtest/gtest.h"

using namespace paddle::framework;
using namespace paddle::platform;

USE_OP(sum);
void test_mul_op() {
Scope scope;
CPUPlace place;
{
auto var = scope.Var("X");
auto x = var->GetMutable<LoDTensor>();
x->Resize({10, 10});
float *expect = x->mutable_data<float>(place);
for (int64_t i = 0; i < x->numel(); ++i) {
expect[i] = static_cast<float>(i);
}
}
{
auto var = scope.Var("Y");
auto x = var->GetMutable<LoDTensor>();
x->Resize({10, 10});
float *expect = x->mutable_data<float>(place);
for (int64_t i = 0; i < x->numel(); ++i) {
expect[i] = static_cast<float>(i);
}
}

{
auto out_var = scope.Var("Out");
out_var->GetMutable<LoDTensor>();
}
AttributeMap attrs;
auto op = OpRegistry::CreateOp(
"sum", {{"X", {"X", "Y"}}}, {{"Out", {"Out"}}}, attrs);
op->Run(scope, place);

// check output
Tensor out;
TensorCopySync(scope.Var("Out")->Get<LoDTensor>(), place, &out);
float *expect = out.data<float>();
for (int64_t i = 0; i < out.numel(); ++i) {
// expect[i] = static_cast<float>(i);
EXPECT_EQ(expect[i], 2 * static_cast<float>(i));
}
}

int main() {
InitDevices(false /* disable p2p*/);
test_mul_op();
return 0;
}