"""simple docstring"""






import argparse
import json
from dataclasses import dataclass, field
from functools import partial
from pathlib import Path
from typing import Callable, Dict, List, Tuple

import timm
import torch
import torch.nn as nn
from classy_vision.models.regnet import RegNet, RegNetParams, RegNetYaagf, RegNetYaagf, RegNetYaaagf
from huggingface_hub import cached_download, hf_hub_url
from torch import Tensor
from vissl.models.model_helpers import get_trunk_forward_outputs

from transformers import AutoImageProcessor, RegNetConfig, RegNetForImageClassification, RegNetModel
from transformers.utils import logging


logging.set_verbosity_info()
__lowerCAmelCase       :			str   	=logging.get_logger()





@dataclass
class 					_A		:
      snake_case__     :  nn.Module
      snake_case__     :  List[nn.Module]				    =      field(default_factory=lowerCAmelCase      )
      snake_case__     :  list				    =      field(default_factory=lowerCAmelCase      )

      def 							A__			(		self						,			__lowerCAmelCase						,			__lowerCAmelCase						,			__lowerCAmelCase		):







            """simple docstring"""



            lowercase								=      len(list(m.modules()		)		) == 1 or isinstance(__lowerCAmelCase						,			nn.Convad		) or isinstance(__lowerCAmelCase						,			nn.BatchNormad		)
            if has_not_submodules:
                  self.traced.append(__lowerCAmelCase		)

      def __call__(		self						,			__lowerCAmelCase		):







            """simple docstring"""



            for m in self.module.modules():
                  self.handles.append(m.register_forward_hook(self._forward_hook		)		)
            self.module(__lowerCAmelCase		)
            [x.remove() for x in self.handles]
            return self







      @property
      def 							A__			(		self		):







            """simple docstring"""



            return list(filter(lambda __lowerCAmelCase		: len(list(x.state_dict().keys()		)		) > 0						,			self.traced		)		)






@dataclass
class 					_A		:
      snake_case__     :  nn.Module
      snake_case__     :  nn.Module
      snake_case__     :  int				    =      1
      snake_case__     :  List				    =      field(default_factory=lowerCAmelCase      )
      snake_case__     :  List				    =      field(default_factory=lowerCAmelCase      )
      snake_case__     :  bool				    =      True

      def __call__(		self						,			__lowerCAmelCase		):







            """simple docstring"""



            lowercase								=      Tracker(self.dest		)(__lowerCAmelCase		).parametrized
            lowercase								=      Tracker(self.src		)(__lowerCAmelCase		).parametrized

            lowercase								=      list(filter(lambda __lowerCAmelCase		: type(__lowerCAmelCase		) not in self.src_skip						,			__lowerCAmelCase		)		)
            lowercase								=      list(filter(lambda __lowerCAmelCase		: type(__lowerCAmelCase		) not in self.dest_skip						,			__lowerCAmelCase		)		)

            if len(__lowerCAmelCase		) != len(__lowerCAmelCase		) and self.raise_if_mismatch:
                  raise Exception(
                      f'Numbers of operations are different. Source module has {len(__lowerCAmelCase		)} operations while'
                      f' destination module has {len(__lowerCAmelCase		)}.'		)

            for dest_m, src_m in zip(__lowerCAmelCase						,			__lowerCAmelCase		):
                  dest_m.load_state_dict(src_m.state_dict()		)
                  if self.verbose == 1:
                        print(f'Transfered from={src_m} to={dest_m}'		)






class 					_A		(			nn.Module      ):

      def __init__(		self						,			__lowerCAmelCase		):







            """simple docstring"""



            super().__init__()

            lowercase								=      []
            # - get the stem
            feature_blocks.append(("""conv1""", model.stem)		)
            # - get all the feature blocks
            for k, v in model.trunk_output.named_children():
                  assert k.startswith("""block"""		), f'Unexpected layer name {k}'
                  lowercase								=      len(__lowerCAmelCase		) + 1
                  feature_blocks.append((f'res{block_index}', v)		)

            lowercase								=      nn.ModuleDict(__lowerCAmelCase		)







      def 							A__			(		self						,			__lowerCAmelCase		):







            """simple docstring"""



            return get_trunk_forward_outputs(
                __lowerCAmelCase						,			out_feat_keys=__lowerCAmelCase						,			feature_blocks=self._feature_blocks						,			)






