Stockfish NNUE – The Complete Guide

NNUE評価関数がStockfishに移植されたので、それについて情報をまとめておきます。

本記事は、日本語と英語でお送り致します。
※ 英語版はDeepL翻訳を使って訳したものを修正したものです。

This article is provided in Japanese and English.
The English version is translated using DeepL translation.

What is the NNUE evaluation function?

Chess and shogi have similar gameplay characteristics. If you take Stockfish’s search engine, which is the thought engine of chess, and bring it to Shogi, you will find that it works well.

For the last five years or so, the developers of the shogi AI have borrowed a lot of ideas from Stockfish. With almost nothing to return to the Stockfish community. However, one of the things that the Shogi AI was unable to borrow from Stockfish was an evaluation function for that. The evaluation function has evolved independently in Shogi AI community, and now the mainstream of Shogi AI evaluation function is called NNUE, using a neural network consisting of all the connections of a lightweight, shallow layer.

The NNUE paper is pasted below.

* If you go through the DeepL translation, “Shogi AI” may be translated as “chess AI”. Be careful when reading the translation into English using the DeepL translation.

NNUE paper.
https://www.apply.computer-shogi.org/wcsc28/appeal/the_end_of_genesis_T.N.K.evolution_turbo_type_D/nnue.pdf

Mr. Noda (nodchip) who ported NNUE to Stockfish on GitHub
https://github.com/nodchip/Stockfish

Mr. Noda (nodchip) on Twitter
https://twitter.com/nodchip

Mr. Noda is the main developer of the tanuki team that developed this NNUE, and he ported the NNUE evaluation function and its training part to Stockfish. We have not surpassed the original Stockfish yet. If it does, it may replace the entire evaluation function of Stockfish with NNUE.

related information on Stockfish NNUE

I’ll paste the relevant thread.

Stockfish NN release (NNUE)
http://talkchess.com/forum3/viewtopic.php?f=2&t=74059

nnue-gui 1.0 released
http://talkchess.com/forum3/viewtopic.php?f=2&t=74212&p=847686

The Stockfish of shogi
http://talkchess.com/forum3/viewtopic.php?f=2&t=72754

My newest Stockfish NNUE versions tournament is on way.
https://chessgod101.forumotion.net/t11784-my-newest-stockfish-nnue-versions-tournament-is-on-way

Discord for Stockfish
https://discord.gg/aefaxmq

Discord #sf-nnue-dev
https://discord.com/channels/435943710472011776/718853716266188890

on standard NNUEs and their variants.

Also ported to Stockfish is the standard NNUE. This is the halfKP-256-32-32-1, as shown in the original paper. It is a neural network. In Shogi AI, this is changed to halfKP-128-32-32-1 or k-p-256-32-32-1, and then including the effects of each piece, etc. Variations have been developed.

Currently, the standard NNUE is often used in the YaneuraOu variations, so there is no doubt that the standard NNUE is strong , but if you want to be the strongest in the future, these You should also follow up on variations.

In particular, the original YaneuraOu have a Long Effect Library. The coding is rather artisanal, so porting it to Stockfish may be a painful work. A variant of the NNUE evaluation function uses this library. If you’re interested, please refer to the following articles

GitHub by YaneuraOu
https://github.com/yaneurao/YaneuraOu

Long Effect Library
https://github.com/yaneurao/YaneuraOu/blob/master/source/extra/long_effect.h
https://github.com/yaneurao/YaneuraOu/blob/master/source/extra/long_effect.cpp

tttatk’s GitHub (YaneuraOu’s fork)
https://github.com/tttak/YaneuraOu

Variants of the NNUE evaluation function developed by tttatk (there are implementations of various variants of NNUE)
https://github.com/tttak/YaneuraOu/tree/NNUE-features/source/eval/nnue/architectures

In addition, NNUE uses an all-coupled neural network; With the AVX-256 instructions, we can calculate 32 of each. It’s not a huge computational cost for a neural network of this size, but it’s possible that it would be better to structure it like CNN.(As with NNUE, the first layer should be able to calculate the difference.)

This is a subject for future research, but the highly skilled stockfish community may develop before the shogi AI community.

on NNUE’s learning commands.

Learning commands of NNUE (Tweets by Mr. Noda)
https://twitter.com/nodchip/status/1266545092648558592

on learning NNUE.

NNUE uses reinforcement learning to train its evaluation function. It uses “gensfen” command to search and generate teachers. However, there is still a lack of understanding among shogi AI developers about what kind of teachers are suitable for training. How best to set up the loss function? How much depth should be used to create a teacher? How much of the teacher positions should be generated? What is the best position ratio for the beginning and ending games? , etc.

The following articles are in Japanese, but I will present related articles if you are interested in it. I hope you will read about it through DeepL translation and other means.

