-
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
add mock class for specific workflow #416
Merged
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
c7a461f
init dev
doombeaker 3571484
Merge branch 'main' of github.com:Oneflow-Inc/onediff into add_mock_c…
doombeaker 338d1f1
refine batchsize changing
doombeaker 4e10a51
refine
doombeaker dcb3f5d
Merge branch 'main' of github.com:Oneflow-Inc/onediff into add_mock_c…
doombeaker 06ee56e
refine, detect the community version
doombeaker 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
153 changes: 153 additions & 0 deletions
153
onediff_comfy_nodes/infer_compiler_registry/register_comfy/openaimodel.py
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,153 @@ | ||
import comfy | ||
import oneflow as th # 'th' is the way ComfyUI name the torch | ||
import oneflow.nn.functional as F | ||
from onediff.infer_compiler.transform import proxy_class | ||
from onediff.infer_compiler.transform import transform_mgr | ||
|
||
onediff_comfy = transform_mgr.transform_package("comfy") | ||
|
||
|
||
class Upsample(proxy_class(comfy.ldm.modules.diffusionmodules.openaimodel.Upsample)): | ||
# https://github.com/comfyanonymous/ComfyUI/blob/b0aab1e4ea3dfefe09c4f07de0e5237558097e22/comfy/ldm/modules/diffusionmodules/openaimodel.py#L82 | ||
def forward(self, x, output_shape=None): | ||
assert x.shape[1] == self.channels | ||
if output_shape is not None and isinstance(output_shape, th.Tensor): | ||
if self.dims == 3: | ||
raise ValueError("output_shape shoud not be Tensor for dims == 3") | ||
else: | ||
x = F.interpolate_like(x, like=output_shape, mode="nearest") | ||
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. 这个是 oneflow 新增的算子 |
||
else: | ||
if self.dims == 3: | ||
shape = [x.shape[2], x.shape[3] * 2, x.shape[4] * 2] | ||
if output_shape is not None: | ||
shape[1] = output_shape[3] | ||
shape[2] = output_shape[4] | ||
else: | ||
shape = [x.shape[2] * 2, x.shape[3] * 2] | ||
if output_shape is not None: | ||
shape[0] = output_shape[2] | ||
shape[1] = output_shape[3] | ||
|
||
x = F.interpolate(x, size=shape, mode="nearest") | ||
|
||
if self.use_conv: | ||
x = self.conv(x) | ||
return x | ||
|
||
|
||
class UNetModel(proxy_class(comfy.ldm.modules.diffusionmodules.openaimodel.UNetModel)): | ||
# https://github.com/comfyanonymous/ComfyUI/blob/b0aab1e4ea3dfefe09c4f07de0e5237558097e22/comfy/ldm/modules/diffusionmodules/openaimodel.py#L823 | ||
def forward( | ||
self, | ||
x, | ||
timesteps=None, | ||
context=None, | ||
y=None, | ||
control=None, | ||
transformer_options={}, | ||
**kwargs | ||
): | ||
timestep_embedding = ( | ||
onediff_comfy.ldm.modules.diffusionmodules.util.timestep_embedding | ||
) | ||
forward_timestep_embed = ( | ||
onediff_comfy.ldm.modules.diffusionmodules.openaimodel.forward_timestep_embed | ||
) | ||
apply_control = ( | ||
onediff_comfy.ldm.modules.diffusionmodules.openaimodel.apply_control | ||
) | ||
|
||
transformer_options["original_shape"] = list(x.shape) | ||
transformer_options["transformer_index"] = 0 | ||
transformer_patches = transformer_options.get("patches", {}) | ||
|
||
num_video_frames = kwargs.get("num_video_frames", self.default_num_video_frames) | ||
image_only_indicator = kwargs.get( | ||
"image_only_indicator", self.default_image_only_indicator | ||
) | ||
time_context = kwargs.get("time_context", None) | ||
|
||
assert (y is not None) == ( | ||
self.num_classes is not None | ||
), "must specify y if and only if the model is class-conditional" | ||
hs = [] | ||
t_emb = timestep_embedding( | ||
timesteps, self.model_channels, repeat_only=False | ||
).to(x.dtype) | ||
emb = self.time_embed(t_emb) | ||
|
||
if self.num_classes is not None: | ||
assert y.shape[0] == x.shape[0] | ||
emb = emb + self.label_emb(y) | ||
|
||
h = x | ||
for id, module in enumerate(self.input_blocks): | ||
transformer_options["block"] = ("input", id) | ||
h = forward_timestep_embed( | ||
module, | ||
h, | ||
emb, | ||
context, | ||
transformer_options, | ||
time_context=time_context, | ||
num_video_frames=num_video_frames, | ||
image_only_indicator=image_only_indicator, | ||
) | ||
h = apply_control(h, control, "input") | ||
if "input_block_patch" in transformer_patches: | ||
patch = transformer_patches["input_block_patch"] | ||
for p in patch: | ||
h = p(h, transformer_options) | ||
|
||
hs.append(h) | ||
if "input_block_patch_after_skip" in transformer_patches: | ||
patch = transformer_patches["input_block_patch_after_skip"] | ||
for p in patch: | ||
h = p(h, transformer_options) | ||
|
||
transformer_options["block"] = ("middle", 0) | ||
h = forward_timestep_embed( | ||
self.middle_block, | ||
h, | ||
emb, | ||
context, | ||
transformer_options, | ||
time_context=time_context, | ||
num_video_frames=num_video_frames, | ||
image_only_indicator=image_only_indicator, | ||
) | ||
h = apply_control(h, control, "middle") | ||
|
||
for id, module in enumerate(self.output_blocks): | ||
transformer_options["block"] = ("output", id) | ||
hsp = hs.pop() | ||
hsp = apply_control(hsp, control, "output") | ||
|
||
if "output_block_patch" in transformer_patches: | ||
patch = transformer_patches["output_block_patch"] | ||
for p in patch: | ||
h, hsp = p(h, hsp, transformer_options) | ||
|
||
h = th.cat([h, hsp], dim=1) | ||
del hsp | ||
if len(hs) > 0: | ||
# output_shape = hs[-1].shape | ||
output_shape = hs[-1] | ||
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. 此处修改为 tensor,目的是传给 oneflow 的参数是 tensor,这样每次推导时能拿到 tensor 的 meta 信息。之前的 shape 作为属性,无法每次推导时更新。 |
||
else: | ||
output_shape = None | ||
h = forward_timestep_embed( | ||
module, | ||
h, | ||
emb, | ||
context, | ||
transformer_options, | ||
output_shape, | ||
time_context=time_context, | ||
num_video_frames=num_video_frames, | ||
image_only_indicator=image_only_indicator, | ||
) | ||
h = h.type(x.dtype) | ||
if self.predict_codebook_ids: | ||
return self.id_predictor(h) | ||
else: | ||
return self.out(h) |
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.
最原始的 ComfyUI 写法,以及上一版的 onediff 写法都保留,是因为这是一个修正动态 shape 问题的例子。
原始需求:
第一版 onediff 的写法:
此时 reshape 中的
b
,c
如果不变,只有一个维度变化(-1
那个维度),那么reshape
可以动态推导出-1
那个维度的具体值。但是,如果
-1
维度变化的同时,b
也变化,reshape 就无法正常推导了。所以要用最新的版本:
先把
(b, c, h, w)
中的后 2 个维度压到一起(b, c, (h*w))
,然后调整维度顺序,得到(b, (h,w), c
,达到与之前等价的效果。