class 					_A		(			lowerCAmelCase      ):

      def 							A__			(		self						,			__lowerCAmelCase		):







            """simple docstring"""



            lowercase								=      x.split("""-"""		)
            return x_split[0] + x_split[1] + "_" + "".join(x_split[2:]		)







      def __getitem__(		self						,			__lowerCAmelCase		):







            """simple docstring"""



            if x not in self:
                  lowercase								=      self.convert_name_to_timm(__lowerCAmelCase		)
                  lowercase								=      partial(lambda: (timm.create_model(__lowerCAmelCase						,			pretrained=__lowerCAmelCase		).eval(), None)		)

            else:
                  lowercase								=      super().__getitem__(__lowerCAmelCase		)

            return val






class 					_A		(			lowerCAmelCase      ):

      def __getitem__(		self						,			__lowerCAmelCase		):







            """simple docstring"""



            if "seer" in x and "in1k" not in x:
                  lowercase								=      RegNetModel
            else:
                  lowercase								=      RegNetForImageClassification
            return val






def      UpperCAmelCase__			(							lowerCAmelCase__	:Optional[int]   ,						lowerCAmelCase__	:int   ,						lowerCAmelCase__	:List[Tuple[str, str]]						)					->       str:
      '''simple docstring'''


      for from_key, to_key in keys:
            lowercase								=      from_state_dict[from_key].clone()
            print(f'Copied key={from_key} to={to_key}'						)
      return to_state_dict


def      UpperCAmelCase__			(							lowerCAmelCase__	:str   ,						lowerCAmelCase__	:Callable[[], nn.Module]   ,						lowerCAmelCase__	:Callable[[], nn.Module]   ,						lowerCAmelCase__	:RegNetConfig   ,						lowerCAmelCase__	:Path   ,						lowerCAmelCase__	:bool = True   ,						)					->       Any:
      '''simple docstring'''


      print(f'Converting {name}...'						)
      with torch.no_grad():
            lowercase				,				lowercase								=      from_model_func()
            lowercase								=      our_model_func(lowerCAmelCase__						).eval()
            lowercase								=      ModuleTransfer(src=lowerCAmelCase__   ,						dest=lowerCAmelCase__   ,						raise_if_mismatch=lowerCAmelCase__						)
            lowercase								=      torch.randn((1, 3, 2_2_4, 2_2_4)						)
            module_transfer(lowerCAmelCase__						)

      if from_state_dict is not None:
            lowercase								=      []
            # for seer - in1k finetuned we have to manually copy the head
            if "seer" in name and "in1k" in name:
                  lowercase								=      [("""0.clf.0.weight""", """classifier.1.weight"""), ("""0.clf.0.bias""", """classifier.1.bias""")]
            lowercase								=      manually_copy_vissl_head(lowerCAmelCase__   ,						our_model.state_dict()   ,						lowerCAmelCase__						)
            our_model.load_state_dict(lowerCAmelCase__						)

      lowercase								=      our_model(lowerCAmelCase__   ,						output_hidden_states=lowerCAmelCase__						)
      lowercase								=      (
          our_outputs.logits if isinstance(lowerCAmelCase__   ,						lowerCAmelCase__						) else our_outputs.last_hidden_state
      )

      lowercase								=      from_model(lowerCAmelCase__						)
      lowercase								=      from_output[-1] if type(lowerCAmelCase__						) is list else from_output

      # now since I don't want to use any config files, vissl seer model doesn't actually have an head, so let's just check the last hidden state
      if "seer" in name and "in1k" in name:
            lowercase								=      our_outputs.hidden_states[-1]

      assert torch.allclose(lowerCAmelCase__   ,						lowerCAmelCase__						), "The model logits don't match the original one."

      if push_to_hub:
            our_model.push_to_hub(
                repo_path_or_name=save_directory / name   ,						commit_message="""Add model"""   ,						use_temp_dir=lowerCAmelCase__   ,						)

            lowercase								=      2_2_4 if """seer""" not in name else 3_8_4
            # we can use the convnext one
            lowercase								=      AutoImageProcessor.from_pretrained("""facebook/convnext-base-224-22k-1k"""   ,						size=lowerCAmelCase__						)
            image_processor.push_to_hub(
                repo_path_or_name=save_directory / name   ,						commit_message="""Add image processor"""   ,						use_temp_dir=lowerCAmelCase__   ,						)

            print(f'Pushed {name}'						)


