first submit
Browse files- continue_pretrain_mt5_base.gin +39 -0
- tasks.py +71 -0
- train_base.sh +11 -0
continue_pretrain_mt5_base.gin
ADDED
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from __gin__ import dynamic_registration
|
2 |
+
import tasks
|
3 |
+
|
4 |
+
import __main__ as train_script
|
5 |
+
from t5.data import mixtures
|
6 |
+
from t5x import models
|
7 |
+
from t5x import partitioning
|
8 |
+
from t5x import utils
|
9 |
+
|
10 |
+
include "t5x/examples/t5/mt5/base.gin"
|
11 |
+
include "t5x/configs/runs/finetune.gin"
|
12 |
+
|
13 |
+
MIXTURE_OR_TASK_NAME = "continue_pretrain"
|
14 |
+
TASK_FEATURE_LENGTHS = {"inputs": 256, "targets": 256}
|
15 |
+
TRAIN_STEPS = 1_100_000 # 1000000 pre-trained steps + 100000 fine-tuning steps.
|
16 |
+
USE_CACHED_TASKS = False
|
17 |
+
DROPOUT_RATE = 0.0
|
18 |
+
RANDOM_SEED = 0
|
19 |
+
|
20 |
+
# Pere: Only necessary if we load a t5 model. We can start with an t5x model here
|
21 |
+
# `LOSS_NORMALIZING_FACTOR`: When fine-tuning a model that was pre-trained
|
22 |
+
# using Mesh Tensorflow (e.g. the public T5 / mT5 / ByT5 models), this should be
|
23 |
+
# set to `pretraining batch_size` * `target_token_length`. For T5 and T5.1.1:
|
24 |
+
# `2048 * 114`. For mT5: `1024 * 229`. For ByT5: `1024 * 189`.
|
25 |
+
#LOSS_NORMALIZING_FACTOR = 234496
|
26 |
+
|
27 |
+
INITIAL_CHECKPOINT_PATH = "gs://t5-data/pretrained_models/t5x/mt5_base/checkpoint_1000000"
|
28 |
+
|
29 |
+
#train_script.train:
|
30 |
+
# eval_period = 500
|
31 |
+
# partitioner = @partitioning.ModelBasedPjitPartitioner()
|
32 |
+
# partitioning.ModelBasedPjitPartitioner.num_partitions = 2
|
33 |
+
|
34 |
+
# `num_decodes` is equivalent to a beam size in a beam search decoding.
|
35 |
+
models.EncoderDecoderModel.predict_batch_with_aux.num_decodes = 4
|
36 |
+
|
37 |
+
#mesh_transformer.learning_rate_schedules.constant_learning_rate.learning_rate = 0.0005
|
38 |
+
#run.learning_rate_schedule = @learning_rate_schedules.constant_learning_rate
|
39 |
+
|
tasks.py
ADDED
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# /home/perk/mymodel/sentencefix/tasks.py
|
2 |
+
|
3 |
+
import functools
|
4 |
+
import seqio
|
5 |
+
import tensorflow_datasets as tfds
|
6 |
+
from t5.evaluation import metrics
|
7 |
+
from t5.data import preprocessors
|
8 |
+
import t5
|
9 |
+
import tensorflow.compat.v1 as tf
|
10 |
+
|
11 |
+
## PERE - Needs to be updated
|
12 |
+
tsv_path = {
|
13 |
+
"train": "gs://nb-t5x/corpus/train/train.tsv",
|
14 |
+
"validation": "gs://nb-t5x/corpus/eval/eval.tsv",
|
15 |
+
"test": "gs://nb-t5x/corpus/test/test.tsv"
|
16 |
+
}
|
17 |
+
|
18 |
+
vocabulary = seqio.SentencePieceVocabulary(
|
19 |
+
'gs://t5-data/vocabs/mc4.250000.100extra/sentencepiece.model', extra_ids=0)
|
20 |
+
|
21 |
+
DEFAULT_OUTPUT_FEATURES = {
|
22 |
+
"inputs":
|
23 |
+
seqio.Feature(
|
24 |
+
vocabulary=vocabulary, add_eos=True),
|
25 |
+
"targets":
|
26 |
+
seqio.Feature(
|
27 |
+
vocabulary=vocabulary, add_eos=True)
|
28 |
+
}
|
29 |
+
|
30 |
+
|
31 |
+
## Pere - this is my sentencefix task
|
32 |
+
## We need to add a "continue_pretrain" task, and set that up correctly
|
33 |
+
|
34 |
+
|
35 |
+
def sentencefix_preprocessor(ds):
|
36 |
+
def normalize_text(text):
|
37 |
+
"""Lowercase and remove quotes from a TensorFlow string."""
|
38 |
+
text = tf.strings.regex_replace(text,"'(.*)'", r"\1")
|
39 |
+
return text
|
40 |
+
|
41 |
+
def to_inputs_and_targets(ex):
|
42 |
+
"""Map {"source": ..., "source": ...}->{"target": ..., "target": ...}."""
|
43 |
+
return {
|
44 |
+
"inputs":
|
45 |
+
tf.strings.join(
|
46 |
+
[normalize_text(ex["source"])]),
|
47 |
+
"targets":
|
48 |
+
tf.strings.join(
|
49 |
+
[normalize_text(ex["target"])]),
|
50 |
+
}
|
51 |
+
return ds.map(to_inputs_and_targets,
|
52 |
+
num_parallel_calls=tf.data.experimental.AUTOTUNE)
|
53 |
+
|
54 |
+
|
55 |
+
seqio.TaskRegistry.add(
|
56 |
+
"sentencefix",
|
57 |
+
source=seqio.TextLineDataSource(
|
58 |
+
split_to_filepattern=tsv_path,
|
59 |
+
#num_input_examples=num_nq_examples
|
60 |
+
),
|
61 |
+
preprocessors=[
|
62 |
+
functools.partial(
|
63 |
+
t5.data.preprocessors.parse_tsv,
|
64 |
+
field_names=["source", "target"]),
|
65 |
+
sentencefix_preprocessor,
|
66 |
+
seqio.preprocessors.tokenize_and_append_eos,
|
67 |
+
],
|
68 |
+
#metric_fns=[metrics.bleu],
|
69 |
+
output_features=DEFAULT_OUTPUT_FEATURES,
|
70 |
+
)
|
71 |
+
|
train_base.sh
ADDED
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
PROJECT_DIR=${HOME}"/models/norwegian-mt5x"
|
2 |
+
T5X_DIR="../../t5x" # directory where the t5x is cloned.
|
3 |
+
#Needs to be updated when moving to tpu-v4 it should then be in another zone
|
4 |
+
MODEL_DIR="gs://nb-t5x/norwegian-mt5x-base"
|
5 |
+
export PYTHONPATH=${PROJECT_DIR}
|
6 |
+
|
7 |
+
python3 ${T5X_DIR}/t5x/train.py \
|
8 |
+
--gin_search_paths=${PROJECT_DIR} \
|
9 |
+
--gin_file="continue_pretrain_mt5_base.gin" \
|
10 |
+
--gin.MODEL_DIR="'${MODEL_DIR}'"
|
11 |
+
|