text: import torch
  
  import collections
  import contextlib
  import types
  import typing
  
  CUDA_AVAILABLE: bool = torch.cuda.is_available()
  APEX_AVAILABLE: bool = False
  
  apex: typing.Optional[types.ModuleType]
  if CUDA_AVAILABLE:
      try:
          import apex
          APEX_AVAILABLE = True
      except ImportError as apex_err:
          apex = None
  else:
      apex = None
  
  
  class ApexModuleBase(types.ModuleType):
      def __getattr__(self, name):
          raise AttributeError('Cannot import {}'.format(self.__nane__)) from apex_err
  
  
  class AmpModule(ApexModuleBase):  # Base Skeletons
  
      def __getattr__(self, name):
          if APEX_AVAILABLE:
              return getattr(apex.amp, name)
          super(AmpModule, self).__getattr__(name)
  
      @staticmethod
      def initialize(
              models,
              optimizers=None,
              *args, **kwargs
      ):
          if not APEX_AVAILABLE:
              return models, optimizers
          return apex.amp.initialize(models, optimizers, *args, **kwargs)
  
      @staticmethod
      @contextlib.contextmanager
      def scale_loss(
              loss,
              *args, **kwargs
      ):
          if not APEX_AVAILABLE:
              yield loss
              return
          yield from apex.amp.scale_loss(loss, *args, **kwargs).gen
  
      @staticmethod
      @contextlib.contextmanager
      def disable_casts():
          if not APEX_AVAILABLE:
              yield
              return
          yield from apex.amp.disable_casts().gen
  
      @staticmethod
      def state_dict():
          if not APEX_AVAILABLE:
              return collections.OrderedDict()
          return apex.amp.state_dict()
  
      @staticmethod
      def load_state_dict(state_dict):
          if not APEX_AVAILABLE:
              return
          apex.amp.load_state_dict(state_dict)
  
  
  class OptimizerModule(ApexModuleBase):
      """"""
  
      def __getattr__(self, name):
          if APEX_AVAILABLE:
              return getattr(apex.optimizers, name)
          super(OptimizerModule, self).__getattr__(name)
  
  
  if APEX_AVAILABLE:
      import apex
      amp = apex.amp
      fp16_utils = apex.fp16_utils
      optimizers = apex.optimizers
      normalization = apex.normalization
      pyprof = apex.pyprof
      try:
          if torch.distributed.is_available():
              parallel = apex.parallel
          else:
              parallel = None
      except AttributeError:
          parallel = None
  
      def __getattr__(name):
          return getattr(apex, name)
  
  else:
      amp = AmpModule('apex.amp')
      optimizers = OptimizerModule('apex.optimizers')
  