tinycodinglm / chess /chess-0085.jsonl
winapiadmin's picture
Upload folder using huggingface_hub (part 12)
3c88a0f verified
|
Raw History Blame Contribute Delete
3.42 kB
{"type": "session", "harness": "opencode", "id": "chess-0085", "name": "Implement NNUE (Efficiently Updatable Neural Network) inference for chess evaluation"}
{"type": "message", "message": {"role": "system", "content": "You are an expert programmer specializing in chess programming and game AI. You implement efficient algorithms with optimal performance."}}
{"type": "message", "message": {"role": "user", "content": "Implement NNUE (Efficiently Updatable Neural Network) inference for chess evaluation"}}
{"type": "message", "message": {"role": "assistant", "content": "#include <cstdint>\n#include <array>\n#include <cmath>\n#include <algorithm>\n\nconstexpr int INPUT_DIM = 768; // 6 piece types * 64 squares * 2 colors\nconstexpr int HIDDEN1 = 256;\nconstexpr int HIDDEN2 = 32;\nconstexpr int OUTPUT = 1;\n\nstruct alignas(64) NNUENetwork {\n int16_t featureWeights[INPUT_DIM][HIDDEN1];\n int16_t featureBiases[HIDDEN1];\n int8_t hiddenWeights1[HIDDEN1][HIDDEN2];\n int32_t hiddenBiases1[HIDDEN2];\n int8_t hiddenWeights2[HIDDEN2][OUTPUT];\n int32_t hiddenBiases2[OUTPUT];\n};\n\nclass NNUEEvaluator {\n NNUENetwork* network;\n alignas(64) int16_t accumulator[2][HIDDEN1]; // [white][black]\n\n static const int QA = 255;\n static const int QB = 64;\n static const int QAB = QA * QB;\n\n void refreshAccumulator(const BitboardBoard& board) {\n for (int perspective = 0; perspective < 2; perspective++) {\n for (int j = 0; j < HIDDEN1; j++) {\n int32_t sum = network->featureBiases[j];\n for (int pt = 0; pt < 12; pt++) {\n uint64_t bb = board.pieces[pt];\n while (bb) {\n int sq = __builtin_ctzll(bb);\n int idx = pt * 64 + sq;\n if (perspective == BLACK) idx ^= 378; // flip\n sum += network->featureWeights[idx][j];\n bb &= bb - 1;\n }\n }\n accumulator[perspective][j] = std::clamp(sum, -32768, 32767);\n }\n }\n }\n\n void updateAccumulatorAdd(int perspective, int featureIdx) {\n for (int j = 0; j < HIDDEN1; j++) {\n accumulator[perspective][j] += network->featureWeights[featureIdx][j];\n }\n }\n\n void updateAccumulatorRemove(int perspective, int featureIdx) {\n for (int j = 0; j < HIDDEN1; j++) {\n accumulator[perspective][j] -= network->featureWeights[featureIdx][j];\n }\n }\n\n int32_t relu(int32_t x) const { return x > 0 ? x : 0; }\n\n int32_t clipped_relu(int32_t x) const {\n return std::clamp(x, (int32_t)0, (int32_t)QA);\n }\n\n int evaluate(const BitboardBoard& board) {\n int perspective = board.sideToMove;\n int32_t hiddenIn[HIDDEN2];\n\n for (int i = 0; i < HIDDEN2; i++) {\n int32_t sum = network->hiddenBiases1[i];\n for (int j = 0; j < HIDDEN1; j++) {\n sum += clipped_relu(accumulator[perspective][j]) * network->hiddenWeights1[j][i];\n }\n hiddenIn[i] = relu(sum / QA);\n }\n\n int32_t output = network->hiddenBiases2[0];\n for (int i = 0; i < HIDDEN2; i++) {\n output += hiddenIn[i] * network->hiddenWeights2[i][0];\n }\n\n return output / QAB;\n }\n};"}}