Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
3 days ago bd039d1e92813358c5c5a1e346b49074baf513f2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// 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<T>()),              \
      RemovePadding<T>);
 
REGISTER_KERNEL_TYPED(float)
REGISTER_KERNEL_TYPED(MLFloat16)
 
using namespace ONNX_NAMESPACE;
 
template <typename T>
RemovePadding<T>::RemovePadding(const OpKernelInfo& op_kernel_info) : CudaKernel(op_kernel_info) {
}
 
template <typename T>
Status RemovePadding<T>::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<Tensor>(0);
  const Tensor* sequence_token_count = context->Input<Tensor>(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<int>(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<int64_t>(1);
  Tensor* cumulated_seq_len = context->Output(2, cumulated_seq_len_shape);
 
  LaunchGetTokenOffset(token_count_buffer.get(),
                       token_offset->MutableData<int>(),
                       cumulated_seq_len->MutableData<int>(),
                       sequence_token_count->Data<int>(),
                       static_cast<int>(batch_size),
                       static_cast<int>(sequence_length),
                       Stream(context));
  CUDA_RETURN_IF_ERROR(cudaGetLastError());
 
  // Copy token_count to CPU
  auto pinned_buffer = AllocateBufferOnCPUPinned<int>(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<int64_t>(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<int>()[0] = max_token_count;
 
  typedef typename ToCudaType<T>::MappedType CudaT;
  LaunchRemovePadding<CudaT>(
      reinterpret_cast<CudaT*>(output->MutableData<T>()),
      reinterpret_cast<const CudaT*>(input->Data<T>()),
      token_offset->Data<int>(),
      total_token_count,
      static_cast<int>(hidden_size),
      Stream(context));
 
  CUDA_RETURN_IF_ERROR(cudaGetLastError());
  return Status::OK();
}
 
}  // namespace cuda
}  // namespace contrib
}  // namespace onnxruntime