Skip to content

Commit

Permalink
make fixup
Browse files Browse the repository at this point in the history
  • Loading branch information
Rocketknight1 committed Aug 9, 2022
1 parent 5ccafe6 commit 5141255
Show file tree
Hide file tree
Showing 12 changed files with 99 additions and 94 deletions.
12 changes: 7 additions & 5 deletions examples/tensorflow/language-modeling/run_clm.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
"""
# You can also adapt this script on your own clm task. Pointers for this are left as comments.

import json

# region Imports
import logging
import math
Expand All @@ -32,7 +34,6 @@
from itertools import chain
from pathlib import Path
from typing import Optional
import json

import datasets
import tensorflow as tf
Expand All @@ -48,11 +49,11 @@
AutoConfig,
AutoTokenizer,
HfArgumentParser,
PushToHubCallback,
TFAutoModelForCausalLM,
TFTrainingArguments,
create_optimizer,
set_seed,
PushToHubCallback,
)
from transformers.utils import send_example_telemetry
from transformers.utils.versions import require_version
Expand Down Expand Up @@ -208,6 +209,7 @@ def __post_init__(self):

# endregion


def main():
# region Argument Parsing
parser = HfArgumentParser((ModelArguments, DataTrainingArguments, TFTrainingArguments))
Expand Down Expand Up @@ -529,7 +531,7 @@ def group_texts(examples):

# region Preparing push_to_hub and model card
push_to_hub_model_id = training_args.push_to_hub_model_id
model_name = model_args.model_name_or_path.split('/')[-1]
model_name = model_args.model_name_or_path.split("/")[-1]
if not push_to_hub_model_id:
if data_args.dataset_name is not None:
push_to_hub_model_id = f"{model_name}-finetuned-{data_args.dataset_name}"
Expand All @@ -553,7 +555,7 @@ def group_texts(examples):
organization=training_args.push_to_hub_organization,
token=training_args.push_to_hub_token,
tokenizer=tokenizer,
**model_card_kwargs
**model_card_kwargs,
)
]
else:
Expand All @@ -576,7 +578,7 @@ def group_texts(examples):
tf_train_dataset,
validation_data=tf_eval_dataset if training_args.do_eval else None,
epochs=int(training_args.num_train_epochs),
callbacks=callbacks
callbacks=callbacks,
)
train_loss = history.history["loss"][-1]
try:
Expand Down
11 changes: 6 additions & 5 deletions examples/tensorflow/language-modeling/run_mlm.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"""
# You can also adapt this script on your own mlm task. Pointers for this are left as comments.

import json
import logging
import math
import os
Expand All @@ -31,7 +32,6 @@
from itertools import chain
from pathlib import Path
from typing import Optional
import json

import datasets
import tensorflow as tf
Expand All @@ -48,11 +48,11 @@
AutoTokenizer,
DataCollatorForLanguageModeling,
HfArgumentParser,
PushToHubCallback,
TFAutoModelForMaskedLM,
TFTrainingArguments,
create_optimizer,
set_seed,
PushToHubCallback
)
from transformers.utils import send_example_telemetry
from transformers.utils.versions import require_version
Expand Down Expand Up @@ -215,6 +215,7 @@ def __post_init__(self):

# endregion


def main():
# region Argument Parsing
parser = HfArgumentParser((ModelArguments, DataTrainingArguments, TFTrainingArguments))
Expand Down Expand Up @@ -552,7 +553,7 @@ def group_texts(examples):

