Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/metal-coreml' into ios-dev
Browse files Browse the repository at this point in the history
  • Loading branch information
ChinChangYang committed Aug 29, 2024
2 parents 54e7528 + 71129f5 commit 3a10279
Show file tree
Hide file tree
Showing 69 changed files with 173 additions and 95 deletions.
17 changes: 11 additions & 6 deletions cpp/configs/gtp_human9d_search_example.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

# This is an example config for configuring KataGo to attempt to imitate a 9d player and then
# further improving the strength using KataGo's evaluations and search.
# further improving the strength (to possibly modestly superhuman) using KataGo's evaluations and search.

# By contrast, gtp_human5k_example.cfg uses only the raw neural net. Raw neural nets like the
# HumanSL net without search do NOT play accurately enough to reach top human levels.
Expand Down Expand Up @@ -44,6 +44,8 @@ resignMinMovesPerBoardArea = 0.40
# This is due to the strong bias of the human SL network.
# You can reduce this number if you are on weaker hardware. It may reduce strength a bit but will still
# provide a huge strength boost over using the humanSL network alone as in gtp_human5k_example.cfg
# It's NOT recommended to reduce this below about 30-40 visits, since that will result in too few explorations of a variety of moves.
# If you want to adjust strength, see humanSLChosenMovePiklLambda instead.
maxVisits = 400 # 40 in gtp_human5k_example.cfg.

# Having more than one thread speeds up search when visits are larger.
Expand All @@ -61,14 +63,17 @@ humanSLProfile = preaz_9d # was preaz_5k in gtp_human5k_example.cfg
humanSLChosenMoveProp = 1.0
humanSLChosenMoveIgnorePass = true

# When a move starts to lose more than 0.06 utility (several percent winrate), downweight it.
# Increase this number to reduce the strength gain and use the human SL policy move and KataGo's evaluations less.
# When a move starts to lose more than 0.08 utility (several percent winrate), downweight it.
# Increase this number to reduce the strength and use the human SL policy more and KataGo's evaluations less.
# Decrease this number a little more to improve strength even further and play less human-like.
# (although below 0.02 you probably are better off going back to a normal KataGo config and scaling visits).
# Since this uses KataGo's judgment, even at large values and with a weak-ranked humanSLProfile
# this may still produce a very strong player.
# To calibrate for some but less strength gain, it will take experimentation - in addition to increasing this value a lot,
# To calibrate for some but less strength gain, it will take experimentation.
# In addition to increasing this value a lot, e.g. (0.2, 0.5, 1.0, 2.0,...)
# you can also try using old/small KataGo nets (e.g. b6c96, b10c128), reducing visits (though reducing below about 30-40
# is not recommended), or using the humanSLModel itself as the main "-model".
humanSLChosenMovePiklLambda = 0.06 # was 100000000 in gtp_human5k_example.cfg.
# is NOT recommended), or using the humanSLModel itself as the main "-model".
humanSLChosenMovePiklLambda = 0.08 # was 100000000 in gtp_human5k_example.cfg.

