






"""simple docstring"""
import hashlib
import importlib.util
import json
import os
import pathlib
from types import ModuleType

import pytest
import requests

A_     :					Tuple							=     pathlib.Path.cwd().joinpath("project_euler")
A_     :					int							=     pathlib.Path.cwd().joinpath(
    "scripts", "project_euler_answers.json"
)

with open(PROJECT_EULER_ANSWERS_PATH) as file_handle:
      A_     :					dict[str, str]							=     json.load(file_handle)






def 			lowerCamelCase_    (		_lowerCamelCase							):
 lowerCamelCase__	:     str								=		importlib.util.spec_from_file_location(file_path.name     ,  str(_lowerCamelCase							)							)
 lowerCamelCase__	:     List[Any]								=		importlib.util.module_from_spec(_lowerCamelCase							)  # type: ignore
 spec.loader.exec_module(_lowerCamelCase							)  # type: ignore
 return module






def 			lowerCamelCase_    (		):
 lowerCamelCase__	:     Optional[Any]								=		[]
 for problem_dir_path in PROJECT_EULER_DIR_PATH.iterdir():
  if problem_dir_path.is_file() or problem_dir_path.name.startswith('_'							):
   continue
  for file_path in problem_dir_path.iterdir():
   if file_path.suffix != ".py" or file_path.name.startswith(('_', 'test')							):
    continue
   solution_file_paths.append(_lowerCamelCase							)
 return solution_file_paths






def 			lowerCamelCase_    (		):
 with open(os.environ['GITHUB_EVENT_PATH']							) as file:
  lowerCamelCase__	:     Dict								=		json.load(_lowerCamelCase							)
 return event["pull_request"]["url"] + "/files"






def 			lowerCamelCase_    (		):
 lowerCamelCase__	:     int								=		[]
 lowerCamelCase__	:     Optional[int]								=		{
     'Accept': 'application/vnd.github.v3+json',
     'Authorization': 'token ' + os.environ['GITHUB_TOKEN'],
 }
 lowerCamelCase__	:     Dict								=		requests.get(get_files_url()     ,  headers=_lowerCamelCase							).json()
 for file in files:
  lowerCamelCase__	:     List[Any]								=		pathlib.Path.cwd().joinpath(file['filename']							)
  if (
      filepath.suffix != ".py"
      or filepath.name.startswith(('_', 'test')							)
      or not filepath.name.startswith('sol'							)
  ):
   continue
  solution_file_paths.append(_lowerCamelCase							)
 return solution_file_paths






def 			lowerCamelCase_    (		):
 if os.environ.get('CI'							) and os.environ.get('GITHUB_EVENT_NAME'							) == "pull_request":
  # Return only if there are any, otherwise default to all solutions
  if filepaths := added_solution_file_path():
   return filepaths
 return all_solution_file_paths()






@pytest.mark.parametrize(
    'solution_path'     ,  collect_solution_file_paths()     ,  ids=lambda _lowerCamelCase							: f'''{path.parent.name}/{path.name}'''     ,  )
def 			lowerCamelCase_    (		_lowerCamelCase							):
 lowerCamelCase__	:     str								=		solution_path.parent.name[8:].zfill(3							)
 lowerCamelCase__	:     str								=		PROBLEM_ANSWERS[problem_number]
 lowerCamelCase__	:     Union[str, Any]								=		convert_path_to_module(_lowerCamelCase							)
 lowerCamelCase__	:     Dict								=		str(solution_module.solution()							)  # type: ignore
 lowerCamelCase__	:     Optional[Any]								=		hashlib.shaaaa(answer.encode()							).hexdigest()
 assert (
     answer == expected
 ), f'''Expected solution to {problem_number} to have hash {expected}, got {answer}'''









"""simple docstring"""
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							):
 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__	:     List[Any]								=		module.bias is not None
   lowerCamelCase__	:     Tuple								=		te.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__	:     List[str]								=		module.bias.data.clone()

   setattr(_lowerCamelCase     ,  _lowerCamelCase     ,  _lowerCamelCase							)
  elif isinstance(_lowerCamelCase     ,  nn.LayerNorm							) and to_transformer_engine and _convert_ln:
   lowerCamelCase__	:     Tuple								=		te.LayerNorm(module.normalized_shape[0]     ,  eps=module.eps     ,  params_dtype=module.weight.dtype							)
   lowerCamelCase__	:     Tuple								=		module.weight.data.clone()
   lowerCamelCase__	:     Optional[Any]								=		module.bias.data.clone()

   setattr(_lowerCamelCase     ,  _lowerCamelCase     ,  _lowerCamelCase							)
  elif isinstance(_lowerCamelCase     ,  te.Linear							) and not to_transformer_engine and _convert_linear:
   lowerCamelCase__	:     Optional[Any]								=		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__	:     str								=		module.weight.data.clone()
   if has_bias:
    lowerCamelCase__	:     Optional[int]								=		module.bias.data.clone()

   setattr(_lowerCamelCase     ,  _lowerCamelCase     ,  _lowerCamelCase							)
  elif isinstance(_lowerCamelCase     ,  te.LayerNorm							) and not to_transformer_engine and _convert_ln:
   lowerCamelCase__	:     str								=		nn.LayerNorm(module.normalized_shape[0]     ,  eps=module.eps     ,  params_dtype=module.weight.dtype							)
   lowerCamelCase__	:     Dict								=		module.weight.data.clone()
   lowerCamelCase__	:     Dict								=		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							):
 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

