Skip to content

Commit

Permalink
- 評価関数ファイルを読み込んで、C++の文字列literalに変換するスクリプト追加。
Browse files Browse the repository at this point in the history
  - ここで変換した.cppをやねうら王のプロジェクトに追加してビルドする。
  - ビルドする時に、EVAL_EMBEDDINGをdefineしてビルドする。

- 一つ前のcommitでビルドできなくなっていたの修正。
  • Loading branch information
yaneurao committed Dec 16, 2023
1 parent 1f23ea6 commit aaccad5
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
51 changes: 51 additions & 0 deletions script/eval_bin_to_cpp_literal.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# 評価関数ファイルを読み込んで、C++の文字列literalに変換する。[2023/12/13]
# ここで変換した.cppをやねうら王のプロジェクトに追加してビルドする。
# ビルドする時に、EVAL_EMBEDDINGをdefineしてビルドする。

def binary_to_cpp_literal(input_file:str, output_file:str):

# バイナリファイルを読み込む
with open(input_file, 'rb') as file:
binary_data = file.read()

# ファイルサイズを取得
file_size:int = len(binary_data)

# 16進数の文字列に変換
hex_string = '\\x' + '\\x'.join(f'{byte:02x}' for byte in binary_data)

with open(output_file, 'w') as file:
file.write('#include <cstddef>\n')
file.write('extern const char* gEmbeddedNNUEData;\n')
file.write('extern const std::size_t gEmbeddedNNUESize;\n\n')
file.write(f'const char* gEmbeddedNNUEData = "{hex_string}";\n')
file.write(f'const std::size_t gEmbeddedNNUESize = {file_size};\n')

def binary_to_cpp_literal2(input_file:str, output_file:str):

# ファイルを開き、ヘッダと宣言を書き込む
with open(output_file, 'w') as outfile:

outfile.write('#include <cstddef>\n')
outfile.write('extern const char* gEmbeddedNNUEData;\n')
outfile.write('extern const std::size_t gEmbeddedNNUESize;\n\n')

outfile.write(f'const char* gEmbeddedNNUEData = "')

# バイナリファイルを開いて、ストリームで読み込みながら処理
file_size = 0
with open(input_file, 'rb') as infile:
while True:
chunk = infile.read(1024) # 1024バイトずつ読み込む
if not chunk:
break
# 16進数の文字列に変換して出力
hex_string = '\\x' + '\\x'.join(f'{byte:02x}' for byte in chunk)
outfile.write(hex_string)
file_size += len(chunk)

outfile.write('";\n')
outfile.write(f'const std::size_t gEmbeddedNNUESize = {file_size};\n')

# 例:'nn.bin'を読み込み、'nnue_bin.cpp'に出力
binary_to_cpp_literal2('nn.bin', 'nnue_bin.cpp')
4 changes: 3 additions & 1 deletion source/book/makebook2023.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1262,7 +1262,9 @@ namespace MakeBook2023

// nの64倍ぐらいで終わるんちゃうんか?
progress.reset(n * 64);
u64 c = 0;

// 進捗出力用カウンター
size_t c = 0;
StateInfo si;

// カスタム比較関数
Expand Down

0 comments on commit aaccad5

Please sign in to comment.