first submit
Browse files- README.md +1 -1
- finetuning_categorisation.gin +39 -0
- finetuning_categorisation_large.gin +40 -0
- interference.sh +16 -0
- tasks.py +65 -0
- train_base.sh +11 -0
- train_large.sh +11 -0
README.md
CHANGED
@@ -1 +1 @@
|
|
1 |
-
|
|
|
1 |
+
Private sample code for running categorisation on the mT5X
|
finetuning_categorisation.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 = "categorise"
|
14 |
+
TASK_FEATURE_LENGTHS = {"inputs": 256, "targets": 256}
|
15 |
+
TRAIN_STEPS = 1_010_000 # 1000000 pre-trained steps + 10000 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 |
+
|
finetuning_categorisation_large.gin
ADDED
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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/large.gin"
|
11 |
+
include "t5x/configs/runs/finetune.gin"
|
12 |
+
|
13 |
+
MIXTURE_OR_TASK_NAME = "categorise"
|
14 |
+
TASK_FEATURE_LENGTHS = {"inputs": 256, "targets": 256}
|
15 |
+
TRAIN_STEPS = 1_010_000 # 1000000 pre-trained steps + 10000 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_large/checkpoint_1000000"
|
28 |
+
# BATCH_SIZE = 64
|
29 |
+
|
30 |
+
#train_script.train:
|
31 |
+
# eval_period = 500
|
32 |
+
# partitioner = @partitioning.ModelBasedPjitPartitioner()
|
33 |
+
# partitioning.ModelBasedPjitPartitioner.num_partitions = 2
|
34 |
+
|
35 |
+
# `num_decodes` is equivalent to a beam size in a beam search decoding.
|
36 |
+
models.EncoderDecoderModel.predict_batch_with_aux.num_decodes = 4
|
37 |
+
|
38 |
+
#mesh_transformer.learning_rate_schedules.constant_learning_rate.learning_rate = 0.0005
|
39 |
+
#run.learning_rate_schedule = @learning_rate_schedules.constant_learning_rate
|
40 |
+
|
interference.sh
ADDED
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
INFER_OUTPUT_DIR="output2" # directory to write infer output
|
2 |
+
T5X_DIR="../../t5x" # directory where the t5x is cloned, e.g., ${HOME}"/t5x".
|
3 |
+
TFDS_DATA_DIR="gs://nb-t5x/corpus_multi_sentencefix_mt5/"
|
4 |
+
CHECKPOINT_PATH="gs://nb-t5x-us-central2/model_mT5X_large_16_e/checkpoint_1140000"
|
5 |
+
PROJECT_DIR=${HOME}"/models/multi-sentencefix-mt5"
|
6 |
+
export PYTHONPATH=${PROJECT_DIR}
|
7 |
+
|
8 |
+
python3 ${T5X_DIR}/t5x/infer.py \
|
9 |
+
--gin_search_paths=${PROJECT_DIR} \
|
10 |
+
--gin_file="large_wmt_infer.gin" \
|
11 |
+
--gin.CHECKPOINT_PATH=\"${CHECKPOINT_PATH}\" \
|
12 |
+
--gin.INFER_OUTPUT_DIR=\"${INFER_OUTPUT_DIR}\" \
|
13 |
+
--tfds_data_dir=${TFDS_DATA_DIR}
|
14 |
+
|
15 |
+
|
16 |
+
|
tasks.py
ADDED
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# /home/perk/mymodel/categorisation-mt5x/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 |
+
tsv_path = {
|
12 |
+
"train": "gs://peregilcloud/italian_tweets/train.tsv",
|
13 |
+
"validation": "gs://peregilcloud/italian_tweets/dev.tsv",
|
14 |
+
"test": "gs://peregilcloud/italian_tweets/test.tsv"
|
15 |
+
}
|
16 |
+
|
17 |
+
vocabulary = seqio.SentencePieceVocabulary(
|
18 |
+
'gs://t5-data/vocabs/mc4.250000.100extra/sentencepiece.model', extra_ids=0)
|
19 |
+
|
20 |
+
DEFAULT_OUTPUT_FEATURES = {
|
21 |
+
"inputs":
|
22 |
+
seqio.Feature(
|
23 |
+
vocabulary=vocabulary, add_eos=True),
|
24 |
+
"targets":
|
25 |
+
seqio.Feature(
|
26 |
+
vocabulary=vocabulary, add_eos=True)
|
27 |
+
}
|
28 |
+
|
29 |
+
def categorise_preprocessor(ds):
|
30 |
+
def normalize_text(text):
|
31 |
+
"""Lowercase and remove quotes from a TensorFlow string."""
|
32 |
+
text = tf.strings.regex_replace(text,"'(.*)'", r"\1")
|
33 |
+
return text
|
34 |
+
|
35 |
+
def to_inputs_and_targets(ex):
|
36 |
+
"""Map {"source": ..., "source": ...}->{"target": ..., "target": ...}."""
|
37 |
+
return {
|
38 |
+
"inputs":
|
39 |
+
tf.strings.join(
|
40 |
+
[normalize_text(ex["source"])]),
|
41 |
+
"targets":
|
42 |
+
tf.strings.join(
|
43 |
+
[normalize_text(ex["target"])]),
|
44 |
+
}
|
45 |
+
return ds.map(to_inputs_and_targets,
|
46 |
+
num_parallel_calls=tf.data.experimental.AUTOTUNE)
|
47 |
+
|
48 |
+
|
49 |
+
seqio.TaskRegistry.add(
|
50 |
+
"categorise",
|
51 |
+
source=seqio.TextLineDataSource(
|
52 |
+
split_to_filepattern=tsv_path,
|
53 |
+
#num_input_examples=num_nq_examples
|
54 |
+
),
|
55 |
+
preprocessors=[
|
56 |
+
functools.partial(
|
57 |
+
t5.data.preprocessors.parse_tsv,
|
58 |
+
field_names=["target","source"]),
|
59 |
+
categorise_preprocessor,
|
60 |
+
seqio.preprocessors.tokenize_and_append_eos,
|
61 |
+
],
|
62 |
+
#metric_fns=[metrics.bleu],
|
63 |
+
output_features=DEFAULT_OUTPUT_FEATURES,
|
64 |
+
)
|
65 |
+
|
train_base.sh
ADDED
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
PROJECT_DIR=${HOME}"/models/categorisation-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/categorisation"
|
5 |
+
export PYTHONPATH=${PROJECT_DIR}
|
6 |
+
|
7 |
+
python3 ${T5X_DIR}/t5x/train.py \
|
8 |
+
--gin_search_paths=${PROJECT_DIR} \
|
9 |
+
--gin_file="finetuning_categorisation.gin" \
|
10 |
+
--gin.MODEL_DIR="'${MODEL_DIR}'"
|
11 |
+
|
train_large.sh
ADDED
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
PROJECT_DIR=${HOME}"/models/categorisation-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/categorisation_large"
|
5 |
+
export PYTHONPATH=${PROJECT_DIR}
|
6 |
+
|
7 |
+
python3 ${T5X_DIR}/t5x/train.py \
|
8 |
+
--gin_search_paths=${PROJECT_DIR} \
|
9 |
+
--gin_file="finetuning_categorisation_large.gin" \
|
10 |
+
--gin.MODEL_DIR="'${MODEL_DIR}'"
|
11 |
+
|