-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from okoge-kaz/feature/phi-3
Fix minor bugs
- Loading branch information
Showing
5 changed files
with
36 additions
and
10 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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,23 @@ | ||
import torch | ||
from contextlib import contextmanager | ||
|
||
|
||
@contextmanager | ||
def preserve_fp32_buffers(model: torch.nn.Module): | ||
fp32_buffers = dict() | ||
for name, param in model.named_buffers(): | ||
if param.dtype == torch.float32: | ||
fp32_buffers[name] = param.clone() | ||
|
||
# model.to(torch.float16) or model.to(torch.bfloat16) | ||
yield | ||
|
||
for name, param in model.named_buffers(): | ||
if name in fp32_buffers: | ||
if "." in name: | ||
module_name, buffer_name = name.rsplit(".", 1) | ||
target_module = model.get_submodule(module_name) | ||
else: | ||
buffer_name = name | ||
target_module = model | ||
setattr(target_module, buffer_name, fp32_buffers[name]) |