





from __future__ import annotations


def        __snake_case       (						lowerCAmelCase_   ,       lowerCAmelCase_  )							->	str:
    SCREAMING_SNAKE_CASE__							,		SCREAMING_SNAKE_CASE__							,		SCREAMING_SNAKE_CASE__       							= '''''', '''''', ''''''
    SCREAMING_SNAKE_CASE__       							= []

    for character in message_part:
        tmp.append(character_to_number[character]  )

    for each in tmp:
        one += each[0]
        two += each[1]
        three += each[2]

    return one + two + three


def        __snake_case       (						lowerCAmelCase_   ,       lowerCAmelCase_  )							->	tuple[str, str, str]:
    SCREAMING_SNAKE_CASE__							,		SCREAMING_SNAKE_CASE__       							= '''''', ''''''
    SCREAMING_SNAKE_CASE__       							= []

    for character in message_part:
        this_part += character_to_number[character]

    for digit in this_part:
        tmp += digit
        if len(lowerCAmelCase_  ) == len(lowerCAmelCase_  ):
            result.append(lowerCAmelCase_  )
            SCREAMING_SNAKE_CASE__       							= ''''''

    return result[0], result[1], result[2]


def        __snake_case       (						lowerCAmelCase_   ,       lowerCAmelCase_  )							->	tuple[str, str, dict[str, str], dict[str, str]]:
    # Validate message and alphabet, set to upper and remove spaces
    SCREAMING_SNAKE_CASE__       							= alphabet.replace(''' '''   ,       ''''''  ).upper()
    SCREAMING_SNAKE_CASE__       							= message.replace(''' '''   ,       ''''''  ).upper()

    # Check length and characters
    if len(lowerCAmelCase_  ) != 2_7:
        raise KeyError('''Length of alphabet has to be 27.'''  )
    for each in message:
        if each not in alphabet:
            raise ValueError('''Each message character has to be included in alphabet!'''  )

    # Generate dictionares
    SCREAMING_SNAKE_CASE__       							= (
        '''111''',
        '''112''',
        '''113''',
        '''121''',
        '''122''',
        '''123''',
        '''131''',
        '''132''',
        '''133''',
        '''211''',
        '''212''',
        '''213''',
        '''221''',
        '''222''',
        '''223''',
        '''231''',
        '''232''',
        '''233''',
        '''311''',
        '''312''',
        '''313''',
        '''321''',
        '''322''',
        '''323''',
        '''331''',
        '''332''',
        '''333''',
    )
    SCREAMING_SNAKE_CASE__       							= {}
    SCREAMING_SNAKE_CASE__       							= {}
    for letter, number in zip(lowerCAmelCase_   ,       lowerCAmelCase_  ):
        SCREAMING_SNAKE_CASE__       							= number
        SCREAMING_SNAKE_CASE__       							= letter

    return message, alphabet, character_to_number, number_to_character


def        __snake_case       (						lowerCAmelCase_   ,       lowerCAmelCase_ = "ABCDEFGHIJKLMNOPQRSTUVWXYZ."   ,       lowerCAmelCase_ = 5  )							->	str:
    SCREAMING_SNAKE_CASE__							,		SCREAMING_SNAKE_CASE__							,		SCREAMING_SNAKE_CASE__							,		SCREAMING_SNAKE_CASE__       							= __prepare(
        lowerCAmelCase_   ,       lowerCAmelCase_  )
    SCREAMING_SNAKE_CASE__							,		SCREAMING_SNAKE_CASE__       							= '''''', ''''''

    for i in range(0   ,       len(lowerCAmelCase_  ) + 1   ,       lowerCAmelCase_  ):
        encrypted_numeric += __encrypt_part(
            message[i : i + period]   ,       lowerCAmelCase_  )

    for i in range(0   ,       len(lowerCAmelCase_  )   ,       3  ):
        encrypted += number_to_character[encrypted_numeric[i : i + 3]]

    return encrypted


def        __snake_case       (						lowerCAmelCase_   ,       lowerCAmelCase_ = "ABCDEFGHIJKLMNOPQRSTUVWXYZ."   ,       lowerCAmelCase_ = 5  )							->	str:
    SCREAMING_SNAKE_CASE__							,		SCREAMING_SNAKE_CASE__							,		SCREAMING_SNAKE_CASE__							,		SCREAMING_SNAKE_CASE__       							= __prepare(
        lowerCAmelCase_   ,       lowerCAmelCase_  )
    SCREAMING_SNAKE_CASE__       							= []
    SCREAMING_SNAKE_CASE__       							= ''''''

    for i in range(0   ,       len(lowerCAmelCase_  ) + 1   ,       lowerCAmelCase_  ):
        SCREAMING_SNAKE_CASE__							,		SCREAMING_SNAKE_CASE__							,		SCREAMING_SNAKE_CASE__       							= __decrypt_part(message[i : i + period]   ,       lowerCAmelCase_  )

        for j in range(0   ,       len(lowerCAmelCase_  )  ):
            decrypted_numeric.append(a[j] + b[j] + c[j]  )

    for each in decrypted_numeric:
        decrypted += number_to_character[each]

    return decrypted


if __name__ == "__main__":
  _A				:							Union[str, Any]							= """DEFEND THE EAST WALL OF THE CASTLE."""
  _A				:							str							= encrypt_message(msg, """EPSDUCVWYM.ZLKXNBTFGORIJHAQ""")
  _A				:							int							= decrypt_message(encrypted, """EPSDUCVWYM.ZLKXNBTFGORIJHAQ""")
  print(F'Encrypted: {encrypted}\nDecrypted: {decrypted}')







import torch.nn as nn

from .imports import is_fpa_available


if is_fpa_available():
  import transformer_engine.pytorch as te


def        __snake_case       (						lowerCAmelCase_   ,       lowerCAmelCase_=True   ,       lowerCAmelCase_=True   ,       lowerCAmelCase_=True  )							->	Tuple:
    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 % 1_6 != 0 for p in module.weight.shape  ):
                return
            SCREAMING_SNAKE_CASE__       							= module.bias is not None
            SCREAMING_SNAKE_CASE__       							= te.Linear(
                module.in_features   ,       module.out_features   ,       bias=lowerCAmelCase_   ,       params_dtype=module.weight.dtype  )
            SCREAMING_SNAKE_CASE__       							= module.weight.data.clone()
            if has_bias:
                SCREAMING_SNAKE_CASE__       							= module.bias.data.clone()

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

            setattr(lowerCAmelCase_   ,       lowerCAmelCase_   ,       lowerCAmelCase_  )
        elif isinstance(lowerCAmelCase_   ,       te.Linear  ) and not to_transformer_engine and _convert_linear:
            SCREAMING_SNAKE_CASE__       							= module.bias is not None
            SCREAMING_SNAKE_CASE__       							= nn.Linear(
                module.in_features   ,       module.out_features   ,       bias=lowerCAmelCase_   ,       params_dtype=module.weight.dtype  )
            SCREAMING_SNAKE_CASE__       							= module.weight.data.clone()
            if has_bias:
                SCREAMING_SNAKE_CASE__       							= module.bias.data.clone()

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