Page 1 of 1

Embedding Stockfish NNUE to ANY CHESS ENGINE: YouTube series

Posted: Sat Oct 17, 2020 11:17 am
by maksimKorzh
Hey what's up guys, Code Monkey King's here.

First of all I'd like to thank Daniel Shawul for creating NNUE probe library based on CFish's code.
https://github.com/dshawul/nnue-probe
because without this library my work would never exist.

I've just made a quick and dirty embedding of SF NNUE to my engine BBC and results are fantastic,
here's the example game:



There are still lots of improvements, but it now already works as a proof of concept.
Yeah, almost forgot - here's a link to YouTube series:
https://www.youtube.com/watch?v=zieTAE2 ... gMaG24HTc4

Re: Embedding Stockfish NNUE to ANY CHESS ENGINE: YouTube series

Posted: Sat Oct 17, 2020 9:59 pm
by maksimKorzh
Finally series is finished!

Here's the BBC with NNUE!
(I used handcrafted eval in the endgame though to speed up engine on ultra fast time controls)
https://github.com/maksimKorzh/bbc/tree ... c/bbc_nnue





Linux version only for now, I'm working on windows compatibility. If you manage to compile it on windows faster than I please share your solution.
Btw, if I'm now using SF NNUE does it mean that I need to change the licence to GNU GPL 3 like in SF?

Re: Embedding Stockfish NNUE to ANY CHESS ENGINE: YouTube series

Posted: Sun Oct 18, 2020 2:49 am
by dkappe
Btw, if I'm now using SF NNUE does it mean that I need to change the licence to GNU GPL 3 like in SF?
If all of the code has licenses that you can change, that would be easiest, but if you distribute MIT and GPL code together, it’s really more of an issue for someone wanting to do something that the MIT License permits but the GPL does not.

Re: Embedding Stockfish NNUE to ANY CHESS ENGINE: YouTube series

Posted: Sun Oct 18, 2020 3:04 am
by Daniel Shawul
maksimKorzh wrote:
Sat Oct 17, 2020 9:59 pm
Linux version only for now, I'm working on windows compatibility. If you manage to compile it on windows faster than I please share your solution.
Btw, if I'm now using SF NNUE does it mean that I need to change the licence to GNU GPL 3 like in SF?
A couple of notes.

You need to define compiler flags that may give you upto 5x speedup with gcc. With clang it is only about 2x but,
I suspect you are missing on this speed up.

Just add this flags when compiling the nnue.cpp file.

Code: Select all

DEFINES =
DEFINES += -DIS_64BIT 
ifeq ($(COMP),$(filter $(COMP),gcc clang win icpc))
  DEFINES += -DUSE_AVX2 -mavx2
  DEFINES += -DUSE_SSE41 -msse4.1
  DEFINES += -DUSE_SSSE3 -mssse3
  DEFINES += -DUSE_SSE2 -msse2
  DEFINES += -DUSE_SSE -msse
endif
Also I have now made it work with the MSVC windows compiler. The above flags are defined by default in the source code when using MSVC
so you can just drop them in your project and use them. There were some intrinsics for BitScanFirst that were missing.

Re: Embedding Stockfish NNUE to ANY CHESS ENGINE: YouTube series

Posted: Sun Oct 18, 2020 6:33 pm
by maksimKorzh
Finally BBC + SF NNUE version is released!
https://github.com/maksimKorzh/bbc/releases/tag/1.3

And here're some games from recent match BBC 1.2 + SF NNUE vs VICE
https://github.com/maksimKorzh/bbc/tree ... ue_vs_vice

Re: Embedding Stockfish NNUE to ANY CHESS ENGINE: YouTube series

Posted: Sun Oct 18, 2020 7:54 pm
by dkappe
A few questions:
1) the nnue should be ~80% of the speed of hce of something like Scorpio (so 20% slower). After the compiler optimizations Daniel suggested, are you seeing something similar?
2) nnue is updated a little bit with each move/null move (and undo of a move). That’s what makes it efficient. Looking at your first video it didn’t seem you were doing that but performing a nnue initialization and eval from scratch, an expensive operation. Did I misunderstand your video?

Re: Embedding Stockfish NNUE to ANY CHESS ENGINE: YouTube series

Posted: Mon Oct 19, 2020 5:17 am
by maksimKorzh
dkappe wrote:
Sun Oct 18, 2020 7:54 pm
A few questions:
1) the nnue should be ~80% of the speed of hce of something like Scorpio (so 20% slower). After the compiler optimizations Daniel suggested, are you seeing something similar?
2) nnue is updated a little bit with each move/null move (and undo of a move). That’s what makes it efficient. Looking at your first video it didn’t seem you were doing that but performing a nnue initialization and eval from scratch, an expensive operation. Did I misunderstand your video?
re: 1
- I didn't use optimization flags - my engine just crashes, I didn't dig deeper. I was interested in proofing the concept

re: 2
- Daniel's lib allows calculate NNUE scores without incremental updates and that's why I love it.

Just to give an overall direction of my work - I'm trying to deliver some complicated concepts to noobs like me so they could pick those concepts up as a proof of concept. I'm not trying to make my engine as strong and as fast as possible, I'm not interested in that because competetive aspect is something I hate the most. For me the FACT that SF NNUE works with engine I've handcrafted on my own is already a victory. Ones person achieves the goal of proving the concept he can then go for optimizations on his own.

If you're going wide spread you just don't have enough power to go deeper, on the other hand when you're endeepening significantly you can't explain complicated things in an easy way. I'm having the following problem all the time: everyone around is MUCH SMARTER than I, but they often can't explain me some very simple things. Well, maybe they can, but I'm too noob to get those explanations. So most of my efforts are spent on trying to dig the very gist of whatever concept. The entire BBC engine is a set of "proof of concepts" in all the parts of the engine (movegen, search techniques and now nnue). There are enough smart people whocreate really strong engines, bit there's noone out there who would explain chess programming to five year old kid (who is monkey at the same time). What I'm trying to do is explaining to 5 year old monkey kids)

Hope that makes sense.