-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Expand file tree
/
Copy pathsaBuffer.h
More file actions
294 lines (245 loc) · 7.03 KB
/
saBuffer.h
File metadata and controls
294 lines (245 loc) · 7.03 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/*
* SPDX-FileCopyrightText: Copyright (c) 2022-2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*
* 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.
*
* Adapted from Baseten's sa_spec library (Apache-2.0)
* https://github.com/basetenlabs/sa_spec
*/
#pragma once
#include <cassert>
#include <cstddef>
#include <cstring>
#include <type_traits>
#include "saCudaCallable.h"
#include "tensorrt_llm/common/config.h"
TRTLLM_NAMESPACE_BEGIN
namespace kernels::speculative_decoding::suffix_automaton
{
/**
* @brief A fixed-capacity buffer that uses external memory (pointer-based).
*
* This is a view into externally-managed memory. The buffer does not own
* the memory and does not perform any allocation/deallocation.
*
* This design enables:
* - Runtime-configurable capacity (no compile-time template parameter)
* - Trivially copyable (can be memcpy'd between host and GPU)
* - CUDA graph compatible (fixed memory addresses after initialization)
*
* @tparam T Element type (must be trivially copyable)
* @tparam IndexT Index type (default size_t)
*/
template <typename T, typename IndexT = size_t>
struct SABuffer
{
T* mData{nullptr};
size_t mCapacity{0};
T const& at(IndexT, IndexT) const = delete;
T& at(IndexT, IndexT) = delete;
SABuffer() = default;
SA_CUDA_CALLABLE void init(T* data, size_t capacity)
{
mData = data;
mCapacity = capacity;
}
SA_CUDA_CALLABLE T const& at(IndexT row) const
{
assert(static_cast<size_t>(+row) < mCapacity);
return mData[+row];
}
SA_CUDA_CALLABLE T& at(IndexT row)
{
assert(static_cast<size_t>(+row) < mCapacity);
return mData[+row];
}
struct Iterator
{
SABuffer const& buffer;
IndexT index;
SA_CUDA_CALLABLE Iterator(SABuffer const& buffer, IndexT index)
: buffer(buffer)
, index(index)
{
}
SA_CUDA_CALLABLE T const& operator*() const
{
return buffer.at(index);
}
SA_CUDA_CALLABLE Iterator& operator++()
{
index = IndexT(+index + 1);
return *this;
}
SA_CUDA_CALLABLE bool operator==(Iterator const& other) const
{
return index == other.index;
}
SA_CUDA_CALLABLE bool operator!=(Iterator const& other) const
{
return index != other.index;
}
};
SA_CUDA_CALLABLE Iterator begin() const
{
return Iterator(*this, IndexT(0));
}
SA_CUDA_CALLABLE Iterator end() const
{
return Iterator(*this, IndexT(mCapacity));
}
SA_CUDA_CALLABLE size_t size() const
{
return mCapacity;
}
SA_CUDA_CALLABLE size_t capacity() const
{
return mCapacity;
}
void clear()
{
if (mData && mCapacity > 0)
{
memset(static_cast<void*>(mData), 0, mCapacity * sizeof(T));
}
}
SA_CUDA_CALLABLE T* data()
{
return mData;
}
SA_CUDA_CALLABLE T const* data() const
{
return mData;
}
/**
* @brief Relocate the data pointer by a given delta.
*
* Used when copying between host and GPU memory to adjust pointers.
*/
void relocate(ptrdiff_t delta)
{
if (mData)
{
mData = reinterpret_cast<T*>(reinterpret_cast<uint8_t*>(mData) + delta);
}
}
static_assert(std::is_trivially_copyable<T>::value, "T must be trivially copyable");
};
/**
* @brief A dynamic buffer with runtime-configurable capacity using external memory.
*
* Like SABuffer, but tracks current length separately from capacity.
* Supports push/pop operations up to the capacity limit.
*
* @tparam T Element type (must be trivially copyable)
* @tparam IndexT Index type (default size_t)
*/
template <typename T, typename IndexT = size_t>
struct SADynamicBuffer
{
T* mData{nullptr};
size_t mCapacity{0};
IndexT mLength{0};
SADynamicBuffer() = default;
SA_CUDA_CALLABLE void init(T* data, size_t capacity)
{
mData = data;
mCapacity = capacity;
mLength = IndexT(0);
}
SA_CUDA_CALLABLE void clear()
{
mLength = IndexT(0);
}
SA_CUDA_CALLABLE IndexT size() const
{
return mLength;
}
SA_CUDA_CALLABLE size_t capacity() const
{
return mCapacity;
}
SA_CUDA_CALLABLE bool empty() const
{
return +size() == 0;
}
SA_CUDA_CALLABLE void extend(size_t n)
{
mLength = IndexT(+mLength + n);
assert(static_cast<size_t>(+mLength) <= mCapacity);
}
SA_CUDA_CALLABLE T& pushBack(T const& value)
{
assert(static_cast<size_t>(+mLength) < mCapacity);
T& result = mData[+mLength];
result = value;
mLength = IndexT(+mLength + 1);
return result;
}
SA_CUDA_CALLABLE T& pushBack(T&& value)
{
assert(static_cast<size_t>(+mLength) < mCapacity);
T& result = mData[+mLength];
result = std::move(value);
mLength = IndexT(+mLength + 1);
return result;
}
SA_CUDA_CALLABLE T& popBack()
{
assert(!empty());
T& result = mData[+mLength - 1];
mLength = IndexT(+mLength - 1);
return result;
}
SA_CUDA_CALLABLE T const& at(IndexT row) const
{
assert(row < mLength);
return mData[+row];
}
SA_CUDA_CALLABLE T& at(IndexT row)
{
assert(row < mLength);
return mData[+row];
}
SA_CUDA_CALLABLE T* data()
{
return mData;
}
SA_CUDA_CALLABLE T const* data() const
{
return mData;
}
SA_CUDA_CALLABLE bool hasCapacity() const
{
return static_cast<size_t>(+mLength) < mCapacity;
}
/**
* @brief Relocate the data pointer by a given delta.
*
* Used when copying between host and GPU memory to adjust pointers.
*/
void relocate(ptrdiff_t delta)
{
if (mData)
{
mData = reinterpret_cast<T*>(reinterpret_cast<uint8_t*>(mData) + delta);
}
}
static_assert(std::is_trivially_copyable<T>::value, "T must be trivially copyable");
};
// Verify that our buffer types are trivially copyable (required for GPU memcpy)
static_assert(std::is_trivially_copyable<SABuffer<int>>::value, "SABuffer must be trivially copyable");
static_assert(std::is_trivially_copyable<SADynamicBuffer<int>>::value, "SADynamicBuffer must be trivially copyable");
} // namespace kernels::speculative_decoding::suffix_automaton
TRTLLM_NAMESPACE_END