-
Notifications
You must be signed in to change notification settings - Fork 1.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 Python bindings. #957
Merged
Merged
Add Python bindings. #957
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
exports_files(glob(["*.BUILD"])) | ||
exports_files(["build_defs.bzl"]) | ||
|
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,38 @@ | ||
load("//bindings/python:build_defs.bzl", "py_extension") | ||
|
||
py_library( | ||
name = "benchmark", | ||
srcs = ["__init__.py"], | ||
visibility = ["//visibility:public"], | ||
deps = [ | ||
":_benchmark", | ||
# pip; absl:app | ||
], | ||
) | ||
|
||
py_extension( | ||
name = "_benchmark", | ||
srcs = ["benchmark.cc"], | ||
copts = [ | ||
"-fexceptions", | ||
"-fno-strict-aliasing", | ||
], | ||
features = ["-use_header_modules"], | ||
deps = [ | ||
"//:benchmark", | ||
"@pybind11", | ||
"@python_headers", | ||
], | ||
) | ||
|
||
py_test( | ||
name = "example", | ||
srcs = ["example.py"], | ||
python_version = "PY3", | ||
srcs_version = "PY3", | ||
visibility = ["//visibility:public"], | ||
deps = [ | ||
":benchmark", | ||
], | ||
) | ||
|
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,62 @@ | ||
# Copyright 2020 Google Inc. 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. | ||
"""Python benchmarking utilities. | ||
|
||
Example usage: | ||
import benchmark | ||
|
||
@benchmark.register | ||
def my_benchmark(state): | ||
... # Code executed outside `while` loop is not timed. | ||
|
||
while state: | ||
... # Code executed within `while` loop is timed. | ||
|
||
if __name__ == '__main__': | ||
benchmark.main() | ||
""" | ||
|
||
from absl import app | ||
from benchmark import _benchmark | ||
|
||
__all__ = [ | ||
"register", | ||
"main", | ||
] | ||
|
||
__version__ = "0.1.0" | ||
|
||
|
||
def register(f=None, *, name=None): | ||
if f is None: | ||
return lambda f: register(f, name=name) | ||
if name is None: | ||
name = f.__name__ | ||
_benchmark.RegisterBenchmark(name, f) | ||
return f | ||
|
||
|
||
def _flags_parser(argv): | ||
argv = _benchmark.Initialize(argv) | ||
return app.parse_flags_with_usage(argv) | ||
|
||
|
||
def _run_benchmarks(argv): | ||
if len(argv) > 1: | ||
raise app.UsageError('Too many command-line arguments.') | ||
return _benchmark.RunSpecifiedBenchmarks() | ||
|
||
|
||
def main(argv=None): | ||
return app.run(_run_benchmarks, argv=argv, flags_parser=_flags_parser) |
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,47 @@ | ||
// Benchmark for Python. | ||
|
||
#include "benchmark/benchmark.h" | ||
#include "pybind11/pybind11.h" | ||
#include "pybind11/stl.h" | ||
|
||
namespace { | ||
namespace py = ::pybind11; | ||
|
||
std::vector<std::string> Initialize(const std::vector<std::string>& argv) { | ||
// The `argv` pointers here become invalid when this function returns, but | ||
// benchmark holds the pointer to `argv[0]`. We create a static copy of it | ||
// so it persists, and replace the pointer below. | ||
static std::string executable_name(argv[0]); | ||
std::vector<char*> ptrs; | ||
ptrs.reserve(argv.size()); | ||
for (auto& arg : argv) { | ||
ptrs.push_back(const_cast<char*>(arg.c_str())); | ||
} | ||
ptrs[0] = const_cast<char*>(executable_name.c_str()); | ||
int argc = static_cast<int>(argv.size()); | ||
benchmark::Initialize(&argc, ptrs.data()); | ||
std::vector<std::string> remaining_argv; | ||
remaining_argv.reserve(argc); | ||
for (int i = 0; i < argc; ++i) { | ||
remaining_argv.emplace_back(ptrs[i]); | ||
} | ||
return remaining_argv; | ||
} | ||
|
||
void RegisterBenchmark(const char* name, py::function f) { | ||
benchmark::RegisterBenchmark(name, [f](benchmark::State& state) { | ||
f(&state); | ||
}); | ||
} | ||
|
||
PYBIND11_MODULE(_benchmark, m) { | ||
m.def("Initialize", Initialize); | ||
m.def("RegisterBenchmark", RegisterBenchmark); | ||
m.def("RunSpecifiedBenchmarks", | ||
[]() { benchmark::RunSpecifiedBenchmarks(); }); | ||
|
||
py::class_<benchmark::State>(m, "State") | ||
.def("__bool__", &benchmark::State::KeepRunning) | ||
.def_property_readonly("keep_running", &benchmark::State::KeepRunning); | ||
}; | ||
} // namespace |
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,32 @@ | ||
# Copyright 2020 Google Inc. 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. | ||
"""Example of Python using C++ benchmark framework.""" | ||
|
||
import benchmark | ||
|
||
|
||
@benchmark.register | ||
def empty(state): | ||
while state: | ||
pass | ||
|
||
|
||
@benchmark.register | ||
def sum_million(state): | ||
while state: | ||
sum(range(1_000_000)) | ||
|
||
|
||
if __name__ == '__main__': | ||
benchmark.main() |
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,25 @@ | ||
_SHARED_LIB_SUFFIX = { | ||
"//conditions:default": ".so", | ||
"//:windows": ".dll", | ||
} | ||
|
||
def py_extension(name, srcs, hdrs = [], copts = [], features = [], deps = []): | ||
for shared_lib_suffix in _SHARED_LIB_SUFFIX.values(): | ||
shared_lib_name = name + shared_lib_suffix | ||
native.cc_binary( | ||
name = shared_lib_name, | ||
linkshared = 1, | ||
linkstatic = 1, | ||
srcs = srcs + hdrs, | ||
copts = copts, | ||
features = features, | ||
deps = deps, | ||
) | ||
|
||
return native.py_library( | ||
name = name, | ||
data = select({ | ||
platform: [name + shared_lib_suffix] | ||
for platform, shared_lib_suffix in _SHARED_LIB_SUFFIX.items() | ||
}), | ||
) |
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,20 @@ | ||
cc_library( | ||
name = "pybind11", | ||
hdrs = glob( | ||
include = [ | ||
"include/pybind11/*.h", | ||
"include/pybind11/detail/*.h", | ||
], | ||
exclude = [ | ||
"include/pybind11/common.h", | ||
"include/pybind11/eigen.h", | ||
], | ||
), | ||
copts = [ | ||
"-fexceptions", | ||
"-Wno-undefined-inline", | ||
"-Wno-pragma-once-outside-header", | ||
], | ||
includes = ["include"], | ||
visibility = ["//visibility:public"], | ||
) |
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,6 @@ | ||
cc_library( | ||
name = "python_headers", | ||
hdrs = glob(["**/*.h"]), | ||
includes = ["."], | ||
visibility = ["//visibility:public"], | ||
) |
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,2 @@ | ||
absl-py>=0.7.1 | ||
|
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,124 @@ | ||
import os | ||
import posixpath | ||
import re | ||
import shutil | ||
import sys | ||
|
||
from distutils import sysconfig | ||
import setuptools | ||
from setuptools.command import build_ext | ||
|
||
|
||
here = os.path.dirname(os.path.abspath(__file__)) | ||
|
||
|
||
IS_WINDOWS = sys.platform.startswith('win') | ||
|
||
|
||
def _get_version(): | ||
"""Parse the version string from __init__.py.""" | ||
with open(os.path.join(here, 'bindings', 'python', 'benchmark', '__init__.py')) as f: | ||
try: | ||
version_line = next( | ||
line for line in f if line.startswith('__version__')) | ||
except StopIteration: | ||
raise ValueError('__version__ not defined in __init__.py') | ||
else: | ||
ns = {} | ||
exec(version_line, ns) # pylint: disable=exec-used | ||
return ns['__version__'] | ||
|
||
|
||
def _parse_requirements(path): | ||
with open(os.path.join(here, path)) as f: | ||
return [ | ||
line.rstrip() for line in f | ||
if not (line.isspace() or line.startswith('#')) | ||
] | ||
|
||
|
||
class BazelExtension(setuptools.Extension): | ||
"""A C/C++ extension that is defined as a Bazel BUILD target.""" | ||
|
||
def __init__(self, name, bazel_target): | ||
self.bazel_target = bazel_target | ||
self.relpath, self.target_name = ( | ||
posixpath.relpath(bazel_target, '//').split(':')) | ||
setuptools.Extension.__init__(self, name, sources=[]) | ||
|
||
|
||
class BuildBazelExtension(build_ext.build_ext): | ||
"""A command that runs Bazel to build a C/C++ extension.""" | ||
|
||
def run(self): | ||
for ext in self.extensions: | ||
self.bazel_build(ext) | ||
build_ext.build_ext.run(self) | ||
|
||
def bazel_build(self, ext): | ||
with open('WORKSPACE', 'r') as f: | ||
workspace_contents = f.read() | ||
|
||
with open('WORKSPACE', 'w') as f: | ||
f.write(re.sub( | ||
r'(?<=path = ").*(?=", # May be overwritten by setup\.py\.)', | ||
sysconfig.get_python_inc().replace(os.path.sep, posixpath.sep), | ||
workspace_contents)) | ||
|
||
if not os.path.exists(self.build_temp): | ||
os.makedirs(self.build_temp) | ||
|
||
bazel_argv = [ | ||
'bazel', | ||
'build', | ||
ext.bazel_target, | ||
'--symlink_prefix=' + os.path.join(self.build_temp, 'bazel-'), | ||
'--compilation_mode=' + ('dbg' if self.debug else 'opt'), | ||
] | ||
|
||
if IS_WINDOWS: | ||
# Link with python*.lib. | ||
for library_dir in self.library_dirs: | ||
bazel_argv.append('--linkopt=/LIBPATH:' + library_dir) | ||
|
||
self.spawn(bazel_argv) | ||
|
||
shared_lib_suffix = '.dll' if IS_WINDOWS else '.so' | ||
ext_bazel_bin_path = os.path.join( | ||
self.build_temp, 'bazel-bin', | ||
ext.relpath, ext.target_name + shared_lib_suffix) | ||
ext_dest_path = self.get_ext_fullpath(ext.name) | ||
ext_dest_dir = os.path.dirname(ext_dest_path) | ||
if not os.path.exists(ext_dest_dir): | ||
os.makedirs(ext_dest_dir) | ||
shutil.copyfile(ext_bazel_bin_path, ext_dest_path) | ||
|
||
|
||
setuptools.setup( | ||
name='google-benchmark', | ||
version=_get_version(), | ||
url='https://github.com/google/benchmark', | ||
description='A library to benchmark code snippets.', | ||
author='Google', | ||
author_email='benchmark-py@google.com', | ||
# Contained modules and scripts. | ||
package_dir={'': 'bindings/python'}, | ||
packages=setuptools.find_packages('bindings/python'), | ||
install_requires=_parse_requirements('bindings/python/requirements.txt'), | ||
cmdclass=dict(build_ext=BuildBazelExtension), | ||
ext_modules=[BazelExtension('benchmark._benchmark', '//bindings/python/benchmark:_benchmark')], | ||
zip_safe=False, | ||
# PyPI package information. | ||
classifiers=[ | ||
'Development Status :: 4 - Beta', | ||
'Intended Audience :: Developers', | ||
'Intended Audience :: Science/Research', | ||
'License :: OSI Approved :: Apache Software License', | ||
'Programming Language :: Python :: 3.6', | ||
'Programming Language :: Python :: 3.7', | ||
'Topic :: Software Development :: Testing', | ||
'Topic :: System :: Benchmark', | ||
], | ||
license='Apache 2.0', | ||
keywords='benchmark', | ||
) |
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.
Hmm.
Reading up on this, and i think either the cmake build system needs to be explicitly deprecated,
or this should be cmake-based.
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.
We could have the Python bindings requiring bazel (given they require absl which doesn't have a good cmake solution). No?