


import os

import torch

from ..logging import get_logger
from .constants import FSDP_PYTORCH_VERSION, MODEL_NAME, OPTIMIZER_NAME
from .versions import is_torch_version


if is_torch_version(""">=""", FSDP_PYTORCH_VERSION):
			import torch.distributed.checkpoint as dist_cp
			from torch.distributed.checkpoint.default_planner import DefaultLoadPlanner, DefaultSavePlanner
			from torch.distributed.checkpoint.optimizer import load_sharded_optimizer_state_dict
			from torch.distributed.fsdp.fully_sharded_data_parallel import FullyShardedDataParallel as FSDP
			from torch.distributed.fsdp.fully_sharded_data_parallel import StateDictType


UpperCAmelCase    =					get_logger(__name__)






def  __lowerCAmelCase					(SCREAMING_SNAKE_CASE      ,      SCREAMING_SNAKE_CASE      ,      SCREAMING_SNAKE_CASE      ,      SCREAMING_SNAKE_CASE      ,      SCREAMING_SNAKE_CASE=0      )->						Tuple:




						"""simple docstring"""

						os.makedirs(SCREAMING_SNAKE_CASE      ,      exist_ok=SCREAMING_SNAKE_CASE      )
						with FSDP.state_dict_type(
						    SCREAMING_SNAKE_CASE      ,      fsdp_plugin.state_dict_type      ,      fsdp_plugin.state_dict_config      ,      fsdp_plugin.optim_state_dict_config      ):
												snake_case_ =   model.state_dict()
												if fsdp_plugin.state_dict_type == StateDictType.FULL_STATE_DICT:
																		snake_case_ =   f'''{MODEL_NAME}.bin''' if model_index == 0 else f'''{MODEL_NAME}_{model_index}.bin'''
																		snake_case_ =   os.path.join(SCREAMING_SNAKE_CASE      ,      SCREAMING_SNAKE_CASE      )
																		if accelerator.process_index == 0:
																								logger.info(f'''Saving model to {output_model_file}'''      )
																								torch.save(SCREAMING_SNAKE_CASE      ,      SCREAMING_SNAKE_CASE      )
																								logger.info(f'''Model saved to {output_model_file}'''      )
												elif fsdp_plugin.state_dict_type == StateDictType.LOCAL_STATE_DICT:
																		snake_case_ =   (
																		    f'''{MODEL_NAME}_rank{accelerator.process_index}.bin'''
																		    if model_index == 0
																		    else f'''{MODEL_NAME}_{model_index}_rank{accelerator.process_index}.bin'''
																		)
																		snake_case_ =   os.path.join(SCREAMING_SNAKE_CASE      ,      SCREAMING_SNAKE_CASE      )
																		logger.info(f'''Saving model to {output_model_file}'''      )
																		torch.save(SCREAMING_SNAKE_CASE      ,      SCREAMING_SNAKE_CASE      )
																		logger.info(f'''Model saved to {output_model_file}'''      )
												elif fsdp_plugin.state_dict_type == StateDictType.SHARDED_STATE_DICT:
																		snake_case_ =   os.path.join(SCREAMING_SNAKE_CASE      ,      f'''{MODEL_NAME}_{model_index}'''      )
																		os.makedirs(SCREAMING_SNAKE_CASE      ,      exist_ok=SCREAMING_SNAKE_CASE      )
																		logger.info(f'''Saving model to {ckpt_dir}'''      )
																		snake_case_ =   {'''model''': state_dict}

																		dist_cp.save_state_dict(
																		    state_dict=SCREAMING_SNAKE_CASE      ,      storage_writer=dist_cp.FileSystemWriter(SCREAMING_SNAKE_CASE      )      ,      planner=DefaultSavePlanner()      ,      )
																		logger.info(f'''Model saved to {ckpt_dir}'''      )






