MCPcopy Create free account
hub / github.com/zai-org/CodeGeeX / layer_norm

Function layer_norm

codegeex/mindspore/scripts/layer_norm.py:726–914  ·  view source on GitHub ↗

layernorm operator interface implementation calculating: x, gamma, beta mean = np.mean(x, reduce_axis, keepdims=True) variance = np.mean(np.power((x - mean),2), reduce_axis, keepdims=True) result = gamma*((x - mean) / np.sqrt(variance + 0.001)) + beta Parameter

(input_x, input_gamma, input_beta,
               output_y, output_mean, output_variance,
               begin_norm_axis, begin_params_axis,
               epsilon=1e-12, kernel_name="layer_norm",
               impl_mode="high_performance")

Source from the content-addressed store, hash-verified

724 para_check.OPTION_ATTR_FLOAT, para_check.KERNEL_NAME,
725 para_check.OPTION_ATTR_STR)
726def layer_norm(input_x, input_gamma, input_beta,
727 output_y, output_mean, output_variance,
728 begin_norm_axis, begin_params_axis,
729 epsilon=1e-12, kernel_name="layer_norm",
730 impl_mode="high_performance"):
731 """
732 layernorm operator interface implementation
733 calculating: x, gamma, beta
734 mean = np.mean(x, reduce_axis, keepdims=True)
735 variance = np.mean(np.power((x - mean),2), reduce_axis, keepdims=True)
736 result = gamma*((x - mean) / np.sqrt(variance + 0.001)) + beta
737
738 Parameters
739 ----------
740 input_x : dict
741 shape and dtype of input x, only support float16, float32
742 input_gamma: dict
743 shape and dtype of input gamma, only support float16, float32
744 input_beta: dict
745 shape and dtype of input beta, only support float16, float32
746 output_y: dict
747 shape and dtype of output, only support float16, float32
748 begin_norm_axis: int
749 The first normalization dimension: normalization will be
750 performed along dimensions `begin_norm_axis : rank(inputs)`
751 begin_params_axis: int
752 The first parameter (beta, gamma) dimension: scale
753 and centering parameters will have dimensions
754 `begin_params_axis : rank(inputs)` and will be broadcast with the
755 normalized inputs accordingly.
756 epsilon: float,
757 Minimum positive number greater than 0
758 kernel_name: str
759 cce kernel name, default value is "layernorm"
760
761 Returns
762 -------
763 None
764 """
765 shape_x = list(input_x.get("shape"))
766 input_gamma_shape = input_gamma.get("shape")
767 input_beta_shape = input_beta.get("shape")
768 ori_shape_x = list(input_x.get("ori_shape"))
769 input_format = input_x.get("format").upper()
770 input_gamma_format = input_gamma.get("format").upper()
771 input_beta_format = input_beta.get("format").upper()
772
773 para_check.check_shape(input_gamma_shape, param_name="input_gamma")
774 para_check.check_shape(input_beta_shape, param_name="input_beta")
775 para_check.check_shape(shape_x, param_name="input_x")
776
777 check_list = ("float16", "float32")
778 dtype = input_x.get("dtype").lower()
779 dtype_gamma = input_gamma.get("dtype").lower()
780 dtype_beta = input_gamma.get("dtype").lower()
781 para_check.check_dtype(dtype, check_list, param_name="input_x")
782 para_check.check_dtype(dtype_gamma, check_list, param_name="input_gamma")
783 para_check.check_dtype(dtype_beta, check_list, param_name="input_gamma")

Callers

nothing calls this directly

Calls 7

_check_vector_to_cubeFunction · 0.85
nz_non_alignedFunction · 0.85
__dynamic_template_apiFunction · 0.85
layer_norm_compute_nzFunction · 0.85
layer_norm_computeFunction · 0.85
getMethod · 0.45

Tested by

no test coverage detected