code: def _fft_1d(
      input_real: Var,
      input_imag: Var,
      n: Optional[Var],
      dim: Optional[Var],
      norm: Optional[Var],
      before_op: Operation,
      inverse: bool = False,  # For inverse FFT.
  ) -> Tuple[Var, Var]:
      """
      1-D FFT by DFT Matrix Multiplication.
  
      Now based on some math formulas including:
          * The addition of complex numbers is: (a+bi)+(c+di)=(a+c)+(b+d)i.
          * The multiplication of complex numbers is: (a+bi)(c+di)=ac+adi+bci−bd=(ac−bd)+(ad+bc)i.
          * Euler’s formula: e^xi=cosx+isinx.
          * Cosine is an even function: cos(−x)=cosx.
          * Sine is an odd function: sin(−x)=−(sinx).
      We can get
          * The real part output is: cos_base * input_real + sin_base * input_imag
          * The imaginary part output is: - (sin_base * input_real - cos_base * input_imag)
      That's how we calculate the real and imaginary part separately for the FFT.
      """
      n, dim = fft_canonicalize_length_dim(input_real, n, dim)
  
      # Swaps target dim axis to the first axis.
      axes = list(range(len(input_real.shape)))
      axes[0] = dim
      axes[dim] = 0
      transposed_input_real = mb.transpose(
          x=input_real, perm=axes, before_op=before_op
      )
      transposed_input_imag = mb.transpose(
          x=input_imag, perm=axes, before_op=before_op
      )
  
      # Trim or pad input according to n.
      transposed_input_real = _resize_data(
          input_data=transposed_input_real,
          dims=(0,),
          sizes=(n,),
          before_op=before_op,
      )
      transposed_input_imag = _resize_data(
          input_data=transposed_input_imag,
          dims=(0,),
          sizes=(n,),
          before_op=before_op,
      )
  
      # Calculate DFT matrix.
      original_shape = transposed_input_real.shape
      N = transposed_input_real.shape[0]
      reshaped_input_real = mb.reshape(
          x=transposed_input_real, shape=[N, -1], before_op=before_op
      )
      reshaped_input_imag = mb.reshape(
          x=transposed_input_imag, shape=[N, -1], before_op=before_op
      )
  
      N = mb.cast(x=N, dtype="fp32", before_op=before_op)
      cos_base, sin_base = _calculate_dft_matrix(
          N, onesided=False, before_op=before_op
      )
  
      if not inverse:
          real_part = mb.add(
              x=mb.matmul(
                  x=cos_base, y=reshaped_input_real, before_op=before_op
              ),
              y=mb.matmul(
                  x=sin_base, y=reshaped_input_imag, before_op=before_op
              ),
              before_op=before_op,
          )
          imag_part = mb.sub(
              x=mb.matmul(
                  x=sin_base, y=reshaped_input_real, before_op=before_op
              ),
              y=mb.matmul(
                  x=cos_base, y=reshaped_input_imag, before_op=before_op
              ),
              before_op=before_op,
          )
          imag_part = mb.mul(x=imag_part, y=-1.0, before_op=before_op)
      else:
          real_part = mb.sub(
              x=mb.matmul(
                  x=cos_base, y=reshaped_input_real, before_op=before_op
              ),
              y=mb.matmul(
                  x=sin_base, y=reshaped_input_imag, before_op=before_op
              ),
              before_op=before_op,
          )
          imag_part = mb.add(
              x=mb.matmul(
                  x=sin_base, y=reshaped_input_real, before_op=before_op
              ),
              y=mb.matmul(
                  x=cos_base, y=reshaped_input_imag, before_op=before_op
              ),
              before_op=before_op,
          )
  
      real_part = mb.reshape(
          x=real_part, shape=original_shape, before_op=before_op
      )
      imag_part = mb.reshape(
          x=imag_part, shape=original_shape, before_op=before_op
      )
  
      # Swaps dim back.
      real_part = mb.transpose(
          x=real_part, perm=axes, before_op=before_op
      )
      imag_part = mb.transpose(
          x=imag_part, perm=axes, before_op=before_op
      )
  
      # Normalization if needed.
      apply_scale = False
      scale = 1
      if norm.val is not None:
          # For FFT, "forward" means normalize 1/N, while in IFFT, "backward" means normalize 1/N.
          if (not inverse) and (norm.val in ["forward", "ortho"]):
              apply_scale = True
              scale = (
                  N
                  if norm.val == "forward"
                  else mb.sqrt(x=N, before_op=before_op)
              )
          if inverse and (norm.val in ["backward", "ortho"]):
              apply_scale = True
              scale = (
                  N
                  if norm.val == "backward"
                  else mb.sqrt(x=N, before_op=before_op)
              )
      if apply_scale:
          real_part = mb.real_div(
              x=real_part, y=scale, before_op=before_op
          )
          imag_part = mb.real_div(
              x=imag_part, y=scale, before_op=before_op
          )
  
      return real_part, imag_part
  
imports: import functools
  from typing import Callable, Dict, Optional, Tuple
  import numpy
  from coremltools.converters.mil.mil import Builder
  from coremltools.converters.mil.mil import types
  from coremltools.converters.mil.mil.operation import Operation
  from coremltools.converters.mil.mil.ops.defs.complex_dialect_ops import fft_canonicalize_length_dim, fft_canonicalize_shapes_dims
  from coremltools.converters.mil.mil.passes.graph_pass import AbstractGraphPass
  from coremltools.converters.mil.mil.passes.helper import block_context_manager
  from coremltools.converters.mil.mil.passes.pass_registry import register_pass
  from coremltools.converters.mil.mil.var import ComplexVar, Var
label_desc: Use image node_id 4 for calling a global function with example usage: _fft_1d(input_real, input_imag, n, dim, norm, before_op, inverse) and returns: real_part,  imag_part