Skip to content

Commit

Permalink
Merge pull request LeelaChessZero#11 from LeelaChessZero/master
Browse files Browse the repository at this point in the history
Merge master into april
  • Loading branch information
Naphthalin authored May 26, 2020
2 parents f1ce36d + 9883568 commit 08f2352
Show file tree
Hide file tree
Showing 39 changed files with 1,842 additions and 214 deletions.
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
4 changes: 2 additions & 2 deletions appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ install:
- cmd: IF %NAME%==cpu-openblas IF NOT EXIST C:\cache\OpenBLAS appveyor DownloadFile https://sjeng.org/ftp/OpenBLAS-0.3.3-win-oldthread.zip
- cmd: IF %NAME%==cpu-openblas IF NOT EXIST C:\cache\OpenBLAS 7z x OpenBLAS-0.3.3-win-oldthread.zip -oC:\cache\OpenBLAS
- cmd: IF %OPENCL%==true nuget install opencl-nug -Version 0.777.77 -OutputDirectory C:\cache
- cmd: IF %BLAS%==true IF NOT EXIST C:\cache\ispc-v1.9.2-windows appveyor DownloadFile https://sourceforge.net/projects/ispcmirror/files/v1.9.2/ispc-v1.9.2-windows.zip
- cmd: IF %BLAS%==true IF NOT EXIST C:\cache\ispc-v1.9.2-windows appveyor DownloadFile https://github.com/ispc/ispc/releases/download/v1.9.2/ispc-v1.9.2-windows.zip
- cmd: IF %BLAS%==true IF NOT EXIST C:\cache\ispc-v1.9.2-windows 7z x ispc-v1.9.2-windows.zip -oC:\cache
- cmd: IF %BLAS%==true set PATH=C:\cache\ispc-v1.9.2-windows;%PATH%
- cmd: set "CUDA_PATH=C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.0"
Expand Down Expand Up @@ -64,7 +64,7 @@ install:
- cmd: IF %GTEST%==true IF NOT EXIST KQvKQ.rtbz curl --remote-name-all https://tablebase.lichess.ovh/tables/standard/3-4-5/K{P,N,R,B,Q}vK{P,N,R,B,Q}.rtb{w,z}
- cmd: cd C:\projects\lc0
cache:
- C:\cache -> appveyor.yml
- C:\cache
- 'C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.0'
- C:\projects\lc0\subprojects\packagecache
- C:\ndk\android-ndk-r19c\toolchains\llvm\prebuilt\windows-x86_64
Expand Down
41 changes: 36 additions & 5 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ if git.found()
# Now let's check if the working directory is clean.
if run_command(git, 'diff-index', '--quiet', 'HEAD').returncode() == 0
short_rev = r.stdout().strip()
if run_command(git, 'describe', '--exact-match', '--tags').returncode() == 0
short_rev = ''
endif
else
short_rev = 'dirty'
warning('Cannot extract valid git short revision from dirty working directory.')
Expand All @@ -109,9 +112,11 @@ if git.found()
endif

# Construct build identifier.
build_identifier = 'git.' + short_rev
add_project_arguments('-DBUILD_IDENTIFIER="' + build_identifier + '"', language : 'cpp')
message('Using build identifier "' + build_identifier + '".')
if short_rev != ''
build_identifier = 'git.' + short_rev
add_project_arguments('-DBUILD_IDENTIFIER="' + build_identifier + '"', language : 'cpp')
message('Using build identifier "' + build_identifier + '".')
endif

#############################################################################
## Main files
Expand Down Expand Up @@ -505,8 +510,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 @@ -544,3 +550,28 @@ 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')
if python.language_version() < '3.7'
error('You need python 3.7 or newer')
endif
py_bindings_generator = find_program('scripts/gen_py_bindings.py')

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],
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 @@
#!/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 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

0 comments on commit 08f2352

Please sign in to comment.