-
Notifications
You must be signed in to change notification settings - Fork 1
/
tests.cpp
418 lines (320 loc) · 17.6 KB
/
tests.cpp
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
#include "../generators.h"
#include "../search.h"
#include "../models/gpt.h"
#if USE_CUDA
#include "../search_cuda.h"
#include "../models/gpt_cuda.h"
#endif
#include <iostream>
#define ASSERT_EQ(a, b) assert((a) == (b))
#define ASSERT_TRUE(a) assert(a)
std::unique_ptr<OrtEnv> g_ort_env;
void Test_BeamSearchTest_GptBeamSearchFp32() {
std::vector<int64_t> input_ids_shape{3, 12};
std::vector<int32_t> input_ids{
0, 0, 0, 0, 0, 52, 195, 731, 321, 301, 734, 620,
41, 554, 74, 622, 206, 222, 75, 223, 221, 198, 224, 572,
0, 0, 0, 52, 328, 219, 328, 206, 288, 227, 896, 328};
std::vector<int64_t> parameter_shape{1};
std::vector<int32_t> max_length{20};
std::vector<int32_t> min_length{1};
std::vector<int32_t> num_beams{4};
std::vector<int32_t> num_return_sequences{1};
std::vector<float> length_penalty{1.0f};
std::vector<float> repetition_penalty{1.0f};
std::vector<int64_t> expected_output_shape{input_ids_shape[0], num_return_sequences[0], max_length[0]};
std::vector<int32_t> expected_output{
0, 0, 0, 0, 0, 52, 195, 731, 321, 301, 734, 620, 131, 131, 131, 181, 638, 638, 638, 638,
41, 554, 74, 622, 206, 222, 75, 223, 221, 198, 224, 572, 292, 292, 292, 292, 292, 292, 292, 292,
0, 0, 0, 52, 328, 219, 328, 206, 288, 227, 896, 328, 328, 669, 669, 669, 669, 669, 669, 669};
auto info = OrtMemoryInfo::Create("Cpu", OrtDeviceAllocator, 0, OrtMemTypeDefault);
auto input_ids_tensor = OrtValue::CreateTensor(
*info, input_ids.data(), input_ids.size(), input_ids_shape.data(), input_ids_shape.size());
auto max_length_tensor = OrtValue::CreateTensor(
*info, max_length.data(), max_length.size(), parameter_shape.data(), parameter_shape.size());
auto min_length_tensor = OrtValue::CreateTensor(
*info, min_length.data(), min_length.size(), parameter_shape.data(), parameter_shape.size());
auto num_beams_tensor = OrtValue::CreateTensor(
*info, num_beams.data(), num_beams.size(), parameter_shape.data(), parameter_shape.size());
auto num_return_sequences_tensor = OrtValue::CreateTensor(
*info, num_return_sequences.data(), num_return_sequences.size(), parameter_shape.data(), parameter_shape.size());
auto length_penalty_tensor = OrtValue::CreateTensor(
*info, length_penalty.data(), length_penalty.size(), parameter_shape.data(), parameter_shape.size());
auto repetition_penalty_tensor = OrtValue::CreateTensor(
*info, repetition_penalty.data(), repetition_penalty.size(), parameter_shape.data(), parameter_shape.size());
std::vector<OrtValue*> ort_inputs;
ort_inputs.push_back(input_ids_tensor.get());
ort_inputs.push_back(max_length_tensor.get());
ort_inputs.push_back(min_length_tensor.get());
ort_inputs.push_back(num_beams_tensor.get());
ort_inputs.push_back(num_return_sequences_tensor.get());
ort_inputs.push_back(length_penalty_tensor.get());
ort_inputs.push_back(repetition_penalty_tensor.get());
const char* input_names[] = {"input_ids", "max_length", "min_length", "num_beams", "num_return_sequences",
"length_penalty", "repetition_penalty"};
const char* const output_names[] = {"sequences"};
auto session_options = OrtSessionOptions::Create();
#ifdef USE_CUDA
OrtCUDAProviderOptions cuda_options;
// cuda_options.has_user_compute_stream=true;
// cuda_options.user_compute_stream=
session_options->AppendExecutionProvider_CUDA(cuda_options);
#endif
// The ONNX model is generated like the following:
// python convert_generation.py --model_type gpt2 -m hf-internal-testing/tiny-random-gpt2
// --output tiny_gpt2_beamsearch_fp16.onnx --use_gpu --max_length 20
// (with separate_gpt2_decoder_for_init_run set to False as it is now set to True by default)
auto session = OrtSession::Create(*g_ort_env, ORT_TSTR("C:/code/github/generators/Generators/models/tiny_gpt2_beamsearch.onnx"), session_options.get());
auto ort_outputs = session->Run(nullptr, input_names, ort_inputs.data(), ort_inputs.size(),
output_names, 1);
ASSERT_EQ(ort_outputs.size(), 1U);
const auto& sequences = ort_outputs[0];
ASSERT_TRUE(sequences->IsTensor());
auto result_ts = sequences->GetTensorTypeAndShapeInfo();
ASSERT_EQ(ONNX_TENSOR_ELEMENT_DATA_TYPE_INT32, result_ts->GetElementType());
ASSERT_EQ(expected_output_shape, result_ts->GetShape());
const auto* result_vals = sequences->GetTensorData<int32_t>();
auto result_span = std::span(result_vals, expected_output.size());
ASSERT_TRUE(std::equal(expected_output.cbegin(), expected_output.cend(), result_span.begin(), result_span.end()));
std::cout << "Test_BeamSearchTest_GptBeamSearchFp32 complete\r\n";
}
void Test_Lib_BeamSearchTest_GptBeamSearchFp32() {
int32_t max_length{20};
float length_penalty{1.0f};
std::vector<int64_t> input_ids_shape{3, 12};
std::vector<int32_t> input_ids{
0, 0, 0, 0, 0, 52, 195, 731, 321, 301, 734, 620,
41, 554, 74, 622, 206, 222, 75, 223, 221, 198, 224, 572,
0, 0, 0, 52, 328, 219, 328, 206, 288, 227, 896, 328};
std::vector<int32_t> expected_output{
0, 0, 0, 0, 0, 52, 195, 731, 321, 301, 734, 620, 131, 131, 131, 181, 638, 638, 638, 638,
41, 554, 74, 622, 206, 222, 75, 223, 221, 198, 224, 572, 292, 292, 292, 292, 292, 292, 292, 292,
0, 0, 0, 52, 328, 219, 328, 206, 288, 227, 896, 328, 328, 669, 669, 669, 669, 669, 669, 669};
// The ONNX model is generated like the following:
// python convert_generation.py --model_type gpt2 -m hf-internal-testing/tiny-random-gpt2
// --output tiny_gpt2_beamsearch_fp16.onnx --use_gpu --max_length 20
// (with separate_gpt2_decoder_for_init_run set to False as it is now set to True by default)
Generators::Gpt gpt(*g_ort_env,
ORT_TSTR("C:/code/github/generators/Generators/models/gpt2_fp32.onnx"));
Generators::SearchParams params;
params.batch_size = static_cast<int>(input_ids_shape[0]);
params.sequence_length = static_cast<int>(input_ids_shape[1]);
params.input_ids = input_ids;
params.max_length = max_length;
params.num_beams = 4;
params.vocab_size = gpt.GetVocabSize();
Generators::BeamSearch search{params};
gpt.CreateInputs(search.sequence_lengths_, params);
while (!search.IsDone()) {
gpt.Run(search.GetNextTokens(), search.GetNextIndices(), search.GetSequenceLength());
search.SetLogits(gpt.GetLogits());
// Scoring
Generators::Processors::MinLength(search, 1);
Generators::Processors::RepetitionPenalty(search, 1.0f);
// Processors::LengthPenalty(search, 1.0f);
// Sampling goes here
// TODO: Are these steps always the same? If so, merge into one function
search.NextTokensFromLogits();
search.CheckForEOS();
search.AppendNextTokensToSequences();
}
std::vector<int32_t> output_sequence(search.params_.batch_size*max_length);
search.Finalize(1, output_sequence, {});
// Verify outputs match expected outputs
for (int i = 0; i < search.params_.batch_size; i++) {
// auto sequence = search.sequences_.GetSequence(i);
auto sequence = std::span<int32_t>(output_sequence.data() + max_length * i, max_length);
auto* expected_output_start = &expected_output[i * search.params_.max_length];
ASSERT_TRUE(std::equal(expected_output_start, expected_output_start + search.params_.max_length, sequence.begin(), sequence.end()));
}
std::cout << "Test_Lib_BeamSearchTest_GptBeamSearchFp32 complete\r\n";
}
void Test_GreedySearchTest_GptGreedySearchFp32() {
std::vector<int64_t> input_ids_shape{2, 4};
std::vector<int32_t> input_ids{
0, 0, 0, 52, 0, 0, 195, 731};
std::vector<int64_t> parameter_shape{1};
std::vector<int32_t> max_length{10};
std::vector<int32_t> min_length{1};
std::vector<float> repetition_penalty{1.0f};
std::vector<int64_t> expected_output_shape{input_ids_shape[0], max_length[0]};
std::vector<int32_t> expected_output{
0, 0, 0, 52, 204, 204, 204, 204, 204, 204,
0, 0, 195, 731, 731, 114, 114, 114, 114, 114};
auto info = OrtMemoryInfo::Create("Cpu", OrtDeviceAllocator, 0, OrtMemTypeDefault);
auto input_ids_tensor = OrtValue::CreateTensor(
*info, input_ids.data(), input_ids.size(), input_ids_shape.data(), input_ids_shape.size());
auto max_length_tensor = OrtValue::CreateTensor(
*info, max_length.data(), max_length.size(), parameter_shape.data(), parameter_shape.size());
auto min_length_tensor = OrtValue::CreateTensor(
*info, min_length.data(), min_length.size(), parameter_shape.data(), parameter_shape.size());
auto repetition_penalty_tensor = OrtValue::CreateTensor(
*info, repetition_penalty.data(), repetition_penalty.size(), parameter_shape.data(), parameter_shape.size());
std::vector<OrtValue*> ort_inputs;
ort_inputs.push_back(input_ids_tensor.get());
ort_inputs.push_back(max_length_tensor.get());
ort_inputs.push_back(min_length_tensor.get());
ort_inputs.push_back(repetition_penalty_tensor.get());
const char* input_names[] = {"input_ids", "max_length", "min_length", "repetition_penalty"};
const char* const output_names[] = {"sequences"};
constexpr int min_cuda_architecture = 530;
auto session_options = OrtSessionOptions::Create();
auto session = OrtSession::Create(*g_ort_env, ORT_TSTR("C:/code/github/generators/Generators/models/tiny_gpt2_greedysearch_with_init_decoder.onnx"), session_options.get());
auto ort_outputs = session->Run(nullptr, input_names, ort_inputs.data(), ort_inputs.size(), output_names, 1);
ASSERT_EQ(ort_outputs.size(), 1U);
const auto& sequences = ort_outputs[0];
ASSERT_TRUE(sequences->IsTensor());
auto result_ts = sequences->GetTensorTypeAndShapeInfo();
ASSERT_EQ(ONNX_TENSOR_ELEMENT_DATA_TYPE_INT32, result_ts->GetElementType());
ASSERT_EQ(expected_output_shape, result_ts->GetShape());
const auto* result_vals = sequences->GetTensorData<int32_t>();
auto result_span = std::span(result_vals, expected_output.size());
ASSERT_TRUE(std::equal(expected_output.cbegin(), expected_output.cend(), result_span.begin(), result_span.end()));
std::cout << "Test_GreedySearchTest_GptGreedySearchFp32 complete\r\n";
}
void Test_Lib_GreedySearchTest_GptGreedySearchFp32() {
std::vector<int64_t> input_ids_shape{2, 4};
std::vector<int32_t> input_ids{0, 0, 0, 52, 0, 0, 195, 731};
int32_t max_length{10};
std::vector<int32_t> expected_output{
0, 0, 0, 52, 204, 204, 204, 204, 204, 204,
0, 0, 195, 731, 731, 114, 114, 114, 114, 114};
auto info = OrtMemoryInfo::Create("Cpu", OrtDeviceAllocator, 0, OrtMemTypeDefault);
// To generate this file:
// python convert_generation.py --model_type gpt2 -m hf-internal-testing/tiny-random-gpt2 --output tiny_gpt2_greedysearch_fp16.onnx --use_gpu --max_length 20
// And copy the resulting gpt2_init_past_fp32.onnx file into these two files (as it's the same for gpt2)
Generators::Gpt gpt(*g_ort_env,
ORT_TSTR("C:/code/github/generators/Generators/models/gpt2_fp32.onnx"));
Generators::SearchParams params;
params.batch_size = static_cast<int>(input_ids_shape[0]);
params.sequence_length = static_cast<int>(input_ids_shape[1]);
params.input_ids = input_ids;
params.vocab_size = gpt.GetVocabSize();
Generators::GreedySearch search{params};
gpt.CreateInputs(search.sequence_lengths_, params);
while (!search.IsDone()) {
gpt.Run(search.GetNextTokens(), {}, search.GetSequenceLength());
search.SetLogits(gpt.GetLogits());
// Scoring
Generators::Processors::MinLength(search, 1);
Generators::Processors::RepetitionPenalty(search, 1.0f);
// Sampling goes here
// TODO: Are these steps always the same? If so, merge into one function
search.NextTokensFromLogits();
search.CheckForEOS();
search.AppendNextTokensToSequences();
}
// Verify outputs match expected outputs
for (int i = 0; i < search.params_.batch_size; i++) {
auto sequence = search.sequences_.GetSequence(i);
auto* expected_output_start = &expected_output[i * search.params_.max_length];
ASSERT_TRUE(std::equal(expected_output_start, expected_output_start+search.params_.max_length, sequence.begin(), sequence.end()));
}
std::cout << "Test_Lib_GreedySearchTest_GptGreedySearchFp32 complete\r\n";
}
#if USE_CUDA
void Test_Lib_GreedySearchTest_GptGreedySearchFp32_Cuda() {
std::vector<int64_t> input_ids_shape{2, 4};
std::vector<int32_t> input_ids{0, 0, 0, 52, 0, 0, 195, 731};
int32_t max_length{10};
std::vector<int32_t> expected_output{
0, 0, 0, 52, 204, 204, 204, 204, 204, 204,
0, 0, 195, 731, 731, 114, 114, 114, 114, 114};
cudaError_t cuda_status = cudaSetDevice(0);
assert(cuda_status == cudaSuccess);
cudaStream_t cuda_stream;
cudaStreamCreate(&cuda_stream);
// To generate this file:
// python convert_generation.py --model_type gpt2 -m hf-internal-testing/tiny-random-gpt2 --output tiny_gpt2_greedysearch_fp16.onnx --use_gpu --max_length 20
// And copy the resulting gpt2_init_past_fp32.onnx file into these two files (as it's the same for gpt2)
Generators::Gpt_Cuda gpt(*g_ort_env, ORT_TSTR("C:/code/github/generators/Generators/models/gpt2_fp32.onnx"), cuda_stream);
Generators::SearchParams_Cuda params;
params.batch_size = static_cast<int>(input_ids_shape[0]);
params.sequence_length = static_cast<int>(input_ids_shape[1]);
params.input_ids = input_ids;
params.vocab_size = gpt.GetVocabSize();
params.p_allocator_cuda = &gpt.GetAllocatorCuda();
params.cuda_stream = cuda_stream;
Generators::GreedySearch_Cuda search{params};
gpt.CreateInputs(search.sequence_lengths_, params);
while (!search.IsDone()) {
gpt.Run(search.GetNextTokens(), {}, search.GetSequenceLength());
search.SetLogits(gpt.GetLogits());
// Scoring
Generators::Processors_Cuda::MinLength(search, 1);
// Sampling goes here
// TODO: Are these steps always the same? If so, merge into one function
search.NextTokensFromLogits();
search.CheckForEOS();
search.AppendNextTokensToSequences();
}
// Verify outputs match expected outputs
for (int i = 0; i < search.params_.batch_size; i++) {
auto sequence_gpu = search.sequences_.GetSequence(i);
auto sequence = std::make_unique<int32_t[]>(max_length);
cudaMemcpyAsync(sequence.get(), sequence_gpu.data(), max_length * sizeof(int32_t), cudaMemcpyDeviceToHost, cuda_stream);
cudaStreamSynchronize(cuda_stream);
auto* expected_output_start = &expected_output[i * search.params_.max_length];
ASSERT_TRUE(std::equal(expected_output_start, expected_output_start + search.params_.max_length, sequence.get(), sequence.get()+max_length));
}
std::cout << "Test_Lib_GreedySearchTest_GptGreedySearchFp32_Cuda complete\r\n";
}
void Test_Lib_BeamSearchTest_GptBeamSearchFp32_Cuda() {
int32_t max_length{20};
float length_penalty{1.0f};
std::vector<int64_t> input_ids_shape{3, 12};
std::vector<int32_t> input_ids{
0, 0, 0, 0, 0, 52, 195, 731, 321, 301, 734, 620,
41, 554, 74, 622, 206, 222, 75, 223, 221, 198, 224, 572,
0, 0, 0, 52, 328, 219, 328, 206, 288, 227, 896, 328};
std::vector<int32_t> expected_output{
0, 0, 0, 0, 0, 52, 195, 731, 321, 301, 734, 620, 131, 131, 131, 181, 638, 638, 638, 638,
41, 554, 74, 622, 206, 222, 75, 223, 221, 198, 224, 572, 292, 292, 292, 292, 292, 292, 292, 292,
0, 0, 0, 52, 328, 219, 328, 206, 288, 227, 896, 328, 328, 669, 669, 669, 669, 669, 669, 669};
cudaError_t cuda_status = cudaSetDevice(0);
assert(cuda_status == cudaSuccess);
cudaStream_t cuda_stream;
cudaStreamCreate(&cuda_stream);
// The ONNX model is generated like the following:
// python convert_generation.py --model_type gpt2 -m hf-internal-testing/tiny-random-gpt2
// --output tiny_gpt2_beamsearch_fp16.onnx --use_gpu --max_length 20
// (with separate_gpt2_decoder_for_init_run set to False as it is now set to True by default)
Generators::Gpt_Cuda gpt(*g_ort_env,
ORT_TSTR("C:/code/github/generators/Generators/models/gpt2_fp32.onnx"), cuda_stream);
Generators::SearchParams_Cuda params;
params.batch_size = static_cast<int>(input_ids_shape[0]);
params.sequence_length = static_cast<int>(input_ids_shape[1]);
params.input_ids = input_ids;
params.max_length = max_length;
params.num_beams = 4;
params.vocab_size = gpt.GetVocabSize();
params.p_allocator_cuda = &gpt.GetAllocatorCuda();
params.cuda_stream = cuda_stream;
Generators::BeamSearch_Cuda search{params};
gpt.CreateInputs(search.sequence_lengths_, params);
while (!search.IsDone()) {
gpt.Run(search.GetNextTokens(), search.GetNextIndices(), search.GetSequenceLength());
search.SetLogits(gpt.GetLogits());
// Scoring
Generators::Processors_Cuda::MinLength(search, 1);
Generators::Processors_Cuda::RepetitionPenalty(search, 1.0f);
// Sampling goes here
// TODO: Are these steps always the same? If so, merge into one function
search.NextTokensFromLogits();
search.AppendNextTokensToSequences();
}
size_t sequence_length=search.params_.batch_size*max_length;
auto output_sequence_cuda = Generators::CudaMallocArray<int32_t>(sequence_length);
auto output_sequence = std::make_unique<int32_t[]>(sequence_length);
search.Finalize(1, std::span<int32_t>(output_sequence_cuda.get(), sequence_length), {});
cudaMemcpyAsync(output_sequence.get(), output_sequence_cuda.get(), sequence_length * sizeof(int32_t), cudaMemcpyDeviceToHost, cuda_stream);
cudaStreamSynchronize(cuda_stream);
// Verify outputs match expected outputs
for (int i = 0; i < search.params_.batch_size; i++) {
auto sequence = std::span<int32_t>(output_sequence.get() + max_length * i, max_length);
auto* expected_output_start = &expected_output[i * search.params_.max_length];
ASSERT_TRUE(std::equal(expected_output_start, expected_output_start + search.params_.max_length, sequence.begin(), sequence.end()));
}
std::cout << "Test_Lib_BeamSearchTest_GptBeamSearchFp32_Cuda complete\r\n";
}
#endif