Skip to content

Commit

Permalink
Fix progress callback deepcopy (huggingface#32070)
Browse files Browse the repository at this point in the history
* Replacing ProgressCallbacks deepcopy with a shallowcopy

* Using items instead of entries

* code cleanup for copy in trainer callback

* Style fix for ProgressCallback
  • Loading branch information
fozziethebeat authored and dataKim1201 committed Oct 7, 2024
1 parent f1d0d34 commit 531e0be
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions src/transformers/trainer_callback.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
Callbacks to use with the Trainer class and customize the training loop.
"""

import copy
import dataclasses
import json
from dataclasses import dataclass
Expand Down Expand Up @@ -617,13 +616,16 @@ def on_predict(self, args, state, control, **kwargs):

def on_log(self, args, state, control, logs=None, **kwargs):
if state.is_world_process_zero and self.training_bar is not None:
# avoid modifying the logs object as it is shared between callbacks
logs = copy.deepcopy(logs)
_ = logs.pop("total_flos", None)
# make a shallow copy of logs so we can mutate the fields copied
# but avoid doing any value pickling.
shallow_logs = {}
for k, v in logs.items():
shallow_logs[k] = v
_ = shallow_logs.pop("total_flos", None)
# round numbers so that it looks better in console
if "epoch" in logs:
logs["epoch"] = round(logs["epoch"], 2)
self.training_bar.write(str(logs))
if "epoch" in shallow_logs:
shallow_logs["epoch"] = round(shallow_logs["epoch"], 2)
self.training_bar.write(str(shallow_logs))

def on_train_end(self, args, state, control, **kwargs):
if state.is_world_process_zero:
Expand Down

0 comments on commit 531e0be

Please sign in to comment.