Attempting to implement NNUE into my engine

Discussion of chess software programming and technical issues.

Moderator: Ras

sovaz1997
Posts: 289
Joined: Sun Nov 13, 2016 10:37 am

Re: Attempting to implement NNUE into my engine

Post by sovaz1997 »

rdhoffmann wrote: Tue Jan 28, 2025 9:48 pm The smallest network I trained was (768+64) x 28 x 1 and it is already much better than my HCE attempts.

Note the extra 64 inputs, I use it for the side to move (+/- 1) and trying out various ideas.

One question though, why would such small networks need so many positions? I don't see how (or why) that will improve performance. There is only so much a small network can learn?
Interesting!
We've actually discussed a lot of things in Discord
Starting with the fact that I didn't do sigmoid to calculate loss :lol:
There are results, there are improvements, but I can’t surpass my eval yet, maybe there are still some errors somewhere
Zevra 2 is my chess engine. Binary, source and description here: https://github.com/sovaz1997/Zevra2
Zevra v2.6 is last version of Zevra: https://github.com/sovaz1997/Zevra2/releases
sovaz1997
Posts: 289
Joined: Sun Nov 13, 2016 10:37 am

Re: Attempting to implement NNUE into my engine

Post by sovaz1997 »

One question though, why would such small networks need so many positions? I don't see how (or why) that will improve performance. There is only so much a small network can learn?
This number of positions is just an attempt to make sure that there are enough of them :)
Zevra 2 is my chess engine. Binary, source and description here: https://github.com/sovaz1997/Zevra2
Zevra v2.6 is last version of Zevra: https://github.com/sovaz1997/Zevra2/releases
sovaz1997
Posts: 289
Joined: Sun Nov 13, 2016 10:37 am

Re: Attempting to implement NNUE into my engine

Post by sovaz1997 »

Funny King run from Zevra NNUE :)

[pgn]
[Event "?"]
[Site "?"]
[Date "2025.01.29"]
[Round "?"]
[White "Zevra NNUE"]
[Black "zevra_classic"]
[Result "1-0"]
[ECO "C40"]
[GameDuration "00:02:12"]
[GameEndTime "2025-01-29T06:39:07.903 +06"]
[GameStartTime "2025-01-29T06:36:55.682 +06"]
[Opening "QP counter-gambit (elephant gambit)"]
[PlyCount "137"]
[TimeControl "1/move"]

