forked from flashinfer-ai/flashinfer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflashinfer_sampling_ops.cu
77 lines (67 loc) · 3.99 KB
/
flashinfer_sampling_ops.cu
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
/*
* Copyright (c) 2023 by FlashInfer team.
*
* 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 "pytorch_extension_utils.h"
void sampling_from_probs(at::Tensor probs, at::Tensor output,
std::optional<at::Tensor> maybe_indices, bool deterministic,
std::optional<at::Generator> gen);
void top_p_sampling_from_probs(at::Tensor probs, at::Tensor output,
std::optional<at::Tensor> maybe_indices,
std::optional<at::Tensor> maybe_top_p_arr, double top_p_val,
bool deterministic, std::optional<at::Generator> gen);
void top_k_sampling_from_probs(at::Tensor probs, at::Tensor output,
std::optional<at::Tensor> maybe_indices,
std::optional<at::Tensor> maybe_top_k_arr, int64_t top_k_val,
bool deterministic, std::optional<at::Generator> gen);
void min_p_sampling_from_probs(at::Tensor probs, at::Tensor output,
std::optional<at::Tensor> maybe_indices,
std::optional<at::Tensor> maybe_min_p_arr, double min_p_val,
bool deterministic, std::optional<at::Generator> gen);
void top_k_top_p_sampling_from_probs(at::Tensor probs, at::Tensor output,
std::optional<at::Tensor> maybe_indices,
std::optional<at::Tensor> maybe_top_k_arr, double top_k_val,
std::optional<at::Tensor> maybe_top_p_arr, double top_p_val,
bool deterministic, std::optional<at::Generator> gen);
void top_p_renorm_probs(at::Tensor probs, at::Tensor renorm_probs,
std::optional<at::Tensor> maybe_top_p_arr, double top_p_val);
void top_k_renorm_probs(at::Tensor probs, at::Tensor renorm_probs,
std::optional<at::Tensor> maybe_top_k_arr, int64_t top_k_val);
void top_k_mask_logits(at::Tensor logits, at::Tensor mask_logits,
std::optional<at::Tensor> maybe_top_k_arr, int64_t top_k_val);
void chain_speculative_sampling(at::Tensor draft_probs, at::Tensor draft_token_ids,
at::Tensor target_probs, at::Tensor output_token_ids,
at::Tensor output_accepted_token_num,
at::Tensor output_emitted_draft_token_num, bool deterministic,
std::optional<at::Generator> gen);
TORCH_LIBRARY_FRAGMENT(TORCH_EXTENSION_NAME, m) {
// Sample from probabilities
m.def("sampling_from_probs", sampling_from_probs);
// Top-k sampling from probabilities
m.def("top_k_sampling_from_probs", top_k_sampling_from_probs);
// Min-p sampling from probabilities
m.def("min_p_sampling_from_probs", min_p_sampling_from_probs);
// Top-p sampling from probabilities
m.def("top_p_sampling_from_probs", top_p_sampling_from_probs);
// Top-k and top-p sampling from probabilities
m.def("top_k_top_p_sampling_from_probs", top_k_top_p_sampling_from_probs);
// Renormalize probabilities by top-k mask
m.def("top_k_renorm_probs", top_k_renorm_probs);
// Renormalize probabilities by top-p mask
m.def("top_p_renorm_probs", top_p_renorm_probs);
// Mask logits by top-k mask
m.def("top_k_mask_logits", top_k_mask_logits);
// Speculative sampling from sequence of probabilities
m.def("chain_speculative_sampling", chain_speculative_sampling);
}