def  __lowerCAmelCase					(SCREAMING_SNAKE_CASE      ,      SCREAMING_SNAKE_CASE      ,      SCREAMING_SNAKE_CASE      ,      SCREAMING_SNAKE_CASE      ,      SCREAMING_SNAKE_CASE=0      )->						int:




						"""simple docstring"""

						accelerator.wait_for_everyone()
						with FSDP.state_dict_type(
						    SCREAMING_SNAKE_CASE      ,      fsdp_plugin.state_dict_type      ,      fsdp_plugin.state_dict_config      ,      fsdp_plugin.optim_state_dict_config      ):
												if fsdp_plugin.state_dict_type == StateDictType.FULL_STATE_DICT:
																		if type(SCREAMING_SNAKE_CASE      ) != FSDP and accelerator.process_index != 0:
																								if not fsdp_plugin.sync_module_states:
																														raise ValueError(
																														    '''Set the `sync_module_states` flag to `True` so that model states are synced across processes when '''
																														    '''initializing FSDP object'''      )
																								return
																		snake_case_ =   f'''{MODEL_NAME}.bin''' if model_index == 0 else f'''{MODEL_NAME}_{model_index}.bin'''
																		snake_case_ =   os.path.join(SCREAMING_SNAKE_CASE      ,      SCREAMING_SNAKE_CASE      )
																		logger.info(f'''Loading model from {input_model_file}'''      )
																		snake_case_ =   torch.load(SCREAMING_SNAKE_CASE      )
																		logger.info(f'''Model loaded from {input_model_file}'''      )
												elif fsdp_plugin.state_dict_type == StateDictType.LOCAL_STATE_DICT:
																		snake_case_ =   (
																		    f'''{MODEL_NAME}_rank{accelerator.process_index}.bin'''
																		    if model_index == 0
																		    else f'''{MODEL_NAME}_{model_index}_rank{accelerator.process_index}.bin'''
																		)
																		snake_case_ =   os.path.join(SCREAMING_SNAKE_CASE      ,      SCREAMING_SNAKE_CASE      )
																		logger.info(f'''Loading model from {input_model_file}'''      )
																		snake_case_ =   torch.load(SCREAMING_SNAKE_CASE      )
																		logger.info(f'''Model loaded from {input_model_file}'''      )
												elif fsdp_plugin.state_dict_type == StateDictType.SHARDED_STATE_DICT:
																		snake_case_ =   (
																		    os.path.join(SCREAMING_SNAKE_CASE      ,      f'''{MODEL_NAME}_{model_index}'''      )
																		    if f'''{MODEL_NAME}''' not in input_dir
																		    else input_dir
																		)
																		logger.info(f'''Loading model from {ckpt_dir}'''      )
																		snake_case_ =   {'''model''': model.state_dict()}
																		dist_cp.load_state_dict(
																		    state_dict=SCREAMING_SNAKE_CASE      ,      storage_reader=dist_cp.FileSystemReader(SCREAMING_SNAKE_CASE      )      ,      planner=DefaultLoadPlanner()      ,      )
																		snake_case_ =   state_dict['''model''']
																		logger.info(f'''Model loaded from {ckpt_dir}'''      )
												model.load_state_dict(SCREAMING_SNAKE_CASE      )






