





'''simple docstring'''






import gc
import threading
import time

import psutil
import torch




class 					UpperCAmelCase	:




      def __init__(     self     :					int    )							-> Dict:
            _lowerCAmelCase					=				psutil.Process()
            _lowerCAmelCase					=				False




      def 							lowercase__       (     self     :					str    )							-> Optional[Any]:
            _lowerCAmelCase					=				-1

            while True:
                  _lowerCAmelCase					=				max(self.process.memory_info().rss     ,     self.cpu_memory_peak    )

                  # can't sleep or will not catch the peak right (this comment is here on purpose)
                  if not self.peak_monitoring:
                        break




      def 							lowercase__       (     self     :					Tuple    )							-> List[Any]:
            _lowerCAmelCase					=				True
            _lowerCAmelCase					=				threading.Thread(target=self.peak_monitor    )
            _lowerCAmelCase					=				True
            self.thread.start()




      def 							lowercase__       (     self     :					Tuple    )							-> Optional[Any]:
            _lowerCAmelCase					=				False
            self.thread.join()
            return self.cpu_memory_peak




A__							:  Any    =PeakCPUMemory()
def   UpperCamelCase__ (						):





      """simple docstring"""


      _lowerCAmelCase					=				{"""time""": time.time()}

      gc.collect()
      torch.cuda.empty_cache()

      # CPU mem
      _lowerCAmelCase					=				psutil.Process().memory_info().rss
      cpu_peak_tracker.start()

      # GPU mem
      for i in range(torch.cuda.device_count()    ):
            _lowerCAmelCase					=				torch.cuda.memory_allocated(lowerCAmelCase    )
      torch.cuda.reset_peak_memory_stats()

      return measures
def   UpperCamelCase__ (						lowerCAmelCase    ):





      """simple docstring"""


      _lowerCAmelCase					=				{"""time""": time.time() - start_measures["""time"""]}

      gc.collect()
      torch.cuda.empty_cache()

      # CPU mem
      _lowerCAmelCase					=				(psutil.Process().memory_info().rss - start_measures["""cpu"""]) / 2**20
      _lowerCAmelCase					=				(cpu_peak_tracker.stop() - start_measures["""cpu"""]) / 2**20

      # GPU mem
      for i in range(torch.cuda.device_count()    ):
            _lowerCAmelCase					=				(torch.cuda.memory_allocated(lowerCAmelCase    ) - start_measures[str(lowerCAmelCase    )]) / 2**20
            _lowerCAmelCase					=				(torch.cuda.max_memory_allocated(lowerCAmelCase    ) - start_measures[str(lowerCAmelCase    )]) / 2**20

      return measures





def   UpperCamelCase__ (						lowerCAmelCase				,      lowerCAmelCase    ):





      """simple docstring"""


      print(f"{description}:"    )
      print(f"- Time: {measures['time']:.2f}s"    )
      for i in range(torch.cuda.device_count()    ):
            print(f"- GPU {i} allocated: {measures[str(lowerCAmelCase    )]:.2f}MiB"    )
            _lowerCAmelCase					=				measures[f"{i}-peak"]
            print(f"- GPU {i} peak: {peak:.2f}MiB"    )
      print(f"- CPU RAM allocated: {measures['cpu']:.2f}MiB"    )
      print(f"- CPU RAM peak: {measures['cpu-peak']:.2f}MiB"    )













'''simple docstring'''






# Copyright 2023 The HuggingFace Team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# 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 typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available


A__							:  int    ={
    '''configuration_xmod''': [
        '''XMOD_PRETRAINED_CONFIG_ARCHIVE_MAP''',
        '''XmodConfig''',
        '''XmodOnnxConfig''',
    ],
}

try:
      if not is_torch_available():
            raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
      pass
else:
      A__							:  List[str]    =[
          '''XMOD_PRETRAINED_MODEL_ARCHIVE_LIST''',
          '''XmodForCausalLM''',
          '''XmodForMaskedLM''',
          '''XmodForMultipleChoice''',
          '''XmodForQuestionAnswering''',
          '''XmodForSequenceClassification''',
          '''XmodForTokenClassification''',
          '''XmodModel''',
          '''XmodPreTrainedModel''',
      ]

if TYPE_CHECKING:
      from .configuration_xmod import XMOD_PRETRAINED_CONFIG_ARCHIVE_MAP, XmodConfig, XmodOnnxConfig

      try:
            if not is_torch_available():
                  raise OptionalDependencyNotAvailable()
      except OptionalDependencyNotAvailable:
            pass
      else:
            from .modeling_xmod import (
                XMOD_PRETRAINED_MODEL_ARCHIVE_LIST,
                XmodForCausalLM,
                XmodForMaskedLM,
                XmodForMultipleChoice,
                XmodForQuestionAnswering,
                XmodForSequenceClassification,
                XmodForTokenClassification,
                XmodModel,
                XmodPreTrainedModel,
            )

else:
      import sys

      A__							:  str    =_LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)