def      UpperCAmelCase__			(							lowerCAmelCase__	:Path   ,						lowerCAmelCase__	:str = None   ,						lowerCAmelCase__	:bool = True						)					->       List[str]:
      '''simple docstring'''


      lowercase								=      """imagenet-1k-id2label.json"""
      lowercase								=      1_0_0_0
      lowercase								=      (1, num_labels)

      lowercase								=      """huggingface/label-files"""
      lowercase								=      num_labels
      lowercase								=      json.load(open(cached_download(hf_hub_url(lowerCAmelCase__   ,						lowerCAmelCase__   ,						repo_type="""dataset"""						)						)   ,						"""r"""						)						)
      lowercase								=      {int(lowerCAmelCase__						): v for k, v in idalabel.items()}

      lowercase								=      idalabel
      lowercase								=      {v: k for k, v in idalabel.items()}

      lowercase								=      partial(lowerCAmelCase__   ,						num_labels=lowerCAmelCase__   ,						idalabel=lowerCAmelCase__   ,						labelaid=lowerCAmelCase__						)

      lowercase								=      {
          """regnet-x-002""": ImageNetPreTrainedConfig(
              depths=[1, 1, 4, 7]   ,						hidden_sizes=[2_4, 5_6, 1_5_2, 3_6_8]   ,						groups_width=8   ,						layer_type="""x"""						),
          """regnet-x-004""": ImageNetPreTrainedConfig(
              depths=[1, 2, 7, 1_2]   ,						hidden_sizes=[3_2, 6_4, 1_6_0, 3_8_4]   ,						groups_width=1_6   ,						layer_type="""x"""						),
          """regnet-x-006""": ImageNetPreTrainedConfig(
              depths=[1, 3, 5, 7]   ,						hidden_sizes=[4_8, 9_6, 2_4_0, 5_2_8]   ,						groups_width=2_4   ,						layer_type="""x"""						),
          """regnet-x-008""": ImageNetPreTrainedConfig(
              depths=[1, 3, 7, 5]   ,						hidden_sizes=[6_4, 1_2_8, 2_8_8, 6_7_2]   ,						groups_width=1_6   ,						layer_type="""x"""						),
          """regnet-x-016""": ImageNetPreTrainedConfig(
              depths=[2, 4, 1_0, 2]   ,						hidden_sizes=[7_2, 1_6_8, 4_0_8, 9_1_2]   ,						groups_width=2_4   ,						layer_type="""x"""						),
          """regnet-x-032""": ImageNetPreTrainedConfig(
              depths=[2, 6, 1_5, 2]   ,						hidden_sizes=[9_6, 1_9_2, 4_3_2, 1_0_0_8]   ,						groups_width=4_8   ,						layer_type="""x"""						),
          """regnet-x-040""": ImageNetPreTrainedConfig(
              depths=[2, 5, 1_4, 2]   ,						hidden_sizes=[8_0, 2_4_0, 5_6_0, 1_3_6_0]   ,						groups_width=4_0   ,						layer_type="""x"""						),
          """regnet-x-064""": ImageNetPreTrainedConfig(
              depths=[2, 4, 1_0, 1]   ,						hidden_sizes=[1_6_8, 3_9_2, 7_8_4, 1_6_2_4]   ,						groups_width=5_6   ,						layer_type="""x"""						),
          """regnet-x-080""": ImageNetPreTrainedConfig(
              depths=[2, 5, 1_5, 1]   ,						hidden_sizes=[8_0, 2_4_0, 7_2_0, 1_9_2_0]   ,						groups_width=1_2_0   ,						layer_type="""x"""						),
          """regnet-x-120""": ImageNetPreTrainedConfig(
              depths=[2, 5, 1_1, 1]   ,						hidden_sizes=[2_2_4, 4_4_8, 8_9_6, 2_2_4_0]   ,						groups_width=1_1_2   ,						layer_type="""x"""						),
          """regnet-x-160""": ImageNetPreTrainedConfig(
              depths=[2, 6, 1_3, 1]   ,						hidden_sizes=[2_5_6, 5_1_2, 8_9_6, 2_0_4_8]   ,						groups_width=1_2_8   ,						layer_type="""x"""						),
          """regnet-x-320""": ImageNetPreTrainedConfig(
              depths=[2, 7, 1_3, 1]   ,						hidden_sizes=[3_3_6, 6_7_2, 1_3_4_4, 2_5_2_0]   ,						groups_width=1_6_8   ,						layer_type="""x"""						),
          # y variant
          """regnet-y-002""": ImageNetPreTrainedConfig(depths=[1, 1, 4, 7]   ,						hidden_sizes=[2_4, 5_6, 1_5_2, 3_6_8]   ,						groups_width=8						),
          """regnet-y-004""": ImageNetPreTrainedConfig(
              depths=[1, 3, 6, 6]   ,						hidden_sizes=[4_8, 1_0_4, 2_0_8, 4_4_0]   ,						groups_width=8						),
          """regnet-y-006""": ImageNetPreTrainedConfig(
              depths=[1, 3, 7, 4]   ,						hidden_sizes=[4_8, 1_1_2, 2_5_6, 6_0_8]   ,						groups_width=1_6						),
          """regnet-y-008""": ImageNetPreTrainedConfig(
              depths=[1, 3, 8, 2]   ,						hidden_sizes=[6_4, 1_2_8, 3_2_0, 7_6_8]   ,						groups_width=1_6						),
          """regnet-y-016""": ImageNetPreTrainedConfig(
              depths=[2, 6, 1_7, 2]   ,						hidden_sizes=[4_8, 1_2_0, 3_3_6, 8_8_8]   ,						groups_width=2_4						),
          """regnet-y-032""": ImageNetPreTrainedConfig(
              depths=[2, 5, 1_3, 1]   ,						hidden_sizes=[7_2, 2_1_6, 5_7_6, 1_5_1_2]   ,						groups_width=2_4						),
          """regnet-y-040""": ImageNetPreTrainedConfig(
              depths=[2, 6, 1_2, 2]   ,						hidden_sizes=[1_2_8, 1_9_2, 5_1_2, 1_0_8_8]   ,						groups_width=6_4						),
          """regnet-y-064""": ImageNetPreTrainedConfig(
              depths=[2, 7, 1_4, 2]   ,						hidden_sizes=[1_4_4, 2_8_8, 5_7_6, 1_2_9_6]   ,						groups_width=7_2						),
          """regnet-y-080""": ImageNetPreTrainedConfig(
              depths=[2, 4, 1_0, 1]   ,						hidden_sizes=[1_6_8, 4_4_8, 8_9_6, 2_0_1_6]   ,						groups_width=5_6						),
          """regnet-y-120""": ImageNetPreTrainedConfig(
              depths=[2, 5, 1_1, 1]   ,						hidden_sizes=[2_2_4, 4_4_8, 8_9_6, 2_2_4_0]   ,						groups_width=1_1_2						),
          """regnet-y-160""": ImageNetPreTrainedConfig(
              depths=[2, 4, 1_1, 1]   ,						hidden_sizes=[2_2_4, 4_4_8, 1_2_3_2, 3_0_2_4]   ,						groups_width=1_1_2						),
          """regnet-y-320""": ImageNetPreTrainedConfig(
              depths=[2, 5, 1_2, 1]   ,						hidden_sizes=[2_3_2, 6_9_6, 1_3_9_2, 3_7_1_2]   ,						groups_width=2_3_2						),
          # models created by SEER -> https://arxiv.org/abs/2202.08360
          """regnet-y-320-seer""": RegNetConfig(depths=[2, 5, 1_2, 1]   ,						hidden_sizes=[2_3_2, 6_9_6, 1_3_9_2, 3_7_1_2]   ,						groups_width=2_3_2						),
          """regnet-y-640-seer""": RegNetConfig(depths=[2, 5, 1_2, 1]   ,						hidden_sizes=[3_2_8, 9_8_4, 1_9_6_8, 4_9_2_0]   ,						groups_width=3_2_8						),
          """regnet-y-1280-seer""": RegNetConfig(
              depths=[2, 7, 1_7, 1]   ,						hidden_sizes=[5_2_8, 1_0_5_6, 2_9_0_4, 7_3_9_2]   ,						groups_width=2_6_4						),
          """regnet-y-2560-seer""": RegNetConfig(
              depths=[3, 7, 1_6, 1]   ,						hidden_sizes=[6_4_0, 1_6_9_6, 2_5_4_4, 5_0_8_8]   ,						groups_width=6_4_0						),
          """regnet-y-10b-seer""": ImageNetPreTrainedConfig(
              depths=[2, 7, 1_7, 1]   ,						hidden_sizes=[2_0_2_0, 4_0_4_0, 1_1_1_1_0, 2_8_2_8_0]   ,						groups_width=1_0_1_0						),
          # finetuned on imagenet
          """regnet-y-320-seer-in1k""": ImageNetPreTrainedConfig(
              depths=[2, 5, 1_2, 1]   ,						hidden_sizes=[2_3_2, 6_9_6, 1_3_9_2, 3_7_1_2]   ,						groups_width=2_3_2						),
          """regnet-y-640-seer-in1k""": ImageNetPreTrainedConfig(
              depths=[2, 5, 1_2, 1]   ,						hidden_sizes=[3_2_8, 9_8_4, 1_9_6_8, 4_9_2_0]   ,						groups_width=3_2_8						),
          """regnet-y-1280-seer-in1k""": ImageNetPreTrainedConfig(
              depths=[2, 7, 1_7, 1]   ,						hidden_sizes=[5_2_8, 1_0_5_6, 2_9_0_4, 7_3_9_2]   ,						groups_width=2_6_4						),
          """regnet-y-2560-seer-in1k""": ImageNetPreTrainedConfig(
              depths=[3, 7, 1_6, 1]   ,						hidden_sizes=[6_4_0, 1_6_9_6, 2_5_4_4, 5_0_8_8]   ,						groups_width=6_4_0						),
          """regnet-y-10b-seer-in1k""": ImageNetPreTrainedConfig(
              depths=[2, 7, 1_7, 1]   ,						hidden_sizes=[2_0_2_0, 4_0_4_0, 1_1_1_1_0, 2_8_2_8_0]   ,						groups_width=1_0_1_0						),
      }

      lowercase								=      NameToOurModelFuncMap()
      lowercase								=      NameToFromModelFuncMap()
      # add seer weights logic

      def load_using_classy_vision(lowerCAmelCase__	:str   ,						lowerCAmelCase__	:Callable[[], nn.Module]						) -> Tuple[nn.Module, Dict]:
            lowercase								=      torch.hub.load_state_dict_from_url(lowerCAmelCase__   ,						model_dir=str(lowerCAmelCase__						)   ,						map_location="""cpu"""						)
            lowercase								=      model_func()
            # check if we have a head, if yes add it
            lowercase								=      files["""classy_state_dict"""]["""base_model"""]["""model"""]
            lowercase								=      model_state_dict["""trunk"""]
            model.load_state_dict(lowerCAmelCase__						)
            return model.eval(), model_state_dict["heads"]

      # pretrained
      lowercase								=      partial(
          lowerCAmelCase__   ,						"""https://dl.fbaipublicfiles.com/vissl/model_zoo/seer_regnet32d/seer_regnet32gf_model_iteration244000.torch"""   ,						lambda: FakeRegNetVisslWrapper(RegNetYaagf()						)   ,						)

      lowercase								=      partial(
          lowerCAmelCase__   ,						"""https://dl.fbaipublicfiles.com/vissl/model_zoo/seer_regnet64/seer_regnet64gf_model_final_checkpoint_phase0.torch"""   ,						lambda: FakeRegNetVisslWrapper(RegNetYaagf()						)   ,						)

      lowercase								=      partial(
          lowerCAmelCase__   ,						"""https://dl.fbaipublicfiles.com/vissl/model_zoo/swav_ig1b_regnet128Gf_cnstant_bs32_node16_sinkhorn10_proto16k_syncBN64_warmup8k/model_final_checkpoint_phase0.torch"""   ,						lambda: FakeRegNetVisslWrapper(RegNetYaaagf()						)   ,						)

      lowercase								=      partial(
          lowerCAmelCase__   ,						"""https://dl.fbaipublicfiles.com/vissl/model_zoo/seer_regnet10B/model_iteration124500_conso.torch"""   ,						lambda: FakeRegNetVisslWrapper(
              RegNet(RegNetParams(depth=2_7   ,						group_width=1_0_1_0   ,						w_a=1_7_4_4   ,						w_a=620.83   ,						w_m=2.52						)						)						)   ,						)

      # IN1K finetuned
      lowercase								=      partial(
          lowerCAmelCase__   ,						"""https://dl.fbaipublicfiles.com/vissl/model_zoo/seer_finetuned/seer_regnet32_finetuned_in1k_model_final_checkpoint_phase78.torch"""   ,						lambda: FakeRegNetVisslWrapper(RegNetYaagf()						)   ,						)

      lowercase								=      partial(
          lowerCAmelCase__   ,						"""https://dl.fbaipublicfiles.com/vissl/model_zoo/seer_finetuned/seer_regnet64_finetuned_in1k_model_final_checkpoint_phase78.torch"""   ,						lambda: FakeRegNetVisslWrapper(RegNetYaagf()						)   ,						)

      lowercase								=      partial(
          lowerCAmelCase__   ,						"""https://dl.fbaipublicfiles.com/vissl/model_zoo/seer_finetuned/seer_regnet128_finetuned_in1k_model_final_checkpoint_phase78.torch"""   ,						lambda: FakeRegNetVisslWrapper(RegNetYaaagf()						)   ,						)

      lowercase								=      partial(
          lowerCAmelCase__   ,						"""https://dl.fbaipublicfiles.com/vissl/model_zoo/seer_finetuned/seer_10b_finetuned_in1k_model_phase28_conso.torch"""   ,						lambda: FakeRegNetVisslWrapper(
              RegNet(RegNetParams(depth=2_7   ,						group_width=1_0_1_0   ,						w_a=1_7_4_4   ,						w_a=620.83   ,						w_m=2.52						)						)						)   ,						)

      if model_name:
            convert_weight_and_push(
                lowerCAmelCase__   ,						names_to_from_model_map[model_name]   ,						names_to_ours_model_map[model_name]   ,						names_to_config[model_name]   ,						lowerCAmelCase__   ,						lowerCAmelCase__   ,						)
      else:
            for model_name, config in names_to_config.items():
                  convert_weight_and_push(
                      lowerCAmelCase__   ,						names_to_from_model_map[model_name]   ,						names_to_ours_model_map[model_name]   ,						lowerCAmelCase__   ,						lowerCAmelCase__   ,						lowerCAmelCase__   ,						)
      return config, expected_shape


