-
Notifications
You must be signed in to change notification settings - Fork 744
[OP][Loader]decrease code && make code clean #7877
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
zhoutianzi666
wants to merge
5
commits into
PaddlePaddle:develop
Choose a base branch
from
zhoutianzi666:add_assert
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.
+24
−49
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 |
|---|---|---|
|
|
@@ -41,6 +41,22 @@ | |
| from .utils import _set_var_distributed, divide, get_tensor, modules_to_convert | ||
|
|
||
|
|
||
| def may_be_do_cast(loaded_weight, param): | ||
|
|
||
| assert param.shape == loaded_weight.shape, ( | ||
| f" Attempted to load weight ({loaded_weight.shape}) " f"into parameter ({param.shape})" | ||
| ) | ||
| # Ensure loaded weight dtype matches model param dtype | ||
| if loaded_weight.dtype != param.dtype: | ||
| if loaded_weight.dtype == paddle.int8 and param.dtype == paddle.float8_e4m3fn: | ||
| loaded_weight = loaded_weight.view(param.dtype) | ||
| else: | ||
| assert ( | ||
This comment was marked as outdated.
Sorry, something went wrong. 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. 🔴 Bug
建议修复方式: else:
raise ValueError(
f"loaded_weight.dtype: {loaded_weight.dtype}, param.dtype: {param.dtype}"
) |
||
| loaded_weight.dtype == param.dtype | ||
This comment was marked as outdated.
Sorry, something went wrong. |
||
| ), f"loaded_weight.dtype: {loaded_weight.dtype}, param.dtype: {param.dtype}" | ||
| return loaded_weight | ||
|
|
||
|
|
||
| class UnquantizedLinearMethod(QuantMethodBase): | ||
| """Linear method without quantization.""" | ||
|
|
||
|
|
@@ -407,15 +423,7 @@ def weight_loader(self, param, loaded_weight, loaded_shard_id: Optional[str] = N | |
| start=param_shard_offset, | ||
| end=param_shard_offset + param_shard_size, | ||
| ) | ||
| assert param.shape == loaded_weight.shape, ( | ||
| f" Attempted to load weight ({loaded_weight.shape}) " f"into parameter ({param.shape})" | ||
| ) | ||
| # Ensure loaded weight dtype matches model param dtype | ||
| if loaded_weight.dtype != param.dtype: | ||
| if loaded_weight.dtype == paddle.int8 and param.dtype == paddle.float8_e4m3fn: | ||
| loaded_weight = loaded_weight.view(param.dtype) | ||
| else: | ||
| loaded_weight = loaded_weight.cast(param.dtype) | ||
| loaded_weight = may_be_do_cast(loaded_weight, param) | ||
| # (bukejiyu) After this fix, the early H2D copy for non-GPU devices is no longer needed and can be safely removed. | ||
| h2d_copy(param, loaded_weight) | ||
|
|
||
|
|
@@ -592,16 +600,7 @@ def weight_loader(self, param, loaded_weight, loaded_shard_id: Optional[str] = N | |
| if hasattr(param, "tensor_track"): | ||
| param.tensor_track.mark(start=param_shard_offset, end=param_shard_offset + param_shard_size) | ||
| param = slice_fn(param, output_dim, start=param_shard_offset, end=param_shard_offset + param_shard_size) | ||
| assert param.shape == loaded_weight.shape, ( | ||
| f" Attempted to load weight ({loaded_weight.shape}) " f"into parameter ({param.shape})" | ||
| ) | ||
| # Ensure loaded weight dtype matches model param dtype | ||
| if loaded_weight.dtype != param.dtype: | ||
| if loaded_weight.dtype == paddle.int8 and param.dtype == paddle.float8_e4m3fn: | ||
| loaded_weight = loaded_weight.view(param.dtype) | ||
| else: | ||
| loaded_weight = loaded_weight.cast(param.dtype) | ||
|
|
||
| loaded_weight = may_be_do_cast(loaded_weight, param) | ||
| h2d_copy(param, loaded_weight) | ||
|
|
||
| def load_state_dict(self, state_dict: dict): | ||
|
|
@@ -753,15 +752,7 @@ def weight_loader(self, param, loaded_weight, loaded_shard_id: Optional[str] = N | |
| param.tensor_track.mark(start=param_shard_offset, end=param_shard_offset + param_shard_size) | ||
|
|
||
| param = slice_fn(param, output_dim, start=param_shard_offset, end=param_shard_offset + param_shard_size) | ||
| assert param.shape == loaded_weight.shape, ( | ||
| f" Attempted to load weight ({loaded_weight.shape}) " f"into parameter ({param.shape})" | ||
| ) | ||
| # Ensure loaded weight dtype matches model param dtype | ||
| if loaded_weight.dtype != param.dtype: | ||
| if loaded_weight.dtype == paddle.int8 and param.dtype == paddle.float8_e4m3fn: | ||
| loaded_weight = loaded_weight.view(param.dtype) | ||
| else: | ||
| loaded_weight = loaded_weight.cast(param.dtype) | ||
| loaded_weight = may_be_do_cast(loaded_weight, param) | ||
| h2d_copy(param, loaded_weight) | ||
|
|
||
| def load_weight(self, state_dict: dict): | ||
|
|
@@ -1279,15 +1270,7 @@ def qkv_weight_loader(self, param, loaded_weight, loaded_shard_id): | |
| param.tensor_track.mark(start=param_shard_offset, end=param_shard_offset + param_shard_size) | ||
|
|
||
| param = slice_fn(param, output_dim, start=param_shard_offset, end=param_shard_offset + param_shard_size) | ||
| assert param.shape == loaded_weight.shape, ( | ||
| f" Attempted to load weight ({loaded_weight.shape}) " f"into parameter ({param.shape})" | ||
| ) | ||
| # Ensure loaded weight dtype matches model param dtype | ||
| if loaded_weight.dtype != param.dtype: | ||
| if loaded_weight.dtype == paddle.int8 and param.dtype == paddle.float8_e4m3fn: | ||
| loaded_weight = loaded_weight.view(param.dtype) | ||
| else: | ||
| loaded_weight = loaded_weight.cast(param.dtype) | ||
| loaded_weight = may_be_do_cast(loaded_weight, param) | ||
| h2d_copy(param, loaded_weight) | ||
|
|
||
| def gate_weight_loader(self, param, loaded_weight): | ||
|
|
@@ -1319,15 +1302,7 @@ def gate_weight_loader(self, param, loaded_weight): | |
| param.tensor_track.mark(start=param_shard_offset, end=param_shard_offset + param_shard_size) | ||
|
|
||
| param = slice_fn(param, output_dim, start=param_shard_offset, end=param_shard_offset + param_shard_size) | ||
| assert param.shape == loaded_weight.shape, ( | ||
| f"Attempted to load weight ({loaded_weight.shape}) " f"into parameter ({param.shape})" | ||
| ) | ||
| # Ensure loaded weight dtype matches model param dtype | ||
| if loaded_weight.dtype != param.dtype: | ||
| if loaded_weight.dtype == paddle.int8 and param.dtype == paddle.float8_e4m3fn: | ||
| loaded_weight = loaded_weight.view(param.dtype) | ||
| else: | ||
| loaded_weight = loaded_weight.cast(param.dtype) | ||
| loaded_weight = may_be_do_cast(loaded_weight, param) | ||
| h2d_copy(param, loaded_weight) | ||
|
|
||
| def load_weight(self, state_dict: dict): | ||
|
|
||
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.
🔴 兼容性 Breaking Change:原
else分支.cast()改为assert,破坏已有隐式 dtype 转换原代码:
允许隐式 dtype 转换(如
float16 → float32),兼容多种权重文件格式。新代码改为assert/raise后,除int8→float8_e4m3fn外的所有 dtype 不匹配均会在模型加载时直接报错,属 Breaking Change。需在 PR 描述中明确说明此语义变更的意图,并确认所有已支持模型/权重格式均不依赖隐式 dtype 转换,再合入。