Update lichess_sf.py
Browse files- lichess_sf.py +57 -44
lichess_sf.py
CHANGED
@@ -1,4 +1,3 @@
|
|
1 |
-
#%%
|
2 |
import datasets
|
3 |
import zstandard as zstd
|
4 |
import io
|
@@ -40,11 +39,10 @@ class Lichess(datasets.GeneratorBasedBuilder):
|
|
40 |
return info
|
41 |
|
42 |
def _get_filepaths(self):
|
43 |
-
|
44 |
-
|
45 |
-
months = ["01", "02", "03", "04", "08", "09", "10", "11", "12"]
|
46 |
shards = ["0", "1", "2", "3"]
|
47 |
-
filepaths = [ self.config.name + "/2023/" + m for m in months]
|
48 |
|
49 |
if self.config.name == "pgn":
|
50 |
paths = []
|
@@ -61,48 +59,63 @@ class Lichess(datasets.GeneratorBasedBuilder):
|
|
61 |
def _split_generators(self, dl_manager: datasets.DownloadManager):
|
62 |
filepaths = self._get_filepaths()
|
63 |
downloaded_files = dl_manager.download(filepaths)
|
|
|
|
|
|
|
64 |
generator = datasets.SplitGenerator(name=datasets.Split.TRAIN,
|
65 |
-
gen_kwargs={'filepaths':
|
66 |
return [generator]
|
67 |
-
|
68 |
def _generate_examples(self, filepaths):
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
|
|
|
|
|
|
|
|
|
|
75 |
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
|
|
|
|
|
|
81 |
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
|
|
|
|
|
|
|
|
96 |
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
|
|
|
|
1 |
import datasets
|
2 |
import zstandard as zstd
|
3 |
import io
|
|
|
39 |
return info
|
40 |
|
41 |
def _get_filepaths(self):
|
42 |
+
months = ["01", "02", "03", "04", "05", "06",
|
43 |
+
"07", "08", "09", "10", "11", "12"]
|
|
|
44 |
shards = ["0", "1", "2", "3"]
|
45 |
+
filepaths = ["lichess/" + self.config.name + "/2023/" + m for m in months]
|
46 |
|
47 |
if self.config.name == "pgn":
|
48 |
paths = []
|
|
|
59 |
def _split_generators(self, dl_manager: datasets.DownloadManager):
|
60 |
filepaths = self._get_filepaths()
|
61 |
downloaded_files = dl_manager.download(filepaths)
|
62 |
+
# Hack.
|
63 |
+
# Hardcoded for 48 shards and 4 num_workers:
|
64 |
+
files = [downloaded_files[12*i:12*i+12] for i in range(4)]
|
65 |
generator = datasets.SplitGenerator(name=datasets.Split.TRAIN,
|
66 |
+
gen_kwargs={'filepaths': files})
|
67 |
return [generator]
|
68 |
+
|
69 |
def _generate_examples(self, filepaths):
|
70 |
+
""" Each worker receives a random set of the .zst files (the raw dataset).
|
71 |
+
Each worker will cycle through its set of files. They read a single game
|
72 |
+
from file 1, then a single game from file 2, etc. ...
|
73 |
+
The purpose is to create batches that contain games from a diverse mix
|
74 |
+
of time periods. -> Reduces distribution shift.
|
75 |
+
"""
|
76 |
+
filepaths = filepaths[0]
|
77 |
+
files = [open(filepath, "rb") for filepath in filepaths]
|
78 |
+
dctxs = [zstd.ZstdDecompressor() for file in files]
|
79 |
+
stream_readers = [dctx.stream_reader(file) for dctx, file in zip(dctxs, files)]
|
80 |
+
pgns = [io.TextIOWrapper(sr) for sr in stream_readers]
|
81 |
|
82 |
+
n = 0
|
83 |
+
n_files = len(files)
|
84 |
+
# lower bound of number of positions per .zst file to stop the loop.
|
85 |
+
# need something smarter at some point
|
86 |
+
n_positions = 2 * 10**6
|
87 |
+
while n <= n_files * n_positions:
|
88 |
+
# cycle through the different shards
|
89 |
+
pgn = pgns[n % n_files]
|
90 |
|
91 |
+
if self.config.name == "pgn":
|
92 |
+
elos = pgn.readline()
|
93 |
+
game_pgn = pgn.readline()
|
94 |
+
next_line = pgn.readline()
|
95 |
+
|
96 |
+
if game_pgn:
|
97 |
+
white_elo, black_elo = elos.split(" ")
|
98 |
+
_id = n
|
99 |
+
n += 1
|
100 |
+
yield _id, {"WhiteElo": int(white_elo),
|
101 |
+
"BlackElo": int(black_elo),
|
102 |
+
"pgn": game_pgn}
|
103 |
+
else:
|
104 |
+
break
|
105 |
+
|
106 |
+
elif self.config.name == "fen":
|
107 |
+
elos = pgn.readline()
|
108 |
+
game = pgn.readline()
|
109 |
|
110 |
+
if game:
|
111 |
+
white_elo, black_elo = elos.split(" ")
|
112 |
+
fens, moves, scores = game.split(";")
|
113 |
+
_id = n
|
114 |
+
n += 1
|
115 |
+
yield _id, {"WhiteElo": white_elo,
|
116 |
+
"BlackElo": black_elo,
|
117 |
+
"fens": fens,
|
118 |
+
"moves": moves,
|
119 |
+
"scores": scores}
|
120 |
+
else:
|
121 |
+
break
|