Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 deletions onnxruntime/contrib_ops/cuda/bert/attention_impl.cu
Original file line number Diff line number Diff line change
Expand Up @@ -762,12 +762,8 @@ Status UnfusedAttention(
} else { // no mask
if (nullptr != data.output_qk) {
int64_t qk_size = (int64_t)batch_size * num_heads * sequence_length * total_sequence_length;
if (std::is_same<T, QK>::value) {
cudaMemcpyAsync(data.output_qk, data.scratch, qk_size * sizeof(QK), cudaMemcpyDeviceToDevice, stream);
} else {
ORT_RETURN_IF_ERROR(
(CopyQK<T, QK>(stream, static_cast<int>(qk_size), data.scratch, reinterpret_cast<QK*>(data.output_qk))));
}
ORT_RETURN_IF_ERROR(
(CopyQK<T, QK>(stream, static_cast<int>(qk_size), data.scratch, reinterpret_cast<QK*>(data.output_qk))));
}
ORT_RETURN_IF_ERROR(
ComputeSoftmax<T>(
Expand Down
22 changes: 22 additions & 0 deletions onnxruntime/contrib_ops/cuda/bert/attention_qk.cu
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,23 @@ __global__ void ConvertAndCopyQK(const int count, const half* input, float* outp
}
}

template <typename T>
__global__ void ConvertAndCopyQK(const int count, const T* input, T* output) {
int idx = threadIdx.x + blockIdx.x * blockDim.x;
if (idx < count) {
output[idx] = input[idx];
}
}

template <typename T, typename QK>
Status CopyQK(cudaStream_t stream,
const int qk_size,
const T* input,
QK* output) {
if constexpr (std::is_same<T, QK>::value) {
CUDA_RETURN_IF_ERROR(cudaMemcpyAsync(output, input, qk_size * sizeof(QK), cudaMemcpyDeviceToDevice, stream));
return Status::OK();
}
const bool half2float = std::is_same<T, half>::value && std::is_same<QK, float>::value;
const bool float2half = std::is_same<T, float>::value && std::is_same<QK, half>::value;
ORT_ENFORCE(half2float || float2half);
Expand All @@ -40,6 +52,11 @@ Status CopyQK(cudaStream_t stream,
return CUDA_CALL(cudaGetLastError());
}

template Status CopyQK<float, float>(cudaStream_t stream,
const int qk_size,
const float* input,
float* output);

template Status CopyQK<float, half>(cudaStream_t stream,
const int qk_size,
const float* input,
Expand All @@ -50,6 +67,11 @@ template Status CopyQK<half, float>(cudaStream_t stream,
const half* input,
float* output);

template Status CopyQK<half, half>(cudaStream_t stream,
const int qk_size,
const half* input,
half* output);

} // namespace cuda
} // namespace contrib
} // namespace onnxruntime
Loading