paddlenlp/ops/faster_transformer/transformer/encoder.py
def convert_to_fp16(transformer_encoder):
    """ Convert paddle.nn.TransformerEncoder's parameter from float32 to float16

    Args:
        transformer_encoder (obeject, paddle.nn.TransformerEncoder):
            The object to be converted to float16 inplaced, it must be an isinstance
            of paddle.nn.TransformerEncoder.
    """
    if not isinstance(transformer_encoder, paddle.nn.TransformerEncoder):
        logger.warning(
            "transformer_encoder is not isinstance of paddle.nn.TransformerEncoder, return itself with no parameters convertion.".
            format)
        return transformer_encoder
    else:
        encoder_layers = transformer_encoder.layers

        for mod in encoder_layers:
            mod.norm1.weight = transfer_param(
                mod.norm1.weight, restore_data=True)
            mod.norm1.bias = transfer_param(
                mod.norm1.bias, is_bias=True, restore_data=True)
            mod.norm2.weight = transfer_param(
                mod.norm2.weight, restore_data=True)
            mod.norm2.bias = transfer_param(
                mod.norm2.bias, is_bias=True, restore_data=True)

            mod.linear1.weight = transfer_param(
                mod.linear1.weight, restore_data=True)
            mod.linear1.bias = transfer_param(
                mod.linear1.bias, is_bias=True, restore_data=True)

            mod.self_attn.q_proj.weight = transfer_param(
                mod.self_attn.q_proj.weight, restore_data=True)
            mod.self_attn.q_proj.bias = transfer_param(
                mod.self_attn.q_proj.bias, is_bias=True, restore_data=True)
            mod.self_attn.k_proj.weight = transfer_param(
                mod.self_attn.k_proj.weight, restore_data=True)
            mod.self_attn.k_proj.bias = transfer_param(
                mod.self_attn.k_proj.bias, is_bias=True, restore_data=True)
            mod.self_attn.v_proj.weight = transfer_param(
                mod.self_attn.v_proj.weight, restore_data=True)
            mod.self_attn.v_proj.bias = transfer_param(
                mod.self_attn.v_proj.bias, is_bias=True, restore_data=True)
            mod.self_attn.out_proj.weight = transfer_param(
                mod.self_attn.out_proj.weight, restore_data=True)
            mod.self_attn.out_proj.bias = transfer_param(
                mod.self_attn.out_proj.bias, is_bias=True, restore_data=True)

            mod.linear2.weight = transfer_param(
                mod.linear2.weight, restore_data=True)
            mod.linear2.bias = transfer_param(
                mod.linear2.bias, is_bias=True, restore_data=True)
        logger.info(
            "Convert transformer_encoder's parameters from float32 to float16 succeessfully."
        )
 Convert paddle.nn.TransformerEncoder's parameter from float32 to float16

    Args:
        transformer_encoder (obeject, paddle.nn.TransformerEncoder):
            The object to be converted to float16 inplaced, it must be an isinstance
            of paddle.nn.TransformerEncoder.
    
