


import posixpath
from pathlib import Path
from unittest.mock import patch

import pytest
from fsspec.implementations.local import AbstractFileSystem, LocalFileSystem, stringify_path
from fsspec.registry import _registry as _fsspec_registry







class 					snake_case__(				UpperCAmelCase__				):







						'''simple docstring'''




						SCREAMING_SNAKE_CASE__   :	Tuple       =				"""mock"""




						def __init__(					self ,    *__lowercase ,    __lowercase ,    **__lowercase				)       ->     Dict:
										super().__init__()
										lowerCAmelCase_			:   Dict     					=					LocalFileSystem(*__lowercase ,    **__lowercase				)
										lowerCAmelCase_			:   int     					=					Path(__lowercase				).resolve().as_posix() + '''/'''




						def     lowercase_			(					self ,    __lowercase ,    *__lowercase ,    **__lowercase				)       ->     Optional[int]:
										lowerCAmelCase_			:   List[str]     					=					posixpath.join(self.local_root_dir ,    self._strip_protocol(__lowercase				)				)
										return self._fs.mkdir(__lowercase ,    *__lowercase ,    **__lowercase				)




						def     lowercase_			(					self ,    __lowercase ,    *__lowercase ,    **__lowercase				)       ->     Any:
										lowerCAmelCase_			:   Union[str, Any]     					=					posixpath.join(self.local_root_dir ,    self._strip_protocol(__lowercase				)				)
										return self._fs.makedirs(__lowercase ,    *__lowercase ,    **__lowercase				)




						def     lowercase_			(					self ,    __lowercase				)       ->     Tuple:
										lowerCAmelCase_			:   int     					=					posixpath.join(self.local_root_dir ,    self._strip_protocol(__lowercase				)				)
										return self._fs.rmdir(__lowercase				)




						def     lowercase_			(					self ,    __lowercase ,    __lowercase=True ,    *__lowercase ,    **__lowercase				)       ->     int:
										lowerCAmelCase_			:   int     					=					posixpath.join(self.local_root_dir ,    self._strip_protocol(__lowercase				)				)
										lowerCAmelCase_			:   List[str]     					=					self._fs.ls(__lowercase ,    detail=__lowercase ,    *__lowercase ,    **__lowercase				)
										if detail:
														return [{**info, "name": info["name"][len(self.local_root_dir				) :]} for info in out]
										else:
														return [name[len(self.local_root_dir				) :] for name in out]




						def     lowercase_			(					self ,    __lowercase ,    *__lowercase ,    **__lowercase				)       ->     str:
										lowerCAmelCase_			:   int     					=					posixpath.join(self.local_root_dir ,    self._strip_protocol(__lowercase				)				)
										lowerCAmelCase_			:   int     					=					dict(self._fs.info(__lowercase ,    *__lowercase ,    **__lowercase				)				)
										lowerCAmelCase_			:   List[Any]     					=					out['''name'''][len(self.local_root_dir				) :]
										return out




						def     lowercase_			(					self ,    __lowercase ,    __lowercase ,    *__lowercase ,    **__lowercase				)       ->     str:
										lowerCAmelCase_			:   int     					=					posixpath.join(self.local_root_dir ,    self._strip_protocol(__lowercase				)				)
										lowerCAmelCase_			:   List[str]     					=					posixpath.join(self.local_root_dir ,    self._strip_protocol(__lowercase				)				)
										return self._fs.cp_file(__lowercase ,    __lowercase ,    *__lowercase ,    **__lowercase				)




						def     lowercase_			(					self ,    __lowercase ,    *__lowercase ,    **__lowercase				)       ->     Dict:
										lowerCAmelCase_			:   Optional[int]     					=					posixpath.join(self.local_root_dir ,    self._strip_protocol(__lowercase				)				)
										return self._fs.rm_file(__lowercase ,    *__lowercase ,    **__lowercase				)




						def     lowercase_			(					self ,    __lowercase ,    *__lowercase ,    **__lowercase				)       ->     Optional[int]:
										lowerCAmelCase_			:   str     					=					posixpath.join(self.local_root_dir ,    self._strip_protocol(__lowercase				)				)
										return self._fs.rm(__lowercase ,    *__lowercase ,    **__lowercase				)




						def     lowercase_			(					self ,    __lowercase ,    *__lowercase ,    **__lowercase				)       ->     Optional[int]:
										lowerCAmelCase_			:   Union[str, Any]     					=					posixpath.join(self.local_root_dir ,    self._strip_protocol(__lowercase				)				)
										return self._fs._open(__lowercase ,    *__lowercase ,    **__lowercase				)




						def     lowercase_			(					self ,    __lowercase				)       ->     int:
										lowerCAmelCase_			:   List[str]     					=					posixpath.join(self.local_root_dir ,    self._strip_protocol(__lowercase				)				)
										return self._fs.created(__lowercase				)




						def     lowercase_			(					self ,    __lowercase				)       ->     Dict:
										lowerCAmelCase_			:   List[str]     					=					posixpath.join(self.local_root_dir ,    self._strip_protocol(__lowercase				)				)
										return self._fs.modified(__lowercase				)




						@classmethod
						def     lowercase_			(					cls ,    __lowercase				)       ->     str:
										lowerCAmelCase_			:   Dict     					=					stringify_path(__lowercase				)
										if path.startswith('''mock://'''				):
														lowerCAmelCase_			:   int     					=					path[7:]
										return path