How to train NNUE
http://yaneuraou.yaneu.com/2018/12/30/nnue%E8%A9%95%E4%BE%A1%E9%96%A2%E6%95%B0%E3%81%AE%E5%AD%A6%E7%BF%92%E6%96%B9%E6%B3%95%E3%81%AB%E3%81%A4%E3%81%84%E3%81%A6/

An Introduction to Shogi AI Development for Machine Learning Engineers Part 1
http://yaneuraou.yaneu.com/2020/05/03/%e6%a9%9f%e6%a2%b0%e5%ad%a6%e7%bf%92%e3%82%a8%e3%83%b3%e3%82%b8%e3%83%8b%e3%82%a2%e3%81%ae%e3%81%9f%e3%82%81%e3%81%ae%e5%b0%86%e6%a3%8bai%e9%96%8b%e7%99%ba%e5%85%a5%e9%96%80%e3%81%9d%e3%81%ae1/

Let’s learn NNUE functions, the next generation of Shogi thinking engine (Part 1: Network structure)
http://qhapaq.hatenablog.com/entry/2018/06/02/221612

Let’s learn NNUE functions, the next generation of Shogi thinking engine (2. Modification/Learning)
http://qhapaq.hatenablog.com/entry/2018/07/08/193316

Also, a lambda in NNUE training is a lambda used in the elmo expression, which You can read more about

About the ooparts brought by elmo
http://yaneuraou.yaneu.com/2017/05/23/elmo%e3%81%8c%e3%82%82%e3%81%9f%e3%82%89%e3%81%97%e3%81%9f%e3%82%aa%e3%83%bc%e3%83%91%e3%83%bc%e3%83%84%e3%81%ab%e3%81%a4%e3%81%84%e3%81%a6/

// 以下、日本語版

NNUE評価関数とは

チェスと将棋とは似たゲーム性を持つ。チェスの思考エンジンであるStockfishの探索部を将棋にそのまま持ってきても、わりとうまく機能する。

ここ5年ぐらい、将棋AIの開発者はStockfishから色々なアイデアを借りてきた。Stockfishのコミュニティにはほとんど何も返せないままに。しかし、将棋AIが、Stockfishから借りることが出来なかったものの一つとして、その評価関数がある。評価関数は、将棋AI界隈では独自に進化し、いま将棋AIで主流となっているのは、NNUEという軽量な浅い層の全結合からなるニューラルネットワークを用いた評価関数である。

以下にNNUEの論文を貼り付けておく。
※ DeepL翻訳を通すと「将棋AI」が「chess AI」と翻訳されることがあるのでDeepL翻訳を用いて英語に訳して読むときには注意して欲しい。

NNUEの論文
https://www.apply.computer-shogi.org/wcsc28/appeal/the_end_of_genesis_T.N.K.evolution_turbo_type_D/nnue.pdf

StockfishにNNUEを移植した野田さん(nodchip)のGitHub
https://github.com/nodchip/Stockfish

野田さん(nodchip)のTwitter
https://twitter.com/nodchip

野田さんは、このNNUEを開発したtanuki-チームの主開発者で、概念実証のために今回、NNUE評価関数とその学習部をStockfishに移植した。現状、Stockfish本家は超えていないものの、強化学習を何度か繰り返せば超えそうにも見える。そうなれば、Stockfishの評価関数が丸ごとNNUEに置き換わる可能性もなくはない。

Stockfish NNUEの関連情報

関連するスレッドを貼り付けておく。

Stockfish NN release (NNUE)
http://talkchess.com/forum3/viewtopic.php?f=2&t=74059

nnue-gui 1.0 released
http://talkchess.com/forum3/viewtopic.php?f=2&t=74212&p=847686

The Stockfish of shogi
http://talkchess.com/forum3/viewtopic.php?f=2&t=72754

My newest Stockfish NNUE versions tournament is on way.
https://chessgod101.forumotion.net/t11784-my-newest-stockfish-nnue-versions-tournament-is-on-way

Discord for Stockfish
https://discord.gg/aefaxmq

Discord #sf-nnue-dev
https://discord.com/channels/435943710472011776/718853716266188890

標準NNUEとその変種について

また、Stockfishに移植されているのは標準NNUEである。これは、元の論文にあるような、halfKP-256-32-32-1のニューラルネットワークである。将棋AIでは、これをhalfKP-128-32-32-1に変更したものや、k-p-256-32-32-1に変更したもの、それから各駒の利きを含めたものなど、様々なバリエーションが開発されている。

現状、やねうら王系では、標準NNUEを用いられることが多いので、標準NNUEでも最強付近に位置していることは間違いないが、今後、最強を目指すのであれば、これらのバリエーションについても追いかけたほうが良いだろう。

