




import torch.nn as nn

from .imports import is_fpa_available


if is_fpa_available():
    import transformer_engine.pytorch as te



def 	UpperCAmelCase_    (	__UpperCamelCase,				__UpperCamelCase=True,				__UpperCamelCase=True,				__UpperCamelCase=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(__UpperCamelCase,				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
            SCREAMING_SNAKE_CASE__								=module.bias is not None
            SCREAMING_SNAKE_CASE__								=te.Linear(
                module.in_features,				module.out_features,				bias=__UpperCamelCase,				params_dtype=module.weight.dtype	)
            SCREAMING_SNAKE_CASE__								=module.weight.data.clone()
            if has_bias:
                SCREAMING_SNAKE_CASE__								=module.bias.data.clone()

            setattr(__UpperCamelCase,				__UpperCamelCase,				__UpperCamelCase	)
        elif isinstance(__UpperCamelCase,				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(__UpperCamelCase,				__UpperCamelCase,				__UpperCamelCase	)
        elif isinstance(__UpperCamelCase,				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=__UpperCamelCase,				params_dtype=module.weight.dtype	)
            SCREAMING_SNAKE_CASE__								=module.weight.data.clone()
            if has_bias:
                SCREAMING_SNAKE_CASE__								=module.bias.data.clone()

            setattr(__UpperCamelCase,				__UpperCamelCase,				__UpperCamelCase	)
        elif isinstance(__UpperCamelCase,				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(__UpperCamelCase,				__UpperCamelCase,				__UpperCamelCase	)
        else:
            convert_model(
                __UpperCamelCase,				to_transformer_engine=__UpperCamelCase,				_convert_linear=_convert_linear,				_convert_ln=_convert_ln,				)




def 	UpperCAmelCase_    (	__UpperCamelCase	):
    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(__UpperCamelCase,				(te.LayerNorm, te.Linear)	):
            return True
    return False












def 	UpperCAmelCase_    (	__UpperCamelCase,				__UpperCamelCase	):
    SCREAMING_SNAKE_CASE__								=1.0
    for i in range(1,				r + 1	):
        ret *= (n + 1 - i) / i
    return round(__UpperCamelCase	)



def 	UpperCAmelCase_    (	__UpperCamelCase	):
    return choose(8 + n,				__UpperCamelCase	) + choose(9 + n,				__UpperCamelCase	) - 10



def 	UpperCAmelCase_    (	__UpperCamelCase	):
    return sum(non_bouncy_exact(__UpperCamelCase	) for i in range(1,				n + 1	)	)



def 	UpperCAmelCase_    (	__UpperCamelCase = 100	):
    return non_bouncy_upto(__UpperCamelCase	)


if __name__ == "__main__":
    print(f"""{solution() = }""")






