-
Notifications
You must be signed in to change notification settings - Fork 75
Add The Pertensor Quant And Fix some BUGs #458
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
Open
Michael20070814
wants to merge
8
commits into
ModelTC:main
Choose a base branch
from
Michael20070814:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
3566f10
add some description
Michael20070814 626ec9b
fix the bug
Michael20070814 e0117aa
Add the calib export and fix some problems
Michael20070814 cac2d91
remove the n_sample and repair the fixing errors
Michael20070814 cecf46c
Make the Project more moduled
Michael20070814 3fa3e9d
Rename the config to make the target explicit
Michael20070814 3911c49
modify the comment and transform the chinese to English
Michael20070814 5589e82
remove the hardcode
Michael20070814 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,3 +22,5 @@ save* | |
| .log | ||
| *.pid | ||
| *.ipynb* | ||
| .venv/ | ||
| *.sh | ||
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,69 @@ | ||
| import torch | ||
|
|
||
|
|
||
| def collect_lightllm_kv_calib_json(blockwise_opt): | ||
| if not getattr(blockwise_opt, 'quant_kvcache', False): | ||
| raise ValueError( | ||
| 'save_lightllm_kv_cache_calib requires kvcache quantization.' | ||
| ) | ||
|
|
||
| kv_cfg = blockwise_opt.quant_config['kvcache'] | ||
| granularity = kv_cfg.get('granularity') | ||
| if granularity not in ['per_tensor', 'per_head']: | ||
| raise ValueError( | ||
| f'LightLLM calib export only supports per_tensor/per_head, got {granularity}' | ||
| ) | ||
|
|
||
| num_layers = blockwise_opt.model.model_config.num_hidden_layers | ||
| num_head = int( | ||
| getattr( | ||
| blockwise_opt.model.model_config, | ||
| 'num_key_value_heads', | ||
| blockwise_opt.model.get_num_attention_heads(), | ||
| ) | ||
| ) | ||
| scales = [] | ||
| for layer_idx in range(num_layers): | ||
| key_scale = blockwise_opt._collect_lightllm_kv_scale( | ||
| blockwise_opt.kv_module.k_scales_buffer[layer_idx], | ||
| blockwise_opt.kv_module.k_zeros_buffer[layer_idx], | ||
| blockwise_opt.kv_module.k_qmin_buffer[layer_idx], | ||
| blockwise_opt.kv_module.k_qmax_buffer[layer_idx], | ||
| ) | ||
| value_scale = blockwise_opt._collect_lightllm_kv_scale( | ||
| blockwise_opt.kv_module.v_scales_buffer[layer_idx], | ||
| blockwise_opt.kv_module.v_zeros_buffer[layer_idx], | ||
| blockwise_opt.kv_module.v_qmin_buffer[layer_idx], | ||
| blockwise_opt.kv_module.v_qmax_buffer[layer_idx], | ||
| ) | ||
| if key_scale is None or value_scale is None: | ||
| raise ValueError(f'Calibration scale for layer {layer_idx} is empty.') | ||
|
|
||
| scale_row = torch.cat([key_scale.reshape(-1), value_scale.reshape(-1)]).tolist() | ||
| scales.append(scale_row) | ||
|
|
||
| scale_width = len(scales[0]) if scales else 0 | ||
| if granularity == 'per_tensor' and scale_width != 2: | ||
| raise ValueError(f'per_tensor export expects 2 scales per layer, got {scale_width}') | ||
| if granularity == 'per_head' and scale_width != num_head * 2: | ||
| raise ValueError( | ||
| f'per_head export expects {num_head * 2} scales per layer, got {scale_width}' | ||
| ) | ||
|
|
||
| architectures = getattr(blockwise_opt.model.model_config, 'architectures', None) | ||
| if isinstance(architectures, list) and len(architectures) > 0: | ||
| architectures = architectures[0] | ||
| elif architectures is None: | ||
| architectures = blockwise_opt.config.model.type | ||
|
|
||
| return { | ||
| 'version': '1.0', | ||
| 'architectures': architectures, | ||
| 'quant_type': granularity, | ||
| 'qmin': float(torch.finfo(torch.float8_e4m3fn).min), | ||
| 'qmax': float(torch.finfo(torch.float8_e4m3fn).max), | ||
| 'num_layers': num_layers, | ||
| 'num_head': num_head, | ||
| 'scales_shape': [num_layers, scale_width], | ||
| 'scales': scales, | ||
| } |
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.
The process of exporting calibration json file strictly relates with LightLLM. It's better to move this part to the utils folder.
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.
Yes. I will make it more moduled