特に、本家やねうら王には、Long Effect Libraryと呼ばれる、長い利きを差分更新で求める、とても便利なライブラリが搭載されている。このライブラリは、やねうら王の開発者であるやねうらおが、技巧という将棋AIのアイデアを基にして開発したものであるが、わりと職人的なコーディングがなされているので、これをStockfishに移植するのは骨の折れるかもしれない。利きを利用するタイプのNNUE評価関数の変種では、これを用いている。

やねうら王のGitHub
https://github.com/yaneurao/YaneuraOu

Long Effect Library
https://github.com/yaneurao/YaneuraOu/blob/master/source/extra/long_effect.h
https://github.com/yaneurao/YaneuraOu/blob/master/source/extra/long_effect.cpp

tttatkさんのGitHub(やねうら王のfork)
https://github.com/tttak/YaneuraOu

tttatkさんの開発したNNUE評価関数の変種(NNUEの様々な変種の実装がある)
https://github.com/tttak/YaneuraOu/tree/NNUE-features/source/eval/nnue/architectures

また、NNUEでは全結合のニューラルネットワークを用いている。AVX-256の命令が使えば、32個ずつ計算できるので、この規模のニューラルネットワークであればさほど大きな計算コストではないが、CNNのような構造にしたほうが良い可能性もある。(NNUE同様に、一層目は差分計算できるはず)

このへんは我々の今後の研究課題ではあるが、開発力の高いStockfish界隈のコミュニティのほうが、将棋AI界隈のコミュニティより先じて開発してしまうかもしれない。

NNUEの学習コマンドについて

NNUEの学習コマンドについて(野田さんのツイート)
https://twitter.com/nodchip/status/1266545092648558592

NNUEの学習について

NNUEでは強化学習を用いてその評価関数を学習させる。これには、探索して教師を生成するgensfenコマンドを用いる。しかし、どういった性質の教師が学習に向いているかについてはまだ将棋AI開発者の間でもよくわかっていないところはある。損失関数はどう設定するのがベストであるか。教師を生成するときのdepthはいくらにすべきか。教師局面はどれくらい用意すべきか。etaは?lambdaは?序盤と終盤の局面はどれくらいの比率であるべきか等々。

以下、日本語の記事になってしまうが、関連記事を提示しておくので、興味のある人はDeepL翻訳などを通じて読んで欲しい。

NNUEの学習方法
http://yaneuraou.yaneu.com/2018/12/30/nnue%E8%A9%95%E4%BE%A1%E9%96%A2%E6%95%B0%E3%81%AE%E5%AD%A6%E7%BF%92%E6%96%B9%E6%B3%95%E3%81%AB%E3%81%A4%E3%81%84%E3%81%A6/

機械学習エンジニアのための将棋AI開発入門その1
http://yaneuraou.yaneu.com/2020/05/03/%e6%a9%9f%e6%a2%b0%e5%ad%a6%e7%bf%92%e3%82%a8%e3%83%b3%e3%82%b8%e3%83%8b%e3%82%a2%e3%81%ae%e3%81%9f%e3%82%81%e3%81%ae%e5%b0%86%e6%a3%8bai%e9%96%8b%e7%99%ba%e5%85%a5%e9%96%80%e3%81%9d%e3%81%ae1/

次世代の将棋思考エンジン、NNUE関数を学ぼう(その1.ネットワーク構造編)
http://qhapaq.hatenablog.com/entry/2018/06/02/221612

次世代の将棋思考エンジン、NNUE関数を学ぼう(その2.改造/学習編)
http://qhapaq.hatenablog.com/entry/2018/07/08/193316

また、NNUEの学習の時のlambdaとは、elmo式で用いるlambdaで、これについては以下の記事に詳しい。

elmoがもたらしたオーパーツについて
http://yaneuraou.yaneu.com/2017/05/23/elmo%e3%81%8c%e3%82%82%e3%81%9f%e3%82%89%e3%81%97%e3%81%9f%e3%82%aa%e3%83%bc%e3%83%91%e3%83%bc%e3%83%84%e3%81%ab%e3%81%a4%e3%81%84%e3%81%a6/

Stockfish NNUE – The Complete Guide」への4件のフィードバック

  1. Ok. So the NNUE’s incremental technique only applies to the first layer, and from the second layer, NNUE gains performance by exploiting SIMD instructions. Is my understanding right?

      • Thank you for the answer. I’ve implemented int8 CNN with AVX2. Input size 11 * 11(1 padding) * 140(input channel) and output size 9 * 9 * 140(output channel). The forward took 500 microsecs with -O3 on colab, still too slow for training with self-play MCTS(800 simulations). Besides I didn’t implement backpropagation yet, and it seems that Stockfish does some scaling when dequantizing and quantizing the weights and biases which I couldn’t understand.

Ta(ry へ返信する コメントをキャンセル

メールアドレスが公開されることはありません。 が付いている欄は必須項目です