1. e4 {+0.62/18 1.0s} e5 {-0.34/16 1.0s} 2. Nf3 {+0.47/18 1.0s}
d5 {-0.40/16 1.0s} 3. exd5 {+1.00/16 1.0s} e4 {-0.67/17 1.0s}
4. Qe2 {+0.81/18 1.0s} Be7 {-0.67/19 1.0s} 5. Qxe4 {+0.81/18 1.0s}
Nf6 {-0.61/18 1.0s} 6. Qa4+ {+0.87/18 1.0s} c6 {-0.63/16 1.0s}
7. d4 {+1.09/18 1.0s} O-O {-0.25/15 1.0s} 8. Ne5 {+0.78/16 1.0s}
Nxd5 {-0.01/15 1.0s} 9. c4 {+0.37/16 1.0s} Nb4 {+0.39/17 1.0s}
10. Be3 {+0.43/18 1.0s} Bf5 {+0.43/17 1.0s} 11. Na3 {+0.31/18 1.0s}
Nd7 {+0.48/16 1.0s} 12. Nxd7 {+0.37/18 1.0s} Qxd7 {+0.47/16 1.0s}
13. Be2 {+0.25/19 1.0s} Nd3+ {+0.30/15 1.0s} 14. Bxd3 {+0.56/19 1.0s}
Bxd3 {+0.08/17 1.0s} 15. Qb3 {+0.43/18 1.0s} Be4 {+0.10/18 1.0s}
16. f3 {+0.50/18 1.0s} Bh4+ {+0.17/15 1.0s} 17. g3 {+0.54/18 1.0s}
Bxf3 {-0.03/16 1.0s} 18. Rf1 {+0.50/19 1.0s} Qg4 {+0.41/17 1.0s}
19. gxh4 {+1.25/15 1.0s} Rae8 {+0.33/17 1.0s} 20. Kd2 {+1.68/17 1.0s}
f5 {+0.31/16 1.0s} 21. d5 {+1.43/16 1.0s} cxd5 {+0.12/15 1.0s}
22. cxd5 {+1.32/15 1.0s} f4 {0.00/15 1.0s} 23. d6+ {+1.68/16 1.0s}
Kh8 {-0.15/16 1.0s} 24. Bxa7 {+1.68/18 1.0s} Re2+ {-0.29/16 1.0s}
25. Kc3 {+1.78/18 1.0s} Rxh2 {-0.30/15 1.0s} 26. Rf2 {+1.89/15 1.0s}
Qg3 {+0.47/15 1.0s} 27. Qb6 {+1.78/18 1.0s} Rxh4 {+0.38/16 1.0s}
28. Kb4 {+1.78/15 1.0s} Bd5 {0.00/16 1.0s} 29. b3 {+2.68/15 1.0s}
f3+ {+0.07/14 1.0s} 30. Nc4 {+2.37/16 1.0s} Qg4 {-0.70/16 1.0s}
31. Qc5 {+2.61/15 1.0s} Bc6 {-1.72/15 1.0s} 32. d7 {+3.31/19 1.0s}
Rg8 {-2.67/17 1.0s} 33. Qe7 {+3.31/18 1.0s} Bxd7 {-3.05/18 1.0s}
34. Rxf3 {+0.97/17 1.0s} Qxf3 {-3.08/15 1.0s} 35. Qxd7 {+1.06/15 1.0s}
b5 {-0.52/13 1.0s} 36. Qxb5 {+1.91/15 1.0s} Qf7 {-0.95/13 1.0s}
37. Bc5 {+1.54/16 1.0s} Qf4 {-1.21/15 1.0s} 38. Qc6 {+1.46/16 1.0s}
Qh2 {-1.35/15 1.0s} 39. Bd6 {+2.53/15 1.0s} Qe2 {-1.21/15 1.0s}
40. Ka5 {+2.62/15 1.0s} Re8 {-1.20/14 1.0s} 41. Kb6 {+2.81/14 1.0s}
Rhe4 {-1.22/16 1.0s} 42. a4 {+3.02/15 1.0s} Qc2 {-1.14/15 1.0s}
43. Ra3 {+3.52/17 1.0s} Qf2+ {-1.82/15 1.0s} 44. Bc5 {+3.37/17 1.0s}
Qg3 {-1.81/16 1.0s} 45. Nd6 {+3.63/16 1.0s} R4e6 {-2.25/17 1.0s}
46. a5 {+3.80/17 1.0s} Rb8+ {-2.62/16 1.0s} 47. Ka7 {+4.13/19 1.0s}
Rf8 {-3.01/17 1.0s} 48. a6 {+4.10/18 1.0s} Qg5 {-2.54/16 1.0s}
49. Ra4 {+4.18/17 1.0s} Re5 {-3.23/16 1.0s} 50. Nb7 {+4.37/16 1.0s}
Rg8 {-3.95/16 1.0s} 51. Kb6 {+6.33/18 1.0s} Qg3 {-4.36/17 1.0s}
52. b4 {+7.00/18 1.0s} Ree8 {-6.29/15 1.0s} 53. a7 {+7.43/18 1.0s}
Ra8 {-6.82/17 1.0s} 54. Nd6 {+7.31/18 1.0s} Qg6 {-6.94/17 1.0s}
55. b5 {+7.67/18 1.0s} h6 {-7.16/17 1.0s} 56. Kb7 {+7.90/17 1.0s}
Qf6 {-7.24/14 1.0s} 57. b6 {+9.16/15 1.0s} Rgd8 {-8.03/15 1.0s}
58. Qe4 {+11.18/16 1.0s} Rf8 {-8.94/14 1.0s} 59. Kc7 {+13.56/15 1.0s}
Qc3 {-11.43/13 1.0s} 60. Kc6 {+13.83/17 1.0s} Rac8+ {-12.98/15 1.0s}
61. Nxc8 {+19.08/18 1.0s} Rxc8+ {-18.82/15 1.0s} 62. Kd7 {+22.36/18 1.0s}
Rg8 {-21.81/16 1.0s} 63. a8=Q {+27.08/16 1.0s} Qh3+ {-25.34/16 1.0s}
64. Ke7 {+M13/24 1.0s} Qb3 {-M14/19 1.0s} 65. Bd4 {+M9/27 1.0s}
Qb4+ {-M8/47 1.0s} 66. Rxb4 {+M7/81 1.0s} Rxa8 {-M6/128 0.49s}
67. Qg6 {+M5/128 0.52s} Re8+ {-M4/128 0.010s} 68. Kxe8 {+M3/128 0.002s}
h5 {-M2/128 0.002s} 69. Qxg7# {0s, White mates} 1-0


