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

Python bindings for Lc0 backend. #1261

Merged
merged 5 commits into from
May 23, 2020
Merged
Show file tree
Hide file tree
Changes from 4 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
.DS_Store
.clangd/
build/
__pycache__/
compile_commands.json
CUDA_NN/
lc0.xcodeproj/
Expand Down
27 changes: 25 additions & 2 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -504,8 +504,9 @@ endif
executable('lc0', 'src/main.cc',
files, include_directories: includes, dependencies: deps, install: true)


### Tests
#############################################################################
## Tests
#############################################################################

if get_option('gtest')
gtest = dependency('gtest', fallback: ['gtest', 'gtest_dep'])
Expand Down Expand Up @@ -543,3 +544,25 @@ if get_option('gtest')
), args: '--gtest_output=xml:encoder.xml', timeout: 90)

endif


#############################################################################
## Python bindings
#############################################################################

if get_option('python_bindings')
pymod = import('python')
python = pymod.find_installation('python3')
py_bindings_generator = find_program('scripts/gen_py_bindings.py')
mooskagh marked this conversation as resolved.
Show resolved Hide resolved

gen_py_bindings = custom_target('backends', input:[], output:['backends.cc'],
command : [py_bindings_generator, '@OUTPUT0@'])

py_files = [ gen_py_bindings ]

cpython = dependency('python3')
python.extension_module('backends',
[py_files + files],
include_directories: [includes],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding cpp_args: cc.get_supported_arguments(['-Wno-c99-extensions', '-Wno-pedantic', '-Wno-missing-field-initializers']), here silences a lot of the warnings, but probably not a good idea.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

c99-extension that I use will appear in C++20, so I'd ignore that. Other ones probably worth fixing, but I'll do it later (clang also emits some set of warnings).

dependencies: [cpython] + deps)
endif
5 changes: 5 additions & 0 deletions meson_options.txt
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,8 @@ option('nvcc_ccbin',
type: 'string',
value: '',
description: 'Override C++ compiler used by cuda nvcc')

option('python_bindings',
type: 'boolean',
value: false,
description: 'Build Python bindings for the python to bind.')
33 changes: 33 additions & 0 deletions scripts/compile_proto.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,30 @@
#!/usr/bin/env python3

# This file is part of Leela Chess Zero.
# Copyright (C) 2020 The LCZero Authors
#
# Leela Chess is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Leela Chess is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Leela Chess. If not, see <http://www.gnu.org/licenses/>.
#
# Additional permission under GNU GPL version 3 section 7
#
# If you modify this Program, or any covered work, by linking or
# combining it with NVIDIA Corporation's libraries from the NVIDIA CUDA
# Toolkit and the NVIDIA CUDA Deep Neural Network library (or a
# modified version of those libraries), containing parts covered by the
# terms of the respective license agreement, the licensors of this
# Program grant you additional permission to convey the resulting work.

import argparse
import os
import re
Expand Down Expand Up @@ -289,6 +314,14 @@ def GenerateFunctions(self, w):
(cpp_type, name, name))
w.Write("const std::vector<%s>& %s() const { return %s_; }" %
(cpp_type, name, name))
if self.type.IsMessage():
w.Write("const %s& %s(size_t idx) const { return %s_[idx]; }" %
(cpp_type, name, name))
else:
w.Write("%s %s(size_t) const { return %s_[idx]; }" %
(cpp_type, name, name))
w.Write("size_t %s_size() const { return %s_.size(); }" %
(name, name))
else:
w.Write("bool has_%s() const { return has_%s_; }" % (name, name))
if self.type.IsMessage():
Expand Down
122 changes: 122 additions & 0 deletions scripts/gen_py_bindings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
#!/bin/env python3
mooskagh marked this conversation as resolved.
Show resolved Hide resolved

# This file is part of Leela Chess Zero.
# Copyright (C) 2020 The LCZero Authors
#
# Leela Chess is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Leela Chess is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Leela Chess. If not, see <http://www.gnu.org/licenses/>.
#
# Additional permission under GNU GPL version 3 section 7
#
# If you modify this Program, or any covered work, by linking or
# combining it with NVIDIA Corporation's libraries from the NVIDIA CUDA
# Toolkit and the NVIDIA CUDA Deep Neural Network library (or a
# modified version of those libraries), containing parts covered by the
# terms of the respective license agreement, the licensors of this
# Program grant you additional permission to convey the resulting work.

import sys
from pybind.writer import Writer
from pybind import Module, Class
from pybind.parameters import (StringParameter, ClassParameter,
NumericParameter, ArgvObjects, IntegralArgv,
ListOfStringsParameter)
from pybind.retval import (StringViewRetVal, StringRetVal, ListOfStringsRetVal,
NumericRetVal, ObjCopyRetval, ObjOwnerRetval,
ObjTupleRetVal, IntegralTupleRetVal)
from pybind.exceptions import CppException