# region Preparing push_to_hub and model card
push_to_hub_model_id = training_args.push_to_hub_model_id
model_name = model_args.model_name_or_path.split('/')[-1]
model_name = model_args.model_name_or_path.split("/")[-1]
if not push_to_hub_model_id:
if data_args.dataset_name is not None:
push_to_hub_model_id = f"{model_name}-finetuned-{data_args.dataset_name}"
Expand All @@ -576,7 +577,7 @@ def group_texts(examples):
organization=training_args.push_to_hub_organization,
token=training_args.push_to_hub_token,
tokenizer=tokenizer,
**model_card_kwargs
**model_card_kwargs,
)
]
else:
Expand All @@ -598,7 +599,7 @@ def group_texts(examples):
tf_train_dataset,
validation_data=tf_eval_dataset if training_args.do_eval else None,
epochs=int(training_args.num_train_epochs),
callbacks=callbacks
callbacks=callbacks,
)
train_loss = history.history["loss"][-1]
try:
Expand Down
16 changes: 7 additions & 9 deletions examples/tensorflow/multiple-choice/run_swag.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@
"""
# You can also adapt this script on your own multiple choice task. Pointers for this are left as comments.

import json
import logging
import os
import sys
from dataclasses import dataclass, field
from itertools import chain
from pathlib import Path
from typing import Optional, Union
import json

import datasets
import tensorflow as tf
Expand All @@ -39,11 +39,11 @@
AutoTokenizer,
DefaultDataCollator,
HfArgumentParser,
PushToHubCallback,
TFAutoModelForMultipleChoice,
TFTrainingArguments,
create_optimizer,
set_seed,
PushToHubCallback
)
from transformers.tokenization_utils_base import PreTrainedTokenizerBase
from transformers.utils import PaddingStrategy, check_min_version, send_example_telemetry
Expand All @@ -57,6 +57,7 @@

# region Helper classes and functions


@dataclass
class DataCollatorForMultipleChoice:
"""
Expand Down Expand Up @@ -397,8 +398,6 @@ def preprocess_function(examples):
if "validation" not in raw_datasets:
raise ValueError("--do_eval requires a validation dataset")
eval_dataset = raw_datasets["validation"]
if not training_args.do_train:
non_label_columns = [feature for feature in eval_dataset.features if feature not in ("label", "labels")]
if data_args.max_eval_samples is not None:
max_eval_samples = min(len(eval_dataset), data_args.max_eval_samples)
eval_dataset = eval_dataset.select(range(max_eval_samples))
Expand Down Expand Up @@ -455,13 +454,12 @@ def preprocess_function(examples):
)
else:
optimizer = None
lr_schedule = None
model.compile(optimizer=optimizer, metrics=['accuracy'], jit_compile=training_args.xla)
model.compile(optimizer=optimizer, metrics=["accuracy"], jit_compile=training_args.xla)
# endregion

# region Preparing push_to_hub and model card
push_to_hub_model_id = training_args.push_to_hub_model_id
model_name = model_args.model_name_or_path.split('/')[-1]
model_name = model_args.model_name_or_path.split("/")[-1]
if not push_to_hub_model_id:
push_to_hub_model_id = f"{model_name}-finetuned-multiplechoice"

Expand All @@ -475,7 +473,7 @@ def preprocess_function(examples):
organization=training_args.push_to_hub_organization,
token=training_args.push_to_hub_token,
tokenizer=tokenizer,
**model_card_kwargs
**model_card_kwargs,
)
]
else:
Expand Down Expand Up @@ -518,7 +516,7 @@ def preprocess_function(examples):
tf_train_dataset,
validation_data=validation_data,
epochs=int(training_args.num_train_epochs),
callbacks=callbacks
callbacks=callbacks,
)
eval_metrics = {key: val[-1] for key, val in history.history.items()}
# endregion
Expand Down
15 changes: 7 additions & 8 deletions examples/tensorflow/question-answering/run_qa.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@
"""
# You can also adapt this script on your own question answering task. Pointers for this are left as comments.

import json
import logging
import os
import sys
from dataclasses import dataclass, field
from pathlib import Path
from typing import Optional
import json

import tensorflow as tf
from datasets import load_dataset
Expand All @@ -37,11 +37,11 @@
EvalPrediction,
HfArgumentParser,
PreTrainedTokenizerFast,
PushToHubCallback,
TFAutoModelForQuestionAnswering,
TFTrainingArguments,
set_seed,
create_optimizer,
PushToHubCallback
set_seed,
)
from transformers.utils import CONFIG_NAME, TF2_WEIGHTS_NAME, check_min_version, send_example_telemetry
from utils_qa import postprocess_qa_predictions
Expand Down Expand Up @@ -658,13 +658,12 @@ def compute_metrics(p: EvalPrediction):
)

# no user-specified loss = will use the model internal loss
model.compile(optimizer=optimizer, jit_compile=training_args.xla, metrics=['accuracy'])
model.compile(optimizer=optimizer, jit_compile=training_args.xla, metrics=["accuracy"])

else:
model.compile(optimizer=None, jit_compile=training_args.xla, metrics=['accuracy'])
model.compile(optimizer=None, jit_compile=training_args.xla, metrics=["accuracy"])
training_dataset = None


if training_args.do_eval:
eval_dataset = model.prepare_tf_dataset(
processed_datasets["validation"],
Expand All @@ -691,7 +690,7 @@ def compute_metrics(p: EvalPrediction):

# region Preparing push_to_hub and model card
push_to_hub_model_id = training_args.push_to_hub_model_id
model_name = model_args.model_name_or_path.split('/')[-1]
model_name = model_args.model_name_or_path.split("/")[-1]
if not push_to_hub_model_id:
if data_args.dataset_name is not None:
push_to_hub_model_id = f"{model_name}-finetuned-{data_args.dataset_name}"
Expand All @@ -715,7 +714,7 @@ def compute_metrics(p: EvalPrediction):
organization=training_args.push_to_hub_organization,
token=training_args.push_to_hub_token,
tokenizer=tokenizer,
**model_card_kwargs
**model_card_kwargs,
)
]
else:
Expand Down
26 changes: 14 additions & 12 deletions examples/tensorflow/summarization/run_summarization.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@
"""
# You can also adapt this script on your own sequence to sequence task. Pointers for this are left as comments.

