mauricett commited on
Commit
87ccba2
1 Parent(s): 98d6702

Update lichess_sf.py

Browse files
Files changed (1) hide show
  1. 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
- #months = ["01", "02", "03", "04", "05", "06",
44
- # "07", "08", "09", "10", "11", "12"]
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': downloaded_files})
66
  return [generator]
67
-
68
  def _generate_examples(self, filepaths):
69
- dctx = zstd.ZstdDecompressor()
70
- for filepath in filepaths:
71
- n = 0
72
- with open(filepath, "rb") as file:
73
- with dctx.stream_reader(file) as sr:
74
- pgn = io.TextIOWrapper(sr)
 
 
 
 
 
75
 
76
- if self.config.name == "pgn":
77
- while True:
78
- elos = pgn.readline()
79
- game_pgn = pgn.readline()
80
- next_line = pgn.readline()
 
 
 
81
 
82
- if game_pgn:
83
- white_elo, black_elo = elos.split(" ")
84
- _id = n
85
- n += 1
86
- yield _id, {"WhiteElo": int(white_elo),
87
- "BlackElo": int(black_elo),
88
- "pgn": game_pgn}
89
- else:
90
- break
91
-
92
- elif self.config.name == "fen":
93
- while True:
94
- elos = pgn.readline()
95
- game = pgn.readline()
 
 
 
 
96
 
97
- if game:
98
- white_elo, black_elo = elos.split(" ")
99
- fens, moves, scores = game.split(";")
100
- _id = n
101
- n += 1
102
- yield _id, {"WhiteElo": white_elo,
103
- "BlackElo": black_elo,
104
- "fens": fens,
105
- "moves": moves,
106
- "scores": scores}
107
- else:
108
- break
 
 
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