// Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. #include "core/common/safeint.h" #include "core/providers/cuda/cuda_common.h" #include "contrib_ops/cuda/bert/restore_padding.h" #include "contrib_ops/cuda/bert/bert_padding.h" namespace onnxruntime { namespace contrib { namespace cuda { #define REGISTER_KERNEL_TYPED(T) \ ONNX_OPERATOR_TYPED_KERNEL_EX( \ RestorePadding, \ kMSDomain, \ 1, \ T, \ kCudaExecutionProvider, \ (*KernelDefBuilder::Create()) \ .TypeConstraint("T", DataTypeImpl::GetTensorType()), \ RestorePadding); REGISTER_KERNEL_TYPED(float) REGISTER_KERNEL_TYPED(MLFloat16) using namespace ONNX_NAMESPACE; template RestorePadding::RestorePadding(const OpKernelInfo& op_kernel_info) : CudaKernel(op_kernel_info) { } template Status RestorePadding::ComputeInternal(OpKernelContext* context) const { // shape of inputs: // input: (total_tokens, hidden_size) // token_offset: (batch_size, sequence_length) // shape of outputs: // output: (batch_size, sequence_length, hidden_size) const Tensor* input = context->Input(0); const Tensor* token_offset = context->Input(1); const auto& dims = input->Shape().GetDims(); if (dims.size() != 2) { return ORT_MAKE_STATUS(ONNXRUNTIME, INVALID_ARGUMENT, "Input 'input' is expected to have 2 dimensions, got ", dims.size()); } int64_t total_tokens = dims[0]; int64_t hidden_size = dims[1]; const auto& token_offset_dims = token_offset->Shape().GetDims(); if (token_offset_dims.size() != 2) { return ORT_MAKE_STATUS(ONNXRUNTIME, INVALID_ARGUMENT, "Input 'token_offset' is expected to have 2 dimensions, got ", token_offset_dims.size()); } int64_t batch_size = token_offset_dims[0]; int64_t sequence_length = token_offset_dims[1]; TensorShapeVector output_shape(3); output_shape[0] = batch_size; output_shape[1] = sequence_length; output_shape[2] = hidden_size; Tensor* output = context->Output(0, output_shape); const int token_offset_count = SafeInt(batch_size) * sequence_length; auto validation_flag = GetScratchBuffer(1, GetComputeStream(context)); ORT_RETURN_IF_ERROR(ValidateTokenOffset( token_offset->Data(), token_offset_count, validation_flag.get(), Stream(context))); typedef typename ToCudaType::MappedType CudaT; LaunchRestorePadding( reinterpret_cast(output->MutableData()), reinterpret_cast(input->Data()), token_offset->Data(), static_cast(total_tokens), static_cast(hidden_size), static_cast(batch_size), static_cast(sequence_length), Stream(context)); CUDA_RETURN_IF_ERROR(cudaGetLastError()); return Status::OK(); } } // namespace cuda } // namespace contrib } // namespace onnxruntime