# Spend 80% of visits to explore humanSL moves to ensure they get evaluations to use with humanSLChosenMovePiklLambda
humanSLRootExploreProbWeightless = 0.8 # was 0 in gtp_human5k_example.cfg.
Expand Down
4 changes: 2 additions & 2 deletions cpp/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -210,11 +210,11 @@ int main(int argc, const char* const* argv) {


string Version::getKataGoVersion() {
return string("1.15.2-coreml1");
return string("1.15.3");
}

string Version::getKataGoVersionForHelp() {
return string("KataGo v1.15.2-coreml1");
return string("KataGo v1.15.3");
}

string Version::getKataGoVersionFullInfo() {
Expand Down
46 changes: 41 additions & 5 deletions cpp/neuralnet/coremlbackend.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,24 +32,26 @@ public class CoreMLBackend {
return "KataGoModel\(xLen)x\(yLen)fp\(precision)\(encoder)"
}

let model: KataGoModel
var model: KataGoModel
let xLen: Int
let yLen: Int
public let version: Int32
let numSpatialFeatures: Int
let numGlobalFeatures: Int
let numMetaFeatures: Int
let metaEncoderVersion: Int
let modelName: String

var spatialSize: Int {
numSpatialFeatures * yLen * xLen
}

init(model: MLModel, xLen: Int, yLen: Int, metaEncoderVersion: Int) {
init(model: MLModel, xLen: Int, yLen: Int, metaEncoderVersion: Int, modelName: String) {
self.model = KataGoModel(model: model)
self.xLen = xLen
self.yLen = yLen
self.metaEncoderVersion = metaEncoderVersion
self.modelName = modelName

// The model version must be at least 8.
self.version = model.version
Expand Down Expand Up @@ -115,8 +117,8 @@ public class CoreMLBackend {

let inputBatch = KataGoModelInputBatch(inputArray: inputArray)
let options = MLPredictionOptions()
let outputBatch = try! model.prediction(from: inputBatch, options: options)

let outputBatch = safelyPredict(from: inputBatch, options: options)!
assert(outputBatch.count == batchSize)

outputBatch.outputArray.enumerated().forEach { index, output in
Expand Down Expand Up @@ -148,6 +150,37 @@ public class CoreMLBackend {
}
}
}

func safelyPredict(from inputBatch: KataGoModelInputBatch,
options: MLPredictionOptions) -> KataGoModelOutputBatch? {
if let prediction = try? model.prediction(from: inputBatch, options: options) {
return prediction
}

let computeUnits = model.model.configuration.computeUnits

for mustCompile in [false, true] {
if let prediction = compileAndPredict(with: computeUnits, from: inputBatch, options: options, mustCompile: mustCompile) {
return prediction
}
}

return nil
}

private func compileAndPredict(with computeUnits: MLComputeUnits,
from inputBatch: KataGoModelInputBatch,
options: MLPredictionOptions,
mustCompile: Bool) -> KataGoModelOutputBatch? {
if let mlmodel = KataGoModel.compileBundleMLModel(modelName: modelName, computeUnits: computeUnits, mustCompile: mustCompile) {
model = KataGoModel(model: mlmodel)
if let outputBatch = try? model.prediction(from: inputBatch, options: options) {
return outputBatch
}
}

return nil
}
}

public func maybeCreateCoreMLBackend(condition: Bool = true,
Expand All @@ -162,15 +195,18 @@ public func maybeCreateCoreMLBackend(condition: Bool = true,
// Get the model name.
let modelName = CoreMLBackend.getModelName(xLen: xLen, yLen: yLen, useFP16: useFP16, metaEncoderVersion: metaEncoderVersion)

// Specify compute units.
let computeUnits: MLComputeUnits = useCpuAndNeuralEngine ? .cpuAndNeuralEngine : .all

// Compile the model in Bundle.
let mlmodel = KataGoModel.compileBundleMLModel(modelName: modelName, useCpuAndNeuralEngine: useCpuAndNeuralEngine)
let mlmodel = KataGoModel.compileBundleMLModel(modelName: modelName, computeUnits: computeUnits)

if let mlmodel {
printError("CoreML backend \(serverThreadIdx): \(xLen)x\(yLen) useFP16 \(useFP16) metaEncoderVersion \(metaEncoderVersion) useCpuAndNeuralEngine \(useCpuAndNeuralEngine)");
printError("CoreML backend \(serverThreadIdx): \(mlmodel.metaDescription)");

// The CoreMLBackend object is created.
return CoreMLBackend(model: mlmodel, xLen: xLen, yLen: yLen, metaEncoderVersion: metaEncoderVersion)
return CoreMLBackend(model: mlmodel, xLen: xLen, yLen: yLen, metaEncoderVersion: metaEncoderVersion, modelName: modelName)
} else {
printError("Unable to compile bundle MLModel from model: \(modelName)")
return nil
Expand Down
19 changes: 10 additions & 9 deletions cpp/neuralnet/coremlmodel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ class KataGoModel {
return bundleModelURL
}

class func compileBundleMLModel(modelName: String, useCpuAndNeuralEngine: Bool) -> MLModel? {
class func compileBundleMLModel(modelName: String, computeUnits: MLComputeUnits, mustCompile: Bool = false) -> MLModel? {
var mlmodel: MLModel?

do {
Expand All @@ -114,7 +114,8 @@ class KataGoModel {
// Compile MLModel
mlmodel = try compileMLModel(modelName: modelName,
modelURL: bundleModelURL,
useCpuAndNeuralEngine: useCpuAndNeuralEngine)
computeUnits: computeUnits,
mustCompile: mustCompile)
} catch {
printError("An error occurred: \(error)")
}
Expand Down Expand Up @@ -225,9 +226,9 @@ class KataGoModel {
try digest.write(to: savedDigestURL, atomically: true, encoding: .utf8)
}

private class func loadModel(permanentURL: URL, modelName: String, useCpuAndNeuralEngine: Bool) throws -> MLModel {
private class func loadModel(permanentURL: URL, modelName: String, computeUnits: MLComputeUnits) throws -> MLModel {
let configuration = MLModelConfiguration()
configuration.computeUnits = useCpuAndNeuralEngine ? .cpuAndNeuralEngine : .all
configuration.computeUnits = computeUnits
configuration.modelDisplayName = modelName
printError("Creating CoreML model with contents \(permanentURL)")
return try MLModel(contentsOf: permanentURL, configuration: configuration)
Expand All @@ -247,14 +248,14 @@ class KataGoModel {
return savedDigestURL
}

class func compileMLModel(modelName: String, modelURL: URL, useCpuAndNeuralEngine: Bool) throws -> MLModel {
class func compileMLModel(modelName: String, modelURL: URL, computeUnits: MLComputeUnits, mustCompile: Bool) throws -> MLModel {
let permanentURL = try getMLModelCPermanentURL(modelName: modelName)
let savedDigestURL = try getSavedDigestURL(modelName: modelName)
let digest = try getDigest(modelURL: modelURL)

let shouldCompileModel = checkShouldCompileModel(permanentURL: permanentURL,
savedDigestURL: savedDigestURL,
digest: digest)
let shouldCompileModel = mustCompile || checkShouldCompileModel(permanentURL: permanentURL,
savedDigestURL: savedDigestURL,
digest: digest)

if shouldCompileModel {
try compileAndSaveModel(permanentURL: permanentURL,
Expand All @@ -265,7 +266,7 @@ class KataGoModel {

return try loadModel(permanentURL: permanentURL,
modelName: modelName,
useCpuAndNeuralEngine: useCpuAndNeuralEngine);
computeUnits: computeUnits);
}

init(model: MLModel) {
Expand Down
17 changes: 15 additions & 2 deletions cpp/search/searchresults.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -295,8 +295,21 @@ bool Search::getPlaySelectionValues(
maxValue = playSelectionValues[i];
}

if(maxValue <= 1e-50)
return false;
if(maxValue <= 1e-50) {
//If we reached this point we have nonzero many children but the children are all weightless.
//In that case, at least set each one to be weighted by its policy.
for(int i = 0; i<numChildren; i++) {
playSelectionValues[i] = std::max(0.0,(double)policyProbs[getPos(locs[i])]);
}
//Recompute max
for(int i = 0; i<numChildren; i++) {
if(playSelectionValues[i] > maxValue)
maxValue = playSelectionValues[i];
}
if(maxValue <= 1e-50) {
return false;
}
}

//Sanity check - if somehow we had more than this, something must have overflowed or gone wrong
assert(maxValue < 1e40);
Expand Down
2 changes: 1 addition & 1 deletion cpp/tests/results/analysis/badoverride.txt.log
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ rootSymmetryPruning = false
trtUseFP16 = false

: Analysis Engine starting...
: KataGo v1.15.2
: KataGo v1.15.3
: nnRandSeed0 = analysisTest
: After dedups: nnModelFile0 = tests/models/g170-b6c96-s175395328-d26788732.bin.gz useFP16 false useNHWC false
: Initializing neural net buffer to be size 19 * 19 allowing smaller boards
Expand Down
2 changes: 1 addition & 1 deletion cpp/tests/results/analysis/badoverride.txt.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ rootSymmetryPruning = false
trtUseFP16 = false

: Analysis Engine starting...
: KataGo v1.15.2
: KataGo v1.15.3
: nnRandSeed0 = analysisTest
: After dedups: nnModelFile0 = tests/models/g170-b6c96-s175395328-d26788732.bin.gz useFP16 false useNHWC false
: Initializing neural net buffer to be size 19 * 19 allowing smaller boards
Expand Down
2 changes: 1 addition & 1 deletion cpp/tests/results/analysis/basic.txt.log
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ rootSymmetryPruning = false
trtUseFP16 = false

: Analysis Engine starting...
: KataGo v1.15.2
: KataGo v1.15.3
: nnRandSeed0 = analysisTest
: After dedups: nnModelFile0 = tests/models/g170-b6c96-s175395328-d26788732.bin.gz useFP16 false useNHWC false
: Initializing neural net buffer to be size 19 * 19 allowing smaller boards
Expand Down
2 changes: 1 addition & 1 deletion cpp/tests/results/analysis/basic.txt.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ rootSymmetryPruning = false
trtUseFP16 = false

: Analysis Engine starting...
: KataGo v1.15.2
: KataGo v1.15.3
: nnRandSeed0 = analysisTest
: After dedups: nnModelFile0 = tests/models/g170-b6c96-s175395328-d26788732.bin.gz useFP16 false useNHWC false
: Initializing neural net buffer to be size 19 * 19 allowing smaller boards
Expand Down
2 changes: 1 addition & 1 deletion cpp/tests/results/analysis/basic_sidetomove.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ rootSymmetryPruning = false
trtUseFP16 = false

: Analysis Engine starting...
: KataGo v1.15.2
: KataGo v1.15.3
: nnRandSeed0 = analysisTest
: After dedups: nnModelFile0 = tests/models/g170-b6c96-s175395328-d26788732.bin.gz useFP16 false useNHWC false
: Initializing neural net buffer to be size 19 * 19 allowing smaller boards
Expand Down
2 changes: 1 addition & 1 deletion cpp/tests/results/analysis/basic_sidetomove.txt.log
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ rootSymmetryPruning = false
trtUseFP16 = false

: Analysis Engine starting...
: KataGo v1.15.2
: KataGo v1.15.3
: nnRandSeed0 = analysisTest
: After dedups: nnModelFile0 = tests/models/g170-b6c96-s175395328-d26788732.bin.gz useFP16 false useNHWC false
: Initializing neural net buffer to be size 19 * 19 allowing smaller boards
Expand Down
2 changes: 1 addition & 1 deletion cpp/tests/results/analysis/humansl_sidetomove.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ rootSymmetryPruning = false
trtUseFP16 = false

: Analysis Engine starting...
: KataGo v1.15.2
: KataGo v1.15.3
: nnRandSeed0 = analysisTest
: After dedups: nnModelFile0 = tests/models/g170-b6c96-s175395328-d26788732.bin.gz useFP16 false useNHWC false
: Initializing neural net buffer to be size 19 * 19 allowing smaller boards
Expand Down
2 changes: 1 addition & 1 deletion cpp/tests/results/analysis/pvvisits.txt.log
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ rootSymmetryPruning = false
trtUseFP16 = false

: Analysis Engine starting...
: KataGo v1.15.2
: KataGo v1.15.3
: nnRandSeed0 = analysisTest
: After dedups: nnModelFile0 = tests/models/g170-b6c96-s175395328-d26788732.bin.gz useFP16 false useNHWC false
: Initializing neural net buffer to be size 19 * 19 allowing smaller boards
Expand Down
2 changes: 1 addition & 1 deletion cpp/tests/results/analysis/pvvisits.txt.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ rootSymmetryPruning = false
trtUseFP16 = false

: Analysis Engine starting...
: KataGo v1.15.2
: KataGo v1.15.3
: nnRandSeed0 = analysisTest
: After dedups: nnModelFile0 = tests/models/g170-b6c96-s175395328-d26788732.bin.gz useFP16 false useNHWC false
: Initializing neural net buffer to be size 19 * 19 allowing smaller boards
Expand Down
2 changes: 1 addition & 1 deletion cpp/tests/results/analysis/pvvisits_nograph.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ trtUseFP16 = false
useGraphSearch = false

: Analysis Engine starting...
: KataGo v1.15.2
: KataGo v1.15.3
: nnRandSeed0 = analysisTest
: After dedups: nnModelFile0 = tests/models/g170-b6c96-s175395328-d26788732.bin.gz useFP16 false useNHWC false
: Initializing neural net buffer to be size 19 * 19 allowing smaller boards
Expand Down
2 changes: 1 addition & 1 deletion cpp/tests/results/analysis/pvvisits_nograph.txt.log
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ trtUseFP16 = false
useGraphSearch = false

: Analysis Engine starting...
: KataGo v1.15.2
: KataGo v1.15.3
: nnRandSeed0 = analysisTest
: After dedups: nnModelFile0 = tests/models/g170-b6c96-s175395328-d26788732.bin.gz useFP16 false useNHWC false
: Initializing neural net buffer to be size 19 * 19 allowing smaller boards
Expand Down
2 changes: 1 addition & 1 deletion cpp/tests/results/analysis/symmetry.txt.log
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ rootSymmetryPruning = false
trtUseFP16 = false

: Analysis Engine starting...
: KataGo v1.15.2
: KataGo v1.15.3
: nnRandSeed0 = analysisTest
: After dedups: nnModelFile0 = tests/models/g170-b6c96-s175395328-d26788732.bin.gz useFP16 false useNHWC false
: Initializing neural net buffer to be size 19 * 19 allowing smaller boards
Expand Down
2 changes: 1 addition & 1 deletion cpp/tests/results/analysis/symmetry.txt.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ rootSymmetryPruning = false
trtUseFP16 = false

: Analysis Engine starting...
: KataGo v1.15.2
: KataGo v1.15.3
: nnRandSeed0 = analysisTest
: After dedups: nnModelFile0 = tests/models/g170-b6c96-s175395328-d26788732.bin.gz useFP16 false useNHWC false
: Initializing neural net buffer to be size 19 * 19 allowing smaller boards
Expand Down
2 changes: 1 addition & 1 deletion cpp/tests/results/analysis/symmetry_with_pruning.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ rootSymmetryPruning = true
trtUseFP16 = false

: Analysis Engine starting...
: KataGo v1.15.2
: KataGo v1.15.3
: nnRandSeed0 = analysisTest
: After dedups: nnModelFile0 = tests/models/g170-b6c96-s175395328-d26788732.bin.gz useFP16 false useNHWC false
: Initializing neural net buffer to be size 19 * 19 allowing smaller boards
Expand Down
2 changes: 1 addition & 1 deletion cpp/tests/results/analysis/symmetry_with_pruning.txt.log
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ rootSymmetryPruning = true
trtUseFP16 = false

: Analysis Engine starting...
: KataGo v1.15.2
: KataGo v1.15.3
: nnRandSeed0 = analysisTest
: After dedups: nnModelFile0 = tests/models/g170-b6c96-s175395328-d26788732.bin.gz useFP16 false useNHWC false
: Initializing neural net buffer to be size 19 * 19 allowing smaller boards
Expand Down
2 changes: 1 addition & 1 deletion cpp/tests/results/gtp/avoidcorners.log
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ searchRandSeed = forTesting
trtUseFP16 = false

: GTP Engine starting...
: KataGo v1.15.2
: KataGo v1.15.3
: Using TrompTaylor rules initially, unless GTP/GUI overrides this
: Using 1 CPU thread(s) for search
: Added 48 shapes to penalize repeats for bot 0 from tests/data/cornermoves.sgf
Expand Down
2 changes: 1 addition & 1 deletion cpp/tests/results/gtp/avoidcorners.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
KataGo v1.15.2
KataGo v1.15.3
Using TrompTaylor rules initially, unless GTP/GUI overrides this
Initializing board with boardXSize 19 boardYSize 19
Loaded config configs/gtp_example.cfg and/or command-line and query overrides
Expand Down
2 changes: 1 addition & 1 deletion cpp/tests/results/gtp/basic.txt.log
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ searchRandSeed = forTesting
trtUseFP16 = false

: GTP Engine starting...
: KataGo v1.15.2
: KataGo v1.15.3
: Using TrompTaylor rules initially, unless GTP/GUI overrides this
: Using 1 CPU thread(s) for search
: nnRandSeed0 = forTesting
Expand Down
2 changes: 1 addition & 1 deletion cpp/tests/results/gtp/basic.txt.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
KataGo v1.15.2
KataGo v1.15.3
Using TrompTaylor rules initially, unless GTP/GUI overrides this
Initializing board with boardXSize 19 boardYSize 19
Loaded config configs/gtp_example.cfg and/or command-line and query overrides
Expand Down
Loading

0 comments on commit 3a10279

Please sign in to comment.