def  __lowerCAmelCase					(SCREAMING_SNAKE_CASE      ,      SCREAMING_SNAKE_CASE      ,      SCREAMING_SNAKE_CASE      ,      SCREAMING_SNAKE_CASE      ,      SCREAMING_SNAKE_CASE      ,      SCREAMING_SNAKE_CASE=0      )->						List[str]:




						"""simple docstring"""

						os.makedirs(SCREAMING_SNAKE_CASE      ,      exist_ok=SCREAMING_SNAKE_CASE      )
						with FSDP.state_dict_type(
						    SCREAMING_SNAKE_CASE      ,      fsdp_plugin.state_dict_type      ,      fsdp_plugin.state_dict_config      ,      fsdp_plugin.optim_state_dict_config      ):
												snake_case_ =   FSDP.optim_state_dict(SCREAMING_SNAKE_CASE      ,      SCREAMING_SNAKE_CASE      )
												if fsdp_plugin.state_dict_type == StateDictType.FULL_STATE_DICT:
																		if accelerator.process_index == 0:
																								snake_case_ =   (
																								    f'''{OPTIMIZER_NAME}.bin''' if optimizer_index == 0 else f'''{OPTIMIZER_NAME}_{optimizer_index}.bin'''
																								)
																								snake_case_ =   os.path.join(SCREAMING_SNAKE_CASE      ,      SCREAMING_SNAKE_CASE      )
																								logger.info(f'''Saving Optimizer state to {output_optimizer_file}'''      )
																								torch.save(SCREAMING_SNAKE_CASE      ,      SCREAMING_SNAKE_CASE      )
																								logger.info(f'''Optimizer state saved in {output_optimizer_file}'''      )
												else:
																		snake_case_ =   os.path.join(SCREAMING_SNAKE_CASE      ,      f'''{OPTIMIZER_NAME}_{optimizer_index}'''      )
																		os.makedirs(SCREAMING_SNAKE_CASE      ,      exist_ok=SCREAMING_SNAKE_CASE      )
																		logger.info(f'''Saving Optimizer state to {ckpt_dir}'''      )
																		dist_cp.save_state_dict(
																		    state_dict={'''optimizer''': optim_state}      ,      storage_writer=dist_cp.FileSystemWriter(SCREAMING_SNAKE_CASE      )      ,      planner=DefaultSavePlanner()      ,      )
																		logger.info(f'''Optimizer state saved in {ckpt_dir}'''      )






def  __lowerCAmelCase					(SCREAMING_SNAKE_CASE      ,      SCREAMING_SNAKE_CASE      ,      SCREAMING_SNAKE_CASE      ,      SCREAMING_SNAKE_CASE      ,      SCREAMING_SNAKE_CASE      ,      SCREAMING_SNAKE_CASE=0      )->						Any:




						"""simple docstring"""

						accelerator.wait_for_everyone()
						with FSDP.state_dict_type(
						    SCREAMING_SNAKE_CASE      ,      fsdp_plugin.state_dict_type      ,      fsdp_plugin.state_dict_config      ,      fsdp_plugin.optim_state_dict_config      ):
												if fsdp_plugin.state_dict_type == StateDictType.FULL_STATE_DICT:
																		snake_case_ =   None
																		# below check should work but currently it isn't working (mostly opytorch issue),
																		# in the meantime disabling it at the cost of excess memory usage
																		# if accelerator.process_index == 0 or not fsdp_plugin.optim_state_dict_config.rank0_only:
																		snake_case_ =   (
																		    f'''{OPTIMIZER_NAME}.bin''' if optimizer_index == 0 else f'''{OPTIMIZER_NAME}_{optimizer_index}.bin'''
																		)
																		snake_case_ =   os.path.join(SCREAMING_SNAKE_CASE      ,      SCREAMING_SNAKE_CASE      )
																		logger.info(f'''Loading Optimizer state from {input_optimizer_file}'''      )
																		snake_case_ =   torch.load(SCREAMING_SNAKE_CASE      )
																		logger.info(f'''Optimizer state loaded from {input_optimizer_file}'''      )
												else:
																		snake_case_ =   (
																		    os.path.join(SCREAMING_SNAKE_CASE      ,      f'''{OPTIMIZER_NAME}_{optimizer_index}'''      )
																		    if f'''{OPTIMIZER_NAME}''' not in input_dir
																		    else input_dir
																		)
																		logger.info(f'''Loading Optimizer from {ckpt_dir}'''      )
																		snake_case_ =   load_sharded_optimizer_state_dict(
																		    model_state_dict=model.state_dict()      ,      optimizer_key='''optimizer'''      ,      storage_reader=dist_cp.FileSystemReader(SCREAMING_SNAKE_CASE      )      ,      )
																		snake_case_ =   optim_state['''optimizer''']
																		logger.info(f'''Optimizer loaded from {ckpt_dir}'''      )
												snake_case_ =   FSDP.optim_state_dict_to_load(SCREAMING_SNAKE_CASE      ,      SCREAMING_SNAKE_CASE      ,      SCREAMING_SNAKE_CASE      )
												optimizer.load_state_dict(SCREAMING_SNAKE_CASE      )



from __future__ import annotations

import json

import requests
from bsa import BeautifulSoup
from fake_useragent import UserAgent

