-
Notifications
You must be signed in to change notification settings - Fork 1.5k
[WIP] support tps #9513
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
[WIP] support tps #9513
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -1629,6 +1629,7 @@ def data_collator(self, batch: List[Dict[str, Any]], *, padding_to: Optional[int | |||||
| if self.packing and isinstance(batch[0], list): | ||||||
| batch = sum(batch, start=[]) | ||||||
| num_samples = len(batch) | ||||||
| num_tokens = sum(sum([b['lengths'] for b in batch], start=[])) | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using
Suggested change
|
||||||
| if self.task_type == 'causal_lm': | ||||||
| if self.mode in {'transformers', 'train'}: | ||||||
| res = self._data_collator(batch, padding_to=padding_to) | ||||||
|
|
@@ -1657,6 +1658,7 @@ def data_collator(self, batch: List[Dict[str, Any]], *, padding_to: Optional[int | |||||
| num_samples = res.pop('num_samples') | ||||||
| if self.use_megatron: | ||||||
| res['num_samples'] = num_samples | ||||||
| res['num_tokens'] = num_tokens | ||||||
| return res | ||||||
|
|
||||||
| @staticmethod | ||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -28,6 +28,15 @@ def add_train_message(logs, state, start_time, start_step) -> None: | |
| if state.max_memory: | ||
| logs['memory(GiB)'] = round(state.max_memory, 2) | ||
| logs['train_speed(s/it)'] = round(train_speed, 6) | ||
| num_tokens = getattr(state, 'num_tokens', None) | ||
| if num_tokens is not None: | ||
| num_tokens = float(num_tokens) | ||
| if dist.is_initialized(): | ||
| num_tokens = torch.tensor(num_tokens) | ||
| dist.all_reduce(num_tokens, op=dist.ReduceOp.SUM) | ||
| tps = num_tokens / elapsed | ||
| logs['num_input_tokens_seen'] = round(num_tokens, 6) | ||
| logs['train_speed(tokens/s)'] = round(tps, 6) | ||
|
Comment on lines
+31
to
+39
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There are several issues here:\n1. num_tokens = getattr(state, 'num_tokens', None)\n if num_tokens is not None:\n import torch\n import torch.distributed as dist\n from swift.utils import get_current_device\n num_tokens = float(num_tokens)\n if dist.is_initialized():\n device = get_current_device()\n num_tokens_tensor = torch.tensor(num_tokens, device=device)\n dist.all_reduce(num_tokens_tensor, op=dist.ReduceOp.SUM)\n num_tokens = num_tokens_tensor.item()\n tps = num_tokens / elapsed\n logs['num_input_tokens_seen'] = round(num_tokens, 6)\n logs['train_speed(tokens/s)'] = round(tps, 6) |
||
|
|
||
|
|
||
| class ProgressCallbackNew(ProgressCallback): | ||
|
|
@@ -50,6 +59,10 @@ def on_prediction_step(self, args, state: TrainerState, control, eval_dataloader | |
|
|
||
| def on_log(self, args: TrainingArguments, state: TrainerState, control, logs=None, **kwargs): | ||
| add_train_message(logs, state, self.start_time, self.start_step) | ||
| n_steps = state.global_step - self.current_step | ||
| num_tokens = logs.pop('num_tokens', None) | ||
| if num_tokens is not None and n_steps > 0: | ||
| logs[''] | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| if not is_pai_training_job() and state.is_world_process_zero: | ||
| jsonl_path = os.path.join(args.output_dir, 'logging.jsonl') | ||
| append_to_jsonl(jsonl_path, logs) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -136,8 +136,11 @@ def compute_loss(self, model, inputs, return_outputs=False, num_items_in_batch=N | |
| logger.warning_once('The cross_entropy loss function defined in Liger Kernel will not ' | ||
| 'take effect, potentially leading to increased GPU memory consumption.') | ||
| labels = inputs.pop('labels') | ||
| num_tokens = inputs.pop('num_tokens', None) | ||
| outputs = model(**inputs) | ||
| mode = 'train' if self.model.training else 'eval' | ||
| if num_tokens is not None: | ||
| self.state.num_tokens += num_tokens | ||
|
Comment on lines
+142
to
+143
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
if num_tokens is not None:\n self.state.num_tokens = getattr(self.state, 'num_tokens', 0) + num_tokens |
||
| if getattr(outputs, 'aux_loss', None) is not None: | ||
| self.custom_metrics[mode]['aux_loss'].update(outputs.aux_loss) | ||
| # Save past state if it exists | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The variable
reductionis not defined in thecomputemethod. It should be accessed viaself.reductionas initialized in__init__.