// Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. #include #include "core/providers/cuda/cuda_common.h" #include "core/common/narrow.h" #include "skip_layer_norm.h" #include "skip_layer_norm_impl.h" #include "contrib_ops/cpu/skip_layer_norm_helper.h" namespace onnxruntime { namespace contrib { namespace cuda { #define REGISTER_KERNEL_TYPED(T) \ ONNX_OPERATOR_TYPED_KERNEL_EX( \ SkipLayerNormalization, \ kMSDomain, \ 1, \ T, \ kCudaExecutionProvider, \ (*KernelDefBuilder::Create()) \ .TypeConstraint("T", DataTypeImpl::GetTensorType()), \ SkipLayerNorm); \ ONNX_OPERATOR_TYPED_KERNEL_EX( \ SkipSimplifiedLayerNormalization, \ kMSDomain, \ 1, \ T, \ kCudaExecutionProvider, \ (*KernelDefBuilder::Create()) \ .TypeConstraint("T", DataTypeImpl::GetTensorType()), \ SkipLayerNorm); REGISTER_KERNEL_TYPED(float) REGISTER_KERNEL_TYPED(MLFloat16) REGISTER_KERNEL_TYPED(BFloat16) using namespace ONNX_NAMESPACE; template SkipLayerNorm::SkipLayerNorm(const OpKernelInfo& op_kernel_info) : CudaKernel(op_kernel_info) { ORT_ENFORCE(op_kernel_info.GetAttr("epsilon", &epsilon_).IsOK()); ORT_ENFORCE(epsilon_ >= 0); // Note: the enable_skip_layer_norm_strict_mode provider option is deprecated and ignored. // The kernel always accumulates in fp32, so the previous strict-mode path is no longer needed. } template Status SkipLayerNorm::ComputeInternal(OpKernelContext* ctx) const { const Tensor* input = ctx->Input(0); const Tensor* skip = ctx->Input(1); const Tensor* gamma = ctx->Input(2); const Tensor* beta = Simplified ? nullptr : ctx->Input(3); const Tensor* bias = Simplified ? ctx->Input(3) : ctx->Input(4); Tensor* output = ctx->Output(0, input->Shape()); // Optional output for the sum of skip, input and bias tensors (It is also the input of Layer Normalization). Tensor* sum_output = ctx->Output(3, input->Shape()); const auto& input_dims = input->Shape().GetDims(); size_t input_dims_size = input_dims.size(); int hidden_size = onnxruntime::narrow(input_dims[input_dims_size - 1]); ORT_RETURN_IF_ERROR(onnxruntime::contrib::skip_layer_norm_helper::CheckInputs(input, skip, gamma, beta, bias, hidden_size, input_dims_size)); int row_count = onnxruntime::narrow(input->Shape().SizeToDimension(input_dims_size - 1)); if (row_count == 0) { return Status::OK(); } // Element offsets into the output are 32-bit on device; reject shapes whose element count would // exceed the 32-bit indexable range. The output shares the input shape, so input->Shape().Size() // is the output element count. The maximum output write index is // row_count * hidden_size - 1 == output element count - 1, so this guard covers every device write site. const int64_t output_element_count = input->Shape().Size(); ORT_RETURN_IF_NOT(output_element_count <= static_cast(std::numeric_limits::max()), "SkipLayerNormalization: output element count (", output_element_count, ") exceeds the supported 32-bit indexing range."); typedef typename ToCudaType::MappedType CudaT; const int skip_size = onnxruntime::narrow(skip->Shape().Size()); if constexpr (std::is_same_v) { LaunchSkipLayerNormKernel( Stream(ctx), reinterpret_cast(output->MutableData()), sum_output != nullptr ? reinterpret_cast(sum_output->MutableData()) : nullptr, reinterpret_cast(input->Data()), reinterpret_cast(skip->Data()), (bias != nullptr) ? reinterpret_cast(bias->Data()) : nullptr, reinterpret_cast(gamma->Data()), (beta != nullptr) ? reinterpret_cast(beta->Data()) : nullptr, epsilon_, hidden_size, row_count, skip_size); } else { LaunchSkipLayerNormKernel( Stream(ctx), reinterpret_cast(output->MutableData()), sum_output != nullptr ? reinterpret_cast(sum_output->MutableData()) : nullptr, reinterpret_cast(input->Data()), reinterpret_cast(skip->Data()), (bias != nullptr) ? reinterpret_cast(bias->Data()) : nullptr, reinterpret_cast(gamma->Data()), (beta != nullptr) ? reinterpret_cast(beta->Data()) : nullptr, epsilon_, hidden_size, row_count, skip_size); } CUDA_RETURN_IF_ERROR(cudaGetLastError()); return Status::OK(); } } // namespace cuda } // namespace contrib } // namespace onnxruntime