deepchopper 1.3.0__cp310-abi3-macosx_11_0_arm64.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- deepchopper/__init__.py +9 -0
- deepchopper/__init__.pyi +67 -0
- deepchopper/__main__.py +4 -0
- deepchopper/cli.py +260 -0
- deepchopper/data/__init__.py +15 -0
- deepchopper/data/components/__init__.py +1 -0
- deepchopper/data/encode_fq.py +41 -0
- deepchopper/data/fq_datamodule.py +352 -0
- deepchopper/data/hg_data.py +39 -0
- deepchopper/data/only_fq.py +388 -0
- deepchopper/deepchopper.abi3.so +0 -0
- deepchopper/eval.py +86 -0
- deepchopper/models/__init__.py +4 -0
- deepchopper/models/basic_module.py +243 -0
- deepchopper/models/callbacks.py +57 -0
- deepchopper/models/cnn.py +54 -0
- deepchopper/models/components/__init__.py +1 -0
- deepchopper/models/dc_hg.py +163 -0
- deepchopper/models/llm/__init__.py +32 -0
- deepchopper/models/llm/caduceus.py +55 -0
- deepchopper/models/llm/components.py +99 -0
- deepchopper/models/llm/head.py +102 -0
- deepchopper/models/llm/hyena.py +41 -0
- deepchopper/models/llm/metric.py +44 -0
- deepchopper/models/llm/tokenizer.py +205 -0
- deepchopper/models/transformer.py +107 -0
- deepchopper/py.typed +0 -0
- deepchopper/train.py +109 -0
- deepchopper/ui/__init__.py +1 -0
- deepchopper/ui/main.py +189 -0
- deepchopper/utils/__init__.py +37 -0
- deepchopper/utils/instantiators.py +54 -0
- deepchopper/utils/logging_utils.py +53 -0
- deepchopper/utils/preprocess.py +62 -0
- deepchopper/utils/print.py +102 -0
- deepchopper/utils/pylogger.py +57 -0
- deepchopper/utils/rich_utils.py +100 -0
- deepchopper/utils/utils.py +138 -0
- deepchopper-1.3.0.dist-info/METADATA +254 -0
- deepchopper-1.3.0.dist-info/RECORD +43 -0
- deepchopper-1.3.0.dist-info/WHEEL +4 -0
- deepchopper-1.3.0.dist-info/entry_points.txt +2 -0
- deepchopper-1.3.0.dist-info/licenses/LICENSE +201 -0
|
@@ -0,0 +1,352 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import multiprocessing
|
|
4
|
+
from functools import partial
|
|
5
|
+
from pathlib import Path
|
|
6
|
+
from typing import TYPE_CHECKING, Any
|
|
7
|
+
|
|
8
|
+
from datasets import load_dataset
|
|
9
|
+
from lightning import LightningDataModule
|
|
10
|
+
from torch.utils.data import DataLoader, Dataset
|
|
11
|
+
|
|
12
|
+
from deepchopper.models.llm import (
|
|
13
|
+
DataCollatorForTokenClassificationWithQual,
|
|
14
|
+
tokenize_and_align_labels_and_quals,
|
|
15
|
+
tokenize_and_align_labels_and_quals_ids,
|
|
16
|
+
)
|
|
17
|
+
|
|
18
|
+
if TYPE_CHECKING:
|
|
19
|
+
from transformers import AutoTokenizer
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
class FqDataModule(LightningDataModule):
|
|
23
|
+
"""`LightningDataModule` for the MNIST dataset.
|
|
24
|
+
|
|
25
|
+
The MNIST database of handwritten digits has a training set of 60,000 examples, and a test set of 10,000 examples.
|
|
26
|
+
It is a subset of a larger set available from NIST. The digits have been size-normalized and centered in a
|
|
27
|
+
fixed-size image. The original black and white images from NIST were size normalized to fit in a 20x20 pixel box
|
|
28
|
+
while preserving their aspect ratio. The resulting images contain grey levels as a result of the anti-aliasing
|
|
29
|
+
technique used by the normalization algorithm. the images were centered in a 28x28 image by computing the center of
|
|
30
|
+
mass of the pixels, and translating the image so as to position this point at the center of the 28x28 field.
|
|
31
|
+
|
|
32
|
+
A `LightningDataModule` implements 7 key methods:
|
|
33
|
+
|
|
34
|
+
```python
|
|
35
|
+
def prepare_data(self):
|
|
36
|
+
# Things to do on 1 GPU/TPU (not on every GPU/TPU in DDP).
|
|
37
|
+
# Download data, pre-process, split, save to disk, etc...
|
|
38
|
+
|
|
39
|
+
def setup(self, stage):
|
|
40
|
+
# Things to do on every process in DDP.
|
|
41
|
+
# Load data, set variables, etc...
|
|
42
|
+
|
|
43
|
+
def train_dataloader(self):
|
|
44
|
+
# return train dataloader
|
|
45
|
+
|
|
46
|
+
def val_dataloader(self):
|
|
47
|
+
# return validation dataloader
|
|
48
|
+
|
|
49
|
+
def test_dataloader(self):
|
|
50
|
+
# return test dataloader
|
|
51
|
+
|
|
52
|
+
def predict_dataloader(self):
|
|
53
|
+
# return predict dataloader
|
|
54
|
+
|
|
55
|
+
def teardown(self, stage):
|
|
56
|
+
# Called on every process in DDP.
|
|
57
|
+
# Clean up after fit or test.
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
This allows you to share a full dataset without explaining how to download,
|
|
61
|
+
split, transform and process the data.
|
|
62
|
+
|
|
63
|
+
Read the docs:
|
|
64
|
+
https://lightning.ai/docs/pytorch/latest/data/datamodule.html
|
|
65
|
+
"""
|
|
66
|
+
|
|
67
|
+
def __init__(
|
|
68
|
+
self,
|
|
69
|
+
tokenizer: AutoTokenizer,
|
|
70
|
+
train_data_path: Path,
|
|
71
|
+
val_data_path: Path | None = None,
|
|
72
|
+
test_data_path: Path | None = None,
|
|
73
|
+
predict_data_path: Path | None = None,
|
|
74
|
+
train_val_test_split: tuple[float, float, float] = (0.8, 0.1, 0.1),
|
|
75
|
+
batch_size: int = 12,
|
|
76
|
+
num_workers: int = 0,
|
|
77
|
+
max_train_samples: int | None = None,
|
|
78
|
+
max_val_samples: int | None = None,
|
|
79
|
+
max_test_samples: int | None = None,
|
|
80
|
+
max_predict_samples: int | None = None,
|
|
81
|
+
*,
|
|
82
|
+
pin_memory: bool = False,
|
|
83
|
+
) -> None:
|
|
84
|
+
"""Initialize a `FqDataModule`.
|
|
85
|
+
|
|
86
|
+
:param train_val_test_split: The train, validation and test split. Defaults to `(55_000, 5_000, 10_000)`.
|
|
87
|
+
:param batch_size: The batch size. Defaults to `64`.
|
|
88
|
+
:param num_workers: The number of workers. Defaults to `0`.
|
|
89
|
+
:param pin_memory: Whether to pin memory. Defaults to `False`.
|
|
90
|
+
"""
|
|
91
|
+
super().__init__()
|
|
92
|
+
|
|
93
|
+
# this line allows to access init params with 'self.hparams' attribute
|
|
94
|
+
# also ensures init params will be stored in ckpt
|
|
95
|
+
self.save_hyperparameters(logger=False)
|
|
96
|
+
|
|
97
|
+
self.data_train: Dataset | None = None
|
|
98
|
+
self.data_val: Dataset | None = None
|
|
99
|
+
self.data_test: Dataset | None = None
|
|
100
|
+
self.batch_size_per_device = batch_size
|
|
101
|
+
self.data_collator = DataCollatorForTokenClassificationWithQual(tokenizer)
|
|
102
|
+
|
|
103
|
+
@property
|
|
104
|
+
def num_classes(self) -> int:
|
|
105
|
+
"""Get the number of classes.
|
|
106
|
+
|
|
107
|
+
:return: The number of MNIST classes (10).
|
|
108
|
+
"""
|
|
109
|
+
return 2
|
|
110
|
+
|
|
111
|
+
def prepare_data(self) -> None:
|
|
112
|
+
"""Encode the FastQ data to Parquet format."""
|
|
113
|
+
data_paths = [self.hparams.train_data_path]
|
|
114
|
+
|
|
115
|
+
if self.hparams.val_data_path is not None:
|
|
116
|
+
data_paths.append(self.hparams.val_data_path)
|
|
117
|
+
|
|
118
|
+
if self.hparams.test_data_path is not None:
|
|
119
|
+
data_paths.append(self.hparams.test_data_path)
|
|
120
|
+
|
|
121
|
+
if self.hparams.predict_data_path is not None:
|
|
122
|
+
data_paths.append(self.hparams.predict_data_path)
|
|
123
|
+
|
|
124
|
+
for data_path in data_paths:
|
|
125
|
+
if Path(data_path).suffix == ".parquet":
|
|
126
|
+
pass
|
|
127
|
+
else:
|
|
128
|
+
msg = f"Data file {data_path} is not in Parquet format."
|
|
129
|
+
raise ValueError(msg)
|
|
130
|
+
|
|
131
|
+
self.hparams.train_data_path = Path(self.hparams.train_data_path).with_suffix(".parquet").as_posix()
|
|
132
|
+
|
|
133
|
+
if self.hparams.val_data_path is not None:
|
|
134
|
+
self.hparams.val_data_path = Path(self.hparams.val_data_path).with_suffix(".parquet").as_posix()
|
|
135
|
+
|
|
136
|
+
if self.hparams.test_data_path is not None:
|
|
137
|
+
self.hparams.test_data_path = Path(self.hparams.test_data_path).with_suffix(".parquet").as_posix()
|
|
138
|
+
|
|
139
|
+
if self.hparams.predict_data_path is not None:
|
|
140
|
+
self.hparams.predict_data_path = Path(self.hparams.predict_data_path).with_suffix(".parquet").as_posix()
|
|
141
|
+
|
|
142
|
+
def setup(self, stage: str | None = None) -> None:
|
|
143
|
+
"""Load data. Set variables: `self.data_train`, `self.data_val`, `self.data_test`.
|
|
144
|
+
|
|
145
|
+
This method is called by Lightning before `trainer.fit()`, `trainer.validate()`, `trainer.test()`, and
|
|
146
|
+
`trainer.predict()`, so be careful not to execute things like random split twice! Also, it is called after
|
|
147
|
+
`self.prepare_data()` and there is a barrier in between which ensures that all the processes proceed to
|
|
148
|
+
`self.setup()` once the data is prepared and available for use.
|
|
149
|
+
|
|
150
|
+
:param stage: The stage to setup. Either `"fit"`, `"validate"`, `"test"`, or `"predict"`. Defaults to ``None``.
|
|
151
|
+
"""
|
|
152
|
+
# Divide batch size by the number of devices.
|
|
153
|
+
if self.trainer is not None:
|
|
154
|
+
if self.hparams.batch_size % self.trainer.world_size != 0:
|
|
155
|
+
msg = f"Batch size ({self.hparams.batch_size}) is not divisible by the number of devices ({self.trainer.world_size})."
|
|
156
|
+
raise RuntimeError(msg)
|
|
157
|
+
self.batch_size_per_device = self.hparams.batch_size // self.trainer.world_size
|
|
158
|
+
|
|
159
|
+
if stage == "predict":
|
|
160
|
+
if not self.hparams.predict_data_path:
|
|
161
|
+
msg = "Predict data path is required for prediction stage."
|
|
162
|
+
raise ValueError(msg)
|
|
163
|
+
|
|
164
|
+
num_proc = min(self.hparams.num_workers, multiprocessing.cpu_count() - 1)
|
|
165
|
+
data_files = {"predict": self.hparams.predict_data_path}
|
|
166
|
+
predict_dataset = load_dataset(
|
|
167
|
+
"parquet",
|
|
168
|
+
data_files=data_files,
|
|
169
|
+
num_proc=max(1, num_proc),
|
|
170
|
+
).with_format("torch")
|
|
171
|
+
|
|
172
|
+
predict_dataset = predict_dataset["predict"]
|
|
173
|
+
if self.hparams.max_predict_samples is not None:
|
|
174
|
+
max_predict_samples = min(self.hparams.max_predict_samples, len(predict_dataset))
|
|
175
|
+
predict_dataset = predict_dataset.select(range(max_predict_samples))
|
|
176
|
+
|
|
177
|
+
self.data_predict = predict_dataset.map(
|
|
178
|
+
partial(
|
|
179
|
+
tokenize_and_align_labels_and_quals_ids,
|
|
180
|
+
tokenizer=self.hparams.tokenizer,
|
|
181
|
+
max_length=self.hparams.tokenizer.max_len_single_sentence,
|
|
182
|
+
),
|
|
183
|
+
num_proc=max(1, num_proc), # type: ignore
|
|
184
|
+
).remove_columns(["seq", "qual", "target"])
|
|
185
|
+
del predict_dataset
|
|
186
|
+
return
|
|
187
|
+
|
|
188
|
+
# load and split datasets only if not loaded already
|
|
189
|
+
if not self.data_train and not self.data_val and not self.data_test:
|
|
190
|
+
num_proc = min(self.hparams.num_workers, multiprocessing.cpu_count() - 1)
|
|
191
|
+
data_files = {}
|
|
192
|
+
data_files["train"] = self.hparams.train_data_path
|
|
193
|
+
|
|
194
|
+
if self.hparams.val_data_path is not None:
|
|
195
|
+
data_files["validation"] = self.hparams.val_data_path
|
|
196
|
+
|
|
197
|
+
if self.hparams.test_data_path is not None:
|
|
198
|
+
data_files["test"] = self.hparams.test_data_path
|
|
199
|
+
|
|
200
|
+
if self.hparams.val_data_path is None or self.hparams.test_data_path is None:
|
|
201
|
+
split_percent = self.hparams.train_val_test_split
|
|
202
|
+
|
|
203
|
+
train_dataset = load_dataset(
|
|
204
|
+
"parquet",
|
|
205
|
+
data_files=data_files,
|
|
206
|
+
num_proc=max(1, num_proc),
|
|
207
|
+
split=f"train[:{split_percent[0]}%]",
|
|
208
|
+
).with_format("torch")
|
|
209
|
+
|
|
210
|
+
val_dataset = load_dataset(
|
|
211
|
+
"parquet",
|
|
212
|
+
data_files=data_files,
|
|
213
|
+
num_proc=max(1, num_proc),
|
|
214
|
+
split=f"train[{split_percent[0]}%:{split_percent[0] + split_percent[1]}%]",
|
|
215
|
+
).with_format("torch")
|
|
216
|
+
|
|
217
|
+
test_dataset = load_dataset(
|
|
218
|
+
"parquet",
|
|
219
|
+
data_files=data_files,
|
|
220
|
+
num_proc=max(1, num_proc),
|
|
221
|
+
split=f"train[{split_percent[0] + split_percent[1]}%:]",
|
|
222
|
+
).with_format("torch")
|
|
223
|
+
|
|
224
|
+
else:
|
|
225
|
+
raw_datasets = load_dataset("parquet", data_files=data_files, num_proc=max(1, num_proc)).with_format(
|
|
226
|
+
"torch"
|
|
227
|
+
)
|
|
228
|
+
|
|
229
|
+
train_dataset = raw_datasets["train"]
|
|
230
|
+
val_dataset = raw_datasets["validation"]
|
|
231
|
+
test_dataset = raw_datasets["test"]
|
|
232
|
+
|
|
233
|
+
if self.hparams.max_train_samples is not None:
|
|
234
|
+
max_train_samples = min(self.hparams.max_train_samples, len(train_dataset))
|
|
235
|
+
train_dataset = train_dataset.select(range(max_train_samples))
|
|
236
|
+
|
|
237
|
+
if self.hparams.max_val_samples is not None:
|
|
238
|
+
max_val_samples = min(self.hparams.max_val_samples, len(val_dataset))
|
|
239
|
+
val_dataset = val_dataset.select(range(max_val_samples))
|
|
240
|
+
|
|
241
|
+
if self.hparams.max_test_samples is not None:
|
|
242
|
+
max_test_samples = min(self.hparams.max_test_samples, len(test_dataset))
|
|
243
|
+
test_dataset = test_dataset.select(range(max_test_samples))
|
|
244
|
+
|
|
245
|
+
self.data_train = train_dataset.map(
|
|
246
|
+
partial(
|
|
247
|
+
tokenize_and_align_labels_and_quals,
|
|
248
|
+
tokenizer=self.hparams.tokenizer,
|
|
249
|
+
max_length=self.hparams.tokenizer.max_len_single_sentence,
|
|
250
|
+
),
|
|
251
|
+
num_proc=max(1, num_proc), # type: ignore
|
|
252
|
+
).remove_columns(["id", "seq", "qual", "target"])
|
|
253
|
+
|
|
254
|
+
self.data_val = val_dataset.map(
|
|
255
|
+
partial(
|
|
256
|
+
tokenize_and_align_labels_and_quals,
|
|
257
|
+
tokenizer=self.hparams.tokenizer,
|
|
258
|
+
max_length=self.hparams.tokenizer.max_len_single_sentence,
|
|
259
|
+
),
|
|
260
|
+
num_proc=max(1, num_proc), # type: ignore
|
|
261
|
+
).remove_columns(["id", "seq", "qual", "target"])
|
|
262
|
+
|
|
263
|
+
self.data_test = test_dataset.map(
|
|
264
|
+
partial(
|
|
265
|
+
tokenize_and_align_labels_and_quals,
|
|
266
|
+
tokenizer=self.hparams.tokenizer,
|
|
267
|
+
max_length=self.hparams.tokenizer.max_len_single_sentence,
|
|
268
|
+
),
|
|
269
|
+
num_proc=max(1, num_proc), # type: ignore
|
|
270
|
+
).remove_columns(["id", "seq", "qual", "target"])
|
|
271
|
+
|
|
272
|
+
del train_dataset, val_dataset, test_dataset
|
|
273
|
+
|
|
274
|
+
def train_dataloader(self) -> DataLoader[Any]:
|
|
275
|
+
"""Create and return the train dataloader.
|
|
276
|
+
|
|
277
|
+
:return: The train dataloader.
|
|
278
|
+
"""
|
|
279
|
+
return DataLoader(
|
|
280
|
+
dataset=self.data_train,
|
|
281
|
+
batch_size=self.batch_size_per_device,
|
|
282
|
+
num_workers=self.hparams.num_workers,
|
|
283
|
+
pin_memory=self.hparams.pin_memory,
|
|
284
|
+
collate_fn=self.data_collator.torch_call,
|
|
285
|
+
shuffle=True,
|
|
286
|
+
)
|
|
287
|
+
|
|
288
|
+
def val_dataloader(self) -> DataLoader[Any]:
|
|
289
|
+
"""Create and return the validation dataloader.
|
|
290
|
+
|
|
291
|
+
:return: The validation dataloader.
|
|
292
|
+
"""
|
|
293
|
+
return DataLoader(
|
|
294
|
+
dataset=self.data_val,
|
|
295
|
+
batch_size=self.batch_size_per_device,
|
|
296
|
+
num_workers=self.hparams.num_workers,
|
|
297
|
+
pin_memory=self.hparams.pin_memory,
|
|
298
|
+
collate_fn=self.data_collator.torch_call,
|
|
299
|
+
shuffle=False,
|
|
300
|
+
)
|
|
301
|
+
|
|
302
|
+
def test_dataloader(self) -> DataLoader[Any]:
|
|
303
|
+
"""Create and return the test dataloader.
|
|
304
|
+
|
|
305
|
+
:return: The test dataloader.
|
|
306
|
+
"""
|
|
307
|
+
return DataLoader(
|
|
308
|
+
dataset=self.data_test,
|
|
309
|
+
batch_size=self.batch_size_per_device,
|
|
310
|
+
num_workers=self.hparams.num_workers,
|
|
311
|
+
pin_memory=self.hparams.pin_memory,
|
|
312
|
+
collate_fn=self.data_collator.torch_call,
|
|
313
|
+
shuffle=False,
|
|
314
|
+
)
|
|
315
|
+
|
|
316
|
+
def predict_dataloader(self) -> DataLoader[Any]:
|
|
317
|
+
"""Create and return the predict dataloader.
|
|
318
|
+
|
|
319
|
+
:return: The predict dataloader.
|
|
320
|
+
"""
|
|
321
|
+
return DataLoader(
|
|
322
|
+
dataset=self.data_predict,
|
|
323
|
+
batch_size=self.batch_size_per_device,
|
|
324
|
+
num_workers=self.hparams.num_workers,
|
|
325
|
+
pin_memory=self.hparams.pin_memory,
|
|
326
|
+
collate_fn=self.data_collator.torch_call,
|
|
327
|
+
shuffle=False,
|
|
328
|
+
)
|
|
329
|
+
|
|
330
|
+
def teardown(self, stage: str | None = None) -> None:
|
|
331
|
+
"""Lightning hook for cleaning up after `trainer.fit()`, `trainer.validate()`,.
|
|
332
|
+
|
|
333
|
+
`trainer.test()`, and `trainer.predict()`.
|
|
334
|
+
|
|
335
|
+
:param stage: The stage being torn down. Either `"fit"`, `"validate"`, `"test"`, or `"predict"`.
|
|
336
|
+
Defaults to ``None``.
|
|
337
|
+
"""
|
|
338
|
+
|
|
339
|
+
def state_dict(self) -> dict[Any, Any]:
|
|
340
|
+
"""Called when saving a checkpoint. Implement to generate and save the datamodule state.
|
|
341
|
+
|
|
342
|
+
:return: A dictionary containing the datamodule state that you want to save.
|
|
343
|
+
"""
|
|
344
|
+
return {}
|
|
345
|
+
|
|
346
|
+
def load_state_dict(self, state_dict: dict[str, Any]) -> None:
|
|
347
|
+
"""Called when loading a checkpoint. Implement to reload datamodule state given datamodule.
|
|
348
|
+
|
|
349
|
+
`state_dict()`.
|
|
350
|
+
|
|
351
|
+
:param state_dict: The datamodule state returned by `self.state_dict()`.
|
|
352
|
+
"""
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import multiprocessing
|
|
2
|
+
from pathlib import Path
|
|
3
|
+
|
|
4
|
+
from datasets import load_dataset
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
def load_and_split_dataset(data_file: str | Path, num_proc: int | None = None):
|
|
8
|
+
"""Load and split a dataset into training, validation, and testing sets.
|
|
9
|
+
|
|
10
|
+
Args:
|
|
11
|
+
data_file (str, Path): A dictionary containing the file paths for the dataset.
|
|
12
|
+
num_proc (int, optional): The number of processes to use for loading the dataset. Defaults to None.
|
|
13
|
+
|
|
14
|
+
Returns:
|
|
15
|
+
Tuple[torch.utils.data.Dataset, torch.utils.data.Dataset, torch.utils.data.Dataset]: A tuple containing the training, validation, and testing datasets.
|
|
16
|
+
|
|
17
|
+
Example:
|
|
18
|
+
data_files = {"train": "train.parquet"}
|
|
19
|
+
train_dataset, val_dataset, test_dataset = load_and_split_dataset(data_files)
|
|
20
|
+
"""
|
|
21
|
+
if num_proc is None:
|
|
22
|
+
num_proc = multiprocessing.cpu_count()
|
|
23
|
+
|
|
24
|
+
data_files = {"train": str(data_file)}
|
|
25
|
+
train_dataset = load_dataset(
|
|
26
|
+
"parquet",
|
|
27
|
+
data_files=data_files,
|
|
28
|
+
num_proc=num_proc,
|
|
29
|
+
split="train[:80%]",
|
|
30
|
+
).with_format("torch")
|
|
31
|
+
|
|
32
|
+
val_dataset = load_dataset("parquet", data_files=data_files, num_proc=num_proc, split="train[80%:90%]").with_format(
|
|
33
|
+
"torch"
|
|
34
|
+
)
|
|
35
|
+
|
|
36
|
+
test_dataset = load_dataset("parquet", data_files=data_files, num_proc=num_proc, split="train[90%:]").with_format(
|
|
37
|
+
"torch"
|
|
38
|
+
)
|
|
39
|
+
return train_dataset, val_dataset, test_dataset
|