if __name__ == "__main__":
       __lowerCAmelCase       :			Union[str, Any]   	=argparse.ArgumentParser()
       # Required parameters
       parser.add_argument(
           """--model_name""",
           default=None,
           type=str,
           help=(
               """The name of the model you wish to convert, it must be one of the supported regnet* architecture,"""
               """ currently: regnetx-*, regnety-*. If `None`, all of them will the converted."""
           ),
       )
       parser.add_argument(
           """--pytorch_dump_folder_path""",
           default=None,
           type=Path,
           required=True,
           help="""Path to the output PyTorch model directory.""",
       )
       parser.add_argument(
           """--push_to_hub""",
           default=True,
           type=bool,
           required=False,
           help="""If True, push model and image processor to the hub.""",
       )

       __lowerCAmelCase       :			Optional[Any]   	=parser.parse_args()

       __lowerCAmelCase       :			Path   	=args.pytorch_dump_folder_path
       pytorch_dump_folder_path.mkdir(exist_ok=True, parents=True)
       convert_weights_and_push(pytorch_dump_folder_path, args.model_name, args.push_to_hub)




"""simple docstring"""






import ast
import os
import re
import shutil
import tempfile
import unittest
from unittest import mock

import torch

from accelerate.test_utils.examples import compare_against_test
from accelerate.test_utils.testing import TempDirTestCase, require_trackers, run_command, slow
from accelerate.utils import write_basic_config


