code: def convert_vae_encoder(pipe, args):
      """Converts the VAE Encoder component of Stable Diffusion"""
      out_path = _get_out_path(args, "vae_encoder")
      if os.path.exists(out_path):
          logger.info(
              f"`vae_encoder` already exists at {out_path}, skipping conversion."
          )
          return
  
      if not hasattr(pipe, "unet"):
          raise RuntimeError(
              "convert_unet() deletes pipe.unet to save RAM. "
              "Please use convert_vae_encoder() before convert_unet()"
          )
  
      height = (args.latent_h or pipe.unet.config.sample_size) * 8
      width = (args.latent_w or pipe.unet.config.sample_size) * 8
  
      x_shape = (
          1,  # B
          3,  # C (RGB range from -1 to 1)
          height,  # H
          width,  # w
      )
  
      if args.xl_version:
          inputs_dtype = torch.float32
          compute_precision = ct.precision.FLOAT32
          # FIXME: Hardcoding to CPU_AND_GPU since ANE doesn't support FLOAT32
          compute_unit = ct.ComputeUnit.CPU_AND_GPU
      else:
          inputs_dtype = torch.float16
          compute_precision = None
          compute_unit = None
  
      sample_vae_encoder_inputs = {
          "x": torch.rand(*x_shape, dtype=inputs_dtype)
      }
  
      class VAEEncoder(nn.Module):
          """Wrapper nn.Module wrapper for pipe.encode() method"""
  
          def __init__(self):
              super().__init__()
              self.quant_conv = pipe.vae.quant_conv.to(
                  dtype=torch.float32
              )
              self.encoder = pipe.vae.encoder.to(dtype=torch.float32)
  
          def forward(self, x):
              return self.quant_conv(self.encoder(x))
  
      baseline_encoder = VAEEncoder().eval()
  
      # No optimization needed for the VAE Encoder as it is a pure ConvNet
      traced_vae_encoder = torch.jit.trace(
          baseline_encoder,
          (sample_vae_encoder_inputs["x"].to(torch.float32),),
      )
  
      modify_coremltools_torch_frontend_badbmm()
      coreml_vae_encoder, out_path = _convert_to_coreml(
          "vae_encoder",
          traced_vae_encoder,
          sample_vae_encoder_inputs,
          ["latent"],
          args,
          precision=compute_precision,
          compute_unit=compute_unit,
      )
  
      # Set model metadata
      coreml_vae_encoder.author = f"Please refer to the Model Card available at huggingface.co/{args.model_version}"
      if args.xl_version:
          coreml_vae_encoder.license = "OpenRAIL++-M (https://huggingface.co/stabilityai/stable-diffusion-xl-base-1.0/blob/main/LICENSE.md)"
      else:
          coreml_vae_encoder.license = "OpenRAIL (https://huggingface.co/spaces/CompVis/stable-diffusion-license)"
      coreml_vae_encoder.version = args.model_version
      coreml_vae_encoder.short_description = (
          "Stable Diffusion generates images conditioned on text and/or other images as input through the diffusion process. "
          "Please refer to https://arxiv.org/abs/2112.10752 for details."
      )
  
      # Set the input descriptions
      coreml_vae_encoder.input_description[
          "x"
      ] = "The input image to base the initial latents on normalized to range [-1, 1]"
  
      # Set the output descriptions
      coreml_vae_encoder.output_description[
          "latent"
      ] = "The latent embeddings from the unet model from the input image."
  
      coreml_vae_encoder.save(out_path)
  
      logger.info(f"Saved vae_encoder into {out_path}")
  
      # Parity check PyTorch vs CoreML
      if args.check_output_correctness:
          baseline_out = baseline_encoder(
              x=sample_vae_encoder_inputs["x"].to(torch.float32)
          ).numpy()
          coreml_out = list(
              coreml_vae_encoder.predict(
                  {
                      k: v.numpy()
                      for k, v in sample_vae_encoder_inputs.items()
                  }
              ).values()
          )[0]
          report_correctness(
              baseline_out,
              coreml_out,
              "vae_encoder baseline PyTorch to baseline CoreML",
          )
  
      del traced_vae_encoder, pipe.vae.encoder, coreml_vae_encoder
      gc.collect()
  
imports: from python_coreml_stable_diffusion import unet, controlnet, chunk_mlprogram
  import argparse
  from collections import OrderedDict, defaultdict
  from copy import deepcopy
  import coremltools
  from diffusers import StableDiffusionPipeline, DiffusionPipeline, ControlNetModel
  import gc
  import logging
  import numpy
  import os
  import requests
  import shutil
  import time
  import re
  import pathlib
  import torch
  import torch.nn
  import torch.nn.functional
  from types import MethodType
  from transformers.models.clip import modeling_clip
label_desc: Use image node_id 14 for calling a global function with example usage: convert_vae_encoder(pipe, args) and returns: self