code: 
  
  
  
  
  
  import warnings
  from dataclasses import dataclass, field
  from typing import Optional, Tuple
  
  from .training_args import TrainingArguments
  from .utils import cached_property, is_tf_available, logging, requires_backends
  
  
  _UpperCamelCase       = logging.get_logger(__name__)
  
  if is_tf_available():
       import tensorflow as tf
  
  
  
  
  @dataclass
  class 				UpperCamelCase (    a    ):
  
  
  
        """simple docstring"""
  
  
  
  
  
  
        UpperCAmelCase_						:      Any       ="tf"
        UpperCAmelCase_						:      Optional[str]       =field(
            default=a ,   metadata={"help": "Name of TPU"} ,   )
  
        UpperCAmelCase_						:      Optional[str]       =field(
            default=a ,   metadata={"help": "Zone of TPU"} ,   )
  
        UpperCAmelCase_						:      Optional[str]       =field(
            default=a ,   metadata={"help": "Name of Cloud TPU-enabled project"} ,   )
  
        UpperCAmelCase_						:      float       =field(
            default=1.0 ,   metadata={"help": "Power for the Polynomial decay LR scheduler."} ,   )
  
        UpperCAmelCase_						:      bool       =field(default=a ,   metadata={"help": "Whether to activate the XLA compilation or not"}    )
  
  
  
  
  
  
  
        @cached_property
        def 						UpperCAmelCase		( self   )       ->   str:
  
  
  
  
  
  
          '''simple docstring'''
  
  
  
  
  
  
          requires_backends(self				,						["tf"]   )
          logger.info("Tensorflow: setting up strategy"   )
  
          __snake_case   :				Optional[Any]        =       tf.config.list_physical_devices("GPU"   )
  
          # Set to float16 at first
          if self.fpaa:
            tf.keras.mixed_precision.set_global_policy("mixed_float16"   )
  
          if self.no_cuda:
            __snake_case   :				Dict        =       tf.distribute.OneDeviceStrategy(device="/cpu:0"   )
          else:
            try:
              if self.tpu_name:
                __snake_case   :				Tuple        =       tf.distribute.cluster_resolver.TPUClusterResolver(
                    self.tpu_name				,						zone=self.tpu_zone				,						project=self.gcp_project   )
              else:
                __snake_case   :				Dict        =       tf.distribute.cluster_resolver.TPUClusterResolver()
            except ValueError:
              if self.tpu_name:
                raise RuntimeError(F"""Couldn't connect to TPU {self.tpu_name}!"""   )
              else:
                __snake_case   :				List[str]        =       None
  
            if tpu:
              # Set to bfloat16 in case of TPU
              if self.fpaa:
                tf.keras.mixed_precision.set_global_policy("mixed_bfloat16"   )
  
              tf.config.experimental_connect_to_cluster(UpperCamelCase_   )
              tf.tpu.experimental.initialize_tpu_system(UpperCamelCase_   )
  
              __snake_case   :				Any        =       tf.distribute.TPUStrategy(UpperCamelCase_   )
  
            elif len(UpperCamelCase_   ) == 0:
              __snake_case   :				List[str]        =       tf.distribute.OneDeviceStrategy(device="/cpu:0"   )
            elif len(UpperCamelCase_   ) == 1:
              __snake_case   :				Union[str, Any]        =       tf.distribute.OneDeviceStrategy(device="/gpu:0"   )
            elif len(UpperCamelCase_   ) > 1:
              # If you only want to use a specific subset of GPUs use `CUDA_VISIBLE_DEVICES=0`
              __snake_case   :				List[str]        =       tf.distribute.MirroredStrategy()
            else:
              raise ValueError("Cannot find the proper strategy, please check your environment properties."   )
  
          return strategy
  
  
  
  
  
  
  
        @property
        def 						UpperCAmelCase		( self   )       ->   Dict:
  
  
  
  
  
  
          '''simple docstring'''
  
  
  
  
  
  
          requires_backends(self				,						["tf"]   )
          return self._setup_strategy
  
  
  
  
  
  
  
        @property
        def 						UpperCAmelCase		( self   )       ->   Dict:
  
  
  
  
  
  
          '''simple docstring'''
  
  
  
  
  
  
          requires_backends(self				,						["tf"]   )
          return self._setup_strategy.num_replicas_in_sync
  
  
  
  
  
  
  
        @property
        def 						UpperCAmelCase		( self   )       ->   List[Any]:
  
  
  
  
  
  
          '''simple docstring'''
  
  
  
  
  
  
          return False  # TF Logging is handled by Keras not the Trainer
  
  
  
  
  
  
  
        @property
        def 						UpperCAmelCase		( self   )       ->   Union[str, Any]:
  
  
  
  
  
  
          '''simple docstring'''
  
  
  
  
  
  
          if self.per_gpu_train_batch_size:
            logger.warning(
                "Using deprecated `--per_gpu_train_batch_size` argument which will be removed in a future "
                "version. Using `--per_device_train_batch_size` is preferred."   )
          __snake_case   :				int        =       self.per_gpu_train_batch_size or self.per_device_train_batch_size
          return per_device_batch_size * self.n_replicas
  
  
  
  
  
  
  
        @property
        def 						UpperCAmelCase		( self   )       ->   str:
  
  
  
  
  
  
          '''simple docstring'''
  
  
  
  
  
  
          if self.per_gpu_eval_batch_size:
            logger.warning(
                "Using deprecated `--per_gpu_eval_batch_size` argument which will be removed in a future "
                "version. Using `--per_device_eval_batch_size` is preferred."   )
          __snake_case   :				Any        =       self.per_gpu_eval_batch_size or self.per_device_eval_batch_size
          return per_device_batch_size * self.n_replicas
  
  
  
  
  
  
  
        @property
        def 						UpperCAmelCase		( self   )       ->   Dict:
  
  
  
  
  
  
          '''simple docstring'''
  
  
  
  
  
  
          requires_backends(self				,						["tf"]   )
          warnings.warn(
              "The n_gpu argument is deprecated and will be removed in a future version, use n_replicas instead."				,						UpperCamelCase_				,						)
          return self._setup_strategy.num_replicas_in_sync
  
  
