Skip to content
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

Refactoring Diffusers pipelines #711

Merged
merged 18 commits into from
Oct 9, 2024
6 changes: 0 additions & 6 deletions optimum/exporters/neuron/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -598,12 +598,6 @@ def main_export(

# Validate compiled model
if do_validation is True:
if library_name == "diffusers":
# Do not validate vae encoder due to the sampling randomness
neuron_outputs.pop("vae_encoder")
models_and_neuron_configs.pop("vae_encoder", None)
output_model_names.pop("vae_encoder", None)

try:
validate_models_outputs(
models_and_neuron_configs=models_and_neuron_configs,
Expand Down
2 changes: 1 addition & 1 deletion optimum/exporters/neuron/model_configs.py
Original file line number Diff line number Diff line change
Expand Up @@ -724,7 +724,7 @@ def inputs(self) -> List[str]:

@property
def outputs(self) -> List[str]:
return ["latent_sample"]
return ["latent_parameters"]

def generate_dummy_inputs(self, return_tuple: bool = False, **kwargs):
dummy_inputs = super().generate_dummy_inputs(**kwargs)
Expand Down
2 changes: 1 addition & 1 deletion optimum/exporters/neuron/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ def get_submodels_for_export_stable_diffusion(

# VAE Encoder
vae_encoder = copy.deepcopy(pipeline.vae)
vae_encoder.forward = lambda sample: {"latent_sample": vae_encoder.encode(x=sample)["latent_dist"].sample()}
vae_encoder.forward = lambda sample: {"latent_parameters": vae_encoder.encode(x=sample)["latent_dist"].parameters}
models_for_export.append((DIFFUSION_MODEL_VAE_ENCODER_NAME, vae_encoder))

# VAE Decoder
Expand Down
784 changes: 466 additions & 318 deletions optimum/neuron/modeling_diffusion.py

Large diffs are not rendered by default.

20 changes: 2 additions & 18 deletions optimum/neuron/pipelines/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,35 +21,19 @@
_import_structure = {
"transformers": ["pipeline"],
"diffusers": [
"NeuronStableDiffusionPipelineMixin",
"NeuronStableDiffusionImg2ImgPipelineMixin",
"NeuronStableDiffusionInpaintPipelineMixin",
"NeuronStableDiffusionInstructPix2PixPipelineMixin",
"NeuronLatentConsistencyPipelineMixin",
"NeuronStableDiffusionControlNetPipelineMixin",
"NeuronStableDiffusionXLPipelineMixin",
"NeuronStableDiffusionXLImg2ImgPipelineMixin",
"NeuronStableDiffusionXLInpaintPipelineMixin",
"NeuronStableDiffusionControlNetPipelineMixin",
"NeuronStableDiffusionXLControlNetPipelineMixin",
],
}

if TYPE_CHECKING:
from .diffusers import (
NeuronLatentConsistencyPipelineMixin,
NeuronStableDiffusionControlNetPipelineMixin,
NeuronStableDiffusionImg2ImgPipelineMixin,
NeuronStableDiffusionInpaintPipelineMixin,
NeuronStableDiffusionInstructPix2PixPipelineMixin,
NeuronStableDiffusionPipelineMixin,
NeuronStableDiffusionXLControlNetPipelineMixin,
NeuronStableDiffusionXLImg2ImgPipelineMixin,
NeuronStableDiffusionXLInpaintPipelineMixin,
NeuronStableDiffusionXLPipelineMixin,
)
from .transformers import (
pipeline,
)
from .transformers import pipeline
else:
import sys

Expand Down
10 changes: 1 addition & 9 deletions optimum/neuron/pipelines/diffusers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,6 @@
# 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 .pipeline_controlnet import NeuronStableDiffusionControlNetPipelineMixin
from .pipeline_controlnet_sd_xl import NeuronStableDiffusionXLControlNetPipelineMixin
from .pipeline_latent_consistency_text2img import NeuronLatentConsistencyPipelineMixin
from .pipeline_stable_diffusion import NeuronStableDiffusionPipelineMixin
from .pipeline_stable_diffusion_img2img import NeuronStableDiffusionImg2ImgPipelineMixin
from .pipeline_stable_diffusion_inpaint import NeuronStableDiffusionInpaintPipelineMixin
from .pipeline_stable_diffusion_instruct_pix2pix import NeuronStableDiffusionInstructPix2PixPipelineMixin
from .pipeline_stable_diffusion_xl import NeuronStableDiffusionXLPipelineMixin
from .pipeline_stable_diffusion_xl_img2img import NeuronStableDiffusionXLImg2ImgPipelineMixin
from .pipeline_stable_diffusion_xl_inpaint import NeuronStableDiffusionXLInpaintPipelineMixin
from .pipeline_utils import NeuronStableDiffusionXLPipelineMixin
197 changes: 30 additions & 167 deletions optimum/neuron/pipelines/diffusers/pipeline_controlnet.py

Large diffs are not rendered by default.

231 changes: 35 additions & 196 deletions optimum/neuron/pipelines/diffusers/pipeline_controlnet_sd_xl.py

Large diffs are not rendered by default.

This file was deleted.

Loading
Loading