Skip to content

Commit

Permalink
use float for scaling factor
Browse files Browse the repository at this point in the history
Also output raw coord for bestline in detail

bench c661d43b
bench (msvc clang) 4580d3aa
  • Loading branch information
dhbloo committed Jun 27, 2024
1 parent ec80539 commit 2e0ec60
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 85 deletions.
10 changes: 5 additions & 5 deletions Rapfi/command/gomocup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -404,11 +404,11 @@ void getOption()
}
else if (token == "EVALUATOR_DRAW_BLACK_WINRATE") {
std::cin >> Config::EvaluatorDrawBlackWinRate;
Config::EvaluatorDrawBlackWinRate = std::clamp(Config::EvaluatorDrawBlackWinRate, 0.0, 1.0);
Config::EvaluatorDrawBlackWinRate = std::clamp(Config::EvaluatorDrawBlackWinRate, 0.0f, 1.0f);
}
else if (token == "EVALUATOR_DRAW_RATIO") {
std::cin >> Config::EvaluatorDrawRatio;
Config::EvaluatorDrawRatio = std::clamp(Config::EvaluatorDrawRatio, 0.0, 1.0);
Config::EvaluatorDrawRatio = std::clamp(Config::EvaluatorDrawRatio, 0.0f, 1.0f);
}
else {
MESSAGEL("Unknown Info Parameter: " << token);
Expand Down Expand Up @@ -1182,16 +1182,16 @@ void traceSearch()
std::vector<Pos> moveList;
while (Pos move = movePicker())
moveList.push_back(move);
MESSAGEL("Legal Moves[" << moveList.size() << "]: " << PVText {moveList});
MESSAGEL("Legal Moves[" << moveList.size() << "]: " << MovesText {moveList});

Opening::filterSymmetryMoves(board, moveList);
MESSAGEL("Root Moves(exclude symmetry)[" << moveList.size() << "]: " << PVText {moveList});
MESSAGEL("Root Moves(exclude symmetry)[" << moveList.size() << "]: " << MovesText {moveList});

// Static evaluation (black point of view)
Value eval = Evaluation::evaluate(board, options.rule);
Value evalPOB = board.sideToMove() == BLACK ? eval : -eval;
MESSAGEL("Static Eval[Black]: " << evalPOB << " (WDL " << std::fixed << std::setprecision(2)
<< (Config::valueToWinRate(evalPOB) * 100.0) << ", SF "
<< (Config::valueToWinRate(evalPOB) * 100.0f) << ", SF "
<< std::setprecision(2) << Config::ScalingFactor << ")");

if (board.evaluator()) {
Expand Down
37 changes: 19 additions & 18 deletions Rapfi/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,13 @@ namespace Config {

/// Scaling Factor is used for conversion between eval value and win rate.
/// Formula: win rate = sigmoid(eval / ScalingFactor)
double ScalingFactor;
double InvScalingFactor;
double EvaluatorMarginWinLossScale;
double EvaluatorMarginWinLossExponent;
double EvaluatorMarginScale;
double EvaluatorDrawBlackWinRate;
double EvaluatorDrawRatio;
float ScalingFactor;
float InvScalingFactor;
float EvaluatorMarginWinLossScale;
float EvaluatorMarginWinLossExponent;
float EvaluatorMarginScale;
float EvaluatorDrawBlackWinRate;
float EvaluatorDrawRatio;

// Classical evaluation and score tables
// Note that Renju has asymmetry eval and score
Expand Down Expand Up @@ -629,13 +629,13 @@ void Config::readEvaluator(const cpptoml::table &t)
}

// Read classical/evaluator switching margin
EvaluatorMarginWinLossScale = t.get_as<double>("margin_winloss_scale").value_or(1.05);
EvaluatorMarginWinLossExponent = t.get_as<double>("margin_winloss_exp").value_or(2.95);
EvaluatorMarginScale = t.get_as<double>("margin_scale").value_or(342);
EvaluatorDrawBlackWinRate = t.get_as<double>("draw_black_winrate").value_or(0.5);
EvaluatorDrawRatio = t.get_as<double>("draw_ratio").value_or(1.0);
EvaluatorDrawBlackWinRate = std::clamp(EvaluatorDrawBlackWinRate, 0.0, 1.0);
EvaluatorDrawRatio = std::clamp(EvaluatorDrawRatio, 0.0, 1.0);
EvaluatorMarginWinLossScale = (float)t.get_as<double>("margin_winloss_scale").value_or(1.05);
EvaluatorMarginWinLossExponent = (float)t.get_as<double>("margin_winloss_exp").value_or(2.95);
EvaluatorMarginScale = (float)t.get_as<double>("margin_scale").value_or(342);
EvaluatorDrawBlackWinRate = (float)t.get_as<double>("draw_black_winrate").value_or(0.5);
EvaluatorDrawRatio = (float)t.get_as<double>("draw_ratio").value_or(1.0);
EvaluatorDrawBlackWinRate = std::clamp(EvaluatorDrawBlackWinRate, 0.0f, 1.0f);
EvaluatorDrawRatio = std::clamp(EvaluatorDrawRatio, 0.0f, 1.0f);

MESSAGEL("Evaluator set to " << *evaluatorType << ".");
}
Expand Down Expand Up @@ -895,9 +895,9 @@ bool Config::loadModel(std::istream &inStream)
if (!in)
return false;

double scalingFactor;
in->read(reinterpret_cast<char *>(&scalingFactor), sizeof(scalingFactor));
setScalingFactor(scalingFactor);
double scalingFactorF64;
in->read(reinterpret_cast<char *>(&scalingFactorF64), sizeof(scalingFactorF64));
setScalingFactor(scalingFactorF64);

in->read(reinterpret_cast<char *>(EVALS), sizeof(EVALS));
in->read(reinterpret_cast<char *>(EVALS_THREAT), sizeof(EVALS_THREAT));
Expand All @@ -922,7 +922,8 @@ void Config::exportModel(std::ostream &outStream)
std::ostream *out = compressor.openOutputStream();
assert(out);

out->write(reinterpret_cast<char *>(&ScalingFactor), sizeof(ScalingFactor));
double scalingFactorF64 = ScalingFactor;
out->write(reinterpret_cast<char *>(&scalingFactorF64), sizeof(scalingFactorF64));
out->write(reinterpret_cast<char *>(EVALS), sizeof(EVALS));
out->write(reinterpret_cast<char *>(EVALS_THREAT), sizeof(EVALS_THREAT));

Expand Down
41 changes: 18 additions & 23 deletions Rapfi/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ struct Pattern4Score
Score scoreSelf() const { return (Score)_scoreSelf; }
Score scoreOppo() const { return (Score)_scoreOppo; }
Pattern4Score &operator=(Pattern4 pattern4) { return _pattern4 = pattern4, *this; }
operator Pattern4() const { return Pattern4(_pattern4); }
operator Pattern4() const { return Pattern4(_pattern4); }
};
static_assert(sizeof(Pattern4Score) == sizeof(int32_t));

