r/ComputerChess • • May 07 '20

Do chess engines always evaluate a position the same way?

Do an engine like Stockfish always make the same evaluation for a position after a set amount of time or for a certain amount of moves analyzed?

Do neural networks act differently in this regard?

I guess so, or engine tournaments would not be a thing, right? So, what exactly is the "random" part of their coding?

11 Upvotes

23 comments sorted by

6

u/CCchess May 08 '20 edited May 08 '20

In Stockfish multi-threaded, there is a large source of randomess from Lazy SMP. BTW can I give a shoutout to whoever populated all the information on that site, it's a gold mine.

There is still only a single hash table (table that stores the evaluation for any position searched so far), and all the threads access it at once.

Every thread still runs the exact same search algorithm (it doesn't attempt to assign variations to threads). But thanks to the hash table, threads mostly avoid re-calculating each other's work because if they find a position already in the hash table then they will go on to sub variations immediately instead of recalculating that position, and so on.

This introduces randomness in two ways:

  • Depending on nano-scale timing issues , whichever thread writes a position first in the table then obviously disturbs how further searches past that position goes, this kind of chaotically expands.

  • The table has no locking, so threads can simultaneously read and write an entry causing effectively random corrupted results.

I have noticed from running multiple Engine Matches from a start position that Leela usually plays identical moves each run if given the chance, while Stockfish (4 threads) can deviate as soon as the first move .

If you run Stockfish in single-threaded mode then it gives much more stable results. Although I'm still not sure if this is entirely deterministic. I often see different output strings from re-running the same position single-threaded (ponder of course), although this might just be in timing to do with when it decides to output an update, maybe not any difference in the actual search tree.

1

u/tsojtsojtsoj May 09 '20

Some engines don't flush their transposition table between runs.

1

u/CCchess May 09 '20

Yes there's that too. Stockfish uses a 5-bit counter on each entry whose only effect is that when the bucket is full it'll prefer overwriting the older entry. Updating the counter each time a new search begins.

3

u/tsojtsojtsoj May 07 '20 edited May 07 '20

The time is a random factor, because sometimes engines search 1000k nodes per second and sometime 1200k nodes per second. This depends on all kinds of things, even down to the temperature of your room.

If you set an engine to search to a specific depth or a specific node count, then it will always return the same evaluation of a position, if your computer doesn't make a mistake (which is pretty rare).

Some engines provide a built-in random feature that allows the engine to choose randomly between moves that are almost equally good. (But these features won't normally be used in tournaments, because the engines tend to be worse if they use it)

I don't think neural network engines act any differently in this aspect, neural networks are deterministic just like alpha-beta search.

There could be a difference for MCTS-engines because of the random rollouts that are done at the leave nodes, but as far as I know chess engines don't really apply this part of MCTS, because random rollouts are just too bad to detect even simple tactics.

In tournaments there is often an opening book, so even if the engines would always play the same against each other, they won't play the same when they encounter different starting positions each tournament.

But even a tournament without opening book would make sense in a limited way: Each engine plays against each other engine two times, once as black, once as white. Then there won't be any duplicate games.

An important point for engine tournaments is that the engines get developed between tournaments, so they get better. Then they won't play exactly the same tournament as last time.

EDIT: u/Ilyps has a good point, multiple threads add to the nondeterministic nature of engines in tournaments (additionally to the time-and-calculation-speed component), since each thread might execute at a different speed each time.

10

u/Ilyps May 07 '20

If you set an engine to search to a specific depth or a specific node count, then it will always return the same evaluation of a position

This is only true for single-threaded calculations. Most engines use multiple threads and are thus non-deterministic.

2

u/tsojtsojtsoj May 07 '20

Most engines use multiple threads

most engines in top tier tournaments like TCEC. I'm not 100% sure but I think most engines on CCRL are single-threaded.

1

u/AlayanT May 08 '20

There is a plethora of weak, old or buggy engines that don't support multiple threads, but most engines in the CCRL top 100 support it. There is a bias to consider mostly middle-tier and top-tier engines when making statements, as weak or old engines have little relevance.

Obviously, any engine supporting multiple threads can still run in single-threaded mode, which is useful if you want to analyze positions or play games in parallel.

2

u/AdmiralAdama99 May 07 '20

if your computer doesn't make a mistake (which is pretty rare).

The chances of your computer making a logical mistake are pretty much non existent. If your computer is making mistakes, then it is likely there is a dying hardware component (RAM for example) and you will get a blue screen. The chances of getting an error without quickly getting a crash are extremely small.

1

u/tsojtsojtsoj May 07 '20

2

u/AdmiralAdama99 May 07 '20

Interesting article. Thanks for linking. It basically says the RAM error rate could be anywhere from 1 bit per hour to 1 bit per millennium. I wish it were more precise.

From my own experiences, I'd say a stick of RAM probably fails every 10 years. The user is alerted to the failure by blue screens. Once every 10 years would be within the range given above. (It's a very large range, pretty much everything is in it, lol)

Anyway, I don't think that article invalidates my statement, which is that most RAM errors quickly trigger a blue screen, and therefore don't really have a chance to quietly wreak havoc, change chess AI calculations, etc. RAM errors tend to loudly announce themselves to the user by crashing the system.

0

u/tsojtsojtsoj May 08 '20

Mmmh, I would agree with you, that for a normal user the memory bit flippings are most of the time not apparent, because they are rare. But I don't see, how a single bit flip in memory that shouldn't have happened could cause an operating system to crash? Memory errors are not necessarily the same as the failure of a RAM module. It could just be that you turned your microwave on and a spike in radio energy caused a current in a transistor in your memory.

3

u/Sapiogram May 07 '20

Stockfish is completely deterministic when run to a certain depth on a single thread. This fact is very useful for debugging and development purposes, so the devs make sure it stays that way.

If you run it for x seconds, it won't be fully deterministic, because other programs running in the background will randomly steal CPU time and cause it to run slower/faster.

If you run it on multiple threads, determinism is completely out the window. If you run Stockfish several times on the same position, it's not unusual for it to sometimes take much longer to find the correct move. The exact timing of when the threads arrive at each position quickly causes the different runs to diverge.

I guess so, or engine tournaments would not be a thing, right?

Engine tournaments usually force the engines to start from pre-decided openings, to avoid this problem. When engines play from the starting position, you never get the exact same game because of the above effects, but they're often similar, and rather dull.

2

u/Zulban May 07 '20 edited May 07 '20

The keyword you're looking for here is "determinism". Your question is "which chess engines are deterministic?"

Generally, the answer is that strictly speaking they are not deterministic. This is due to lots of factors you can learn about if you study computer science. Some of the big reasons for non-determinism are hashing and pseudorandom number generation for MC search. However in a practical sense, they are fairly deterministic.

Engines tend to converge on a preferred move. So the more they think, the less likely it is for them to change their mind. Or at the start and end of a game they may simply use a lookup table of exactly what to do.

6

u/Sapiogram May 07 '20

Some of the big reasons for non-determinism are hashing

Nope. You'd have to deliberately initialize your zobrist keys with randomness from the OS, which is a terrible idea that nobody would ever do. In fact, I have never heard of any kind of hashing scheme that is not deterministic.

pseudorandom number generation for MC search

No serious chess engines uses random MC rollouts. Even if they did, they would probably seed the randomness from the position, not from some outside source of entropy.

Engines tend to converge on a preferred move.

I don't think this is accurate. For reasonably quiet positions, most engines can think for days and still change their mind every few iterations. In my experience, if an engines doesn't converge on a move rather quickly, it will keep changing its mind indefinitely.

However in a practical sense, they are fairly deterministic.

I can only really speak for Stockfish here, but with higher core counts, Stockfish is not very deterministic at all. Play it against itself multiple times, and it won't play the same game twice.

1

u/CCchess May 08 '20

In my experience, if an engines doesn't converge on a move rather quickly, it will keep changing its mind indefinitely.

After doing some work on Stockfish I feel this has to do with hash collisions. Say we have a 2GB hash table; this works out to 226 buckets with 3 entries each. As per the birthday paradox, collisions can be expected after about 213 nodes and so this will pretty quickly overflow the buckets. (If you set it to log hash collisions, it's frightening!).

So once it has been running for a while I suspect it ends up in a pattern where a position is analyzed, and then overwritten by different positions that happen to land in the same bucket, which is then overwritten again by the first position etc. etc.

0

u/Zulban May 07 '20 edited May 08 '20

Nope. You'd have to deliberately initialize your zobrist keys with randomness from the OS, which is a terrible idea that nobody would ever do. In fact, I have never heard of any kind of hashing scheme that is not deterministic.

Ah, not the kind of hashing I was talking about, and indeed I've implemented zobrist hashing before. In lots of data types, a memory reference is hashed. Those memory references are different every run. That's why some data types (like the Python dictionary for example) are not ordered, and you shouldn't make code that assumes they are because it's non-deterministic.

I guess when I said hashing it was misleading.

No serious chess engines uses random MC rollouts.

Well, Alpha Zero does, the world champion AI. Give the paper a read, it's great.

2

u/AlayanT May 09 '20

If you think Alpha Zero uses random MC rollouts, you don't understand it.

And AlphaZero is not a "world champion AI". It has never competed in any open engine competition, only in internal matches supervised by its creators, some results and games of which were disclosed.

1

u/Zulban May 09 '20

If you think Alpha Zero uses random MC rollouts, you don't understand it.

Have you read the paper? I know of two.

1

u/AlayanT May 09 '20

There are two, and you seem to have not properly understood how AlphaZero works. As I said, there are no random rollouts at all. A0 uses PUCT with a "neural network" as the predictor.

1

u/Zulban May 09 '20

From one of the papers:

Instead of an alpha-beta search with domain-specific enhancements,AlphaZerouses a general-purpose Monte-Carlo tree search (MCTS) algorithm.

As I recall, the first one used two neural nets. The second just one - the NN evaluation of a board is also used to generate the probability of exploring a branch deeper.

It sounds like you're really interested in this topic, again I really recommend both papers.

1

u/AlayanT May 10 '20

UCT and PUCT : https://www.chessprogramming.org/UCT

The so-called "MCTS" used by AlphaZero and LeelaChessZero has no random rollouts at all, so the "monte carlo" part implying random rollout is a misnomer.

There are some historical reasons in algorithm improvements that led to this terrible naming choice, as UCT is refinement of true MCTS (that is, with actual random rollouts).

But in AlphaZero, the rollouts are taken out and replaced by a prediction from the neural network of the likelihood of a given move being the best one. For a given net, this is completely deterministic. So back to the start, Sapiogram was right when claiming : "No serious chess engines uses random MC rollouts".

1

u/Zulban May 10 '20

I have two questions then. Perhaps you can help me better understand this section, starting at the end of page 2 of my link (the PDF copy paste here is a mess, so maybe look at the paper):

This neural network takes the board po-sitionsas an input and outputs a vector of move probabilitiespwith componentspa=Pr(a|s)2 for each actiona, and a scalar valuevestimating the expected outcomezfrom positions,v≈E[z|s].AlphaZerolearns these move probabilities and value estimates entirely from self-play; these are then used to guide its search.Instead of an alpha-beta search with domain-specific enhancements,AlphaZerouses a general-purpose Monte-Carlo tree search (MCTS) algorithm. Each search consists of a series of simu-lated games of self-play that traverse a tree from rootsrootto leaf. Each simulation proceeds byselecting in each statesa moveawith low visit count, high move probability and high value(averaged over the leaf states of simulations that selectedafroms) according to the currentneural networkfθ. The search returns a vectorπrepresenting a probability distribution overmoves, either proportionally or greedily with respect to the visit counts at the root state.The parametersθof the deep neural network inAlphaZeroare trained by self-play reinforce-ment learning, starting from randomly initialised parametersθ. Games are played by selectingmoves for both players by MCTS,at∼πππt. At the end of the game, the terminal positionsTisscored according to the rules of the game to compute the game outcomez:−1for a loss,0fora draw, and+1for a win. The neural network parametersθare updated so as to minimise theerror between the predicted outcomevtand the game outcomez, and to maximise the similarityof the policy vectorptto the search probabilitiesπππt.

Specifically:

  1. why are they talking about "move probabilities" and "search probabilities" if no randomness is involved?

  2. why didn't the AlphaZero paper say UCT or PUCT instead of MCTS?

The so-called "MCTS" used by AlphaZero

It is not "so-called". It is "called". If the authors of the A0 paper chose to say MCTS and not UCT/PUCT, they talk about search probabilities, and tons of sources talking about MCTS online mention "random", I'm more inclined to side with them than a random reddit comment. Though the depth of your criticism is intriguing me.

2

u/AlayanT May 10 '20

The "move probabilities" in outputs are estimations made by the neural network, not involving any MC rollout."Simulated games of self-play" is a fancy way to say that the expansion of the tree is prioritized based on NN's prediction + subsequent leaf evals, with nodes that have been under-explored according to this being scheduled for more explorations. The initial output probabilities from the NN get updated by the search algorithm during search, producing the "search probabilities" referenced at the end and that are used for training.

My previous message explains the origin of the MCTS misnaming. I insist on so-called. Cambridge dictionary :

used to show that you think a word that is used to describe someone or something is not suitable or not correct:

Just go ask the Leela Chess Zero folks on their discord if you don't believe me on no random rollouts whatsoever happening in their A0-inspired engine. Or checkout their source code, it's available on Github.