forked from NVIDIA/Megatron-LM
-
Notifications
You must be signed in to change notification settings - Fork 0
Measure scales #3
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
Merged
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
9973d6c
Measure scales
jlamypoirier 47c0549
Add parameter names
jlamypoirier da3fd22
stuff
jlamypoirier 253aa6e
Merge branch 'llm-custom' into measure_scales
jlamypoirier 5c2c6a3
wip
jlamypoirier 3598b23
stuff
jlamypoirier 5171dfa
logging
jlamypoirier 839c793
fixes
jlamypoirier 96c7993
fix
jlamypoirier 89a9bd3
Merge branch 'llm-custom' into measure_scales
jlamypoirier cf49f87
Fix and wait for file
jlamypoirier 1b69da9
More loading fix and waiting log
jlamypoirier 8e2d5e9
Fixes and more logging
jlamypoirier da43385
fixes
jlamypoirier 23935dd
More fixes and tweaks
jlamypoirier 1527f75
Update names
jlamypoirier d347b2f
fix
jlamypoirier File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| import logging | ||
| import math | ||
|
|
||
| import torch | ||
| from megatron.global_vars import get_args | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
| _iteration=0 | ||
| _metrics={} | ||
| _LOGGING_WIDTH=50 | ||
|
|
||
| def next_iteration(iteration:int): | ||
| global _iteration, _metrics | ||
| _metrics={} | ||
| _iteration=iteration | ||
|
|
||
|
|
||
| def record_scale(name:str,x:torch.Tensor,grad=True, bias=None): | ||
| global _metrics | ||
| if get_log_scales(): | ||
| _metrics[f"{name}.scale" if grad else name]=get_scale(x if bias is None else x+bias) | ||
| if grad and x.requires_grad: | ||
| x.register_hook(lambda g: record_scale(f"{name}.grad",g,False)) | ||
|
|
||
|
|
||
| def get_scale(x): | ||
| return x.detach().float().pow(2).mean().pow(0.5) | ||
|
|
||
|
|
||
| def get_log_scales(): | ||
| args=get_args() | ||
| return args.log_scales and (_iteration+1) % args.log_interval == 0 | ||
|
|
||
|
|
||
| def log_metrics(): | ||
| metrics = {} | ||
| for key, value in _metrics.items(): | ||
| metrics_ = metrics | ||
| keys = key.split(".") | ||
| for prefix in keys[:-1]: | ||
| if prefix not in metrics_: | ||
| metrics_[prefix] = {} | ||
| metrics_ = metrics_[prefix] | ||
| metrics_[keys[-1]] = _format_value(value) | ||
| _log_dicts(metrics) | ||
|
|
||
|
|
||
| def _log_dicts(metrics, indent=0): | ||
| for key, value in metrics.items(): | ||
| key_ = key.rjust(len(key) + indent) | ||
|
|
||
| # Merge keys when there is only one entry. | ||
| while isinstance(value, dict) and len(value) == 1: | ||
| for value_key, value_ in value.items(): | ||
| key_ = ".".join([key_, value_key]) | ||
| value = value_ | ||
| if isinstance(value, dict): | ||
| logger.info(key_ + ":") | ||
| _log_dicts(value, indent + 2) | ||
| else: | ||
| sep = _LOGGING_WIDTH - len(value) - len(key_) - 2 | ||
| logger.info(f"{key_.ljust(len(key_)+sep,'.')} {value}") | ||
|
|
||
|
|
||
| def _format_value(value, precision=5,max_leading_zeros=3): | ||
| decimals = 0 if value == 0 or not math.isfinite(value) else precision - math.floor(math.log10(abs(value))) | ||
|
|
||
| if 0 <= decimals <= precision + max_leading_zeros: | ||
| value = f"{value:.{decimals}f}" | ||
| else: | ||
| value = f"{value:.{precision}e}" | ||
| return value |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
May add a log message, so the job is not stuck (maybe each 10 s)
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.
Is it really necessary? Worst cast it will crash after 2 minutes
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.
Added some log and copied to the other datasets