Expand All @@ -87,22 +87,22 @@ extern const std::string InternalConfig;

// -------------------------------------------------
// Model configs
extern double ScalingFactor;
extern double InvScalingFactor;
extern double EvaluatorMarginWinLossScale;
extern double EvaluatorMarginWinLossExponent;
extern double EvaluatorMarginScale;
extern double EvaluatorDrawBlackWinRate;
extern double EvaluatorDrawRatio;
extern float ScalingFactor;
extern float InvScalingFactor;
extern float EvaluatorMarginWinLossScale;
extern float EvaluatorMarginWinLossExponent;
extern float EvaluatorMarginScale;
extern float EvaluatorDrawBlackWinRate;
extern float EvaluatorDrawRatio;
extern Eval EVALS[RULE_NB + 1][PCODE_NB];
extern Eval EVALS_THREAT[RULE_NB + 1][THREAT_NB];
extern Pattern4Score P4SCORES[RULE_NB + 1][PCODE_NB];

/// Set a new eval scaling factor. Only use this function to change scaling factor.
inline void setScalingFactor(double sf)
inline void setScalingFactor(float sf)
{
ScalingFactor = sf;
InvScalingFactor = 1.0 / sf;
InvScalingFactor = 1.0f / sf;
}

// -------------------------------------------------
Expand Down Expand Up @@ -201,26 +201,21 @@ inline Pattern4Score getP4Score(Rule R, Color C, PatternCode pcode)

/// Converts a evaluation value to winning rate (in [0, 1]) using current ScalingFactor.
template <bool Strict = true>
inline double valueToWinRate(Value eval)
inline float valueToWinRate(Value eval)
{
if (eval >= (Strict ? VALUE_MATE_IN_MAX_PLY : VALUE_EVAL_MAX))
return 1.0;
return 1.0f;
if (eval <= (Strict ? VALUE_MATED_IN_MAX_PLY : VALUE_EVAL_MIN))
return 0.0;
return 1.0 / (1.0 + std::exp(-double(eval) * InvScalingFactor));
return 0.0f;
return 1.0f / (1.0f + ::expf(-float(eval) * InvScalingFactor));
}

