diff --git a/cpp/external/katagocoreml/src/parser/KataGoParser.cpp b/cpp/external/katagocoreml/src/parser/KataGoParser.cpp index 79f832ba46..2f7c6dc515 100644 --- a/cpp/external/katagocoreml/src/parser/KataGoParser.cpp +++ b/cpp/external/katagocoreml/src/parser/KataGoParser.cpp @@ -20,6 +20,32 @@ void checkDimsEqual(const std::string& name, const char* aDesc, int64_t a, const bDesc + " (" + std::to_string(b) + ")"); } } + +// IEEE 754 half precision bits to the float they stand for, exactly (an "@BIN16@" block's weights). +float halfBitsToFloat(uint16_t h) { + uint32_t sign = (uint32_t)(h & 0x8000) << 16; + uint32_t exponent = (h >> 10) & 0x1F; + uint32_t mantissa = h & 0x3FF; + uint32_t bits; + if (exponent == 0x1F) { + bits = sign | 0x7F800000 | (mantissa << 13); + } else if (exponent != 0) { + bits = sign | ((exponent + 112) << 23) | (mantissa << 13); + } else if (mantissa == 0) { + bits = sign; + } else { + // Subnormal half, normal as a float + exponent = 113; + while ((mantissa & 0x400) == 0) { + mantissa <<= 1; + exponent--; + } + bits = sign | (exponent << 23) | ((mantissa & 0x3FF) << 13); + } + float x; + std::memcpy(&x, &bits, sizeof(x)); + return x; +} } // namespace // ============================================================================ @@ -201,17 +227,29 @@ std::vector KataGoParser::readFloats(size_t count, const std::string& nam for(size_t i = 0; i < count; i++) floats.push_back(readFloat()); } else { - // Binary: consume the "@BIN@" marker, then read count*4 raw bytes. - char marker[5]; + // Binary: consume the "@BIN@" marker, then read count*4 raw bytes; or the "@BIN16@" + // marker, then count*2 bytes of IEEE 754 half floats, widened to 32 bits. + char marker[7]; readExact(reinterpret_cast(marker), 5, name); - if(std::memcmp(marker, "@BIN@", 5) != 0) - throw std::runtime_error(name + ": expected @BIN@ marker for binary float block"); + bool halfFloats = std::memcmp(marker, "@BIN1", 5) == 0; + if(halfFloats) + readExact(reinterpret_cast(marker + 5), 2, name); + if(std::memcmp(marker, halfFloats ? "@BIN16@" : "@BIN@", halfFloats ? 7 : 5) != 0) + throw std::runtime_error(name + ": expected @BIN@ or @BIN16@ marker for binary float block"); + std::vector halves; while(floats.size() < count) { size_t take = std::min(CHUNK_FLOATS, count - floats.size()); size_t done = floats.size(); floats.resize(done + take); - readExact(reinterpret_cast(floats.data() + done), take * 4, name); + if(halfFloats) { + halves.resize(take); + readExact(reinterpret_cast(halves.data()), take * 2, name); + for(size_t i = 0; i < take; i++) + floats[done + i] = halfBitsToFloat(halves[i]); + } else { + readExact(reinterpret_cast(floats.data() + done), take * 4, name); + } } } diff --git a/cpp/neuralnet/desc.cpp b/cpp/neuralnet/desc.cpp index 51fd3155dd..924d92e55f 100644 --- a/cpp/neuralnet/desc.cpp +++ b/cpp/neuralnet/desc.cpp @@ -1,6 +1,7 @@ #include "../neuralnet/desc.h" #include +#include #include #include @@ -37,6 +38,32 @@ static float readFloatFast(istream& in, string& tmp) { return x; } +//IEEE 754 half precision bits to the float they stand for, exactly +static float halfBitsToFloat(uint16_t h) { + uint32_t sign = (uint32_t)(h & 0x8000) << 16; + uint32_t exponent = (h >> 10) & 0x1F; + uint32_t mantissa = h & 0x3FF; + uint32_t bits; + if(exponent == 0x1F) + bits = sign | 0x7F800000 | (mantissa << 13); + else if(exponent != 0) + bits = sign | ((exponent + 112) << 23) | (mantissa << 13); + else if(mantissa == 0) + bits = sign; + else { + //Subnormal half, normal as a float + exponent = 113; + while((mantissa & 0x400) == 0) { + mantissa <<= 1; + exponent--; + } + bits = sign | (exponent << 23) | ((mantissa & 0x3FF) << 13); + } + float x; + std::memcpy(&x, &bits, sizeof(x)); + return x; +} + static void readFloats(istream& in, size_t numFloats, bool binaryFloats, const string& name, vector& buf) { buf.resize(numFloats); if(!binaryFloats) { @@ -48,7 +75,9 @@ static void readFloats(istream& in, size_t numFloats, bool binaryFloats, const s } else { //KataGo hacky model format - "@BIN@" followed by the expected number of 32 bit floats, in little-endian binary + //or "@BIN16@" followed by that many IEEE 754 half floats, in little-endian binary, widened to 32 bits here testAssert(sizeof(float) == 4); + bool halfFloats = false; { string s; int numCharsBeforeAt = 0; @@ -63,23 +92,47 @@ static void readFloats(istream& in, size_t numFloats, bool binaryFloats, const s s += (char)in.get(); s += (char)in.get(); s += (char)in.get(); - if(s != "BIN@") + if(s == "BIN1") { + s += (char)in.get(); + s += (char)in.get(); + halfFloats = true; + } + if(s != (halfFloats ? "BIN16@" : "BIN@")) throw StringError(name + ": did not find expected header for binary float block"); } - float* data = buf.data(); - char* bytes = (char*)data; - in.read(bytes, numFloats*sizeof(float)); + if(halfFloats) { + vector halves(numFloats); + char* bytes = (char*)halves.data(); + in.read(bytes, numFloats*sizeof(uint16_t)); - if(in.fail()) - throw StringError(name + ": did not find the expected number of floats in binary float block"); + if(in.fail()) + throw StringError(name + ": did not find the expected number of floats in binary float block"); #if BYTE_ORDER == BIG_ENDIAN - for(size_t i = 0; i