# Module
mod = Module('backends')
mod.AddInclude('python/weights.h')
mod.AddInitialization('lczero::InitializeMagicBitboards();')
ex = mod.AddException(
CppException('LczeroException', cpp_name='lczero::Exception'))

# Weights class
weights = mod.AddClass(Class('Weights', cpp_name='lczero::python::Weights'))
weights.constructor.AddParameter(
StringParameter('filename', optional=True, can_be_none=True)).AddEx(ex)
weights.AddMethod('filename').AddRetVal(StringViewRetVal())
weights.AddMethod('license').AddRetVal(StringViewRetVal())
weights.AddMethod('min_version').AddRetVal(StringRetVal())
weights.AddMethod('input_format').AddRetVal(NumericRetVal('i'))
weights.AddMethod('policy_format').AddRetVal(NumericRetVal('i'))
weights.AddMethod('value_format').AddRetVal(NumericRetVal('i'))
weights.AddMethod('moves_left_format').AddRetVal(NumericRetVal('i'))
weights.AddMethod('blocks').AddRetVal(NumericRetVal('i'))
weights.AddMethod('filters').AddRetVal(NumericRetVal('i'))

# Input class
input = mod.AddClass(Class('Input', cpp_name='lczero::python::Input'))
input.AddMethod('set_mask').AddParameter(NumericParameter('plane'),
NumericParameter(
'mask', type='u64')).AddEx(ex)
input.AddMethod('set_val').AddParameter(NumericParameter('plane'),
NumericParameter('value',
type='f32')).AddEx(ex)
input.AddMethod('mask').AddParameter(NumericParameter('plane')).AddRetVal(
NumericRetVal('u64')).AddEx(ex)
input.AddMethod('val').AddParameter(NumericParameter('plane')).AddRetVal(
NumericRetVal('f32')).AddEx(ex)
input.AddMethod('clone').AddRetVal(ObjOwnerRetval(input))

# Output class
output = mod.AddClass(
Class('Output',
cpp_name='lczero::python::Output',
disable_constructor=True))
output.AddMethod('q').AddRetVal(NumericRetVal('f32'))
output.AddMethod('d').AddRetVal(NumericRetVal('f32'))
output.AddMethod('m').AddRetVal(NumericRetVal('f32'))
output.AddMethod('p_raw').AddParameter(IntegralArgv('samples', 'i')).AddRetVal(
IntegralTupleRetVal('f32')).AddEx(ex)
output.AddMethod('p_softmax').AddParameter(IntegralArgv(
'samples', 'i')).AddRetVal(IntegralTupleRetVal('f32')).AddEx(ex)

# Backend capabilities class
backend_caps = mod.AddClass(
Class('BackendCapabilities',
cpp_name='lczero::python::BackendCapabilities',
disable_constructor=True))
backend_caps.AddMethod('input_format').AddRetVal(NumericRetVal('i'))
backend_caps.AddMethod('moves_left_format').AddRetVal(NumericRetVal('i'))

# Backend class
backend = mod.AddClass(Class('Backend', cpp_name='lczero::python::Backend'))
backend.AddStaticMethod('available_backends').AddRetVal(ListOfStringsRetVal())
backend.constructor.AddParameter(
ClassParameter(weights, 'weights', optional=True),
StringParameter('backend', optional=True, can_be_none=True),
StringParameter('options', optional=True, can_be_none=True)).AddEx(ex)
backend.AddMethod('evaluate').AddParameter(ArgvObjects(
'inputs', input)).AddRetVal(ObjTupleRetVal(output)).AddEx(ex)
backend.AddMethod('capabilities').AddRetVal(ObjCopyRetval(backend_caps))

# PositionHistory class
game_state = mod.AddClass(
Class('GameState', cpp_name='lczero::python::GameState'))
game_state.constructor.AddParameter(
StringParameter('fen', optional=True, can_be_none=True),
ListOfStringsParameter('moves', optional=True),
).AddEx(ex)
game_state.AddMethod('as_input').AddParameter(
ClassParameter(backend, 'backend',
optional=False)).AddRetVal(ObjOwnerRetval(input)).AddEx(ex)
game_state.AddMethod('moves').AddRetVal(ListOfStringsRetVal())
game_state.AddMethod('policy_indices').AddRetVal(IntegralTupleRetVal('i'))
game_state.AddMethod('as_string').AddRetVal(StringRetVal())

with open(sys.argv[1], 'wt') as f:
writer = Writer(f)
mod.Generate(writer)
Loading