Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions modules/devices.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,11 @@ def forward_wrapper(self, *args, **kwargs):

@contextlib.contextmanager
def manual_cast(target_dtype):
applied = False
for module_type in patch_module_list:
if hasattr(module_type, "org_forward"):
continue
applied = True
org_forward = module_type.forward
if module_type == torch.nn.MultiheadAttention and has_xpu():
module_type.forward = manual_cast_forward(torch.float32)
Expand All @@ -174,8 +178,11 @@ def manual_cast(target_dtype):
try:
yield None
finally:
for module_type in patch_module_list:
module_type.forward = module_type.org_forward
if applied:
for module_type in patch_module_list:
if hasattr(module_type, "org_forward"):
module_type.forward = module_type.org_forward
delattr(module_type, "org_forward")


def autocast(disable=False):
Expand Down