Skip to content

Commit fc1f2a2

Browse files
Merge branch 'master' into remove/OVClassModelTestP
2 parents 656ffb0 + 4272f47 commit fc1f2a2

File tree

6 files changed

+49
-24
lines changed

6 files changed

+49
-24
lines changed

.github/workflows/token_merging.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ jobs:
2323
strategy:
2424
fail-fast: false
2525
matrix:
26-
python-version: [3.8]
26+
python-version: [3.11]
2727

2828
runs-on: ubuntu-latest
2929
steps:

modules/token_merging/setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
author="Alexander Kozlov",
1414
url="https://github.com/openvinotoolkit/openvino_contrib/tree/master/modules/token_merging",
1515
description="Token Merging for OpenVINO",
16-
install_requires=["torch~=1.13.1", "torchvision~=0.14.1"],
16+
install_requires=["torch~=2.4", "torchvision~=0.19.1"],
1717
dependency_links=["https://download.pytorch.org/whl/cpu"],
1818
extras_require=EXTRAS_REQUIRE,
1919
packages=find_packages(exclude=("examples", "build")),

modules/token_merging/tests/test_precommit.py

+10-18
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@
99
from PIL import Image
1010
import torch
1111
import openvino.runtime as ov
12+
from openvino import convert_model
1213

1314
import tomeov
1415
from diffusers import StableDiffusionPipeline, DDPMScheduler
1516
from optimum.intel.openvino import OVStableDiffusionPipeline
17+
from optimum.exporters.openvino import export_from_model
1618
import open_clip
1719
import timm
1820

@@ -33,7 +35,7 @@ def test_stable_diffusion(self):
3335
tomeov.patch_stable_diffusion(loaded_pipeline, ratio=0.3)
3436

3537
with tempfile.TemporaryDirectory() as tmpdirname:
36-
tomeov.export_diffusion_pipeline(loaded_pipeline, tmpdirname)
38+
export_from_model(loaded_pipeline, tmpdirname)
3739
ov_pipe = OVStableDiffusionPipeline.from_pretrained(tmpdirname, compile=False)
3840
ov_pipe.reshape(batch_size=1, height=height, width=width, num_images_per_prompt=1)
3941
ov_pipe.compile()
@@ -42,26 +44,16 @@ def test_stable_diffusion(self):
4244
def test_openclip(self):
4345
model, _, transform = open_clip.create_model_and_transforms(self.OPENCLIP_MODEL[0], pretrained=self.OPENCLIP_MODEL[1])
4446
tomeov.patch_openclip(model, 8)
45-
dummy_image = np.random.rand(100, 100, 3) * 255
47+
dummy_image = np.random.rand(224, 224, 3) * 255
4648
dummy_image = Image.fromarray(dummy_image.astype("uint8"))
4749
dummy_image = transform(dummy_image).unsqueeze(0)
4850

49-
with tempfile.TemporaryDirectory(suffix = ".onnx") as tmpdirname:
50-
model_file = os.path.join(tmpdirname, "image_encoder.onnx")
51-
torch.onnx.export(
52-
model.visual,
53-
dummy_image,
54-
model_file,
55-
opset_version=14,
56-
input_names=["image"],
57-
output_names=["image_embedding"],
58-
dynamic_axes={
59-
"image": {0: "batch"},
60-
"image_embedding": {0: "batch"},
61-
}
62-
)
63-
compiled_model = ov.compile_model(model_file)
64-
self.assertTrue(compiled_model)
51+
ov_model = convert_model(
52+
model.visual,
53+
example_input=dummy_image
54+
)
55+
compiled_model = ov.compile_model(ov_model)
56+
self.assertTrue(compiled_model)
6557

6658
def test_timm(self):
6759
model = timm.create_model(self.TIMM_MODEL, pretrained=False)

modules/token_merging/tomeov/openclip.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ def forward(self, x: torch.Tensor) -> torch.Tensor:
119119
self._tome_info["source"] = None
120120

121121
# to patches - whether to use dual patchnorm - https://arxiv.org/abs/2302.01327v1
122-
if self.input_patchnorm:
122+
if hasattr(self, "input_patchnorm") and self.input_patchnorm:
123123
# einops - rearrange(x, 'b c (h p1) (w p2) -> b (h w) (c p1 p2)')
124124
x = x.reshape(x.shape[0], x.shape[1], self.grid_size[0], self.patch_size[0], self.grid_size[1], self.patch_size[1])
125125
x = x.permute(0, 2, 4, 1, 3, 5)

modules/token_merging/tomeov/stable_diffusion.py

+33-1
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,9 @@ def patch_stable_diffusion(
177177
use_rand: bool = True,
178178
merge_attn: bool = True,
179179
merge_crossattn: bool = False,
180-
merge_mlp: bool = False):
180+
merge_mlp: bool = False,
181+
optimize_image_encoder: bool = True,
182+
):
181183
"""
182184
Patches a stable diffusion model with ToMe.
183185
Apply this to the highest level stable diffusion object (i.e., it should have a .model.diffusion_model).
@@ -242,6 +244,36 @@ def patch_stable_diffusion(
242244
if not hasattr(module, "disable_self_attn") and not is_diffusers:
243245
module.disable_self_attn = False
244246

247+
if optimize_image_encoder and hasattr(model, "vae_encoder"):
248+
image_encoder = model.vae_encoder
249+
250+
image_encoder._tome_info = {
251+
"size": None,
252+
"hooks": [],
253+
"args": {
254+
"ratio": ratio,
255+
"max_downsample": max_downsample,
256+
"sx": sx, "sy": sy,
257+
"use_rand": use_rand,
258+
"generator": None,
259+
"merge_attn": merge_attn,
260+
"merge_crossattn": merge_crossattn,
261+
"merge_mlp": merge_mlp
262+
}
263+
}
264+
hook_tome_model(image_encoder)
265+
266+
for _, module in image_encoder.named_modules():
267+
# If for some reason this has a different name, create an issue and I'll fix it
268+
if isinstance_str(module, "BasicTransformerBlock"):
269+
make_tome_block_fn = make_diffusers_tome_block if is_diffusers else make_tome_block
270+
module.__class__ = make_tome_block_fn(module.__class__)
271+
module._tome_info = image_encoder._tome_info
272+
273+
# Something introduced in SD 2.0 (LDM only)
274+
if not hasattr(module, "disable_self_attn") and not is_diffusers:
275+
module.disable_self_attn = False
276+
245277
return model
246278

247279

modules/token_merging/tomeov/utils.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010

1111
from openvino._offline_transformations import apply_moc_transformations, compress_quantize_weights_transformation
1212

13-
from optimum.exporters.onnx import export_models, get_stable_diffusion_models_for_export
13+
from optimum.exporters.onnx import export_models
14+
from optimum.exporters.utils import get_diffusion_models_for_export
1415
from optimum.intel import OVStableDiffusionPipeline
1516
from optimum.utils import (
1617
DIFFUSION_MODEL_TEXT_ENCODER_SUBFOLDER,
@@ -91,7 +92,7 @@ def _export_to_onnx(pipeline, save_dir):
9192
]
9293

9394
with torch.no_grad():
94-
models_and_onnx_configs = get_stable_diffusion_models_for_export(pipeline)
95+
models_and_onnx_configs = get_diffusion_models_for_export(pipeline)
9596
pipeline.save_config(save_dir)
9697
export_models(
9798
models_and_onnx_configs=models_and_onnx_configs, output_dir=Path(save_dir), output_names=output_names

0 commit comments

Comments
 (0)