style_context: 
  
  
  
  
  
  import torch
  from torch import autograd
  
  
  
  
  class 				_lowerCamelCase (    autograd.Function    ):
  
  
  
        """simple docstring"""
  
  
  
  
  
  
        @staticmethod
        def 						UpperCAmelCase		( UpperCAmelCase				,						UpperCAmelCase				,						UpperCAmelCase				,						UpperCAmelCase   )       ->   str:
  
  
  
  
  
  
          '''simple docstring'''
  
  
  
  
  
  
          __snake_case   :				Dict        =       inputs.numel()
          __snake_case   :				Optional[int]        =       int(0.005 * nb_elems   ) + 1
          if sigmoid:
            __snake_case   :				Any        =       (torch.sigmoid(UpperCAmelCase   ) > threshold).type(inputs.type()   )
          else:
            __snake_case   :				Optional[int]        =       (inputs > threshold).type(inputs.type()   )
          if mask.sum() < nb_min:
            # We limit the pruning so that at least 0.5% (half a percent) of the weights are remaining
            __snake_case   :				List[str]        =       inputs.flatten().kthvalue(max(nb_elems - nb_min				,						1   )   ).values
            __snake_case   :				Union[str, Any]        =       (inputs > k_threshold).type(inputs.type()   )
          return mask
  
  
  
  
  
  
  
        @staticmethod
        def 						UpperCAmelCase		( UpperCAmelCase				,						UpperCAmelCase   )       ->   Optional[int]:
  
  
  
  
  
  
          '''simple docstring'''
  
  
  
  
  
  
          return gradOutput, None, None
  
  
  
  
  class 				_lowerCamelCase (    autograd.Function    ):
  
  
  
        """simple docstring"""
  
  
  
  
  
  
        @staticmethod
        def 						UpperCAmelCase		( UpperCAmelCase				,						UpperCAmelCase				,						UpperCAmelCase   )       ->   Optional[int]:
  
  
  
  
  
  
          '''simple docstring'''
  
  
  
  
  
  
          __snake_case   :				Optional[int]        =       inputs.clone()
          __snake_case    ,   __snake_case   :				int        =       inputs.flatten().sort(descending=UpperCAmelCase   )
          __snake_case   :				Tuple        =       int(threshold * inputs.numel()   )
  
          # flat_out and mask access the same memory.
          __snake_case   :				Optional[int]        =       mask.flatten()
          __snake_case   :				Union[str, Any]        =       0
          __snake_case   :				Any        =       1
          return mask
  
  
  
  
  
  
  
        @staticmethod
        def 						UpperCAmelCase		( UpperCAmelCase				,						UpperCAmelCase   )       ->   List[str]:
  
  
  
  
  
  
          '''simple docstring'''
  
  
  
  
  
  
          return gradOutput, None
  
  
  
  
  class 				_lowerCamelCase (    a    ):
  
  
  
        """simple docstring"""
  
  
  
  
  
  
        @staticmethod
        def 						UpperCAmelCase		( UpperCAmelCase				,						UpperCAmelCase   )       ->   Any:
  
  
  
  
  
  
          '''simple docstring'''
  
  
  
  
  
  
          __snake_case   :				Union[str, Any]        =       inputs.clone()
          __snake_case    ,   __snake_case   :				Optional[Any]        =       inputs.abs().flatten().sort(descending=UpperCAmelCase   )
          __snake_case   :				List[Any]        =       int(threshold * inputs.numel()   )
  
          # flat_out and mask access the same memory.
          __snake_case   :				Optional[Any]        =       mask.flatten()
          __snake_case   :				Tuple        =       0
          __snake_case   :				Any        =       1
          return mask
  
  