Closed benjaminpjones closed 3 months ago
Fixed an off by one error introduced by this PR.
BTW no worries if this isn't a change you'd like to merge. Just let me know!
I would like to merge this change! It's some serious performance boost. I just wanted to take the time to understand the changes that you made before merging so I can improve other code down the line.
Awesome! No worries and thanks for taking time to understand it :)
Let me know if I can answer any questions too
Looks good!
I was also able to create an action map using modulus and division with only one for loop but it seems those operations take a little bit more time.
Fixed an off by one error introduced by this PR.
I did find an issue with this. By adding one, we are changing the move played on the board. For example, if the best move determined by the engine was action 359, the engine plays some other move like action 360 on the board. By removing the + 1 in switch_19x19.get(action[0] + 1, default_case_19x19)()
the engine plays the correct move on the board.
I think the issue is that we changed the index of x and y coordinates of the action map from 0 to 1. The hardcoded action map's first element was [1,1] while this action map's first element is [0,0]
Fixed an off by one error introduced by this PR.
I haven't been able to reproduce this error after removing the + 1. Could you provide some more details?
I was having trouble with, for example, action == 19
which lead to get_action_offset((0, 1))
The unit test in test_offset_moves.py will fail on (0,0) if the +1
is removed.
I'm not at a computer right now, will try to find where the discrepancy comes from later.
I think the issue is that we changed the index of x and y coordinates of the action map from 0 to 1.
This was by design - the Coordinate
class did a conversion to 0-based index. In order to use the same objects in the action map as the Coordinate code, I had to choose between 0-based and 1-based.
If we change it to 1-based, a lot of code that used Coordinate
class will break.
Just tried to repro, not having much luck. Bot played 166, and here I count to 166 it seems right to me:
I checked the white stone in the same way. Is there like a code test that would demonstrate the issue?
Bot played 166, and here I count to 166 it seems right to me:
Is this with or without the +1 in the code?
Fixed the issue! Adding the +1 in the offset_moves file was correct. The error came from the get_sgf_data function which was 1 indexed.
Oh cheers! Thanks for finding that!
Background
I ran a CPU profile with Scalene and it shows
Coordinate.__init__()
taking roughly 31% of CPU time. Related functions likeBoard.at()
andCoordinate.__hash__()
are also taking non-negligible time.This PR attempts to address the performance cost by replacing List and Coordinate representations with Tuples. There are a couple principles at play here:
Coordinate
are on the hot path, it might be worth switching totuple
tuple
uses less resources thanlist
. Dynamically sized containers likelist
need to over-allocate memory in case elements are added later. Moreover,tuple
are immutable, so atuple
may have some optimizations enabled by the fact that its elements may not be changed.Proposed Changes
list
totuple
python -m unittest
Results
Roughly 60% speedup in simulation time
Before:
After: