diff mbox series

[bug#63088,3/3] gnu: Add Lc0. * gnu/packages/games.scm (lc0): New variable.

Message ID be3972ed787ef023a56892fa826f54a8c4a25eff.1682514072.git.zamfofex@twdb.moe
State New
Headers show
Series Add Lc0 | expand

Commit Message

zamfofex April 26, 2023, 1:17 p.m. UTC
---
 gnu/packages/games.scm | 60 +++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 59 insertions(+), 1 deletion(-)

Comments

宋文武 May 11, 2023, 2:29 p.m. UTC | #1
Hello, I have pushed ispc and oneapi-dnnl, thank you!


zamfofex <zamfofex@twdb.moe> writes:
> +(define lc0-neural-network
> +  (let ((hash
> +         "f404e156ceb2882470fd8c032b8754af0fa0b71168328912eaef14671a256e34"))
> +      (origin
> +        (method url-fetch)
> +        (uri (string-append "https://storage.lczero.org/files/networks/"
> +                            hash))
> +        (sha256
> +         (base32
> +          "03b9xl9vkiihdilz5dzcpg6g4inb6n4k5gs911i3gbd8h9sh9ixi"))
> +        (file-name "lc0-neural-network"))))

Will we able to train a model from source?  And how much will it cost?

As far as I know, guix haven't decided to accept pre-trained models:

https://yhetil.org/guix/xanfHBZT3lYlyrr_OqHHMWkunLeZZlcxzY37_T3TeZo7mfJClD5-OTbkXDH2f3lMTkn94YIFVUj-Z31BP2Wj0W2rISNP6glC2PzXcPdb560=@protonmail.com/
zamfofex May 11, 2023, 9:25 p.m. UTC | #2
> Will we able to train a model from source?  And how much will it cost?
> 
> As far as I know, guix haven't decided to accept pre-trained models:
> 
> https://yhetil.org/guix/xanfHBZT3lYlyrr_OqHHMWkunLeZZlcxzY37_T3TeZo7mfJClD5-OTbkXDH2f3lMTkn94YIFVUj-Z31BP2Wj0W2rISNP6glC2PzXcPdb560=@protonmail.com/

I don’t think it is feasible. Lc0’s approach is based on self‐play reinforcement‐learning, which in effect means that training starts with no knowledge about chess except for its rules (playing seemingly random moves), and going from there to learn increasingly more about it. So, in practice, this means it would take multiple months or at least several weeks to attain a neural network model that is anywhere close to as effective as the ones provided and pre‐trained. Besides, I believe training requires a (reasonably decent) GPU.

But the network isn’t prohibitively large and its license is not proprietary. I’ll also note that Stockfish (packaged immediately above my Lc0 package) *does* itself include a provided pre‐trained network and no‐one has really complained about it, so I think it should be fine here too.
Simon Tournier May 16, 2023, 12:16 p.m. UTC | #3
Hi,

On Thu, 11 May 2023 at 18:25, zamfofex <zamfofex@twdb.moe> wrote:
>> Will we able to train a model from source?  And how much will it cost?
>> 
>> As far as I know, guix haven't decided to accept pre-trained models:
>> 
>> https://yhetil.org/guix/xanfHBZT3lYlyrr_OqHHMWkunLeZZlcxzY37_T3TeZo7mfJClD5-OTbkXDH2f3lMTkn94YIFVUj-Z31BP2Wj0W2rISNP6glC2PzXcPdb560=@protonmail.com/
>
> I don’t think it is feasible. Lc0’s approach is based on self‐play
> reinforcement‐learning, which in effect means that training starts
> with no knowledge about chess except for its rules (playing seemingly
> random moves), and going from there to learn increasingly more about
> it. So, in practice, this means it would take multiple months or at
> least several weeks to attain a neural network model that is anywhere
> close to as effective as the ones provided and pre‐trained. Besides, I
> believe training requires a (reasonably decent) GPU.

I concur.


> But the network isn’t prohibitively large and its license is not
> proprietary. I’ll also note that Stockfish (packaged immediately above
> my Lc0 package) *does* itself include a provided pre‐trained network
> and no‐one has really complained about it, so I think it should be
> fine here too.

I will try to dedicate some time for reviewing this patch set.


Cheers,
simon
diff mbox series

Patch

diff --git a/gnu/packages/games.scm b/gnu/packages/games.scm
index 23b6c39c46..9350c9224c 100644
--- a/gnu/packages/games.scm
+++ b/gnu/packages/games.scm
@@ -70,7 +70,7 @@ 
 ;;; Copyright © 2021 Foo Chuan Wei <chuanwei.foo@hotmail.com>
 ;;; Copyright © 2022, 2023 Yovan Naumovski <yovan@gorski.stream>
 ;;; Copyright © 2022 Roman Riabenko <roman@riabenko.com>
-;;; Copyright © 2022 zamfofex <zamfofex@twdb.moe>
+;;; Copyright © 2022, 2023 zamfofex <zamfofex@twdb.moe>
 ;;; Copyright © 2022 Gabriel Arazas <foo.dogsquared@gmail.com>
 ;;; Copyright © 2022 Maxim Cournoyer <maxim.cournoyer@gmail.com>
 ;;; Copyright © 2022 Hendursaga <hendursaga@aol.com>
@@ -114,6 +114,7 @@  (define-module (gnu packages games)
   #:use-module (gnu packages bash)
   #:use-module (gnu packages bison)
   #:use-module (gnu packages boost)
+  #:use-module (gnu packages c)
   #:use-module (gnu packages check)
   #:use-module (gnu packages cmake)
   #:use-module (gnu packages compression)
@@ -166,6 +167,7 @@  (define-module (gnu packages games)
   #:use-module (gnu packages linux)
   #:use-module (gnu packages llvm)
   #:use-module (gnu packages lua)
+  #:use-module (gnu packages machine-learning)
   #:use-module (gnu packages man)
   #:use-module (gnu packages maths)
   #:use-module (gnu packages messaging)
@@ -10395,6 +10397,62 @@  (define-public stockfish
       (home-page "https://stockfishchess.org/")
       (license license:gpl3+))))
 
+(define lc0-neural-network
+  (let ((hash
+         "f404e156ceb2882470fd8c032b8754af0fa0b71168328912eaef14671a256e34"))
+      (origin
+        (method url-fetch)
+        (uri (string-append "https://storage.lczero.org/files/networks/"
+                            hash))
+        (sha256
+         (base32
+          "03b9xl9vkiihdilz5dzcpg6g4inb6n4k5gs911i3gbd8h9sh9ixi"))
+        (file-name "lc0-neural-network"))))
+
+(define-public lc0
+  (package
+    (name "lc0")
+    (version "0.29.0")
+    (source
+     (origin
+       (method git-fetch)
+       (uri (git-reference
+             (url "https://github.com/LeelaChessZero/lc0")
+             (commit (string-append "v" version))
+             (recursive? #t)))
+       (file-name (git-file-name name version))
+       (sha256
+        (base32 "1yn91738wd8zlbvrcw0dszy7hqjpkml0wi5xhh36j4zgid6x8i2m"))))
+    (build-system meson-build-system)
+    (native-search-paths
+       (list (search-path-specification
+              (variable "XDG_DATA_DIRS")
+              (files (list "share")))))
+    (inputs
+     `(("neural-network" ,lc0-neural-network)
+       ("eigen" ,eigen)
+       ("oneapi-dnnl" ,oneapi-dnnl)
+       ("zlib" ,zlib)))
+    (native-inputs
+     (list googletest ispc pkg-config python))
+    (arguments
+     '(#:phases (modify-phases %standard-phases
+                  (add-after 'install 'copy-net
+                    (lambda* (#:key outputs inputs #:allow-other-keys)
+                      (mkdir-p (string-append (assoc-ref outputs "out")
+                                              "/share/lc0"))
+                      (copy-file (assoc-ref inputs "neural-network")
+                                 (string-append (assoc-ref outputs "out")
+                                                "/share/lc0/neural-network")))))
+       #:configure-flags (list "-Ddnnl=true"
+                               (string-append
+                                 "-Ddnnl_dir="
+                                 (assoc-ref %build-inputs "oneapi-dnnl")))))
+    (synopsis "Neural network based chess engine")
+    (description "Lc0 is a chess engine based on neural networks")
+    (home-page "https://lczero.org")
+    (license license:gpl3+)))
+
 (define-public barrage
   (package
     (name "barrage")