Skip to content

Commit

Permalink
- Stockfishからmemory.h / cppをporting
Browse files Browse the repository at this point in the history
- LargeMemoryクラス削除
- TT.clear()で、generation8もクリアするように。
- NNUEのFeatureTransformerをLargePageから確保できるようにした。(LargePageが使える環境なら少し高速化されるかも?)
- ASSERT_ALIGNED追加。
- affine_transform_sparse_input.hでcastの警告でてたの修正。
  • Loading branch information
yaneurao committed Oct 8, 2024
1 parent 44bbb88 commit 2a70f5c
Show file tree
Hide file tree
Showing 15 changed files with 543 additions and 372 deletions.
1 change: 1 addition & 0 deletions source/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,7 @@ SOURCES = \
tt.cpp \
movepick.cpp \
timeman.cpp \
memory.cpp \
book/book.cpp \
book/apery_book.cpp \
extra/bitop.cpp \
Expand Down
2 changes: 2 additions & 0 deletions source/YaneuraOu.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -664,6 +664,7 @@
<ClInclude Include="learn\multi_think.h" />
<ClInclude Include="mate\mate.h" />
<ClInclude Include="mate\mate_move_picker.h" />
<ClInclude Include="memory.h" />
<ClInclude Include="misc.h" />
<ClInclude Include="movepick.h" />
<ClInclude Include="search.h" />
Expand Down Expand Up @@ -733,6 +734,7 @@
<ClCompile Include="mate\mate1ply_with_effect.cpp" />
<ClCompile Include="mate\mate_dfpn.hpp" />
<ClCompile Include="mate\mate_solver.cpp" />
<ClCompile Include="memory.cpp" />
<ClCompile Include="misc.cpp" />
<ClCompile Include="movegen.cpp" />
<ClCompile Include="movepick.cpp" />
Expand Down
6 changes: 6 additions & 0 deletions source/YaneuraOu.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,9 @@
<ClInclude Include="eval\nnue\architectures\halfkp_1024x2-8-64.h">
<Filter>リソース ファイル\eval\nnue\architectures</Filter>
</ClInclude>
<ClInclude Include="memory.h">
<Filter>リソース ファイル</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="movegen.cpp">
Expand Down Expand Up @@ -567,6 +570,9 @@
<ClCompile Include="engine\dlshogi-engine\PvMateSearch.cpp">
<Filter>リソース ファイル\engine\dlshogi-engine</Filter>
</ClCompile>
<ClCompile Include="memory.cpp">
<Filter>リソース ファイル</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<Text Include="..\script\readme.txt">
Expand Down
3 changes: 3 additions & 0 deletions source/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -659,6 +659,9 @@ extern GlobalOptions_ GlobalOptions;
#define ASSERT_LV4(X) ASSERT_LV_EX(4, X)
#define ASSERT_LV5(X) ASSERT_LV_EX(5, X)

// memoryがalignされているかのassert
#define ASSERT_ALIGNED(ptr, alignment) assert(reinterpret_cast<uintptr_t>(ptr) % alignment == 0)

// --- declaration of unreachablity

// switchにおいてdefaultに到達しないことを明示して高速化させる
Expand Down
54 changes: 38 additions & 16 deletions source/eval/nnue/evaluate_nnue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

#include "../../evaluate.h"
#include "../../position.h"
#include "../../misc.h"
#include "../../memory.h"
#include "../../usi.h"

#if defined(USE_EVAL_HASH)
Expand All @@ -17,14 +17,16 @@

#include "evaluate_nnue.h"

using namespace Stockfish;