UpperCAmelCase    =					{"""UserAgent""": UserAgent().random}






def  __lowerCAmelCase					(SCREAMING_SNAKE_CASE      )->						dict:




      """simple docstring"""

      snake_case_ =   script.contents[0]
      snake_case_ =   json.loads(data[data.find('''{"config"'''      ) : -1]      )
      return info["entry_data"]["ProfilePage"][0]["graphql"]["user"]







class lowerCAmelCase_		:

 '''simple docstring'''

 def __init__(			self	,   _UpperCAmelCase      ):
       snake_case_ =   F'''https://www.instagram.com/{username}/'''
       snake_case_ =   self.get_json()



 def 		UpperCamelCase__  (			self      ):
       snake_case_ =   requests.get(self.url	,   headers=_UpperCAmelCase      ).text
       snake_case_ =   BeautifulSoup(_UpperCAmelCase	,   '''html.parser'''      ).find_all('''script'''      )
       try:
             return extract_user_profile(scripts[4]      )
       except (json.decoder.JSONDecodeError, KeyError):
             return extract_user_profile(scripts[3]      )



 def __repr__(			self      ):
       return F'''{self.__class__.__name__}(\'{self.username}\')'''



 def __str__(			self      ):
       return F'''{self.fullname} ({self.username}) is {self.biography}'''



 @property
 def 		UpperCamelCase__  (			self      ):
       return self.user_data["username"]



 @property
 def 		UpperCamelCase__  (			self      ):
       return self.user_data["full_name"]



 @property
 def 		UpperCamelCase__  (			self      ):
       return self.user_data["biography"]



 @property
 def 		UpperCamelCase__  (			self      ):
       return self.user_data["business_email"]



 @property
 def 		UpperCamelCase__  (			self      ):
       return self.user_data["external_url"]



 @property
 def 		UpperCamelCase__  (			self      ):
       return self.user_data["edge_followed_by"]["count"]



 @property
 def 		UpperCamelCase__  (			self      ):
       return self.user_data["edge_follow"]["count"]



 @property
 def 		UpperCamelCase__  (			self      ):
       return self.user_data["edge_owner_to_timeline_media"]["count"]



 @property
 def 		UpperCamelCase__  (			self      ):
       return self.user_data["profile_pic_url_hd"]



 @property
 def 		UpperCamelCase__  (			self      ):
       return self.user_data["is_verified"]





 @property
 def 		UpperCamelCase__  (			self      ):
       return self.user_data["is_private"]






def  __lowerCAmelCase					(SCREAMING_SNAKE_CASE = "github"      )->						None:




      """simple docstring"""

      import os

      if os.environ.get('''CI'''      ):
            return  # test failing on GitHub Actions
      snake_case_ =   InstagramUser(SCREAMING_SNAKE_CASE      )
      assert instagram_user.user_data
      assert isinstance(instagram_user.user_data      ,      SCREAMING_SNAKE_CASE      )
      assert instagram_user.username == username
      if username != "github":
            return
      assert instagram_user.fullname == "GitHub"
      assert instagram_user.biography == "Built for developers."
      assert instagram_user.number_of_posts > 150
      assert instagram_user.number_of_followers > 12_0000
      assert instagram_user.number_of_followings > 15
      assert instagram_user.email == "support@github.com"
      assert instagram_user.website == "https://github.com/readme"
      assert instagram_user.profile_picture_url.startswith('''https://instagram.'''      )
      assert instagram_user.is_verified is True
      assert instagram_user.is_private is False


if __name__ == "__main__":
   import doctest

   doctest.testmod()
   UpperCAmelCase    =					InstagramUser("""github""")
   print(instagram_user)
   print(f'''{instagram_user.number_of_posts = }''')
   print(f'''{instagram_user.number_of_followers = }''')
   print(f'''{instagram_user.number_of_followings = }''')
   print(f'''{instagram_user.email = }''')
   print(f'''{instagram_user.website = }''')
   print(f'''{instagram_user.profile_picture_url = }''')
   print(f'''{instagram_user.is_verified = }''')
   print(f'''{instagram_user.is_private = }''')