Skip to content
Open
Changes from 2 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
29 changes: 17 additions & 12 deletions onnxruntime/contrib_ops/cpu/transformers/greedy_search_impl_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,30 @@ struct SamplingState : public ISamplingState<T> {
int seed,
bool is_cuda,
Stream* stream) {
int total_count = batch_size * vocab_size;
// Compute the product in SafeInt's checked domain by casting an operand first; otherwise
// an `int * int` multiply with model-controlled operands can silently positive-wrap before
// the SafeInt cast sees it, leading to under-allocated buffers (heap-buffer-overflow on the
// downstream memcpy in SamplingCpuHelper::Sample). Matches the pattern used for
// next_token_scores in GreedySearchState::Init below.
const SafeInt<size_t> total_count = SafeInt<size_t>(batch_size) * static_cast<size_t>(vocab_size);
Comment thread
apsonawane marked this conversation as resolved.
Outdated

this->h_softmaxed_score = AllocateBuffer<float>(cpu_allocator, h_softmaxed_score_buffer_, SafeInt<size_t>(total_count), stream);
this->h_softmaxed_score = AllocateBuffer<float>(cpu_allocator, h_softmaxed_score_buffer_, total_count, stream);

this->generator = std::default_random_engine{gsl::narrow_cast<uint32_t>(seed)};

if (is_cuda) {
this->d_index_in = AllocateBuffer<int>(allocator, d_index_in_buffer_, SafeInt<size_t>(total_count), stream);
this->d_index_out = AllocateBuffer<int>(allocator, d_index_out_buffer_, SafeInt<size_t>(total_count), stream);
this->d_offset = AllocateBuffer<int>(allocator, d_offset_buffer_, SafeInt<size_t>(batch_size + 1), stream);
this->d_sorted_score = AllocateBuffer<T>(allocator, d_sorted_score_buffer_, SafeInt<size_t>(total_count), stream);
this->d_sorted_softmaxed_score = AllocateBuffer<float>(allocator, d_sorted_softmaxed_score_buffer_, SafeInt<size_t>(total_count), stream);
this->d_softmaxed_score = AllocateBuffer<float>(allocator, d_softmaxed_score_buffer_, SafeInt<size_t>(total_count), stream);
this->d_index_in = AllocateBuffer<int>(allocator, d_index_in_buffer_, total_count, stream);
this->d_index_out = AllocateBuffer<int>(allocator, d_index_out_buffer_, total_count, stream);
this->d_offset = AllocateBuffer<int>(allocator, d_offset_buffer_, SafeInt<size_t>(batch_size) + 1, stream);
this->d_sorted_score = AllocateBuffer<T>(allocator, d_sorted_score_buffer_, total_count, stream);
this->d_sorted_softmaxed_score = AllocateBuffer<float>(allocator, d_sorted_softmaxed_score_buffer_, total_count, stream);
this->d_softmaxed_score = AllocateBuffer<float>(allocator, d_softmaxed_score_buffer_, total_count, stream);
this->d_sampled = AllocateBuffer<float>(allocator, d_sampled_buffer_, SafeInt<size_t>(batch_size), stream);
this->h_sampled_all = AllocateBuffer<float>(cpu_allocator, h_sampled_all_buffer_, SafeInt<size_t>(batch_size * max_iter), stream);
this->h_sampled_all = AllocateBuffer<float>(cpu_allocator, h_sampled_all_buffer_, SafeInt<size_t>(batch_size) * static_cast<size_t>(max_iter), stream);
this->d_indices = AllocateBuffer<int32_t>(allocator, d_indices_buffer_, SafeInt<size_t>(batch_size), stream);
this->temp_storage_bytes = 0;
// TODO: Do not allocate this buffer if there's no presence_mask
this->d_presence_mask = AllocateBuffer<int>(allocator, d_presence_mask_buffer_, SafeInt<size_t>(total_count), stream);
this->d_presence_mask = AllocateBuffer<int>(allocator, d_presence_mask_buffer_, total_count, stream);

std::uniform_real_distribution<float> distribution(0.0, 1.0);
static_cast<void>(distribution(this->generator));
Expand All @@ -49,8 +54,8 @@ struct SamplingState : public ISamplingState<T> {
}
} else {
// TODO: Some buffer can be reused for CPU
this->sorted_scores = AllocateBuffer<T>(cpu_allocator, sorted_scores_buffer_, SafeInt<size_t>(total_count), stream);
this->cumulative_probs = AllocateBuffer<T>(cpu_allocator, cumulative_probs_buffer_, SafeInt<size_t>(total_count), stream);
this->sorted_scores = AllocateBuffer<T>(cpu_allocator, sorted_scores_buffer_, total_count, stream);
this->cumulative_probs = AllocateBuffer<T>(cpu_allocator, cumulative_probs_buffer_, total_count, stream);
}
}

Expand Down
Loading