Spaces:
Running
Running
fix: fixes training script
Browse files- dev/seq2seq/run_seq2seq_flax.py +34 -21
dev/seq2seq/run_seq2seq_flax.py
CHANGED
@@ -46,7 +46,7 @@ from transformers import (
|
|
46 |
HfArgumentParser,
|
47 |
TrainingArguments,
|
48 |
)
|
49 |
-
from transformers.models.bart.modeling_flax_bart import
|
50 |
|
51 |
import wandb
|
52 |
|
@@ -398,7 +398,7 @@ def main():
|
|
398 |
config.normalize_text = model_args.normalize_text
|
399 |
|
400 |
# Create a custom model and initialize it randomly
|
401 |
-
model = CustomFlaxBartForConditionalGeneration
|
402 |
config, seed=training_args.seed, dtype=getattr(jnp, model_args.dtype)
|
403 |
)
|
404 |
|
@@ -566,13 +566,16 @@ def main():
|
|
566 |
steps_per_epoch = (
|
567 |
len_train_dataset // train_batch_size if len_train_dataset is not None else None
|
568 |
)
|
|
|
|
|
|
|
569 |
|
570 |
# Create learning rate schedule
|
571 |
learning_rate_fn = create_learning_rate_fn(
|
572 |
training_args.warmup_steps,
|
573 |
training_args.learning_rate,
|
574 |
data_args.use_decay,
|
575 |
-
|
576 |
)
|
577 |
|
578 |
# We use Optax's "masking" functionality to not apply weight decay
|
@@ -602,8 +605,8 @@ def main():
|
|
602 |
# For more details about the parameters please check https://github.com/deepmind/optax/blob/ed02befef9bf81cbbf236be3d2b0e032e9ed4a40/optax/_src/alias.py#L74
|
603 |
optimizer = optax.adafactor(
|
604 |
learning_rate=learning_rate_fn,
|
605 |
-
weight_decay_rate=training_args.weight_decay,
|
606 |
-
weight_decay_mask=decay_mask_fn,
|
607 |
)
|
608 |
else:
|
609 |
optimizer = optax.adamw(
|
@@ -721,7 +724,11 @@ def main():
|
|
721 |
eval_loader = data_loader_streaming(eval_dataset, eval_batch_size)
|
722 |
else:
|
723 |
eval_loader = data_loader(input_rng, eval_dataset, eval_batch_size)
|
724 |
-
eval_steps =
|
|
|
|
|
|
|
|
|
725 |
for batch in tqdm(
|
726 |
eval_loader,
|
727 |
desc="Evaluating...",
|
@@ -738,7 +745,7 @@ def main():
|
|
738 |
eval_metrics = jax.tree_map(jnp.mean, eval_metrics)
|
739 |
|
740 |
# log metrics
|
741 |
-
wandb_log(eval_metrics, step=
|
742 |
|
743 |
# Print metrics and update progress bar
|
744 |
desc = f"Epoch... ({epoch + 1}/{num_epochs} | Eval Loss: {eval_metrics['loss']})"
|
@@ -763,14 +770,15 @@ def main():
|
|
763 |
opt_state = unreplicate(state.opt_state)
|
764 |
with (Path(training_args.output_dir) / "opt_state.msgpack").open("wb") as f:
|
765 |
f.write(to_bytes(opt_state))
|
|
|
|
|
|
|
|
|
766 |
with (Path(training_args.output_dir) / "training_state.json").open(
|
767 |
"w"
|
768 |
) as f:
|
769 |
json.dump(
|
770 |
-
|
771 |
-
k: get_metrics(state[k])
|
772 |
-
for k in ["step", "epoch", "train_time", "train_samples"]
|
773 |
-
},
|
774 |
f,
|
775 |
)
|
776 |
|
@@ -780,10 +788,7 @@ def main():
|
|
780 |
c = wandb.wandb_sdk.wandb_artifacts.get_artifacts_cache()
|
781 |
c.cleanup(wandb.util.from_human_size("10GB"))
|
782 |
|
783 |
-
metadata =
|
784 |
-
k: get_metrics(state[k])
|
785 |
-
for k in ["step", "epoch", "train_time", "train_samples"]
|
786 |
-
}
|
787 |
if eval_metrics is not None:
|
788 |
metadata["eval"] = eval_metrics
|
789 |
artifact = wandb.Artifact(
|
@@ -819,7 +824,7 @@ def main():
|
|
819 |
training_args.output_dir,
|
820 |
params=params,
|
821 |
push_to_hub=training_args.push_to_hub,
|
822 |
-
commit_message=f"Saving weights and logs at step {
|
823 |
temp_dir=True, # avoid issues with being in a repository
|
824 |
)
|
825 |
|
@@ -830,7 +835,7 @@ def main():
|
|
830 |
for epoch in epochs:
|
831 |
state.replace(epoch=jax_utils.replicate(epoch))
|
832 |
# ======================== Training ================================
|
833 |
-
wandb_log({"train/epoch": epoch}, step=
|
834 |
|
835 |
# Generate an epoch by shuffling sampling indices from the train dataset
|
836 |
if data_args.streaming:
|
@@ -856,12 +861,20 @@ def main():
|
|
856 |
last_time = new_time
|
857 |
|
858 |
# train step
|
859 |
-
state, train_metric = p_train_step(
|
860 |
-
|
|
|
|
|
861 |
|
862 |
if step % data_args.log_interval == 0 and jax.process_index() == 0:
|
863 |
# log metrics
|
864 |
-
wandb_log(
|
|
|
|
|
|
|
|
|
|
|
|
|
865 |
|
866 |
eval_metrics = None
|
867 |
if training_args.eval_steps and step % training_args.eval_steps == 0:
|
@@ -872,7 +885,7 @@ def main():
|
|
872 |
|
873 |
# log final train metrics
|
874 |
if train_metric is not None:
|
875 |
-
train_metric =
|
876 |
wandb_log(train_metric, step=step, prefix="train")
|
877 |
|
878 |
epochs.write(
|
|
|
46 |
HfArgumentParser,
|
47 |
TrainingArguments,
|
48 |
)
|
49 |
+
from transformers.models.bart.modeling_flax_bart import BartConfig
|
50 |
|
51 |
import wandb
|
52 |
|
|
|
398 |
config.normalize_text = model_args.normalize_text
|
399 |
|
400 |
# Create a custom model and initialize it randomly
|
401 |
+
model = CustomFlaxBartForConditionalGeneration(
|
402 |
config, seed=training_args.seed, dtype=getattr(jnp, model_args.dtype)
|
403 |
)
|
404 |
|
|
|
566 |
steps_per_epoch = (
|
567 |
len_train_dataset // train_batch_size if len_train_dataset is not None else None
|
568 |
)
|
569 |
+
num_train_steps = (
|
570 |
+
steps_per_epoch * num_epochs if steps_per_epoch is not None else None
|
571 |
+
)
|
572 |
|
573 |
# Create learning rate schedule
|
574 |
learning_rate_fn = create_learning_rate_fn(
|
575 |
training_args.warmup_steps,
|
576 |
training_args.learning_rate,
|
577 |
data_args.use_decay,
|
578 |
+
num_train_steps,
|
579 |
)
|
580 |
|
581 |
# We use Optax's "masking" functionality to not apply weight decay
|
|
|
605 |
# For more details about the parameters please check https://github.com/deepmind/optax/blob/ed02befef9bf81cbbf236be3d2b0e032e9ed4a40/optax/_src/alias.py#L74
|
606 |
optimizer = optax.adafactor(
|
607 |
learning_rate=learning_rate_fn,
|
608 |
+
# weight_decay_rate=training_args.weight_decay,
|
609 |
+
# weight_decay_mask=decay_mask_fn,
|
610 |
)
|
611 |
else:
|
612 |
optimizer = optax.adamw(
|
|
|
724 |
eval_loader = data_loader_streaming(eval_dataset, eval_batch_size)
|
725 |
else:
|
726 |
eval_loader = data_loader(input_rng, eval_dataset, eval_batch_size)
|
727 |
+
eval_steps = (
|
728 |
+
len_eval_dataset // eval_batch_size
|
729 |
+
if len_eval_dataset is not None
|
730 |
+
else None
|
731 |
+
)
|
732 |
for batch in tqdm(
|
733 |
eval_loader,
|
734 |
desc="Evaluating...",
|
|
|
745 |
eval_metrics = jax.tree_map(jnp.mean, eval_metrics)
|
746 |
|
747 |
# log metrics
|
748 |
+
wandb_log(eval_metrics, step=unreplicate(state.step), prefix="eval")
|
749 |
|
750 |
# Print metrics and update progress bar
|
751 |
desc = f"Epoch... ({epoch + 1}/{num_epochs} | Eval Loss: {eval_metrics['loss']})"
|
|
|
770 |
opt_state = unreplicate(state.opt_state)
|
771 |
with (Path(training_args.output_dir) / "opt_state.msgpack").open("wb") as f:
|
772 |
f.write(to_bytes(opt_state))
|
773 |
+
state_dict = {
|
774 |
+
k: unreplicate(getattr(state, k))
|
775 |
+
for k in ["step", "epoch", "train_time", "train_samples"]
|
776 |
+
}
|
777 |
with (Path(training_args.output_dir) / "training_state.json").open(
|
778 |
"w"
|
779 |
) as f:
|
780 |
json.dump(
|
781 |
+
state_dict,
|
|
|
|
|
|
|
782 |
f,
|
783 |
)
|
784 |
|
|
|
788 |
c = wandb.wandb_sdk.wandb_artifacts.get_artifacts_cache()
|
789 |
c.cleanup(wandb.util.from_human_size("10GB"))
|
790 |
|
791 |
+
metadata = dict(state_dict)
|
|
|
|
|
|
|
792 |
if eval_metrics is not None:
|
793 |
metadata["eval"] = eval_metrics
|
794 |
artifact = wandb.Artifact(
|
|
|
824 |
training_args.output_dir,
|
825 |
params=params,
|
826 |
push_to_hub=training_args.push_to_hub,
|
827 |
+
commit_message=f"Saving weights and logs at step {unreplicate(state.step)+1}",
|
828 |
temp_dir=True, # avoid issues with being in a repository
|
829 |
)
|
830 |
|
|
|
835 |
for epoch in epochs:
|
836 |
state.replace(epoch=jax_utils.replicate(epoch))
|
837 |
# ======================== Training ================================
|
838 |
+
wandb_log({"train/epoch": epoch}, step=unreplicate(state.step))
|
839 |
|
840 |
# Generate an epoch by shuffling sampling indices from the train dataset
|
841 |
if data_args.streaming:
|
|
|
861 |
last_time = new_time
|
862 |
|
863 |
# train step
|
864 |
+
state, train_metric = p_train_step(
|
865 |
+
state, batch, jax_utils.replicate(delta_time)
|
866 |
+
)
|
867 |
+
step = unreplicate(state.step)
|
868 |
|
869 |
if step % data_args.log_interval == 0 and jax.process_index() == 0:
|
870 |
# log metrics
|
871 |
+
wandb_log(unreplicate(train_metric), step=step, prefix="train")
|
872 |
+
# log state parameters
|
873 |
+
state_dict = {
|
874 |
+
k.split("_")[-1]: unreplicate(getattr(state, k))
|
875 |
+
for k in ["epoch", "train_time", "train_samples"]
|
876 |
+
}
|
877 |
+
wandb_log(state_dict, step=step, prefix="train")
|
878 |
|
879 |
eval_metrics = None
|
880 |
if training_args.eval_steps and step % training_args.eval_steps == 0:
|
|
|
885 |
|
886 |
# log final train metrics
|
887 |
if train_metric is not None:
|
888 |
+
train_metric = unreplicate(train_metric)
|
889 |
wandb_log(train_metric, step=step, prefix="train")
|
890 |
|
891 |
epochs.write(
|