Convert paddle.nn.TransformerEncoder's parameter from float32 to float16
Convert paddle . nn . TransformerEncoder ' s parameter from float32 to float16
def convert_to_fp16(transformer_encoder):
    if not isinstance(transformer_encoder, paddle.nn.TransformerEncoder):
        logger.warning(
            "transformer_encoder is not isinstance of paddle.nn.TransformerEncoder, return itself with no parameters convertion.".
            format)
        return transformer_encoder
    else:
        encoder_layers = transformer_encoder.layers
        for mod in encoder_layers:
            mod.norm1.weight = transfer_param(
                mod.norm1.weight, restore_data=True)
            mod.norm1.bias = transfer_param(
                mod.norm1.bias, is_bias=True, restore_data=True)
            mod.norm2.weight = transfer_param(
                mod.norm2.weight, restore_data=True)
            mod.norm2.bias = transfer_param(
                mod.norm2.bias, is_bias=True, restore_data=True)
            mod.linear1.weight = transfer_param(
                mod.linear1.weight, restore_data=True)
            mod.linear1.bias = transfer_param(
                mod.linear1.bias, is_bias=True, restore_data=True)
            mod.self_attn.q_proj.weight = transfer_param(
                mod.self_attn.q_proj.weight, restore_data=True)
            mod.self_attn.q_proj.bias = transfer_param(
                mod.self_attn.q_proj.bias, is_bias=True, restore_data=True)
            mod.self_attn.k_proj.weight = transfer_param(
                mod.self_attn.k_proj.weight, restore_data=True)
            mod.self_attn.k_proj.bias = transfer_param(
                mod.self_attn.k_proj.bias, is_bias=True, restore_data=True)
            mod.self_attn.v_proj.weight = transfer_param(
                mod.self_attn.v_proj.weight, restore_data=True)
            mod.self_attn.v_proj.bias = transfer_param(
                mod.self_attn.v_proj.bias, is_bias=True, restore_data=True)
            mod.self_attn.out_proj.weight = transfer_param(
                mod.self_attn.out_proj.weight, restore_data=True)
            mod.self_attn.out_proj.bias = transfer_param(
                mod.self_attn.out_proj.bias, is_bias=True, restore_data=True)
            mod.linear2.weight = transfer_param(
                mod.linear2.weight, restore_data=True)
            mod.linear2.bias = transfer_param(
                mod.linear2.bias, is_bias=True, restore_data=True)
        logger.info(
            "Convert transformer_encoder's parameters from float32 to float16 succeessfully."
        )
def convert_to_fp16 ( transformer_encoder ) : if not isinstance ( transformer_encoder , paddle . nn . TransformerEncoder ) : logger . warning ( "transformer_encoder is not isinstance of paddle.nn.TransformerEncoder, return itself with no parameters convertion." . format ) return transformer_encoder else : encoder_layers = transformer_encoder . layers for mod in encoder_layers : mod . norm1 . weight = transfer_param ( mod . norm1 . weight , restore_data = True ) mod . norm1 . bias = transfer_param ( mod . norm1 . bias , is_bias = True , restore_data = True ) mod . norm2 . weight = transfer_param ( mod . norm2 . weight , restore_data = True ) mod . norm2 . bias = transfer_param ( mod . norm2 . bias , is_bias = True , restore_data = True ) mod . linear1 . weight = transfer_param ( mod . linear1 . weight , restore_data = True ) mod . linear1 . bias = transfer_param ( mod . linear1 . bias , is_bias = True , restore_data = True ) mod . self_attn . q_proj . weight = transfer_param ( mod . self_attn . q_proj . weight , restore_data = True ) mod . self_attn . q_proj . bias = transfer_param ( mod . self_attn . q_proj . bias , is_bias = True , restore_data = True ) mod . self_attn . k_proj . weight = transfer_param ( mod . self_attn . k_proj . weight , restore_data = True ) mod . self_attn . k_proj . bias = transfer_param ( mod . self_attn . k_proj . bias , is_bias = True , restore_data = True ) mod . self_attn . v_proj . weight = transfer_param ( mod . self_attn . v_proj . weight , restore_data = True ) mod . self_attn . v_proj . bias = transfer_param ( mod . self_attn . v_proj . bias , is_bias = True , restore_data = True ) mod . self_attn . out_proj . weight = transfer_param ( mod . self_attn . out_proj . weight , restore_data = True ) mod . self_attn . out_proj . bias = transfer_param ( mod . self_attn . out_proj . bias , is_bias = True , restore_data = True ) mod . linear2 . weight = transfer_param ( mod . linear2 . weight , restore_data = True ) mod . linear2 . bias = transfer_param ( mod . linear2 . bias , is_bias = True , restore_data = True ) logger . info ( "Convert transformer_encoder's parameters from float32 to float16 succeessfully." )
Convert paddle.nn.TransformerEncoder's parameter from float32 to float16
Convert paddle . nn . TransformerEncoder ' s parameter from float32 to float16
""" Convert paddle.nn.TransformerEncoder's parameter from float32 to float16

    Args:
        transformer_encoder (obeject, paddle.nn.TransformerEncoder):
            The object to be converted to float16 inplaced, it must be an isinstance
            of paddle.nn.TransformerEncoder.
    """
transformer_encoder
The object to be converted to float16 inplaced, it must be an isinstance
of paddle.nn.TransformerEncoder.
The object to be converted to float16 inplaced it must be an isinstance of paddle . nn . TransformerEncoder .