# DataLoaders built from `test_samples/MRPC` for quick testing
# Should mock `{script_name}.get_dataloaders` via:
# @mock.patch("{script_name}.get_dataloaders", mocked_dataloaders)

__lowerCAmelCase       :			List[str]   	=[
    """cross_validation.py""",
    """gradient_accumulation.py""",
    """local_sgd.py""",
    """multi_process_metrics.py""",
    """memory.py""",
    """automatic_gradient_accumulation.py""",
    """fsdp_with_peak_mem_tracking.py""",
    """deepspeed_with_config_support.py""",
    """megatron_lm_gpt_pretraining.py""",
]





class 					_A		(			unittest.TestCase      ):

      def 							A__			(		self						,			__lowerCAmelCase						,			__lowerCAmelCase						,			__lowerCAmelCase = None						,			__lowerCAmelCase = None		):







            """simple docstring"""



            lowercase								=      None
            lowercase								=      os.path.abspath(os.path.join("""examples"""						,			"""by_feature"""		)		)
            lowercase								=      os.path.abspath("""examples"""		)
            for item in os.listdir(__lowerCAmelCase		):
                  if item not in EXCLUDE_EXAMPLES:
                        lowercase								=      os.path.join(__lowerCAmelCase						,			__lowerCAmelCase		)
                        if os.path.isfile(__lowerCAmelCase		) and ".py" in item_path:
                              with self.subTest(
                                  tested_script=__lowerCAmelCase						,			feature_script=__lowerCAmelCase						,			tested_section="""main()""" if parser_only else """training_function()"""						,			):
                                    lowercase								=      compare_against_test(
                                        os.path.join(__lowerCAmelCase						,			__lowerCAmelCase		)						,			__lowerCAmelCase						,			__lowerCAmelCase						,			__lowerCAmelCase		)
                                    lowercase								=      """\n""".join(__lowerCAmelCase		)
                                    if special_strings is not None:
                                          for string in special_strings:
                                                lowercase								=      diff.replace(__lowerCAmelCase						,			""""""		)
                                    self.assertEqual(__lowerCAmelCase						,			""""""		)

      def 							A__			(		self		):







            """simple docstring"""



            self.one_complete_example("""complete_nlp_example.py"""						,			__lowerCAmelCase		)
            self.one_complete_example("""complete_nlp_example.py"""						,			__lowerCAmelCase		)







      def 							A__			(		self		):







            """simple docstring"""



            lowercase								=      os.path.abspath(os.path.join("""examples"""						,			"""cv_example.py"""		)		)
            lowercase								=      [
                """ """ * 16 + """{\n\n""",
                """ """ * 20 + """\"accuracy\": eval_metric[\"accuracy\"],\n\n""",
                """ """ * 20 + """\"f1\": eval_metric[\"f1\"],\n\n""",
                """ """ * 20 + """\"train_loss\": total_loss.item() / len(train_dataloader),\n\n""",
                """ """ * 20 + """\"epoch\": epoch,\n\n""",
                """ """ * 16 + """},\n\n""",
                """ """ * 16 + """step=epoch,\n""",
                """ """ * 12,
                """ """ * 8 + """for step, batch in enumerate(active_dataloader):\n""",
            ]
            self.one_complete_example("""complete_cv_example.py"""						,			__lowerCAmelCase						,			__lowerCAmelCase						,			__lowerCAmelCase		)
            self.one_complete_example("""complete_cv_example.py"""						,			__lowerCAmelCase						,			__lowerCAmelCase						,			__lowerCAmelCase		)






