-
Notifications
You must be signed in to change notification settings - Fork 95
issue/890 - 为nn.module添加to函数 #891
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?
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 |
|---|---|---|
|
|
@@ -32,6 +32,7 @@ | |
|
|
||
| import infinicore | ||
|
|
||
| from ...device import device as InfiniCoreDevice | ||
| from ...tensor import Tensor | ||
| from ..parameter import InfiniCoreParameter as Parameter | ||
|
|
||
|
|
@@ -481,15 +482,14 @@ def _load_from_state_dict( | |
| f"While copying the parameter named {key}, expected Tensor from checkpoint but received {type(input_param)}" | ||
| ) | ||
|
|
||
| if ( | ||
| (param.shape == input_param.shape) | ||
| and (param.dtype == input_param.dtype) | ||
| and (param.device == input_param.device) | ||
| if (param.shape == input_param.shape) and ( | ||
| param.dtype == input_param.dtype | ||
| ): | ||
| param.copy_(input_param) | ||
| else: | ||
| print(f"param '{name}' don't match input_param '{key}'") | ||
| setattr(self, name, input_param) | ||
| raise KeyError( | ||
| f"param '{name}' don't match input_param '{key}' with shape or dtype" | ||
| ) | ||
|
|
||
| elif strict: | ||
| missing_keys.append(key) | ||
|
|
@@ -848,10 +848,29 @@ def eval(self: T) -> T: | |
| Returns: | ||
| Module: self | ||
| """ | ||
| pass | ||
| raise KeyError("not support") | ||
|
|
||
| def _apply(self, fn, recurse=True): | ||
|
Collaborator
Author
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. to函数的部分参考了torch的写法 |
||
| raise KeyError("not support") | ||
| if recurse: | ||
| for module in self.children(): | ||
| module._apply(fn) | ||
|
|
||
| def to(self, *args, **kwargs): | ||
| raise KeyError("not support") | ||
| for key, param in self._parameters.items(): | ||
| if param is not None: | ||
| setattr(self, key, fn(param)) | ||
|
Collaborator
Author
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. 使用=符号,赋值不成功,不知为何。
Collaborator
Author
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. 最后用了setattr(self, key, fn(param)) |
||
|
|
||
| for key, buf in self._buffers.items(): | ||
| if buf is not None: | ||
| setattr(self, key, fn(buf)) | ||
|
|
||
| return self | ||
|
|
||
| def to(self, device: str | InfiniCoreDevice): | ||
| if device is None: | ||
| raise ValueError("device cannot be None") | ||
| device = InfiniCoreDevice(device) | ||
|
|
||
| def convert(t): | ||
| return t.to(device) | ||
|
|
||
| return self._apply(convert) | ||
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.
因为,infinicore的两个tensor之间的copy操作,是支持从 cpu直接拷贝到gpu的。
现在的权重加载判断是: 模型weight和权重文件,二者shape和dtype同一样时,可以拷贝数据,否则报错。