Skip to content

Commit

Permalink
Fix SPSA
Browse files Browse the repository at this point in the history
  • Loading branch information
Viren6 committed Apr 7, 2024
1 parent 75b67db commit 7f9ce2d
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 7 deletions.
11 changes: 8 additions & 3 deletions server/fishtest/rundb.py
Original file line number Diff line number Diff line change
Expand Up @@ -1553,20 +1553,25 @@ def generate_spsa(self, run):
# and avoid division by zero
for param in spsa["params"]:
c = param["c"] / iter_local ** spsa["gamma"]
r = random.uniform(0, 1)
flip = 1 if random.getrandbits(1) else -1
# Stochastic rounding and probability for float N.p: (N, 1-p); (N+1, p)
w_value = math.floor(self.spsa_param_clip(param, c * flip) + r)
b_value = math.floor(self.spsa_param_clip(param, -c * flip) + r)
result["w_params"].append(
{
"name": param["name"],
"value": self.spsa_param_clip(param, c * flip),
"value": w_value,
"R": param["a"] / (spsa["A"] + iter_local) ** spsa["alpha"] / c**2,
"c": c,
#Set c to the real delta after stochastic rounding is applied
"c": abs(w_value - b_value),
"flip": flip,
}
)
result["b_params"].append(
{
"name": param["name"],
"value": self.spsa_param_clip(param, -c * flip),
"value": b_value
}
)

Expand Down
5 changes: 2 additions & 3 deletions worker/games.py
Original file line number Diff line number Diff line change
Expand Up @@ -1085,13 +1085,12 @@ def launch_cutechess(
b_params = []

# Run cutechess-cli binary.
# Stochastic rounding and probability for float N.p: (N, 1-p); (N+1, p)
idx = cmd.index("_spsa_")
cmd = (
cmd[:idx]
+ [
"option.{}={}".format(
x["name"], math.floor(x["value"] + random.uniform(0, 1))
x["name"], x["value"]
)
for x in w_params
]
Expand All @@ -1102,7 +1101,7 @@ def launch_cutechess(
cmd[:idx]
+ [
"option.{}={}".format(
x["name"], math.floor(x["value"] + random.uniform(0, 1))
x["name"], x["value"]
)
for x in b_params
]
Expand Down
2 changes: 1 addition & 1 deletion worker/sri.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"__version": 232, "updater.py": "Mg+pWOgGA0gSo2TuXuuLCWLzwGwH91rsW1W3ixg3jYauHQpRMtNdGnCfuD1GqOhV", "worker.py": "DRLqEwLvuSBzNt6k3MYtlC87bp6uwdUZ/O7Xs3/d+gDox0jTOT6SO/FNWzsXVs51", "games.py": "PrNaUxAFP0wPV/jCLivA0g9mcO1vq1Og1nN3vuPa408qcKoAYuza5ZHgDCZitkQG"}
{"__version": 232, "updater.py": "Mg+pWOgGA0gSo2TuXuuLCWLzwGwH91rsW1W3ixg3jYauHQpRMtNdGnCfuD1GqOhV", "worker.py": "DRLqEwLvuSBzNt6k3MYtlC87bp6uwdUZ/O7Xs3/d+gDox0jTOT6SO/FNWzsXVs51", "games.py": "xwE2as6BgI1PKjem3C89n3CV7TNH5M3uNmsOwr23Sm7Y31PA0WXE3VvRDYFh+iUp"}

0 comments on commit 7f9ce2d

Please sign in to comment.