forked from NVIDIA/TensorRT-LLM
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKernelRunner.cpp
More file actions
105 lines (82 loc) · 3.44 KB
/
KernelRunner.cpp
File metadata and controls
105 lines (82 loc) · 3.44 KB
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
/*
* Copyright (c) 2020-2025, NVIDIA CORPORATION. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <vector>
#include "KernelRunner.h"
#include "tensorrt_llm/common/assert.h"
#include "trtllmGen_export/GemmInterface.h"
namespace tensorrt_llm
{
namespace kernels
{
TrtllmGenGemmRunner::TrtllmGenGemmRunner(tg::Dtype eltType, tg::Dtype outputType)
: mEltType(eltType)
, mOutputType(outputType)
{
// Select a GEMM kernel config to use
auto const gemm = gemm::GemmInterface();
auto const configs = gemm.getGemmConfigs();
std::vector<int32_t> selectedIndex;
for (size_t i = 0; i < gemm.getNumGemmConfigs(); ++i)
{
auto const options = configs[i].mOptions;
// When we include low-latency kernels we can set transposeMmaOutput via constructor
if (options.mDtypeElt == eltType && options.mDtypeC == outputType && !options.mTransposeMmaOutput)
{
selectedIndex.push_back(i);
}
}
TLLM_CHECK_WITH_INFO(selectedIndex.size() != 0, "No kernel found for the given output type");
TLLM_CHECK_WITH_INFO(selectedIndex.size() == 1, "Multiple kernels found for the given output type");
mGemmConfig = &configs[selectedIndex[0]];
}
size_t TrtllmGenGemmRunner::getWorkspaceSizeInBytes(
int32_t m, int32_t n, int32_t k, tg::Dtype eltType, tg::Dtype outputType) const
{
gemm::GemmData gemmData;
gemmData.mProblemDimensions.mM = m;
gemmData.mProblemDimensions.mN = n;
gemmData.mProblemDimensions.mK = k;
auto gemm = gemm::GemmInterface();
return gemm.getWorkspaceSizeInBytes(*mGemmConfig, gemmData);
}
void TrtllmGenGemmRunner::run(int32_t m, int32_t n, int32_t k, void const* a, float const* aScale, void const* b,
float const* bScale, void* c, float* cScale, void* workspace, CUstream stream, int device)
{
auto gemm = gemm::GemmInterface();
gemm::GemmData gemmData;
// Dims
gemmData.mProblemDimensions.mM = m;
gemmData.mProblemDimensions.mN = n;
gemmData.mProblemDimensions.mK = k;
// Inputs
gemmData.mInputBuffers.mPtrA = a;
gemmData.mInputBuffers.mPtrSfA = aScale;
gemmData.mInputBuffers.mPtrB = b;
gemmData.mInputBuffers.mPtrSfB = bScale;
gemmData.mInputBuffers.mPtrScaleC = cScale;
// Outputs
gemmData.mOutputBuffers.mPtrC = c;
auto isValidConfig = gemm.isValidConfig(*mGemmConfig, gemmData);
TLLM_CHECK_WITH_INFO(isValidConfig, "Invalid GEMM config selected!");
cudaDeviceProp deviceProperties;
cudaGetDeviceProperties(&deviceProperties, device);
// FIXME once we start using all-reduce in the epilogue of the gemm this can be moved elsewhere
gemm.runInitBeforeWorldSync(*mGemmConfig, gemmData, static_cast<void*>(stream));
auto const err = gemm.run(*mGemmConfig, workspace, gemmData, static_cast<void*>(stream), deviceProperties);
TLLM_CHECK_WITH_INFO(err == 0, "Error occurred when running GEMM!");
}
} // namespace kernels
} // namespace tensorrt_llm