[/pgn]
Zevra 2 is my chess engine. Binary, source and description here: https://github.com/sovaz1997/Zevra2
Zevra v2.6 is last version of Zevra: https://github.com/sovaz1997/Zevra2/releases
sovaz1997
Posts: 289
Joined: Sun Nov 13, 2016 10:37 am

Re: Attempting to implement NNUE into my engine

Post by sovaz1997 »

Some update: I have good progress here,
So, I generate new 140M positions and have this results with net 768x256 with 10+0.1:

Code: Select all

Score of Zevra NNUE vs Zevra Classic: 189 - 267 - 168  [0.438] 624
...      Zevra NNUE playing White: 110 - 120 - 82  [0.484] 312
...      Zevra NNUE playing Black: 79 - 147 - 86  [0.391] 312
...      White vs Black: 257 - 199 - 168  [0.546] 624
Elo difference: -43.7 +/- 23.4, LOS: 0.0 %, DrawRatio: 26.9 

So, really close to classic eval for now

My next goals:
1) More positions (about 500 millions)
2) the net structure with opposite inputs
3) Try lower or larger nets
4) Use WDL stats
Zevra 2 is my chess engine. Binary, source and description here: https://github.com/sovaz1997/Zevra2
Zevra v2.6 is last version of Zevra: https://github.com/sovaz1997/Zevra2/releases
sovaz1997
Posts: 289
Joined: Sun Nov 13, 2016 10:37 am

Re: Attempting to implement NNUE into my engine

Post by sovaz1997 »

Fully-zero NNUE (started from random games, and now after 4th gens) vs HCE
Still much weaker, but I just try to play one game, and it was funny

[pgn]
[Event "?"]
[Site "?"]
[Date "2025.02.04"]
[Round "?"]
[White "Zevra NNUE"]
[Black "zevra_classic"]
[Result "1-0"]
[ECO "A00"]
[GameDuration "00:01:31"]
[GameEndTime "2025-02-04T06:28:59.730 +06"]
[GameStartTime "2025-02-04T06:27:27.992 +06"]
[Opening "Polish (Sokolsky) opening"]
[PlyCount "87"]
[TimeControl "1/move"]