/// Converts a winning rate in [0, 1] to a evaluation value using current ScalingFactor.
inline Value winRateToValue(double winRate)
inline Value winRateToValue(float winRate)
{
if (winRate > 0.999995f)
return VALUE_EVAL_MAX;
else if (winRate < 0.000005f)
return VALUE_EVAL_MIN;
else {
Value v = Value(ScalingFactor * std::log(winRate / (1.0f - winRate)));
return std::clamp(v, VALUE_EVAL_MIN, VALUE_EVAL_MAX);
}
float valueF32 = ScalingFactor * ::logf(winRate / (1.0f - winRate));
valueF32 = std::clamp<float>(valueF32, VALUE_EVAL_MIN, VALUE_EVAL_MAX);
return Value(valueF32);
}

// -------------------------------------------------
Expand Down
23 changes: 8 additions & 15 deletions Rapfi/core/iohelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,24 +170,17 @@ std::ostream &operator<<(std::ostream &out, Rule rule)
return out << RuleName[rule];
}

static void outputMoveSequence(std::ostream &out, const std::vector<Pos> &moves, bool withSpace)
std::ostream &operator<<(std::ostream &out, MovesText movesRef)
{
for (size_t i = 0; i < moves.size(); i++) {
if (withSpace && i)
for (size_t i = 0; i < movesRef.moves.size(); i++) {
if (movesRef.withSpace && i)
out << ' ';
out << moves[i];
if (movesRef.rawCoords)
out << outputCoordXConvert(movesRef.moves[i], movesRef.boardsize) << ','
<< outputCoordYConvert(movesRef.moves[i], movesRef.boardsize);
else
out << movesRef.moves[i];
}
}

std::ostream &operator<<(std::ostream &out, MoveSeqText movesRef)
{
outputMoveSequence(out, movesRef.moves, false);
return out;
}

std::ostream &operator<<(std::ostream &out, PVText movesRef)
{
outputMoveSequence(out, movesRef.moves, true);
return out;
}

Expand Down
13 changes: 6 additions & 7 deletions Rapfi/core/iohelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,13 @@ int outputCoordYConvert(Pos pos, int boardsize);
// -------------------------------------------------
// Formatters

struct MoveSeqText
{
const std::vector<Pos> &moves;
};
struct PVText
struct MovesText
{
const std::vector<Pos> &moves;

bool withSpace = true;
bool rawCoords = false;
int boardsize = 15;
};

std::ostream &operator<<(std::ostream &out, Pos pos);
Expand All @@ -65,8 +65,7 @@ std::ostream &operator<<(std::ostream &out, Pattern p);
std::ostream &operator<<(std::ostream &out, Pattern4 p4);
std::ostream &operator<<(std::ostream &out, Value value);
std::ostream &operator<<(std::ostream &out, Rule rule);
std::ostream &operator<<(std::ostream &out, MoveSeqText movesRef);
std::ostream &operator<<(std::ostream &out, PVText movesRef);
std::ostream &operator<<(std::ostream &out, MovesText movesRef);

// -------------------------------------------------
// Compression helper
Expand Down
10 changes: 5 additions & 5 deletions Rapfi/eval/eval.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
*/

#include "eval.h"

Expand Down Expand Up @@ -79,11 +79,11 @@ inline Value evaluateBasic(const StateInfo &st, Color self)
/// it falls outside alpha-beta window with this margin.
inline int classicalEvalMargin(Value bound)
{
double winLossRate = 2 * (Config::valueToWinRate(bound) - 0.5);
double x = Config::EvaluatorMarginWinLossScale * winLossRate;
double x2 = x * x;
float winLossRate = 2 * (Config::valueToWinRate(bound) - 0.5f);
float x = Config::EvaluatorMarginWinLossScale * winLossRate;
float x2 = x * x;
return (int)(Config::EvaluatorMarginScale
* std::exp(-std::pow(x2, Config::EvaluatorMarginWinLossExponent)));
* ::expf(-::powf(x2, Config::EvaluatorMarginWinLossExponent)));
}

} // namespace
Expand Down
16 changes: 8 additions & 8 deletions Rapfi/search/searchoutput.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,23 +119,23 @@ void SearchPrinter::printPvCompletes(MainSearchThread &th,
INFO("SPEED", speed);
INFO("EVAL", curMove.value);
INFO("WINRATE", Config::valueToWinRate(curMove.value));
INFO("BESTLINE", PVText {curMove.pv});
INFO("BESTLINE", MovesText {curMove.pv, true, true, th.board->size()});
INFO("PV", "DONE");
}