namespace Eval {

namespace NNUE {

int FV_SCALE = 16; // 水匠5では24がベストらしいのでエンジンオプション"FV_SCALE"で変更可能にした。

// 入力特徴量変換器
AlignedPtr<FeatureTransformer> feature_transformer;
LargePagePtr<FeatureTransformer> feature_transformer;

// 評価関数
AlignedPtr<Network> network;
Expand All @@ -35,7 +37,7 @@ namespace Eval {
// 評価関数の構造を表す文字列を取得する
std::string GetArchitectureString() {
return "Features=" + FeatureTransformer::GetStructureString() +
",Network=" + Network::GetStructureString();
",Network=" + Network::GetStructureString();
}

namespace {
Expand All @@ -45,14 +47,15 @@ namespace Eval {
// 評価関数パラメータを初期化する
template <typename T>
void Initialize(AlignedPtr<T>& pointer) {

// → メモリはLarge Pageから確保することで高速化する。
void* ptr = LargeMemory::static_alloc(sizeof(T) , alignof(T), true);
pointer.reset(reinterpret_cast<T*>(ptr));

//sync_cout << "nnue.alloc(" << sizeof(T) << "," << alignof(T) << ")" << sync_endl;
pointer = make_unique_aligned<T>();
}

template <typename T>
void Initialize(LargePagePtr<T>& pointer) {
// → メモリはLarge Pageから確保することで高速化する。
pointer = make_unique_large_page<T>();
}

// 評価関数パラメータを読み込む
template <typename T>
Tools::Result ReadParameters(std::istream& stream, const AlignedPtr<T>& pointer) {
Expand All @@ -63,20 +66,39 @@ namespace Eval {
return pointer->ReadParameters(stream);
}

// 評価関数パラメータを書き込む
// 評価関数パラメータを読み込む
template <typename T>
Tools::Result ReadParameters(std::istream& stream, const LargePagePtr<T>& pointer) {
std::uint32_t header;
stream.read(reinterpret_cast<char*>(&header), sizeof(header));
if (!stream) return Tools::ResultCode::FileReadError;
if (header != T::GetHashValue()) return Tools::ResultCode::FileMismatch;
return pointer->ReadParameters(stream);
}

// 評価関数パラメータを書き込む
template <typename T>
bool WriteParameters(std::ostream& stream, const AlignedPtr<T>& pointer) {
constexpr std::uint32_t header = T::GetHashValue();
stream.write(reinterpret_cast<const char*>(&header), sizeof(header));
return pointer->WriteParameters(stream);
}

// 評価関数パラメータを書き込む
template <typename T>
bool WriteParameters(std::ostream& stream, const LargePagePtr<T>& pointer) {
constexpr std::uint32_t header = T::GetHashValue();
stream.write(reinterpret_cast<const char*>(&header), sizeof(header));
return pointer->WriteParameters(stream);
}


} // namespace Detail

// 評価関数パラメータを初期化する
void Initialize() {
Detail::Initialize(feature_transformer);
Detail::Initialize(network);
Detail::Initialize<FeatureTransformer>(feature_transformer);
Detail::Initialize<Network>(network);
}

} // namespace
Expand Down Expand Up @@ -112,16 +134,16 @@ namespace Eval {
Tools::Result result = ReadHeader(stream, &hash_value, &architecture);
if (result.is_not_ok()) return result;
if (hash_value != kHashValue) return Tools::ResultCode::FileMismatch;
result = Detail::ReadParameters(stream, feature_transformer); if (result.is_not_ok()) return result;
result = Detail::ReadParameters(stream, network); if (result.is_not_ok()) return result;
result = Detail::ReadParameters<FeatureTransformer>(stream, feature_transformer); if (result.is_not_ok()) return result;
result = Detail::ReadParameters<Network>(stream, network); if (result.is_not_ok()) return result;
return (stream && stream.peek() == std::ios::traits_type::eof()) ? Tools::ResultCode::Ok : Tools::ResultCode::FileCloseError;
}

// 評価関数パラメータを書き込む
bool WriteParameters(std::ostream& stream) {
if (!WriteHeader(stream, kHashValue, GetArchitectureString())) return false;
if (!Detail::WriteParameters(stream, feature_transformer)) return false;
if (!Detail::WriteParameters(stream, network)) return false;
if (!Detail::WriteParameters<FeatureTransformer>(stream, feature_transformer)) return false;
if (!Detail::WriteParameters<Network>(stream, network)) return false;
return !stream.fail();
}

Expand Down
28 changes: 4 additions & 24 deletions source/eval/nnue/evaluate_nnue.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@

#include "nnue_feature_transformer.h"
#include "nnue_architecture.h"
#include "../../misc.h"

#include <memory>
//#include "../../misc.h"
#include "../../memory.h"

// 評価関数のソースコードへの埋め込みをする時は、EVAL_EMBEDDINGをdefineして、
// ⇓この2つのシンボルを正しく定義するembedded_nnue.cppを書けば良い。
Expand All @@ -31,30 +30,11 @@ namespace Eval::NNUE {
constexpr std::uint32_t kHashValue =
FeatureTransformer::GetHashValue() ^ Network::GetHashValue();

// Deleter for automating release of memory area
// メモリ領域の解放を自動化するためのデリータ
template <typename T>
struct LargeMemoryDeleter {

void operator()(T* ptr) const {

// Tクラスのデストラクタ
ptr->~T();

// このメモリはLargeMemoryクラスを利用して確保したものなので、
// このクラスのfree()を呼び出して開放する。
LargeMemory::static_free(ptr);
}
};

template <typename T>
using AlignedPtr = std::unique_ptr<T, LargeMemoryDeleter<T>>;

// 入力特徴量変換器
extern AlignedPtr<FeatureTransformer> feature_transformer;
extern Stockfish::LargePagePtr<FeatureTransformer> feature_transformer;

// 評価関数
extern AlignedPtr<Network> network;
extern Stockfish::AlignedPtr<Network> network;

// 評価関数ファイル名
extern const char* const kFileName;
Expand Down
2 changes: 1 addition & 1 deletion source/eval/nnue/layers/affine_transform.h
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ class AffineTransform {
for (std::size_t i = 0; i < kOutputDimensions; ++i)
biases_[i] = read_little_endian<BiasType>(stream);
for (std::size_t i = 0; i < kOutputDimensions * kPaddedInputDimensions; ++i)
weights_[get_weight_index(i)] = read_little_endian<WeightType>(stream);
weights_[get_weight_index(IndexType(i))] = read_little_endian<WeightType>(stream);
return !stream.fail() ? Tools::ResultCode::Ok : Tools::ResultCode::FileReadError;
}

Expand Down
2 changes: 1 addition & 1 deletion source/eval/nnue/layers/affine_transform_sparse_input.h
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ class AffineTransformSparseInput {
for (std::size_t i = 0; i < kOutputDimensions; ++i)
biases_[i] = read_little_endian<BiasType>(stream);
for (std::size_t i = 0; i < kOutputDimensions * kPaddedInputDimensions; ++i)
weights_[get_weight_index(i)] = read_little_endian<WeightType>(stream);
weights_[get_weight_index(IndexType(i))] = read_little_endian<WeightType>(stream);
return !stream.fail() ? Tools::ResultCode::Ok : Tools::ResultCode::FileReadError;
}

Expand Down
Loading

0 comments on commit 2a70f5c

Please sign in to comment.