1. b4 {+0.90/17 1.1s} e5 {+0.75/15 1.1s} 2. Bb2 {+0.82/17 1.1s}
d5 {+0.96/15 1.1s} 3. Bxe5 {+1.78/16 1.1s} Nc6 {+0.76/17 1.1s}
4. Bb2 {+1.36/17 1.1s} Nxb4 {+0.60/18 1.1s} 5. e3 {+2.03/16 1.1s}
Bf5 {+0.85/16 1.1s} 6. d3 {+2.16/17 1.1s} Nf6 {+0.67/16 1.1s}
7. h3 {+2.42/16 1.1s} Bd6 {+0.81/14 1.1s} 8. Nf3 {+2.86/15 1.1s}
O-O {+0.82/14 1.1s} 9. g4 {+2.40/15 1.1s} Bg6 {+0.56/14 1.1s}
10. g5 {+2.23/16 1.1s} Nd7 {+1.05/15 1.1s} 11. a3 {+1.86/16 1.1s}
Nc6 {+1.05/16 1.1s} 12. Nc3 {+1.33/17 1.1s} d4 {+1.29/16 1.1s}
13. exd4 {+1.10/16 1.1s} Re8+ {+1.11/17 1.1s} 14. Be2 {+1.21/16 1.1s}
Bh5 {+1.19/17 1.1s} 15. Ne4 {+2.81/18 1.1s} Bxf3 {+1.12/17 1.1s}
16. Bxf3 {+2.86/16 1.1s} Qxg5 {+1.03/17 1.1s} 17. Kf1 {+2.09/18 1.1s}
Qh4 {+0.92/16 1.1s} 18. Bg4 {+1.75/17 1.1s} Nb6 {+1.18/15 1.1s}
19. Rg1 {+2.74/16 1.1s} Rad8 {+1.17/14 1.1s} 20. Qf3 {+3.18/15 1.1s}
Be7 {+0.80/13 1.1s} 21. d5 {+8.61/16 1.1s} Nxd5 {-3.27/13 1.1s}
22. Be6 {+12.23/16 1.1s} Bf6 {-9.85/13 1.1s} 23. Nxf6+ {+19.00/16 1.1s}
Nxf6 {-11.17/15 1.1s} 24. Bxf6 {+20.57/18 1.1s} Rxe6 {-11.38/17 1.1s}
25. Bxh4 {+20.84/17 1.1s} Rde8 {-11.54/17 1.1s} 26. Bf6 {+20.79/18 1.1s}
g6 {-11.52/18 1.1s} 27. Rg5 {+21.49/18 1.1s} Nb8 {-11.35/17 1.1s}
28. Rd5 {+21.69/19 1.1s} c6 {-11.83/17 1.1s} 29. Rd8 {+22.42/20 1.1s}
Rxd8 {-12.02/16 1.1s} 30. Bxd8 {+22.92/20 1.2s} Nd7 {-12.52/16 1.1s}
31. Kg2 {+22.44/18 1.1s} Nf8 {-11.58/15 1.1s} 32. Rb1 {+23.43/19 1.1s}
b5 {-12.33/18 1.1s} 33. Rb4 {+23.66/18 1.1s} f5 {-12.59/16 1.1s}
34. Bg5 {+25.72/19 1.1s} a6 {-13.20/14 1.1s} 35. Rd4 {+28.55/18 1.1s}
Kg7 {-14.93/15 1.1s} 36. Qf4 {+32.72/18 1.1s} Re8 {-17.32/17 1.1s}
37. Qc7+ {+34.15/20 1.1s} Kg8 {-21.00/17 1.1s} 38. Qxc6 {+36.46/19 1.1s}
Re6 {-22.43/17 1.1s} 39. Rd6 {+39.93/18 1.1s} Re5 {-24.68/16 1.1s}
40. Bf6 {+54.53/17 1.1s} Re7 {-M10/25 1.1s} 41. Bxe7 {+M9/27 1.1s}
Nd7 {-M8/33 1.1s} 42. Rxd7 {+M7/33 1.1s} f4 {-M6/128 0.92s}
43. Qd5+ {+M5/128 0.87s} Kh8 {-24.28/128 0.011s} 44. Bf6# {0s, White mates} 1-0
[/pgn]

Hard to improve, I hope this way will works :)
Zevra 2 is my chess engine. Binary, source and description here: https://github.com/sovaz1997/Zevra2
Zevra v2.6 is last version of Zevra: https://github.com/sovaz1997/Zevra2/releases
nionita
Posts: 180
Joined: Fri Oct 22, 2010 9:47 pm
Location: Austria
Full name: Niculae Ionita

Re: Attempting to implement NNUE into my engine

Post by nionita »

How many epochs do you train? Did the 500 million positions still not help?
sovaz1997
Posts: 289
Joined: Sun Nov 13, 2016 10:37 am

Re: Attempting to implement NNUE into my engine

Post by sovaz1997 »

nionita wrote: Thu Feb 06, 2025 7:53 pm How many epochs do you train? Did the 500 million positions still not help?
I tried different, from maybe 10-20 to 200

Now I do self-play, and for now -300 elo to classic with 768x32. Plays beautifully, now I'm on 7th-generation