if (numPv > 1 && Config::MessageMode == MsgMode::NORMAL)
MESSAGEL("(" << pvIdx + 1 << ") " << curMove.value << " | " << rootDepth << "-"
<< curMove.selDepth << " | " << PVText {curMove.pv});
<< curMove.selDepth << " | " << MovesText {curMove.pv});
else if (Config::MessageMode == MsgMode::UCILIKE) {
if (numPv > 1)
MESSAGEL("depth " << rootDepth << "-" << curMove.selDepth << " multipv "
<< pvIdx + 1 << " ev " << curMove.value << " n "
<< nodesText(nodes) << " n/ms " << speed << " tm " << tc.elapsed()
<< " pv " << PVText {curMove.pv});
<< " pv " << MovesText {curMove.pv});
else
MESSAGEL("depth " << rootDepth << "-" << curMove.selDepth << " ev " << curMove.value
<< " n " << nodesText(nodes) << " n/ms " << speed << " tm "
<< tc.elapsed() << " pv " << PVText {curMove.pv});
<< tc.elapsed() << " pv " << MovesText {curMove.pv});
}
}

Expand All @@ -153,7 +153,7 @@ void SearchPrinter::printDepthCompletes(MainSearchThread &th, const TimeControl
MESSAGEL((showPonder ? "[Pondering] " : "")
<< "Depth " << rootDepth << "-" << th.rootMoves[0].selDepth << " | Eval "
<< th.rootMoves[0].value << " | Time " << timeText(tc.elapsed()) << " | "
<< PVText {th.rootMoves[0].pv});
<< MovesText {th.rootMoves[0].pv});
}
}

Expand All @@ -177,9 +177,9 @@ void SearchPrinter::printSearchEnds(MainSearchThread &th,
// Select a longer PV for final output
if (bestThread.rootMoves[0].pv.size() <= 2
&& bestThread.rootMoves[0].previousPv.size() > 2)
MESSAGEL("Bestline " << PVText {bestThread.rootMoves[0].previousPv});
MESSAGEL("Bestline " << MovesText {bestThread.rootMoves[0].previousPv});
else
MESSAGEL("Bestline " << PVText {bestThread.rootMoves[0].pv});
MESSAGEL("Bestline " << MovesText {bestThread.rootMoves[0].pv});
}
}
}
Expand All @@ -193,7 +193,7 @@ void SearchPrinter::printBestmoveWithoutSearch(Pos bestMove,
if (Config::MessageMode == MsgMode::UCILIKE) {
if (pv)
MESSAGEL("depth " << rootDepth << "-" << 0 << " ev " << moveValue
<< " n 0 n/ms 0 tm 0 pv " << PVText {*pv});
<< " n 0 n/ms 0 tm 0 pv " << MovesText {*pv});
else
MESSAGEL("depth " << rootDepth << "-" << 0 << " ev " << moveValue
<< " n 0 n/ms 0 tm 0 pv (NONE)");
Expand Down
4 changes: 2 additions & 2 deletions Rapfi/tuning/dataset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ bool SimpleBinaryDataset::next(DataEntry *entry)
else if (movedPos.find(pos) != movedPos.end()) {
std::stringstream ss;
ss << "duplicate move in sequence ([" << pos << "], current sequence ["
<< MoveSeqText {entry->position} << "])";
<< MovesText {entry->position, false} << "])";
throw std::runtime_error(ss.str());
}

Expand Down Expand Up @@ -581,7 +581,7 @@ class KatagoNumpyDataset::DataSource
Compressor compressor(fileStream, Compressor::Type::ZIP_DEFAULT);

auto openEntryThen = [&](std::string entryName,
bool (DataSource::*receiver)(std::istream & is)) {
bool (DataSource::*receiver)(std::istream &is)) {
std::istream *is = compressor.openInputStream(entryName);
if (!is)
throw std::runtime_error("unable to open " + entryName + " in file "
Expand Down
4 changes: 2 additions & 2 deletions Rapfi/tuning/datawriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,8 @@ PlainTextDataWriter::~PlainTextDataWriter() {}
void PlainTextDataWriter::writeEntry(const DataEntry &entry)
{
std::ostream &dst = dataStream->getStream();
dst << int(entry.boardsize) << ',' << entry.rule << ',' << MoveSeqText {entry.position} << ','
<< int(entry.result) << ',' << entry.move;
dst << int(entry.boardsize) << ',' << entry.rule << ',' << MovesText {entry.position, false}
<< ',' << int(entry.result) << ',' << entry.move;
if (entry.eval != VALUE_NONE)
dst << '(' << entry.eval << ')';

Expand Down

0 comments on commit 2e0ec60

Please sign in to comment.