-
Notifications
You must be signed in to change notification settings - Fork 110
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
Supporting obj is not an object. #621
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 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 |
---|---|---|
@@ -1,4 +1,5 @@ | ||
"""Convert torch object to oneflow object.""" | ||
|
||
import os | ||
import importlib | ||
import types | ||
|
@@ -105,7 +106,10 @@ def __getattribute__(self, attribute): | |
): | ||
return flow.Generator() | ||
elif ( | ||
isinstance(self._oflow_proxy_submod, (torch.nn.Conv1d, torch.nn.Conv2d, torch.nn.Conv3d)) | ||
isinstance( | ||
self._oflow_proxy_submod, | ||
(torch.nn.Conv1d, torch.nn.Conv2d, torch.nn.Conv3d), | ||
) | ||
and attribute == "channel_pos" | ||
): | ||
return "channels_first" | ||
|
@@ -148,6 +152,13 @@ def torch2oflow(mod, *args, **kwargs): | |
return default_converter(mod, *args, **kwargs) | ||
|
||
|
||
@torch2oflow.register | ||
def _(mod: type): | ||
if not is_need_mock(mod): | ||
return mod | ||
return proxy_class(mod) | ||
|
||
|
||
def default_converter(obj, verbose=False, *, proxy_cls=None): | ||
# Higher versions of diffusers might use torch.nn.modules.Linear | ||
if obj is torch.nn.Linear: | ||
|
@@ -162,7 +173,7 @@ def init(self): | |
for k, _ in obj.__dict__.items(): | ||
attr = getattr(obj, k) | ||
self.__dict__[k] = torch2oflow(attr) | ||
|
||
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. 来自 black 格式化,black.version '24.1.1 |
||
of_obj_cls = type(str(new_obj_cls), (new_obj_cls,), {"__init__": init}) | ||
of_obj = of_obj_cls() | ||
|
||
|
@@ -174,11 +185,12 @@ def init(self): | |
# raise NotImplementedError(f"Unsupported type: {obj}") | ||
return obj | ||
|
||
|
||
@torch2oflow.register | ||
def _(mod: torch.nn.Module, verbose=False): | ||
proxy_md = ProxySubmodule(mod) | ||
new_md_cls = proxy_class(type(mod)) | ||
|
||
def init(self): | ||
nonlocal proxy_md | ||
|
||
|
@@ -223,7 +235,7 @@ def proxy_getattr(self, attr): | |
str(new_md_cls), (new_md_cls,), {"__init__": init, "__getattr__": proxy_getattr} | ||
) | ||
of_mod = of_mod_cls() | ||
|
||
if of_mod.training: | ||
of_mod.training = False | ||
if verbose: | ||
|
@@ -244,7 +256,7 @@ def proxy_getattr(self, attr): | |
def _(mod: torch.nn.BatchNorm1d, verbose=False): | ||
of_mod = torch2oflow.dispatch(torch.nn.Module)(mod, verbose) | ||
of_mod.channel_axis = 1 | ||
|
||
return of_mod | ||
|
||
|
||
|
@@ -254,7 +266,7 @@ def _(mod: torch.nn.BatchNorm2d, verbose=False): | |
if os.getenv("ONEFLOW_ENABLE_NHWC"): | ||
of_mod.channel_axis = 3 | ||
else: | ||
of_mod.channel_axis = 1 | ||
of_mod.channel_axis = 1 | ||
|
||
return of_mod | ||
|
||
|
@@ -263,72 +275,72 @@ def _(mod: torch.nn.BatchNorm2d, verbose=False): | |
def _(mod: torch.nn.BatchNorm3d, verbose=False): | ||
of_mod = torch2oflow.dispatch(torch.nn.Module)(mod, verbose) | ||
of_mod.channel_axis = 1 | ||
|
||
return of_mod | ||
|
||
|
||
@torch2oflow.register | ||
def _(mod: torch.nn.MaxPool1d, verbose=False): | ||
of_mod = torch2oflow.dispatch(torch.nn.Module)(mod, verbose) | ||
of_mod.channel_pos = 'channels_first' | ||
of_mod.channel_pos = "channels_first" | ||
|
||
return of_mod | ||
|
||
|
||
@torch2oflow.register | ||
def _(mod: torch.nn.MaxPool2d, verbose=False): | ||
of_mod = torch2oflow.dispatch(torch.nn.Module)(mod, verbose) | ||
if os.getenv("ONEFLOW_ENABLE_NHWC"): | ||
of_mod.channel_pos = 'channels_last' | ||
of_mod.channel_pos = "channels_last" | ||
else: | ||
of_mod.channel_pos = 'channels_first' | ||
of_mod.channel_pos = "channels_first" | ||
|
||
return of_mod | ||
|
||
|
||
@torch2oflow.register | ||
def _(mod: torch.nn.MaxPool3d, verbose=False): | ||
of_mod = torch2oflow.dispatch(torch.nn.Module)(mod, verbose) | ||
of_mod.channel_pos = 'channels_first' | ||
of_mod.channel_pos = "channels_first" | ||
|
||
return of_mod | ||
|
||
|
||
@torch2oflow.register | ||
def _(mod: torch.nn.AvgPool1d, verbose=False): | ||
of_mod = torch2oflow.dispatch(torch.nn.Module)(mod, verbose) | ||
of_mod.channel_pos = 'channels_first' | ||
of_mod.channel_pos = "channels_first" | ||
|
||
return of_mod | ||
|
||
|
||
@torch2oflow.register | ||
def _(mod: torch.nn.AvgPool2d, verbose=False): | ||
of_mod = torch2oflow.dispatch(torch.nn.Module)(mod, verbose) | ||
if os.getenv("ONEFLOW_ENABLE_NHWC"): | ||
of_mod.channel_pos = 'channels_last' | ||
of_mod.channel_pos = "channels_last" | ||
else: | ||
of_mod.channel_pos = 'channels_first' | ||
of_mod.channel_pos = "channels_first" | ||
|
||
return of_mod | ||
|
||
|
||
@torch2oflow.register | ||
def _(mod: torch.nn.AvgPool3d, verbose=False): | ||
of_mod = torch2oflow.dispatch(torch.nn.Module)(mod, verbose) | ||
of_mod.channel_pos = 'channels_first' | ||
of_mod.channel_pos = "channels_first" | ||
|
||
return of_mod | ||
|
||
|
||
@torch2oflow.register | ||
def _(mod: torch.nn.AdaptiveAvgPool2d, verbose=False): | ||
of_mod = torch2oflow.dispatch(torch.nn.Module)(mod, verbose) | ||
if os.getenv("ONEFLOW_ENABLE_NHWC"): | ||
of_mod.channel_pos = 'channels_last' | ||
of_mod.channel_pos = "channels_last" | ||
else: | ||
of_mod.channel_pos = 'channels_first' | ||
of_mod.channel_pos = "channels_first" | ||
|
||
return of_mod | ||
|
||
|
||
|
@@ -350,7 +362,7 @@ def _(mod: torch.nn.Sequential, verbose=False): | |
of_mod_list.append(submod) | ||
|
||
of_mod_seq = proxy_class(type(mod))(*of_mod_list) | ||
|
||
return of_mod_seq | ||
|
||
|
||
|
@@ -419,6 +431,7 @@ def _(mod, verbose=False) -> Union[int, float, str, bool]: | |
def _(mod: None, verbose=False): | ||
return mod | ||
|
||
|
||
@torch2oflow.register | ||
def _(mod: types.BuiltinFunctionType, verbose=False): | ||
if hasattr(mod, "__module__"): | ||
|
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.
支持 obj 不是对象,就是一个 torch 的 class