-
Notifications
You must be signed in to change notification settings - Fork 742
【Do Not Merge】 测试dynamic loading用,存储模型pdparams #7789
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
DDDivano
wants to merge
1
commit into
develop
Choose a base branch
from
save_model_for_rl
base: develop
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.
+46
−0
Open
Changes from all commits
Commits
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 |
|---|---|---|
|
|
@@ -1426,6 +1426,52 @@ def load_model(self) -> None: | |
| # 4. Init proposer for speculative method | ||
| self._init_speculative_proposer() | ||
|
|
||
| # Usage: FD_SAVE_PDPARAMS=1 FD_SAVE_DIR=/path/to/output python -m fastdeploy... | ||
| if os.getenv("FD_SAVE_PDPARAMS", "0") == "1": | ||
| import shutil | ||
| import glob as glob_mod | ||
|
|
||
| visible_devices = os.getenv("CUDA_VISIBLE_DEVICES", "0").split(",") | ||
| meta_src_id = int(visible_devices[int(os.getenv("FLAGS_selected_gpus", "0"))]) | ||
| rank = paddle.distributed.get_rank() | ||
|
Comment on lines
+1434
to
+1436
|
||
|
|
||
| # Determine save directory: FD_SAVE_DIR or default to model directory | ||
| save_dir = os.getenv("FD_SAVE_DIR", self.fd_config.model_config.model) | ||
| os.makedirs(save_dir, exist_ok=True) | ||
|
|
||
| # Copy config and tokenizer files (only rank 0 to avoid race) | ||
| if rank == 0: | ||
| src_dir = self.fd_config.model_config.model | ||
| copy_patterns = [ | ||
| "config.json", "generation_config.json", | ||
| "tokenizer*", "added_tokens.json", | ||
| "special_tokens_map.json", "chat_template*", | ||
| ] | ||
| for pattern in copy_patterns: | ||
| for f in glob_mod.glob(os.path.join(src_dir, pattern)): | ||
| dst = os.path.join(save_dir, os.path.basename(f)) | ||
| if not os.path.exists(dst): | ||
| shutil.copy2(f, dst) | ||
|
|
||
| # Save model weights (main model + proposer/MTP model if exists) | ||
| model_state_dict = self.model.state_dict() | ||
| if hasattr(self, 'proposer') and self.proposer is not None and hasattr(self.proposer, 'model'): | ||
| proposer_state_dict = self.proposer.model.state_dict() | ||
|
|
||
| model_state_dict.update(proposer_state_dict) | ||
| logger.info(f"Including proposer model weights ({len(proposer_state_dict)} params)") | ||
|
|
||
| clean_state_dict = { | ||
| k: paddle.to_tensor(v.contiguous().numpy()) | ||
|
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. 🟡 建议
clean_state_dict = {k: v.contiguous() for k, v in model_state_dict.items()}或直接传入 |
||
| for k, v in model_state_dict.items() | ||
| } | ||
| model_path = os.path.join( | ||
| save_dir, | ||
| f"model_state.tp{rank}.{meta_src_id}.pdparams", | ||
| ) | ||
| paddle.save(clean_state_dict, model_path, safetensors=True) | ||
|
|
||
| del clean_state_dict | ||
| logger.info(f"Saved model state dict to {model_path}") | ||
|
Comment on lines
+1429
to
+1473
|
||
|
|
||
| # Load RL dynamic model | ||
| if self.fd_config.load_config.dynamic_load_weight: | ||
| from fastdeploy.rl.dynamic_weight_manager import DynamicWeightManager | ||
|
|
||
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.
🔴 Bug
visible_devices数组可能越界,当FLAGS_selected_gpus值 ≥visible_devices长度时将抛出 IndexError。例如
CUDA_VISIBLE_DEVICES=0但FLAGS_selected_gpus=1时触发;或CUDA_VISIBLE_DEVICES为空字符串时split(',')产生[''],int('')同样报错。建议修复: