Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 43 additions & 5 deletions cpp/external/katagocoreml/src/parser/KataGoParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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

// ============================================================================
Expand Down Expand Up @@ -201,17 +227,29 @@ std::vector<float> 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<uint8_t*>(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<uint8_t*>(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<uint16_t> 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<uint8_t*>(floats.data() + done), take * 4, name);
if(halfFloats) {
halves.resize(take);
readExact(reinterpret_cast<uint8_t*>(halves.data()), take * 2, name);
for(size_t i = 0; i < take; i++)
floats[done + i] = halfBitsToFloat(halves[i]);
} else {
readExact(reinterpret_cast<uint8_t*>(floats.data() + done), take * 4, name);
}
}
}

Expand Down
73 changes: 63 additions & 10 deletions cpp/neuralnet/desc.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "../neuralnet/desc.h"

#include <cmath>
#include <cstring>
#include <fstream>
#include <zlib.h>

Expand Down Expand Up @@ -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<float>& buf) {
buf.resize(numFloats);
if(!binaryFloats) {
Expand All @@ -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;
Expand All @@ -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<uint16_t> 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<numFloats; i++) {
//Reverse byte order for big endian
std::swap(bytes[i*4 + 0], bytes[i*4 + 3]);
std::swap(bytes[i*4 + 1], bytes[i*4 + 2]);
for(size_t i = 0; i<numFloats; i++) {
//Reverse byte order for big endian
std::swap(bytes[i*2 + 0], bytes[i*2 + 1]);
}
#endif
for(size_t i = 0; i<numFloats; i++)
buf[i] = halfBitsToFloat(halves[i]);
}
else {
float* data = buf.data();
char* bytes = (char*)data;
in.read(bytes, numFloats*sizeof(float));

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<numFloats; i++) {
//Reverse byte order for big endian
std::swap(bytes[i*4 + 0], bytes[i*4 + 3]);
std::swap(bytes[i*4 + 1], bytes[i*4 + 2]);
}
#endif
}
}

//Flush fp32 subnormals to zero. Some model files have such values, which is negligible for behavior
Expand Down
Loading