// Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. #include "core/providers/cuda/cuda_common.h" #include "contrib_ops/cuda/bert/remove_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( \ RemovePadding, \ kMSDomain, \ 1, \ T, \ kCudaExecutionProvider, \ (*KernelDefBuilder::Create()) \ .OutputMemoryType(OrtMemTypeCPUOutput, 3) /*max_token_count on CPU*/ \ .TypeConstraint("T", DataTypeImpl::GetTensorType()), \ RemovePadding); REGISTER_KERNEL_TYPED(float) REGISTER_KERNEL_TYPED(MLFloat16) using namespace ONNX_NAMESPACE; template RemovePadding::RemovePadding(const OpKernelInfo& op_kernel_info) : CudaKernel(op_kernel_info) { } template Status RemovePadding::ComputeInternal(OpKernelContext* context) const { // shape of inputs: // input: (batch_size, sequence_length, hidden_size) // sequence_token_count: (batch_size) // shape of outputs: // output: (total_tokens, hidden_size) // token_offset: (batch_size, sequence_length) // cumulated_seq_len: (batch_size + 1) // max_token_count: (1) const Tensor* input = context->Input(0); const Tensor* sequence_token_count = context->Input(1); const auto& dims = input->Shape().GetDims(); if (dims.size() != 3) { return ORT_MAKE_STATUS(ONNXRUNTIME, INVALID_ARGUMENT, "Input 'input' is expected to have 3 dimensions, got ", dims.size()); } int64_t batch_size = dims[0]; int64_t sequence_length = dims[1]; int64_t hidden_size = dims[2]; const auto& sequence_token_count_dims = sequence_token_count->Shape().GetDims(); if (sequence_token_count_dims.size() != 1 || sequence_token_count_dims[0] != batch_size) { return ORT_MAKE_STATUS(ONNXRUNTIME, INVALID_ARGUMENT, "Input 'sequence_token_count' is expected to have shape (batch_size), got shape ", sequence_token_count->Shape(), " for batch_size ", batch_size); } auto token_count_buffer = GetScratchBuffer(2, GetComputeStream(context)); TensorShapeVector token_offset_shape(2); token_offset_shape[0] = batch_size; token_offset_shape[1] = sequence_length; Tensor* token_offset = context->Output(1, token_offset_shape); TensorShapeVector cumulated_seq_len_shape(1); cumulated_seq_len_shape[0] = batch_size + static_cast(1); Tensor* cumulated_seq_len = context->Output(2, cumulated_seq_len_shape); LaunchGetTokenOffset(token_count_buffer.get(), token_offset->MutableData(), cumulated_seq_len->MutableData(), sequence_token_count->Data(), static_cast(batch_size), static_cast(sequence_length), Stream(context)); CUDA_RETURN_IF_ERROR(cudaGetLastError()); // Copy token_count to CPU auto pinned_buffer = AllocateBufferOnCPUPinned(2); int* token_count_pinned = pinned_buffer.get(); CUDA_RETURN_IF_ERROR(cudaMemcpyAsync(token_count_pinned, token_count_buffer.get(), sizeof(int) * 2, cudaMemcpyDeviceToHost, Stream(context))); // Wait until token_count is copied to host. CUDA_RETURN_IF_ERROR(cudaStreamSynchronize(Stream(context))); int total_token_count = token_count_pinned[0]; int max_token_count = token_count_pinned[1]; TensorShapeVector output_shape(2); output_shape[0] = static_cast(total_token_count); output_shape[1] = hidden_size; Tensor* output = context->Output(0, output_shape); TensorShapeVector max_token_count_shape(1); max_token_count_shape[0] = 1; Tensor* max_token_count_tensor = context->Output(3, max_token_count_shape); max_token_count_tensor->MutableData()[0] = max_token_count; typedef typename ToCudaType::MappedType CudaT; LaunchRemovePadding( reinterpret_cast(output->MutableData()), reinterpret_cast(input->Data()), token_offset->Data(), total_token_count, static_cast(hidden_size), Stream(context)); CUDA_RETURN_IF_ERROR(cudaGetLastError()); return Status::OK(); } } // namespace cuda } // namespace contrib } // namespace onnxruntime