[pgn]
[Event "?"]
[Site "?"]
[Date "2025.02.07"]
[Round "?"]
[White "zevra-self-v7"]
[Black "zevra_classic"]
[Result "1-0"]
[ECO "C40"]
[GameDuration "00:01:30"]
[GameEndTime "2025-02-07T14:49:46.660 +06"]
[GameStartTime "2025-02-07T14:48:16.650 +06"]
[Opening "QP counter-gambit (elephant gambit)"]
[PlyCount "91"]
[TimeControl "1/move"]

1. e4 {+0.22/16 1.0s} e5 {-0.34/16 1.0s} 2. Nf3 {+0.46/16 1.0s}
d5 {-0.45/17 1.0s} 3. Nxe5 {+0.86/17 1.0s} dxe4 {+0.13/16 1.0s}
4. Bc4 {+1.32/18 1.0s} Qg5 {+0.97/17 1.0s} 5. d4 {+0.69/18 1.0s}
Qxg2 {+1.03/17 1.0s} 6. Bxf7+ {+1.22/18 1.0s} Ke7 {+1.38/18 1.0s}
7. Rf1 {+1.14/18 1.0s} Nf6 {+1.14/18 1.0s} 8. Nc3 {+1.04/17 1.0s}
Bh3 {+1.00/17 1.0s} 9. Bc4 {+1.48/18 1.0s} Nbd7 {+0.85/16 1.0s}
10. Bf4 {+1.57/17 1.0s} Nb6 {+1.03/15 1.0s} 11. Be2 {+1.92/18 1.0s}
Ke8 {+1.23/16 1.0s} 12. Qd2 {+4.34/17 1.0s} Nbd5 {+0.23/13 1.0s}
13. O-O-O {+4.48/18 1.0s} Be6 {+0.06/15 1.0s} 14. Bb5+ {+4.03/16 1.0s}
c6 {-0.15/18 1.0s} 15. Nxc6 {+4.18/18 1.0s} a6 {+0.42/16 1.0s}
16. Ba4 {+4.14/18 1.0s} Bd7 {+0.22/17 1.0s} 17. Ne5 {+3.16/18 1.0s}
Bxa4 {+0.14/18 1.0s} 18. Nxa4 {+3.53/17 1.0s} Rc8 {+0.26/17 1.0s}
19. Kb1 {+3.14/16 1.0s} Qh3 {+0.09/15 1.0s} 20. Bg5 {+2.77/16 1.0s}
Qf5 {+0.11/13 1.0s} 21. c4 {+3.83/17 1.0s} Bb4 {-0.37/16 1.0s}
22. Qe2 {+3.60/18 1.0s} Qxg5 {-0.62/16 1.0s} 23. cxd5 {+3.59/18 1.0s}
Rg8 {-0.93/16 1.0s} 24. Nb6 {+4.46/17 1.0s} Rd8 {-1.44/15 1.0s}
25. d6 {+7.15/16 1.0s} Bxd6 {-1.24/16 1.0s} 26. Qc4 {+9.16/18 1.0s}
Bxe5 {-3.94/17 1.0s} 27. Qe6+ {+10.12/19 1.0s} Kf8 {-4.70/19 1.0s}
28. dxe5 {+11.30/18 1.0s} Re8 {-5.03/17 1.0s} 29. Qd6+ {+12.29/18 1.0s}
Kf7 {-6.36/17 1.0s} 30. Qc7+ {+12.54/18 1.0s} Re7 {-6.81/17 1.0s}
31. Qc4+ {+14.33/19 1.0s} Re6 {-6.50/19 1.0s} 32. exf6 {+14.19/20 1.0s}
Qf5 {-7.17/17 1.0s} 33. Rd5 {+14.53/20 1.0s} Qxf6 {-8.16/17 1.0s}
34. Rd7+ {+18.65/19 1.0s} Kg6 {-9.69/18 1.0s} 35. Nd5 {+19.48/17 1.0s}
Qe5 {-10.52/18 1.0s} 36. Rg1+ {+25.49/18 1.0s} Kh6 {-16.10/17 1.0s}
37. Ne3 {+23.82/20 1.0s} b5 {-17.58/16 1.0s} 38. Qc1 {+44.16/18 1.0s}
g5 {-24.90/17 1.0s} 39. Ng4+ {+54.87/22 1.0s} Kg6 {-30.13/20 1.0s}
40. Nxe5+ {+61.32/22 1.0s} Kf5 {-112.36/21 1.0s} 41. Rf7+ {+62.24/23 1.0s}
Rf6 {-M14/24 1.0s} 42. Rxf6+ {+63.43/22 1.0s} Kxf6 {-M12/28 1.0s}
43. Ng4+ {+64.92/22 1.0s} Kf7 {-M10/29 1.0s} 44. Nh6+ {+69.64/20 1.0s}
Ke7 {-M8/36 1.0s} 45. Qc7+ {+M7/34 1.0s} Kf8 {-20.45/128 0.62s}
46. Qf7# {0s, White mates} 1-0
[/pgn]
Zevra 2 is my chess engine. Binary, source and description here: https://github.com/sovaz1997/Zevra2
Zevra v2.6 is last version of Zevra: https://github.com/sovaz1997/Zevra2/releases
sovaz1997
Posts: 289
Joined: Sun Nov 13, 2016 10:37 am