class 					snake_case__(				UpperCAmelCase__				):







						'''simple docstring'''




						SCREAMING_SNAKE_CASE__   :	Union[str, Any]       =				"""tmp"""
						SCREAMING_SNAKE_CASE__   :	str       =				None




						def __init__(					self ,    *__lowercase ,    **__lowercase				)       ->     Dict:
										assert self.tmp_dir is not None, "TmpDirFileSystem.tmp_dir is not set"
										super().__init__(*__lowercase ,    **__lowercase ,    local_root_dir=self.tmp_dir ,    auto_mkdir=__lowercase				)




						@classmethod
						def     lowercase_			(					cls ,    __lowercase				)       ->     Tuple:
										lowerCAmelCase_			:   Any     					=					stringify_path(__lowercase				)
										if path.startswith('''tmp://'''				):
														lowerCAmelCase_			:   List[str]     					=					path[6:]
										return path






@pytest.fixture
def       lowerCAmelCase			(		)->			Optional[int]:
				lowerCAmelCase_			:   str     					=					MockFileSystem
				lowerCAmelCase_			:   List[str]     					=					TmpDirFileSystem
				yield
				del _fsspec_registry["mock"]
				del _fsspec_registry["tmp"]



@pytest.fixture
def       lowerCAmelCase			(		lowerCAmelCase_			, lowerCAmelCase_			)->			List[Any]:
				lowerCAmelCase_			:   str     					=					tmp_path_factory.mktemp('''mockfs'''			)
				return MockFileSystem(local_root_dir=lowerCAmelCase_			, auto_mkdir=lowerCAmelCase_			)




@pytest.fixture
def       lowerCAmelCase			(		lowerCAmelCase_			, lowerCAmelCase_			)->			Dict:
				lowerCAmelCase_			:   List[str]     					=					tmp_path_factory.mktemp('''tmpfs'''			)
				with patch.object(lowerCAmelCase_			, '''tmp_dir'''			, lowerCAmelCase_			):
								yield TmpDirFileSystem()



import torch.nn as nn

from .imports import is_fpa_available


if is_fpa_available():
						import transformer_engine.pytorch as te



def       lowerCAmelCase			(		lowerCAmelCase_			, lowerCAmelCase_=True			, lowerCAmelCase_=True			, lowerCAmelCase_=True			)->			Union[str, Any]:
				if not is_fpa_available():
								raise ImportError('''Using `convert_model` requires transformer_engine to be installed.'''			)
				for name, module in model.named_children():
								if isinstance(lowerCAmelCase_			, nn.Linear			) and to_transformer_engine and _convert_linear:
												# Return early if the linear layer weights are not multiples of 16
												if any(p % 16 != 0 for p in module.weight.shape			):
																return
												lowerCAmelCase_			:   Optional[int]     					=					module.bias is not None
												lowerCAmelCase_			:   Any     					=					te.Linear(
												    module.in_features			, module.out_features			, bias=lowerCAmelCase_			, params_dtype=module.weight.dtype			)
												lowerCAmelCase_			:   Dict     					=					module.weight.data.clone()
												if has_bias:
																lowerCAmelCase_			:   Any     					=					module.bias.data.clone()

												setattr(lowerCAmelCase_			, lowerCAmelCase_			, lowerCAmelCase_			)
								elif isinstance(lowerCAmelCase_			, nn.LayerNorm			) and to_transformer_engine and _convert_ln:
												lowerCAmelCase_			:   List[str]     					=					te.LayerNorm(module.normalized_shape[0]			, eps=module.eps			, params_dtype=module.weight.dtype			)
												lowerCAmelCase_			:   Union[str, Any]     					=					module.weight.data.clone()
												lowerCAmelCase_			:   List[Any]     					=					module.bias.data.clone()

												setattr(lowerCAmelCase_			, lowerCAmelCase_			, lowerCAmelCase_			)
								elif isinstance(lowerCAmelCase_			, te.Linear			) and not to_transformer_engine and _convert_linear:
												lowerCAmelCase_			:   Dict     					=					module.bias is not None
												lowerCAmelCase_			:   Union[str, Any]     					=					nn.Linear(
												    module.in_features			, module.out_features			, bias=lowerCAmelCase_			, params_dtype=module.weight.dtype			)
												lowerCAmelCase_			:   Optional[Any]     					=					module.weight.data.clone()
												if has_bias:
																lowerCAmelCase_			:   Union[str, Any]     					=					module.bias.data.clone()

												setattr(lowerCAmelCase_			, lowerCAmelCase_			, lowerCAmelCase_			)
								elif isinstance(lowerCAmelCase_			, te.LayerNorm			) and not to_transformer_engine and _convert_ln:
												lowerCAmelCase_			:   Union[str, Any]     					=					nn.LayerNorm(module.normalized_shape[0]			, eps=module.eps			, params_dtype=module.weight.dtype			)
												lowerCAmelCase_			:   List[str]     					=					module.weight.data.clone()
												lowerCAmelCase_			:   List[Any]     					=					module.bias.data.clone()

												setattr(lowerCAmelCase_			, lowerCAmelCase_			, lowerCAmelCase_			)
								else:
												convert_model(
												    lowerCAmelCase_			, to_transformer_engine=lowerCAmelCase_			, _convert_linear=_convert_linear			, _convert_ln=_convert_ln			, )




def       lowerCAmelCase			(		lowerCAmelCase_			)->			str:
				if not is_fpa_available():
								raise ImportError('''Using `has_transformer_engine_layers` requires transformer_engine to be installed.'''			)
				for m in model.modules():
								if isinstance(lowerCAmelCase_			, (te.LayerNorm, te.Linear)			):
												return True
				return False