-
Notifications
You must be signed in to change notification settings - Fork 684
Arm backend: Add initial module tests for Stable Diffusion 3.5 Medium #12242
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
Merged
Sebastian-Larsson
merged 3 commits into
pytorch:main
from
YufengShi-dudu:stable-diffusion-module-tests
Jul 29, 2025
Merged
Changes from all commits
Commits
Show all changes
3 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 hidden or 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 hidden or 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 hidden or 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,10 @@ | ||
#!/usr/bin/env bash | ||
YufengShi-dudu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# Copyright 2025 Arm Limited and/or its affiliates. | ||
# | ||
# This source code is licensed under the BSD-style license found in the | ||
# LICENSE file in the root directory of this source tree. | ||
|
||
set -e | ||
|
||
# Install diffusers for Stable Diffusion model test | ||
pip install "diffusers[torch]==0.33.1" |
114 changes: 114 additions & 0 deletions
114
backends/arm/test/models/stable_diffusion/stable_diffusion_module_test_configs.py
This file contains hidden or 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,114 @@ | ||
# Copyright 2025 Arm Limited and/or its affiliates. | ||
# | ||
# This source code is licensed under the BSD-style license found in the | ||
# LICENSE file in the root directory of this source tree. | ||
# | ||
# Adapted from Hugging Face's diffusers library: | ||
# https://github.com/huggingface/diffusers/blob/v0.33.1/tests/pipelines/stable_diffusion_3/test_pipeline_stable_diffusion_3.py | ||
# | ||
# Licensed under the Apache License, Version 2.0 | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
|
||
from transformers import CLIPTextConfig, T5Config | ||
|
||
|
||
""" | ||
This file defines test configs used to initialize Stable Diffusion module tests. | ||
Module tests in the same directory will import these configs. | ||
|
||
To stay aligned with the Stable Diffusion implementation in the HuggingFace Diffusers library, | ||
the configs here are either directly copied from corresponding test files or exported from | ||
pre-trained models used in the Diffusers library. | ||
|
||
Licenses: | ||
The test parameters are from Hugging Face's diffusers library and under the Apache 2.0 License, | ||
while the remainder of the code is under the BSD-style license found in the LICENSE file in the | ||
root directory of this source tree. | ||
""" | ||
|
||
|
||
# Source: https://github.com/huggingface/diffusers/blob/v0.33.1/tests/pipelines/stable_diffusion_3/test_pipeline_stable_diffusion_3.py#L56 | ||
CLIP_text_encoder_config = CLIPTextConfig( | ||
bos_token_id=0, | ||
eos_token_id=2, | ||
hidden_size=32, | ||
intermediate_size=37, | ||
layer_norm_eps=1e-05, | ||
num_attention_heads=4, | ||
num_hidden_layers=5, | ||
pad_token_id=1, | ||
vocab_size=1000, | ||
hidden_act="gelu", | ||
projection_dim=32, | ||
) | ||
|
||
|
||
# Source: https://github.com/huggingface/diffusers/blob/v0.33.1/tests/pipelines/stable_diffusion_3/test_pipeline_stable_diffusion_3.py#L76 | ||
# Exported from: T5EncoderModel.from_pretrained("hf-internal-testing/tiny-random-t5").config | ||
T5_encoder_config = T5Config( | ||
bos_token_id=0, | ||
classifier_dropout=0.0, | ||
d_ff=37, | ||
d_kv=8, | ||
d_model=32, | ||
decoder_start_token_id=0, | ||
dense_act_fn="relu", | ||
dropout_rate=0.1, | ||
eos_token_id=1, | ||
feed_forward_proj="relu", | ||
gradient_checkpointing=False, | ||
initializer_factor=0.002, | ||
is_encoder_decoder=True, | ||
is_gated_act=False, | ||
layer_norm_epsilon=1e-06, | ||
model_type="t5", | ||
num_decoder_layers=5, | ||
num_heads=4, | ||
num_layers=5, | ||
pad_token_id=0, | ||
relative_attention_max_distance=128, | ||
relative_attention_num_buckets=8, | ||
transformers_version="4.47.1", | ||
vocab_size=1000, | ||
) | ||
|
||
|
||
# Source: https://github.com/huggingface/diffusers/blob/v0.33.1/tests/models/transformers/test_models_transformer_sd3.py#L142 | ||
SD3Transformer2DModel_init_dict = { | ||
"sample_size": 32, | ||
"patch_size": 1, | ||
"in_channels": 4, | ||
"num_layers": 4, | ||
"attention_head_dim": 8, | ||
"num_attention_heads": 4, | ||
"caption_projection_dim": 32, | ||
"joint_attention_dim": 32, | ||
"pooled_projection_dim": 64, | ||
"out_channels": 4, | ||
"pos_embed_max_size": 96, | ||
"dual_attention_layers": (0,), | ||
"qk_norm": "rms_norm", | ||
} | ||
|
||
|
||
# Source: https://github.com/huggingface/diffusers/blob/v0.33.1/tests/pipelines/stable_diffusion_3/test_pipeline_stable_diffusion_3.py#L83 | ||
AutoencoderKL_config = { | ||
"sample_size": 32, | ||
"in_channels": 3, | ||
"out_channels": 3, | ||
"block_out_channels": (4,), | ||
"layers_per_block": 1, | ||
"latent_channels": 4, | ||
"norm_num_groups": 1, | ||
"use_quant_conv": False, | ||
"use_post_quant_conv": False, | ||
"shift_factor": 0.0609, | ||
"scaling_factor": 1.5035, | ||
} |
103 changes: 103 additions & 0 deletions
103
backends/arm/test/models/stable_diffusion/test_CLIPTextModelWithProjection.py
This file contains hidden or 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,103 @@ | ||
# Copyright 2025 Arm Limited and/or its affiliates. | ||
# | ||
# This source code is licensed under the BSD-style license found in the | ||
# LICENSE file in the root directory of this source tree. | ||
|
||
|
||
import unittest | ||
|
||
import torch | ||
from executorch.backends.arm._passes import InsertCastForOpsWithInt64InputPass | ||
|
||
from executorch.backends.arm.test import common | ||
from executorch.backends.arm.test.models.stable_diffusion.stable_diffusion_module_test_configs import ( | ||
CLIP_text_encoder_config, | ||
) | ||
from executorch.backends.arm.test.tester.arm_tester import ArmTester | ||
from transformers import CLIPTextModelWithProjection | ||
|
||
|
||
class TestCLIPTextModelWithProjection(unittest.TestCase): | ||
""" | ||
Test class of CLIPTextModelWithProjection. | ||
CLIPTextModelWithProjection is one of the text_encoder used by Stable Diffusion 3.5 Medium | ||
""" | ||
|
||
# Adjust nbr below as we increase op support. Note: most of the delegates | ||
# calls are directly consecutive to each other in the .pte. The reason | ||
# for that is some assert ops are removed by passes in the | ||
# .to_executorch step, i.e. after Arm partitioner. | ||
ops_after_partitioner = { | ||
"executorch_exir_dialects_edge__ops_aten__to_copy_default": 3, | ||
"executorch_exir_dialects_edge__ops_aten_argmax_default": 1, | ||
"executorch_exir_dialects_edge__ops_aten_index_Tensor": 1, | ||
"executorch_exir_dialects_edge__ops_aten_lt_Tensor": 1, | ||
"executorch_exir_dialects_edge__ops_aten_view_copy_default": 2, | ||
"executorch_exir_dialects_edge__ops_dim_order_ops__to_dim_order_copy_default": 1, | ||
"torch.ops.higher_order.executorch_call_delegate": 3, | ||
} | ||
|
||
def _prepare_inputs( | ||
self, | ||
batch_size=12, | ||
seq_length=7, | ||
vocab_size=1000, | ||
): | ||
input_ids = torch.randint( | ||
low=0, | ||
high=vocab_size, | ||
size=(batch_size, seq_length), | ||
dtype=torch.long, | ||
) | ||
return (input_ids,) | ||
|
||
def prepare_model_and_inputs(self): | ||
clip_text_encoder_config = CLIP_text_encoder_config | ||
|
||
text_encoder_model = CLIPTextModelWithProjection(clip_text_encoder_config) | ||
text_encoder_model.eval() | ||
text_encoder_model_inputs = self._prepare_inputs() | ||
|
||
return text_encoder_model, text_encoder_model_inputs | ||
|
||
def test_CLIPTextModelWithProjection_tosa_MI(self): | ||
text_encoder_model, text_encoder_model_inputs = self.prepare_model_and_inputs() | ||
with torch.no_grad(): | ||
( | ||
ArmTester( | ||
text_encoder_model, | ||
example_inputs=text_encoder_model_inputs, | ||
compile_spec=common.get_tosa_compile_spec(tosa_spec="TOSA-1.0+FP"), | ||
transform_passes=[InsertCastForOpsWithInt64InputPass()], | ||
) | ||
.export() | ||
.to_edge_transform_and_lower() | ||
.dump_operator_distribution() | ||
.check_count(self.ops_after_partitioner) | ||
.to_executorch() | ||
.run_method_and_compare_outputs( | ||
inputs=text_encoder_model_inputs, | ||
) | ||
) | ||
|
||
# MLETORCH-867, MLETORCH-1059 | ||
# Failures: "Fatal Python error: Aborted, Dependency cycles, KeyError in CastInt64BuffersToInt32Pass") | ||
@unittest.expectedFailure | ||
def test_CLIPTextModelWithProjection_tosa_BI(self): | ||
text_encoder_model, text_encoder_model_inputs = self.prepare_model_and_inputs() | ||
with torch.no_grad(): | ||
( | ||
ArmTester( | ||
text_encoder_model, | ||
example_inputs=text_encoder_model_inputs, | ||
compile_spec=common.get_tosa_compile_spec(tosa_spec="TOSA-1.0+INT"), | ||
) | ||
.quantize() | ||
.export() | ||
.to_edge_transform_and_lower() | ||
.dump_operator_distribution() | ||
.to_executorch() | ||
.run_method_and_compare_outputs( | ||
inputs=text_encoder_model_inputs, | ||
) | ||
) |
Oops, something went wrong.
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.
this is also the case with xnnpack and other backends, perhaps we should share this pass.
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.
This pass currently has some tosa-specific code/log info. Could we refactor it to exir in a follow up PR if needed by other backends?
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.
Issue created: #12957