-
Notifications
You must be signed in to change notification settings - Fork 3.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Some changes to Sampling Op #14218
Merged
Merged
Some changes to Sampling Op #14218
Changes from 2 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
#include <memory> | ||
#include <vector> | ||
#include "gtest/gtest.h" | ||
#include "core/common/gsl.h" | ||
#include "core/session/onnxruntime_cxx_api.h" | ||
#include "test/common/cuda_op_test_utils.h" | ||
|
||
extern std::unique_ptr<Ort::Env> ort_env; | ||
|
||
namespace onnxruntime { | ||
namespace test { | ||
|
||
TEST(SamplingTest, GptSampling) { | ||
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{15}; | ||
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, 0, 0, 52, 195, 731, 321, 301, 734, 620, 125, 543, 668, | ||
41, 554, 74, 622, 206, 222, 75, 223, 221, 198, 224, 572, 776, 213, 697, | ||
0, 0, 0, 52, 328, 219, 328, 206, 288, 227, 896, 328, 450}; | ||
|
||
Ort::MemoryInfo info("Cpu", OrtDeviceAllocator, 0, OrtMemTypeDefault); | ||
auto input_ids_tensor = Ort::Value::CreateTensor( | ||
info, input_ids.data(), input_ids.size(), input_ids_shape.data(), input_ids_shape.size()); | ||
|
||
auto max_length_tensor = Ort::Value::CreateTensor( | ||
info, max_length.data(), max_length.size(), parameter_shape.data(), parameter_shape.size()); | ||
|
||
auto min_length_tensor = Ort::Value::CreateTensor( | ||
info, min_length.data(), min_length.size(), parameter_shape.data(), parameter_shape.size()); | ||
|
||
auto repetition_penalty_tensor = Ort::Value::CreateTensor( | ||
info, repetition_penalty.data(), repetition_penalty.size(), parameter_shape.data(), parameter_shape.size()); | ||
|
||
std::vector<Ort::Value> ort_inputs; | ||
ort_inputs.push_back(std::move(input_ids_tensor)); | ||
ort_inputs.push_back(std::move(max_length_tensor)); | ||
ort_inputs.push_back(std::move(min_length_tensor)); | ||
ort_inputs.push_back(std::move(repetition_penalty_tensor)); | ||
const char* input_names[] = {"input_ids", "max_length", "min_length", "repetition_penalty"}; | ||
const char* const output_names[] = {"sequences"}; | ||
|
||
constexpr int min_cuda_architecture = 530; | ||
if (HasCudaEnvironment(min_cuda_architecture)) { | ||
wangyems marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Ort::SessionOptions session_options; | ||
#ifdef USE_CUDA | ||
Ort::ThrowOnError(OrtSessionOptionsAppendExecutionProvider_CUDA(session_options, 0)); | ||
#endif | ||
|
||
Ort::Session session(*ort_env, ORT_TSTR("testdata/transformers/tiny_gpt2_sampling.onnx"), session_options); | ||
|
||
auto ort_outputs = session.Run(Ort::RunOptions{}, 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 = gsl::make_span(result_vals, expected_output.size()); | ||
ASSERT_TRUE(std::equal(expected_output.cbegin(), expected_output.cend(), result_span.begin(), result_span.end())); | ||
} | ||
} | ||
|
||
} // namespace test | ||
} // namespace onnxruntime |
Binary file not shown.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about to make seed configurable? default set to -1. if >0, use the seed passed in by args
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
currently we only test sampling with default seed and small top_p. so this flag limits to help create the model with that input