Exporting the currently loaded network file

This PR adds an ability to export any currently loaded network.
The export_net command now takes an optional filename parameter.
If the loaded net is not the embedded net the filename parameter is required.

Two changes were required to support this:

* the "architecture" string, which is really just a some kind of description in the net, is now saved into netDescription on load and correctly saved on export.
* the AffineTransform scrambles weights for some architectures and sparsifies them, such that retrieving the index is hard. This is solved by having a temporary scrambled<->unscrambled index lookup table when loading the network, and the actual index is saved for each individual weight that makes it to canSaturate16. This increases the size of the canSaturate16 entries by 6 bytes.

closes https://github.com/official-stockfish/Stockfish/pull/3456

No functional change
This commit is contained in:
Tomasz Sobczyk
2021-05-07 12:24:12 +02:00
committed by Joost VandeVondele
parent d777ea79ff
commit 58054fd0fa
10 changed files with 159 additions and 27 deletions

View File

@@ -39,6 +39,7 @@ namespace Stockfish::Eval::NNUE {
// Evaluation function file name
std::string fileName;
std::string netDescription;
namespace Detail {
@@ -68,6 +69,14 @@ namespace Stockfish::Eval::NNUE {
return reference.read_parameters(stream);
}
// Write evaluation function parameters
template <typename T>
bool write_parameters(std::ostream& stream, const T& reference) {
write_little_endian<std::uint32_t>(stream, T::get_hash_value());
return reference.write_parameters(stream);
}
} // namespace Detail
// Initialize the evaluation function parameters
@@ -78,7 +87,7 @@ namespace Stockfish::Eval::NNUE {
}
// Read network header
bool read_header(std::istream& stream, std::uint32_t* hashValue, std::string* architecture)
bool read_header(std::istream& stream, std::uint32_t* hashValue, std::string* desc)
{
std::uint32_t version, size;
@@ -86,8 +95,18 @@ namespace Stockfish::Eval::NNUE {
*hashValue = read_little_endian<std::uint32_t>(stream);
size = read_little_endian<std::uint32_t>(stream);
if (!stream || version != Version) return false;
architecture->resize(size);
stream.read(&(*architecture)[0], size);
desc->resize(size);
stream.read(&(*desc)[0], size);
return !stream.fail();
}
// Write network header
bool write_header(std::ostream& stream, std::uint32_t hashValue, const std::string& desc)
{
write_little_endian<std::uint32_t>(stream, Version);
write_little_endian<std::uint32_t>(stream, hashValue);
write_little_endian<std::uint32_t>(stream, desc.size());
stream.write(&desc[0], desc.size());
return !stream.fail();
}
@@ -95,14 +114,22 @@ namespace Stockfish::Eval::NNUE {
bool read_parameters(std::istream& stream) {
std::uint32_t hashValue;
std::string architecture;
if (!read_header(stream, &hashValue, &architecture)) return false;
if (!read_header(stream, &hashValue, &netDescription)) return false;
if (hashValue != HashValue) return false;
if (!Detail::read_parameters(stream, *featureTransformer)) return false;
if (!Detail::read_parameters(stream, *network)) return false;
return stream && stream.peek() == std::ios::traits_type::eof();
}
// Write network parameters
bool write_parameters(std::ostream& stream) {
if (!write_header(stream, HashValue, netDescription)) return false;
if (!Detail::write_parameters(stream, *featureTransformer)) return false;
if (!Detail::write_parameters(stream, *network)) return false;
return (bool)stream;
}
// Evaluation function. Perform differential calculation.
Value evaluate(const Position& pos) {
@@ -141,4 +168,13 @@ namespace Stockfish::Eval::NNUE {
return read_parameters(stream);
}
// Save eval, to a file stream or a memory stream
bool save_eval(std::ostream& stream) {
if (fileName.empty())
return false;
return write_parameters(stream);
}
} // namespace Stockfish::Eval::NNUE