-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathrandom_sample.h
More file actions
160 lines (135 loc) · 5.89 KB
/
random_sample.h
File metadata and controls
160 lines (135 loc) · 5.89 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
#ifndef INFINI_OPS_BASE_RANDOM_SAMPLE_H_
#define INFINI_OPS_BASE_RANDOM_SAMPLE_H_
#include <cstdint>
#include <optional>
#include "operator.h"
namespace infini::ops {
class RandomSample : public Operator<RandomSample> {
public:
// clang-format off
//
// logits: [batch_size, vocab_size] or [vocab_size] (batch_size=1)
// out: [batch_size] sampled token ids (int32/int64)
// valid: [batch_size] uint8 (0 or 1), whether sample is valid
//
// Per-batch parameters support two modes:
// - optional<Tensor> has value: per-batch tensor of shape [batch_size]
// - optional<Tensor> is nullopt: use the scalar _val for all requests
//
// When both (optional<Tensor> == nullopt) and (_val == default),
// the corresponding filtering is disabled.
//
// seed: per-request RNG seed. Same seed + offset produces identical
// results (reproducibility). Different requests should use
// different seeds.
// offset: per-step counter, increments each decode step within a request.
// Ensures different steps produce different samples even with the
// same seed.
//
// clang-format on
RandomSample(const Tensor logits, Tensor out, Tensor valid,
std::optional<Tensor> temperature, float temperature_val,
std::optional<Tensor> top_k, int top_k_val,
std::optional<Tensor> top_p, float top_p_val,
std::optional<Tensor> min_p, float min_p_val, std::uint64_t seed,
std::uint64_t offset, bool deterministic)
: logits_dtype_{logits.dtype()},
out_dtype_{out.dtype()},
ndim_{logits.ndim()},
batch_size_{ndim_ == 2 ? logits.size(-2) : 1},
vocab_size_{logits.size(-1)},
logits_strides_{logits.strides()},
temperature_{temperature},
temperature_val_{temperature_val},
top_k_{top_k},
top_k_val_{top_k_val},
top_p_{top_p},
top_p_val_{top_p_val},
min_p_{min_p},
min_p_val_{min_p_val},
seed_{seed},
offset_{offset},
deterministic_{deterministic} {
assert((ndim_ == 1 || ndim_ == 2) &&
"`RandomSample` requires 1D [vocab_size] or 2D [batch, vocab_size] "
"logits");
assert(out.ndim() == 1 && out.size(0) == batch_size_ &&
"`RandomSample` requires 1D output [batch_size]");
assert(valid.ndim() == 1 && valid.size(0) == batch_size_ &&
"`RandomSample` requires 1D valid [batch_size]");
assert((out_dtype_ == DataType::kInt32 || out_dtype_ == DataType::kInt64) &&
"`RandomSample` requires int32 or int64 output");
ValidateParams(temperature, top_k, top_p, min_p);
}
// Simplified constructor: no filtering, default temperature.
RandomSample(const Tensor logits, Tensor out, Tensor valid,
std::uint64_t seed, std::uint64_t offset)
: RandomSample{logits, out, valid, std::nullopt,
1.0f, std::nullopt, 0, std::nullopt,
1.0f, std::nullopt, 0.0f, seed,
offset, false} {}
virtual void operator()(const Tensor logits, Tensor out, Tensor valid,
std::optional<Tensor> temperature,
float temperature_val, std::optional<Tensor> top_k,
int top_k_val, std::optional<Tensor> top_p,
float top_p_val, std::optional<Tensor> min_p,
float min_p_val, std::uint64_t seed,
std::uint64_t offset, bool deterministic) const = 0;
virtual void operator()(const Tensor logits, Tensor out, Tensor valid,
std::uint64_t seed, std::uint64_t offset) const {
return operator()(logits, out, valid, temperature_, temperature_val_,
top_k_, top_k_val_, top_p_, top_p_val_, min_p_,
min_p_val_, seed, offset, deterministic_);
}
protected:
static void ValidateIntParam(std::optional<Tensor> t,
Tensor::Size batch_size) {
if (!t.has_value()) return;
const auto& tensor = *t;
assert(tensor.ndim() == 1 && tensor.size(0) == batch_size &&
"per-batch int param must be 1D [batch_size]");
assert((tensor.dtype() == DataType::kInt32 ||
tensor.dtype() == DataType::kInt64) &&
"per-batch int param must be int32 or int64");
}
static void ValidateFloatParam(std::optional<Tensor> t,
Tensor::Size batch_size) {
if (!t.has_value()) return;
const auto& tensor = *t;
assert(tensor.ndim() == 1 && tensor.size(0) == batch_size &&
"per-batch float param must be 1D [batch_size]");
assert((tensor.dtype() == DataType::kFloat32 ||
tensor.dtype() == DataType::kFloat64 ||
tensor.dtype() == DataType::kFloat16 ||
tensor.dtype() == DataType::kBFloat16) &&
"per-batch float param must be float16/bfloat16/float32/float64");
}
void ValidateParams(std::optional<Tensor> temperature,
std::optional<Tensor> top_k, std::optional<Tensor> top_p,
std::optional<Tensor> min_p) const {
ValidateFloatParam(temperature, batch_size_);
ValidateIntParam(top_k, batch_size_);
ValidateFloatParam(top_p, batch_size_);
ValidateFloatParam(min_p, batch_size_);
}
const DataType logits_dtype_;
const DataType out_dtype_;
Tensor::Size ndim_{0};
Tensor::Size batch_size_{1};
Tensor::Size vocab_size_{0};
Tensor::Strides logits_strides_;
// Per-batch or scalar sampling parameters.
std::optional<Tensor> temperature_;
float temperature_val_{1.0f};
std::optional<Tensor> top_k_;
int top_k_val_{0};
std::optional<Tensor> top_p_;
float top_p_val_{1.0f};
std::optional<Tensor> min_p_;
float min_p_val_{0.0f};
std::uint64_t seed_{0};
std::uint64_t offset_{0};
bool deterministic_{false};
};
} // namespace infini::ops
#endif