import json
import logging
import os
import sys
from dataclasses import dataclass, field
from typing import Optional
import json

import datasets
import nltk # Here to have a nice missing dependency error message early on
Expand All @@ -37,14 +37,14 @@
from transformers import (
AutoConfig,
AutoTokenizer,
DataCollatorForSeq2Seq,
HfArgumentParser,
KerasMetricCallback,
PushToHubCallback,
TFAutoModelForSeq2SeqLM,
TFTrainingArguments,
create_optimizer,
set_seed,
DataCollatorForSeq2Seq,
KerasMetricCallback,
PushToHubCallback
)
from transformers.trainer_utils import get_last_checkpoint
from transformers.utils import check_min_version, is_offline_mode, send_example_telemetry
Expand Down Expand Up @@ -272,6 +272,7 @@ def __post_init__(self):
}
# endregion


def main():
# region Argument parsing
# See all possible arguments in src/transformers/training_args.py
Expand Down Expand Up @@ -528,7 +529,7 @@ def postprocess_text(preds, labels):
model=model,
label_pad_token_id=label_pad_token_id,
pad_to_multiple_of=128, # Reduce the number of unique shapes for XLA, especially for generation
return_tensors='tf',
return_tensors="tf",
)

dataset_options = tf.data.Options()
Expand Down Expand Up @@ -582,7 +583,6 @@ def postprocess_text(preds, labels):
)
else:
optimizer = None
lr_schedule = None

# endregion

Expand All @@ -596,7 +596,7 @@ def postprocess_text(preds, labels):
gen_kwargs = {
"max_length": data_args.val_max_target_length if data_args is not None else config.max_length,
"num_beams": data_args.num_beams,
"no_repeat_ngram_size": 0 # Not supported under XLA right now, and some models set it by default
"no_repeat_ngram_size": 0, # Not supported under XLA right now, and some models set it by default
}

def compute_metrics(preds):
Expand All @@ -623,7 +623,7 @@ def compute_metrics(preds):
eval_dataset=tf_eval_dataset,
predict_with_generate=True,
use_xla_generation=True,
generate_kwargs=gen_kwargs
generate_kwargs=gen_kwargs,
)
callbacks = [metric_callback]
else:
Expand All @@ -632,7 +632,7 @@ def compute_metrics(preds):

# region Preparing push_to_hub and model card
push_to_hub_model_id = training_args.push_to_hub_model_id
model_name = model_args.model_name_or_path.split('/')[-1]
model_name = model_args.model_name_or_path.split("/")[-1]
if not push_to_hub_model_id:
if data_args.dataset_name is not None:
push_to_hub_model_id = f"{model_name}-finetuned-{data_args.dataset_name}"
Expand All @@ -657,7 +657,7 @@ def compute_metrics(preds):
organization=training_args.push_to_hub_organization,
token=training_args.push_to_hub_token,
tokenizer=tokenizer,
**model_card_kwargs
**model_card_kwargs,
)
)
# endregion
Expand All @@ -674,8 +674,10 @@ def compute_metrics(preds):
logger.info(f" Total optimization steps = {num_train_steps}")

if training_args.xla and not data_args.pad_to_max_length:
logger.warning("XLA training may be slow at first when --pad_to_max_length is not set "
"until all possible shapes have been compiled.")
logger.warning(
"XLA training may be slow at first when --pad_to_max_length is not set "
"until all possible shapes have been compiled."
)
history = model.fit(tf_train_dataset, epochs=int(training_args.num_train_epochs), callbacks=callbacks)
eval_metrics = {key: val[-1] for key, val in history.history.items()}
# endregion
Expand Down
Loading

0 comments on commit 5141255

Please sign in to comment.