
import collections
import inspect
import unittest

from transformers import FocalNetConfig
from transformers.testing_utils import require_torch, require_vision, slow, torch_device
from transformers.utils import cached_property, is_torch_available, is_vision_available

from ...test_backbone_common import BackboneTesterMixin
from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, _config_zero_init, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
		import torch
		from torch import nn

		from transformers import (
		    FocalNetBackbone,
		    FocalNetForImageClassification,
		    FocalNetForMaskedImageModeling,
		    FocalNetModel,
		)
		from transformers.models.focalnet.modeling_focalnet import FOCALNET_PRETRAINED_MODEL_ARCHIVE_LIST

if is_vision_available():
		from PIL import Image

		from transformers import AutoImageProcessor




class 				lowerCamelCase__							:




	'''simple docstring'''




	def __init__(      self	:Dict  ,     a	:Tuple  ,     a	:Any=1_3  ,     a	:int=3_2  ,     a	:Optional[Any]=2  ,     a	:List[Any]=3  ,     a	:List[str]=1_6  ,     a	:Union[str, Any]=[3_2, 6_4, 1_2_8]  ,     a	:List[Any]=[1, 2, 1]  ,     a	:Optional[int]=[2, 2, 4]  ,     a	:Dict=2  ,     a	:List[str]=2.0  ,     a	:Any=True  ,     a	:List[str]=0.0  ,     a	:Optional[int]=0.0  ,     a	:Tuple=0.1  ,     a	:Tuple="gelu"  ,     a	:List[Any]=False  ,     a	:List[Any]=True  ,     a	:Optional[Any]=0.02  ,     a	:Optional[int]=1E-5  ,     a	:int=True  ,     a	:List[Any]=None  ,     a	:Optional[int]=True  ,     a	:Optional[int]=1_0  ,     a	:Optional[Any]=8  ,     a	:int=["stage1", "stage2"]  ,     a	:List[Any]=[1, 2]  ,     )	->				List[Any]:
								__UpperCamelCase			:     Tuple         =      parent
								__UpperCamelCase			:     List[str]         =      batch_size
								__UpperCamelCase			:     Optional[Any]         =      image_size
								__UpperCamelCase			:     int         =      patch_size
								__UpperCamelCase			:     Tuple         =      num_channels
								__UpperCamelCase			:     str         =      embed_dim
								__UpperCamelCase			:     Optional[int]         =      hidden_sizes
								__UpperCamelCase			:     Optional[Any]         =      depths
								__UpperCamelCase			:     str         =      num_heads
								__UpperCamelCase			:     Any         =      window_size
								__UpperCamelCase			:     List[Any]         =      mlp_ratio
								__UpperCamelCase			:     Optional[int]         =      qkv_bias
								__UpperCamelCase			:     Optional[Any]         =      hidden_dropout_prob
								__UpperCamelCase			:     List[str]         =      attention_probs_dropout_prob
								__UpperCamelCase			:     int         =      drop_path_rate
								__UpperCamelCase			:     List[Any]         =      hidden_act
								__UpperCamelCase			:     Optional[Any]         =      use_absolute_embeddings
								__UpperCamelCase			:     Optional[int]         =      patch_norm
								__UpperCamelCase			:     List[str]         =      layer_norm_eps
								__UpperCamelCase			:     int         =      initializer_range
								__UpperCamelCase			:     Dict         =      is_training
								__UpperCamelCase			:     Tuple         =      scope
								__UpperCamelCase			:     Optional[Any]         =      use_labels
								__UpperCamelCase			:     int         =      type_sequence_label_size
								__UpperCamelCase			:     str         =      encoder_stride
								__UpperCamelCase			:     Dict         =      out_features
								__UpperCamelCase			:     Union[str, Any]         =      out_indices







	def      _lowerCamelCase       (      self	:Optional[Any]		)	->				Dict:
								__UpperCamelCase			:     int         =      floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]		)

								__UpperCamelCase			:     List[str]         =      None
								if self.use_labels:
															__UpperCamelCase			:     str         =      ids_tensor([self.batch_size]  ,     self.type_sequence_label_size		)

								__UpperCamelCase			:     Optional[Any]         =      self.get_config()

								return config, pixel_values, labels







	def      _lowerCamelCase       (      self	:Tuple		)	->				Optional[int]:
								return FocalNetConfig(
								    image_size=self.image_size  ,     patch_size=self.patch_size  ,     num_channels=self.num_channels  ,     embed_dim=self.embed_dim  ,     hidden_sizes=self.hidden_sizes  ,     depths=self.depths  ,     num_heads=self.num_heads  ,     window_size=self.window_size  ,     mlp_ratio=self.mlp_ratio  ,     qkv_bias=self.qkv_bias  ,     hidden_dropout_prob=self.hidden_dropout_prob  ,     attention_probs_dropout_prob=self.attention_probs_dropout_prob  ,     drop_path_rate=self.drop_path_rate  ,     hidden_act=self.hidden_act  ,     use_absolute_embeddings=self.use_absolute_embeddings  ,     path_norm=self.patch_norm  ,     layer_norm_eps=self.layer_norm_eps  ,     initializer_range=self.initializer_range  ,     encoder_stride=self.encoder_stride  ,     out_features=self.out_features  ,     out_indices=self.out_indices  ,     )







	def      _lowerCamelCase       (      self	:Dict  ,     a	:Tuple  ,     a	:Union[str, Any]  ,     a	:Optional[Any]		)	->				List[Any]:
								__UpperCamelCase			:     List[str]         =      FocalNetModel(config=a		)
								model.to(a		)
								model.eval()
								__UpperCamelCase			:     Union[str, Any]         =      model(a		)

								__UpperCamelCase			:     Tuple         =      ((config.image_size // config.patch_size) ** 2) // (4 ** (len(config.depths		) - 1))
								__UpperCamelCase			:     Tuple         =      int(config.embed_dim * 2 ** (len(config.depths		) - 1)		)

								self.parent.assertEqual(result.last_hidden_state.shape  ,     (self.batch_size, expected_seq_len, expected_dim)		)







	def      _lowerCamelCase       (      self	:Union[str, Any]  ,     a	:List[Any]  ,     a	:int  ,     a	:Optional[int]		)	->				Union[str, Any]:
								__UpperCamelCase			:     Any         =      FocalNetBackbone(config=a		)
								model.to(a		)
								model.eval()
								__UpperCamelCase			:     List[Any]         =      model(a		)

								# verify feature maps
								self.parent.assertEqual(len(result.feature_maps		)  ,     len(config.out_features		)		)
								self.parent.assertListEqual(list(result.feature_maps[0].shape		)  ,     [self.batch_size, self.image_size, 8, 8]		)

								# verify channels
								self.parent.assertEqual(len(model.channels		)  ,     len(config.out_features		)		)
								self.parent.assertListEqual(model.channels  ,     config.hidden_sizes[:-1]		)

								# verify backbone works with out_features=None
								__UpperCamelCase			:     str         =      None
								__UpperCamelCase			:     List[str]         =      FocalNetBackbone(config=a		)
								model.to(a		)
								model.eval()
								__UpperCamelCase			:     Any         =      model(a		)

								# verify feature maps
								self.parent.assertEqual(len(result.feature_maps		)  ,     1		)
								self.parent.assertListEqual(list(result.feature_maps[0].shape		)  ,     [self.batch_size, self.image_size * 2, 4, 4]		)

								# verify channels
								self.parent.assertEqual(len(model.channels		)  ,     1		)
								self.parent.assertListEqual(model.channels  ,     [config.hidden_sizes[-1]]		)







	def      _lowerCamelCase       (      self	:Tuple  ,     a	:Tuple  ,     a	:int  ,     a	:str		)	->				Tuple:
								__UpperCamelCase			:     Any         =      FocalNetForMaskedImageModeling(config=a		)
								model.to(a		)
								model.eval()
								__UpperCamelCase			:     Dict         =      model(a		)
								self.parent.assertEqual(
								    result.reconstruction.shape  ,     (self.batch_size, self.num_channels, self.image_size, self.image_size)		)

								# test greyscale images
								__UpperCamelCase			:     int         =      1
								__UpperCamelCase			:     str         =      FocalNetForMaskedImageModeling(a		)
								model.to(a		)
								model.eval()

								__UpperCamelCase			:     str         =      floats_tensor([self.batch_size, 1, self.image_size, self.image_size]		)
								__UpperCamelCase			:     int         =      model(a		)
								self.parent.assertEqual(result.reconstruction.shape  ,     (self.batch_size, 1, self.image_size, self.image_size)		)







	def      _lowerCamelCase       (      self	:Dict  ,     a	:Optional[Any]  ,     a	:str  ,     a	:List[str]		)	->				Any:
								__UpperCamelCase			:     Union[str, Any]         =      self.type_sequence_label_size
								__UpperCamelCase			:     List[Any]         =      FocalNetForImageClassification(a		)
								model.to(a		)
								model.eval()
								__UpperCamelCase			:     Optional[int]         =      model(a  ,     labels=a		)
								self.parent.assertEqual(result.logits.shape  ,     (self.batch_size, self.type_sequence_label_size)		)

								# test greyscale images
								__UpperCamelCase			:     Union[str, Any]         =      1
								__UpperCamelCase			:     Any         =      FocalNetForImageClassification(a		)
								model.to(a		)
								model.eval()

								__UpperCamelCase			:     Union[str, Any]         =      floats_tensor([self.batch_size, 1, self.image_size, self.image_size]		)
								__UpperCamelCase			:     Tuple         =      model(a		)
								self.parent.assertEqual(result.logits.shape  ,     (self.batch_size, self.type_sequence_label_size)		)







	def      _lowerCamelCase       (      self	:str		)	->				List[Any]:
								__UpperCamelCase			:     Dict         =      self.prepare_config_and_inputs()

								__UpperCamelCase     ,		__UpperCamelCase     ,		__UpperCamelCase			:     List[str]         =      config_and_inputs
								__UpperCamelCase			:     Union[str, Any]         =      {"pixel_values": pixel_values}
								return config, inputs_dict




@require_torch
class 				lowerCamelCase__							(       __lowercase  ,		__lowercase  ,		unittest.TestCase):




	'''simple docstring'''




	_A			=  (
	    (
	        FocalNetModel,
	        FocalNetForImageClassification,
	        FocalNetForMaskedImageModeling,
	        FocalNetBackbone,
	    )
	    if is_torch_available()
	    else ()
	)
	_A			=  (
	    {'feature-extraction': FocalNetModel, 'image-classification': FocalNetForImageClassification}
	    if is_torch_available()
	    else {}
	)
	_A			=  False

	_A			=  False
	_A			=  False
	_A			=  False
	_A			=  False







	def      _lowerCamelCase       (      self	:Tuple		)	->				Tuple:
								__UpperCamelCase			:     Tuple         =      FocalNetModelTester(self		)
								__UpperCamelCase			:     Any         =      ConfigTester(self  ,     config_class=a  ,     embed_dim=3_7  ,     has_text_modality=a		)







	def      _lowerCamelCase       (      self	:Dict		)	->				Tuple:
								self.create_and_test_config_common_properties()
								self.config_tester.create_and_test_config_to_json_string()
								self.config_tester.create_and_test_config_to_json_file()
								self.config_tester.create_and_test_config_from_and_save_pretrained()
								self.config_tester.create_and_test_config_with_num_labels()
								self.config_tester.check_config_can_be_init_without_params()
								self.config_tester.check_config_arguments_init()







	def      _lowerCamelCase       (      self	:str		)	->				Any:
								return







	def      _lowerCamelCase       (      self	:Optional[Any]		)	->				int:
								__UpperCamelCase			:     List[Any]         =      self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_model(*a		)







	def      _lowerCamelCase       (      self	:int		)	->				Tuple:
								__UpperCamelCase			:     Optional[Any]         =      self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_backbone(*a		)







	def      _lowerCamelCase       (      self	:int		)	->				str:
								__UpperCamelCase			:     Dict         =      self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_masked_image_modeling(*a		)







	def      _lowerCamelCase       (      self	:str		)	->				List[str]:
								__UpperCamelCase			:     Optional[Any]         =      self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_image_classification(*a		)







	@unittest.skip(reason="FocalNet does not use inputs_embeds"		)
	def      _lowerCamelCase       (      self	:Any		)	->				Any:
								pass







	@unittest.skip(reason="FocalNet does not use feedforward chunking"		)
	def      _lowerCamelCase       (      self	:str		)	->				str:
								pass







	def      _lowerCamelCase       (      self	:Union[str, Any]		)	->				str:
								__UpperCamelCase     ,		__UpperCamelCase			:     int         =      self.model_tester.prepare_config_and_inputs_for_common()

								for model_class in self.all_model_classes[:-1]:
															__UpperCamelCase			:     Optional[Any]         =      model_class(a		)
															self.assertIsInstance(model.get_input_embeddings()  ,     (nn.Module)		)
															__UpperCamelCase			:     List[Any]         =      model.get_output_embeddings()
															self.assertTrue(x is None or isinstance(a  ,     nn.Linear		)		)







	def      _lowerCamelCase       (      self	:List[Any]		)	->				Any:
								__UpperCamelCase     ,		__UpperCamelCase			:     Any         =      self.model_tester.prepare_config_and_inputs_for_common()

								for model_class in self.all_model_classes[:-1]:
															__UpperCamelCase			:     Optional[Any]         =      model_class(a		)
															__UpperCamelCase			:     str         =      inspect.signature(model.forward		)
															# signature.parameters is an OrderedDict => so arg_names order is deterministic
															__UpperCamelCase			:     List[str]         =      [*signature.parameters.keys()]

															__UpperCamelCase			:     Optional[int]         =      ["pixel_values"]
															self.assertListEqual(arg_names[:1]  ,     a		)







	def      _lowerCamelCase       (      self	:Any  ,     a	:Optional[Any]  ,     a	:Union[str, Any]  ,     a	:Optional[Any]  ,     a	:str		)	->				List[str]:
								__UpperCamelCase			:     List[Any]         =      model_class(a		)
								model.to(a		)
								model.eval()

								with torch.no_grad():
															__UpperCamelCase			:     int         =      model(**self._prepare_for_class(a  ,     a		)		)

								__UpperCamelCase			:     str         =      outputs.hidden_states

								__UpperCamelCase			:     Union[str, Any]         =      getattr(
								    self.model_tester  ,     "expected_num_hidden_layers"  ,     len(self.model_tester.depths		) + 1		)
								self.assertEqual(len(a		)  ,     a		)

								# FocalNet has a different seq_length
								__UpperCamelCase			:     Any         =      (
								    config.patch_size
								    if isinstance(config.patch_size  ,     collections.abc.Iterable		)
								    else (config.patch_size, config.patch_size)
								)

								__UpperCamelCase			:     List[Any]         =      (image_size[1] // patch_size[1]) * (image_size[0] // patch_size[0])

								self.assertListEqual(
								    list(hidden_states[0].shape[-2:]		)  ,     [num_patches, self.model_tester.embed_dim]  ,     )

								__UpperCamelCase			:     Optional[Any]         =      outputs.reshaped_hidden_states
								self.assertEqual(len(a		)  ,     a		)

								__UpperCamelCase     ,		__UpperCamelCase     ,		__UpperCamelCase     ,		__UpperCamelCase			:     List[Any]         =      reshaped_hidden_states[0].shape
								__UpperCamelCase			:     Any         =      (
								    reshaped_hidden_states[0].view(a  ,     a  ,     height * width		).permute(0  ,     2  ,     1		)
								)
								self.assertListEqual(
								    list(reshaped_hidden_states.shape[-2:]		)  ,     [num_patches, self.model_tester.embed_dim]  ,     )







	def      _lowerCamelCase       (      self	:Dict		)	->				Optional[int]:
								__UpperCamelCase     ,		__UpperCamelCase			:     Optional[Any]         =      self.model_tester.prepare_config_and_inputs_for_common()

								__UpperCamelCase			:     Any         =      (
								    self.model_tester.image_size
								    if isinstance(self.model_tester.image_size  ,     collections.abc.Iterable		)
								    else (self.model_tester.image_size, self.model_tester.image_size)
								)

								for model_class in self.all_model_classes[:-1]:
															__UpperCamelCase			:     str         =      True
															self.check_hidden_states_output(a  ,     a  ,     a  ,     a		)

															# check that output_hidden_states also work using config
															del inputs_dict["output_hidden_states"]
															__UpperCamelCase			:     int         =      True

															self.check_hidden_states_output(a  ,     a  ,     a  ,     a		)







	def      _lowerCamelCase       (      self	:List[Any]		)	->				Tuple:
								__UpperCamelCase     ,		__UpperCamelCase			:     Tuple         =      self.model_tester.prepare_config_and_inputs_for_common()
								__UpperCamelCase			:     Any         =      3

								__UpperCamelCase			:     List[Any]         =      (
								    self.model_tester.image_size
								    if isinstance(self.model_tester.image_size  ,     collections.abc.Iterable		)
								    else (self.model_tester.image_size, self.model_tester.image_size)
								)
								__UpperCamelCase			:     List[str]         =      (
								    config.patch_size
								    if isinstance(config.patch_size  ,     collections.abc.Iterable		)
								    else (config.patch_size, config.patch_size)
								)

								__UpperCamelCase			:     Union[str, Any]         =      image_size[0] + patch_size[0] - (image_size[0] % patch_size[0])
								__UpperCamelCase			:     List[Any]         =      image_size[1] + patch_size[1] - (image_size[1] % patch_size[1])

								for model_class in self.all_model_classes[:-1]:
															__UpperCamelCase			:     str         =      True
															self.check_hidden_states_output(a  ,     a  ,     a  ,     (padded_height, padded_width)		)

															# check that output_hidden_states also work using config
															del inputs_dict["output_hidden_states"]
															__UpperCamelCase			:     Union[str, Any]         =      True
															self.check_hidden_states_output(a  ,     a  ,     a  ,     (padded_height, padded_width)		)







	@slow
	def      _lowerCamelCase       (      self	:Dict		)	->				Optional[int]:
								for model_name in FOCALNET_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
															__UpperCamelCase			:     Tuple         =      FocalNetModel.from_pretrained(a		)
															self.assertIsNotNone(a		)







	def      _lowerCamelCase       (      self	:Tuple		)	->				List[str]:
								__UpperCamelCase     ,		__UpperCamelCase			:     List[str]         =      self.model_tester.prepare_config_and_inputs_for_common()

								__UpperCamelCase			:     Tuple         =      _config_zero_init(a		)
								for model_class in self.all_model_classes:
															__UpperCamelCase			:     int         =      model_class(config=a		)
															for name, param in model.named_parameters():
																						if "embeddings" not in name and param.requires_grad:
																													self.assertIn(
																													    ((param.data.mean() * 1E9).round() / 1E9).item()  ,     [0.0, 1.0]  ,     msg=f'Parameter {name} of model {model_class} seems not properly initialized'  ,     )




@require_vision
@require_torch
class 				lowerCamelCase__							(       unittest.TestCase):




	'''simple docstring'''




	@cached_property
	def      _lowerCamelCase       (      self	:Tuple		)	->				List[str]:
								# TODO update organization
								return AutoImageProcessor.from_pretrained("microsoft/focalnet-tiny"		) if is_vision_available() else None







	@slow
	def      _lowerCamelCase       (      self	:int		)	->				Optional[Any]:
								__UpperCamelCase			:     str         =      FocalNetForImageClassification.from_pretrained("microsoft/focalnet-tiny"		).to(a		)
								__UpperCamelCase			:     Tuple         =      self.default_image_processor

								__UpperCamelCase			:     List[Any]         =      Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"		)
								__UpperCamelCase			:     int         =      image_processor(images=a  ,     return_tensors="pt"		).to(a		)

								# forward pass
								with torch.no_grad():
															__UpperCamelCase			:     Any         =      model(**a		)

								# verify the logits
								__UpperCamelCase			:     Any         =      torch.Size((1, 1_0_0_0)		)
								self.assertEqual(outputs.logits.shape  ,     a		)
								__UpperCamelCase			:     List[Any]         =      torch.tensor([0.2166, -0.4368, 0.2191]		).to(a		)
								self.assertTrue(torch.allclose(outputs.logits[0, :3]  ,     a  ,     atol=1E-4		)		)
								self.assertTrue(outputs.logits.argmax(dim=-1		).item()  ,     2_8_1		)




@require_torch
class 				lowerCamelCase__							(       __lowercase  ,		unittest.TestCase):




	'''simple docstring'''




	_A			=  (FocalNetBackbone,) if is_torch_available() else ()
	_A			=  FocalNetConfig

	_A			=  False







	def      _lowerCamelCase       (      self	:Tuple		)	->				Optional[int]:
								__UpperCamelCase			:     Optional[Any]         =      FocalNetModelTester(self		)

from .constants import (
    MODEL_NAME,
    OPTIMIZER_NAME,
    RNG_STATE_NAME,
    SAFE_WEIGHTS_INDEX_NAME,
    SAFE_WEIGHTS_NAME,
    SCALER_NAME,
    SCHEDULER_NAME,
    TORCH_LAUNCH_PARAMS,
    WEIGHTS_INDEX_NAME,
    WEIGHTS_NAME,
)
from .dataclasses import (
    BnbQuantizationConfig,
    ComputeEnvironment,
    CustomDtype,
    DeepSpeedPlugin,
    DistributedDataParallelKwargs,
    DistributedType,
    DynamoBackend,
    FPaRecipeKwargs,
    FullyShardedDataParallelPlugin,
    GradientAccumulationPlugin,
    GradScalerKwargs,
    InitProcessGroupKwargs,
    KwargsHandler,
    LoggerType,
    MegatronLMPlugin,
    PrecisionType,
    ProjectConfiguration,
    RNGType,
    SageMakerDistributedType,
    TensorInformation,
    TorchDynamoPlugin,
)
from .environment import get_int_from_env, parse_choice_from_env, parse_flag_from_env
from .imports import (
    get_ccl_version,
    is_abit_bnb_available,
    is_abit_bnb_available,
    is_aim_available,
    is_bfaa_available,
    is_bnb_available,
    is_botoa_available,
    is_ccl_available,
    is_comet_ml_available,
    is_datasets_available,
    is_deepspeed_available,
    is_fpa_available,
    is_ipex_available,
    is_megatron_lm_available,
    is_mlflow_available,
    is_mps_available,
    is_npu_available,
    is_rich_available,
    is_safetensors_available,
    is_sagemaker_available,
    is_tensorboard_available,
    is_tpu_available,
    is_transformers_available,
    is_wandb_available,
    is_xpu_available,
)
from .modeling import (
    check_device_map,
    check_tied_parameters_in_config,
    check_tied_parameters_on_same_device,
    compute_module_sizes,
    convert_file_size_to_int,
    dtype_byte_size,
    find_tied_parameters,
    get_balanced_memory,
    get_max_layer_size,
    get_max_memory,
    get_mixed_precision_context_manager,
    id_tensor_storage,
    infer_auto_device_map,
    load_checkpoint_in_model,
    load_offloaded_weights,
    load_state_dict,
    named_module_tensors,
    retie_parameters,
    set_module_tensor_to_device,
    shard_checkpoint,
)
from .offload import (
    OffloadedWeightsLoader,
    PrefixedDataset,
    extract_submodules_state_dict,
    load_offloaded_weight,
    offload_state_dict,
    offload_weight,
    save_offload_index,
)
from .operations import (
    broadcast,
    broadcast_object_list,
    concatenate,
    convert_outputs_to_fpaa,
    convert_to_fpaa,
    find_batch_size,
    find_device,
    gather,
    gather_object,
    get_data_structure,
    honor_type,
    initialize_tensors,
    is_namedtuple,
    is_tensor_information,
    is_torch_tensor,
    listify,
    pad_across_processes,
    recursively_apply,
    reduce,
    send_to_device,
    slice_tensors,
)
from .versions import compare_versions, is_torch_version


if is_deepspeed_available():
		from .deepspeed import (
		    DeepSpeedEngineWrapper,
		    DeepSpeedOptimizerWrapper,
		    DeepSpeedSchedulerWrapper,
		    DummyOptim,
		    DummyScheduler,
		    HfDeepSpeedConfig,
		)

from .bnb import has_abit_bnb_layers, load_and_quantize_model
from .fsdp_utils import load_fsdp_model, load_fsdp_optimizer, save_fsdp_model, save_fsdp_optimizer
from .launch import (
    PrepareForLaunch,
    _filter_args,
    prepare_deepspeed_cmd_env,
    prepare_multi_gpu_env,
    prepare_sagemager_args_inputs,
    prepare_simple_launcher_cmd_env,
    prepare_tpu,
)
from .megatron_lm import (
    AbstractTrainStep,
    BertTrainStep,
    GPTTrainStep,
    MegatronEngine,
    MegatronLMDummyDataLoader,
    MegatronLMDummyScheduler,
    MegatronLMOptimizerWrapper,
    MegatronLMSchedulerWrapper,
    TaTrainStep,
    avg_losses_across_data_parallel_group,
    gather_across_data_parallel_groups,
)
from .megatron_lm import initialize as megatron_lm_initialize
from .megatron_lm import prepare_data_loader as megatron_lm_prepare_data_loader
from .megatron_lm import prepare_model as megatron_lm_prepare_model
from .megatron_lm import prepare_optimizer as megatron_lm_prepare_optimizer
from .megatron_lm import prepare_scheduler as megatron_lm_prepare_scheduler
from .memory import find_executable_batch_size, release_memory
from .other import (
    extract_model_from_parallel,
    get_pretty_name,
    is_port_in_use,
    merge_dicts,
    patch_environment,
    save,
    wait_for_everyone,
    write_basic_config,
)
from .random import set_seed, synchronize_rng_state, synchronize_rng_states
from .torch_xla import install_xla
from .tqdm import tqdm
from .transformer_engine import convert_model, has_transformer_engine_layers