Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
55 commits
Select commit Hold shift + click to select a range
70321a4
Stage-1 export debug
quic-amitraj Jul 10, 2025
15d34bb
Stage-2 Export inital working version done
quic-amitraj Jul 16, 2025
b9fba19
Stage-3 compilation work is under progress
quic-amitraj Jul 17, 2025
966352c
Stage-4 Working pipeline with wrong output
quic-amitraj Jul 23, 2025
2405b7b
Testing
quic-amitraj Jul 30, 2025
2567b11
Testing
quic-amitraj Jul 30, 2025
b9e8400
Working sd3-turbo
quic-amitraj Aug 3, 2025
49478f6
Working with cleaned code
quic-amitraj Aug 5, 2025
cf4c274
Working with cleaned code
quic-amitraj Aug 5, 2025
ccefb16
Working with vae_included
quic-amitraj Aug 6, 2025
6ce1a91
Fix-1
quic-amitraj Aug 8, 2025
fcb6b11
Fix-2
quic-amitraj Aug 10, 2025
dea04f5
Fix-3
quic-amitraj Aug 13, 2025
4b4263d
Added readme for diffusers
quic-amitraj Aug 14, 2025
401c95d
Code cleanup
quic-amitraj Aug 14, 2025
bc3ae68
Code cleanup-2
quic-amitraj Aug 15, 2025
51d8161
Minor fix
quic-amitraj Aug 20, 2025
701a8f4
Added Support of flux
quic-amitraj Sep 19, 2025
e63e2a4
Updated seq_len of flux transformers
tv-karthikeya Sep 24, 2025
1a2c3c3
Removing SD3, adding small fix for flux model hash
tv-karthikeya Oct 9, 2025
ce1142a
adding device id support for flux for all stages
tv-karthikeya Oct 9, 2025
0648374
[WIP] Adding support for custom Height,width
tv-karthikeya Nov 3, 2025
84dc2d8
Flux support with Custom config
quic-amitraj Nov 4, 2025
5a9cae9
Added OnnxfunctionTransform and code cleanup while modifying compile …
quic-amitraj Nov 4, 2025
359ab95
Compile fix
quic-amitraj Nov 5, 2025
3e5baf4
Modification of Pipeline-1
Nov 6, 2025
d0897cd
Modification of Pipeline-2
Nov 7, 2025
4c5b209
Update readme for diffusers
Nov 10, 2025
77a37b7
Added support of output dataclass
Nov 11, 2025
36014cc
Replaced output dict with dataclass to make it more user friendly
Nov 11, 2025
b132412
Rebased with main and fixed some issues
Nov 12, 2025
5664e85
Code cleaning and removed redundant code
Nov 13, 2025
ce990e3
Code cleaning and removed redundant code-2
Nov 13, 2025
0f98242
Added tqdm for export and compile
Nov 13, 2025
c34a2be
Parallel compilation and onnx subfunction is added
Nov 14, 2025
77d4461
Height and widht now can be passed from compile and __call__ method a…
Nov 24, 2025
f5a2fd9
Removed redundant code
Nov 24, 2025
5847136
Minor fixes-1
Nov 24, 2025
686d671
addressed comments and fixes
Nov 26, 2025
50fa2db
Handled 1. Mutiple time export issue 2. Meta device error after first…
Nov 26, 2025
ab95e55
Code cleanup and fixes
Nov 27, 2025
6c2f172
Updated readme
Nov 27, 2025
5d5b172
Updated the class names
Dec 1, 2025
00e66ca
Addressed comments-2
Dec 2, 2025
6291c41
Addressed comments-3
Dec 2, 2025
5ea7766
Addressed comments-4
Dec 4, 2025
fbecc32
Working T5
Dec 5, 2025
de0a079
Minor fixes
Dec 7, 2025
41201b1
Comments addressed
Dec 8, 2025
26e8368
Adding pytest for flux (#18)
tv-karthikeya Dec 8, 2025
e822679
Comments addressed-2
Dec 8, 2025
4ae608b
Hash fix
Dec 8, 2025
0b28841
Hash fix-2 for CI
Dec 9, 2025
f011ded
Hash fix-3 for CI
Dec 9, 2025
119245a
Increasaed stage time
Dec 9, 2025
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
2 changes: 2 additions & 0 deletions QEfficient/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
QEFFCommonLoader,
)
from QEfficient.compile.compile_helper import compile
from QEfficient.diffusers.pipelines.flux.pipeline_flux import QEffFluxPipeline
from QEfficient.exporter.export_hf_to_cloud_ai_100 import qualcomm_efficient_converter
from QEfficient.generation.text_generation_inference import cloud_ai_100_exec_kv
from QEfficient.peft import QEffAutoPeftModelForCausalLM
Expand All @@ -39,6 +40,7 @@
"QEFFAutoModelForImageTextToText",
"QEFFAutoModelForSpeechSeq2Seq",
"QEFFCommonLoader",
"QEffFluxPipeline",
]
# For faster downloads via hf_transfer
# This code is put above import statements as this needs to be executed before
Expand Down
55 changes: 29 additions & 26 deletions QEfficient/base/modeling_qeff.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import gc
import inspect
import logging
import re
import shutil
import subprocess
import warnings
Expand All @@ -21,26 +20,21 @@