Re: Attempting to implement NNUE into my engine

Post by sovaz1997 »

nionita wrote: Thu Feb 06, 2025 7:53 pm How many epochs do you train? Did the 500 million positions still not help?
Hmm, I tested my net on Depth=1

And I found that my zero-trained 768x32 net plays close to classic HCE:

Code: Select all

Score of Zevra Self (Gen 7) vs Zevra Classic: 1641 - 1807 - 225  [0.477] 3673
...      Zevra Self (Gen 7) playing White: 813 - 909 - 115  [0.474] 1837
...      Zevra Self (Gen 7) playing Black: 828 - 898 - 110  [0.481] 1836
...      White vs Black: 1711 - 1737 - 225  [0.496] 3673
Elo difference: -15.7 +/- 10.9, LOS: 0.2 %, DrawRatio: 6.1 %
SPRT: llr -2.95 (-100.2%), lbound -2.94, ubound 2.94 - H0 was accepted
But it much faster! Maybe not much, about x1.5.

So I think, tomorrow I'll adapt search params to NNUE. Maybe this will solution, because on larger tc I have lost about 300 elo points. And I feel, that my eval maybe scaled too much for my search params. Maybe I can even do other thing: calculate average evals on hce and on NNUE, after compare it's and do right scale.
Zevra 2 is my chess engine. Binary, source and description here: https://github.com/sovaz1997/Zevra2
Zevra v2.6 is last version of Zevra: https://github.com/sovaz1997/Zevra2/releases
sovaz1997
Posts: 289
Joined: Sun Nov 13, 2016 10:37 am

Re: Attempting to implement NNUE into my engine

Post by sovaz1997 »

And I was right... I try to scale eval x0.5

Code: Select all

nodes=10K
Score of Zevra Self (Gen 7 Scaled) vs Zevra Self (Gen 7): 199 - 34 - 33  [0.810] 266
...      Zevra Self (Gen 7 Scaled) playing White: 101 - 18 - 14  [0.812] 133
...      Zevra Self (Gen 7 Scaled) playing Black: 98 - 16 - 19  [0.808] 133
...      White vs Black: 117 - 116 - 33  [0.502] 266
Elo difference: 252.1 +/- 48.2, LOS: 100.0 %, DrawRatio: 12.4 %
Zevra 2 is my chess engine. Binary, source and description here: https://github.com/sovaz1997/Zevra2
Zevra v2.6 is last version of Zevra: https://github.com/sovaz1997/Zevra2/releases
sovaz1997
Posts: 289
Joined: Sun Nov 13, 2016 10:37 am

Re: Attempting to implement NNUE into my engine

Post by sovaz1997 »

Overall, it looks like I managed to catch up with HCE +-
Tomorrow there will be a new train dataset, I will train a new 768x64 on new data of a stronger version with a correctly working search

Right now I am already training 768x64 on the current data (200M train + 50M validate)
Zevra 2 is my chess engine. Binary, source and description here: https://github.com/sovaz1997/Zevra2
Zevra v2.6 is last version of Zevra: https://github.com/sovaz1997/Zevra2/releases