@mock.patch.dict(os.environ    ,       {'TESTING_MOCKED_DATALOADERS': '1'}      )
class 					_A		(			lowerCAmelCase      ):
      snake_case__     :  Any				    =      False

      @classmethod
      def 							A__			(		cls		):







            """simple docstring"""



            super().setUpClass()
            lowercase								=      tempfile.mkdtemp()
            lowercase								=      os.path.join(cls._tmpdir						,			"""default_config.yml"""		)

            write_basic_config(save_location=cls.configPath		)
            lowercase								=      ["""accelerate""", """launch""", """--config_file""", cls.configPath]

      @classmethod
      def 							A__			(		cls		):







            """simple docstring"""



            super().tearDownClass()
            shutil.rmtree(cls._tmpdir		)

      def 							A__			(		self		):







            """simple docstring"""



            lowercase								=      f'\n        examples/by_feature/checkpointing.py\n        --checkpointing_steps epoch\n        --output_dir {self.tmpdir}\n        '.split()
            run_command(self._launch_args + testargs		)
            self.assertTrue(os.path.exists(os.path.join(self.tmpdir						,			"""epoch_0"""		)		)		)

      def 							A__			(		self		):







            """simple docstring"""



            lowercase								=      f'\n        examples/by_feature/checkpointing.py\n        --checkpointing_steps 1\n        --output_dir {self.tmpdir}\n        '.split()
            lowercase								=      run_command(self._launch_args + testargs		)
            self.assertTrue(os.path.exists(os.path.join(self.tmpdir						,			"""step_2"""		)		)		)

      def 							A__			(		self		):







            """simple docstring"""



            lowercase								=      f'\n        examples/by_feature/checkpointing.py\n        --resume_from_checkpoint {os.path.join(self.tmpdir						,			"epoch_0"		)}\n        '.split()
            lowercase								=      run_command(self._launch_args + testargs						,			return_stdout=__lowerCAmelCase		)
            self.assertNotIn("""epoch 0:"""						,			__lowerCAmelCase		)
            self.assertIn("""epoch 1:"""						,			__lowerCAmelCase		)

      def 							A__			(		self		):







            """simple docstring"""



            lowercase								=      f'\n        examples/by_feature/checkpointing.py\n        --resume_from_checkpoint {os.path.join(self.tmpdir						,			"step_2"		)}\n        '.split()
            lowercase								=      run_command(self._launch_args + testargs						,			return_stdout=__lowerCAmelCase		)
            if torch.cuda.is_available():
                  lowercase								=      torch.cuda.device_count()
            else:
                  lowercase								=      1
            if num_processes > 1:
                  self.assertNotIn("""epoch 0:"""						,			__lowerCAmelCase		)
                  self.assertIn("""epoch 1:"""						,			__lowerCAmelCase		)
            else:
                  self.assertIn("""epoch 0:"""						,			__lowerCAmelCase		)
                  self.assertIn("""epoch 1:"""						,			__lowerCAmelCase		)

      @slow
      def 							A__			(		self		):







            """simple docstring"""



            lowercase								=      """
        examples/by_feature/cross_validation.py
        --num_folds 2
        """.split()
            with mock.patch.dict(os.environ						,			{"""TESTING_MOCKED_DATALOADERS""": """0"""}		):
                  lowercase								=      run_command(self._launch_args + testargs						,			return_stdout=__lowerCAmelCase		)
                  lowercase								=      re.findall("""({.+})"""						,			__lowerCAmelCase		)
                  lowercase								=      [r for r in results if """accuracy""" in r][-1]
                  lowercase								=      ast.literal_eval(__lowerCAmelCase		)
                  self.assertGreaterEqual(results["""accuracy"""]						,			0.7_5		)

      def 							A__			(		self		):







            """simple docstring"""



            lowercase								=      ["""examples/by_feature/multi_process_metrics.py"""]
            run_command(self._launch_args + testargs		)

      @require_trackers
      @mock.patch.dict(os.environ						,			{"""WANDB_MODE""": """offline"""}		)
      def 							A__			(		self		):







            """simple docstring"""



            with tempfile.TemporaryDirectory() as tmpdir:
                  lowercase								=      f'\n            examples/by_feature/tracking.py\n            --with_tracking\n            --project_dir {tmpdir}\n            '.split()
                  run_command(self._launch_args + testargs		)
                  self.assertTrue(os.path.exists(os.path.join(__lowerCAmelCase						,			"""tracking"""		)		)		)

      def 							A__			(		self		):







            """simple docstring"""



            lowercase								=      ["""examples/by_feature/gradient_accumulation.py"""]
            run_command(self._launch_args + testargs		)







      def 							A__			(		self		):







            """simple docstring"""



            lowercase								=      ["""examples/by_feature/local_sgd.py"""]
            run_command(self._launch_args + testargs		)