from QEfficient.base.onnx_transforms import (
BaseOnnxTransform,
CustomOpTransform,
OnnxTransformPipeline,
RenameFunctionOutputsTransform,
)
from QEfficient.base.pytorch_transforms import PytorchTransform
from QEfficient.compile.qnn_compiler import compile as qnn_compile
from QEfficient.generation.cloud_infer import QAICInferenceSession
from QEfficient.transformers.cache_utils import InvalidIndexProvider
from QEfficient.transformers.models.pytorch_transforms import get_decoder_layer_classes_for_export
from QEfficient.utils import (
constants,
create_json,
create_model_params,
dump_qconfig,
export_wrapper,
generate_mdp_partition_config,
hash_dict_params,
load_json,
)
from QEfficient.utils.torch_patches import apply_torch_patches, undo_torch_patches
from QEfficient.utils.export_utils import export_wrapper

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -125,9 +119,35 @@ def _model_offloaded_check(self) -> None:
logger.error(error_msg)
raise RuntimeError(error_msg)

@property
def model_name(self) -> str:
"""
Get the model class name without QEff/QEFF prefix.

This property extracts the underlying model's class name and removes
any QEff or QEFF prefix that may have been added during wrapping.

Returns:
str: Model class name (e.g., "CLIPTextModel" instead of "QEffCLIPTextModel")
"""
mname = self.model.__class__.__name__
if mname.startswith("QEff") or mname.startswith("QEFF"):
mname = mname[4:]
return mname

@property
@abstractmethod
def model_name(self) -> str: ...
def get_model_config(self) -> Dict:
"""
Get the model configuration as a dictionary.

This is an abstract property that must be implemented by all subclasses.
Typically returns: self.model.config.__dict__

Returns:
Dict: The configuration dictionary of the underlying model
"""
pass

@abstractmethod
def export(self, export_dir: Optional[str] = None) -> Path:
Expand Down Expand Up @@ -188,7 +208,6 @@ def _export(
onnx_transform_kwargs: Optional[Dict[str, any]] = None,
export_dir: Optional[str] = None,
offload_pt_weights: bool = True,
use_onnx_subfunctions: bool = False,
) -> str:
"""
Export the PyTorch model to ONNX and apply ONNX transforms
Expand Down Expand Up @@ -253,18 +272,8 @@ def _export(
input_names.append(param)

try:
# Initialize the registry with your custom ops
# Export to ONNX
export_kwargs = {} if export_kwargs is None else export_kwargs
if use_onnx_subfunctions:
warnings.warn(
"The subfunction feature is experimental. Please note that using compile consecutively with and without subfunction may produce inconsistent results."
)
apply_torch_patches()
InvalidIndexProvider.SUBFUNC_ENABLED = True
output_names = [re.sub("_RetainedState", "_InternalRetainedState", s) for s in output_names]
export_kwargs["export_modules_as_functions"] = get_decoder_layer_classes_for_export(self.model)
self._onnx_transforms.append(RenameFunctionOutputsTransform)
self._onnx_transforms.append(CustomOpTransform)

torch.onnx.export(
self.model,
Expand Down Expand Up @@ -309,12 +318,6 @@ def _export(
finally:
shutil.rmtree(tmp_onnx_dir, ignore_errors=True)

if use_onnx_subfunctions:
undo_torch_patches()
InvalidIndexProvider.SUBFUNC_ENABLED = False
self._onnx_transforms.remove(CustomOpTransform)
self._onnx_transforms.remove(RenameFunctionOutputsTransform)

self.onnx_path = onnx_path
return onnx_path

Expand Down
95 changes: 95 additions & 0 deletions QEfficient/diffusers/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@

<div align="center">


# **Diffusion Models on Qualcomm Cloud AI 100**


<div align="center">

### 🎨 **Experience the Future of AI Image Generation**

* Optimized for Qualcomm Cloud AI 100*

<img src="../../docs/image/girl_laughing.png" alt="Sample Output" width="400">

**Generated with**: `black-forest-labs/FLUX.1-schnell` • `"A girl laughing"` • 4 steps • 0.0 guidance scale • ⚡



</div>



[![Diffusers](https://img.shields.io/badge/Diffusers-0.35.1-orange.svg)](https://github.com/huggingface/diffusers)
</div>

---

## ✨ Overview

QEfficient Diffusers brings the power of state-of-the-art diffusion models to Qualcomm Cloud AI 100 hardware for text-to-image generation. Built on top of the popular HuggingFace Diffusers library, our optimized pipeline provides seamless inference on Qualcomm Cloud AI 100 hardware.

## 🛠️ Installation

### Prerequisites

Ensure you have Python 3.8+ and the required dependencies:

```bash
# Create Python virtual environment (Recommended Python 3.10)
sudo apt install python3.10-venv
python3.10 -m venv qeff_env
source qeff_env/bin/activate
pip install -U pip
```

### Install QEfficient

```bash
# Install from GitHub (includes diffusers support)
pip install git+https://github.com/quic/efficient-transformers

# Or build from source
git clone https://github.com/quic/efficient-transformers.git
cd efficient-transformers
pip install build wheel
python -m build --wheel --outdir dist
pip install dist/qefficient-0.0.1.dev0-py3-none-any.whl
```

---

## 🎯 Supported Models
- ✅ [`black-forest-labs/FLUX.1-schnell`](https://huggingface.co/black-forest-labs/FLUX.1-schnell)

---


## 📚 Examples

Check out our comprehensive examples in the [`examples/diffusers/`](../../examples/diffusers/) directory:

---

## 🤝 Contributing

We welcome contributions! Please see our [Contributing Guide](../../CONTRIBUTING.md) for details.



---

## 🙏 Acknowledgments

- **HuggingFace Diffusers**: For the excellent foundation library
- **Stability AI**: For the amazing Stable Diffusion models
---

## 📞 Support

- 📖 **Documentation**: [https://quic.github.io/efficient-transformers/](https://quic.github.io/efficient-transformers/)
- 🐛 **Issues**: [GitHub Issues](https://github.com/quic/efficient-transformers/issues)

---

6 changes: 6 additions & 0 deletions QEfficient/diffusers/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# -----------------------------------------------------------------------------
#
# Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries.
# SPDX-License-Identifier: BSD-3-Clause
#
# ----------------------------------------------------------------------------
6 changes: 6 additions & 0 deletions QEfficient/diffusers/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# -----------------------------------------------------------------------------
#
# Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries.
# SPDX-License-Identifier: BSD-3-Clause
#
# ----------------------------------------------------------------------------
40 changes: 40 additions & 0 deletions QEfficient/diffusers/models/normalization.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# -----------------------------------------------------------------------------
#
# Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries.
# SPDX-License-Identifier: BSD-3-Clause
#
# ----------------------------------------------------------------------------
from typing import Optional, Tuple

import torch
from diffusers.models.normalization import AdaLayerNormContinuous, AdaLayerNormZero, AdaLayerNormZeroSingle


class QEffAdaLayerNormZero(AdaLayerNormZero):
def forward(
self,
x: torch.Tensor,
shift_msa: Optional[torch.Tensor] = None,
scale_msa: Optional[torch.Tensor] = None,
) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor, torch.Tensor, torch.Tensor]:
x = self.norm(x) * (1 + scale_msa[:, None]) + shift_msa[:, None]
return x


class QEffAdaLayerNormZeroSingle(AdaLayerNormZeroSingle):
def forward(
self,
x: torch.Tensor,
scale_msa: Optional[torch.Tensor] = None,
shift_msa: Optional[torch.Tensor] = None,
) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor, torch.Tensor, torch.Tensor]:
x = self.norm(x) * (1 + scale_msa[:, None]) + shift_msa[:, None]
return x


class QEffAdaLayerNormContinuous(AdaLayerNormContinuous):
def forward(self, x: torch.Tensor, conditioning_embedding: torch.Tensor) -> torch.Tensor:
emb = conditioning_embedding
scale, shift = torch.chunk(emb, 2, dim=1)
x = self.norm(x) * (1 + scale)[:, None, :] + shift[:, None, :]
return x
56 changes: 56 additions & 0 deletions QEfficient/diffusers/models/pytorch_transforms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# -----------------------------------------------------------------------------
#
# Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries.
# SPDX-License-Identifier: BSD-3-Clause
#
# -----------------------------------------------------------------------------

from diffusers.models.normalization import AdaLayerNormContinuous, AdaLayerNormZero, AdaLayerNormZeroSingle, RMSNorm
from diffusers.models.transformers.transformer_flux import (
FluxAttention,
FluxAttnProcessor,
FluxSingleTransformerBlock,
FluxTransformer2DModel,
FluxTransformerBlock,
)
from torch import nn

from QEfficient.base.pytorch_transforms import ModuleMappingTransform
from QEfficient.customop.rms_norm import CustomRMSNormAIC
from QEfficient.diffusers.models.normalization import (
QEffAdaLayerNormContinuous,
QEffAdaLayerNormZero,
QEffAdaLayerNormZeroSingle,
)
from QEfficient.diffusers.models.transformers.transformer_flux import (
QEffFluxAttention,
QEffFluxAttnProcessor,
QEffFluxSingleTransformerBlock,
QEffFluxTransformer2DModel,
QEffFluxTransformerBlock,
)


class CustomOpsTransform(ModuleMappingTransform):
_module_mapping = {
RMSNorm: CustomRMSNormAIC,
nn.RMSNorm: CustomRMSNormAIC, # for torch.nn.RMSNorm
}


class AttentionTransform(ModuleMappingTransform):
_module_mapping = {
FluxSingleTransformerBlock: QEffFluxSingleTransformerBlock,
FluxTransformerBlock: QEffFluxTransformerBlock,
FluxTransformer2DModel: QEffFluxTransformer2DModel,
FluxAttention: QEffFluxAttention,
FluxAttnProcessor: QEffFluxAttnProcessor,
}


class NormalizationTransform(ModuleMappingTransform):
_module_mapping = {
AdaLayerNormZero: QEffAdaLayerNormZero,
AdaLayerNormZeroSingle: QEffAdaLayerNormZeroSingle,
AdaLayerNormContinuous: QEffAdaLayerNormContinuous,
}
6 changes: 6 additions & 0 deletions QEfficient/diffusers/models/transformers/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# -----------------------------------------------------------------------------
#
# Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries.
# SPDX-License-Identifier: BSD-3-Clause
#
# ----------------------------------------------------------------------------
Loading
Loading