-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsampling.ts
195 lines (184 loc) · 6.14 KB
/
sampling.ts
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
import * as z from "@/common/myZod.ts";
const GenerationOptionsSchema = z.aliasedObject(
z.object({
max_tokens: z.number().gte(0).nullish()
.openapi({
description: "Aliases: max_length",
}),
stop: z.union([
z.string().transform((str) => [str]),
z.array(z.string()),
])
.nullish().coalesce([])
.openapi({
description: "Aliases: stop_sequence",
}),
add_bos_token: z.boolean().nullish().coalesce(true),
ban_eos_token: z.boolean().nullish().coalesce(false)
.openapi({
description: "Aliases: ignore_eos",
}),
skip_special_tokens: z.boolean().nullish().coalesce(false),
seed: z.number().nullish(),
logit_bias: z.record(z.string(), z.number()).nullish().coalesce({}),
grammar_string: z.string().nullish(),
banned_tokens: z.union([
z.array(z.number()),
z.string()
.transform((str) =>
str.replaceAll(" ", "")
.split(",")
.filter((x) => /^\d+$/.test(x))
.map((x) => parseInt(x))
),
])
.nullish().coalesce([])
.openapi({
description: "Aliases: custom_token_bans",
}),
banned_strings: z.union([
z.string().transform((str) => [str]),
z.array(z.string()),
])
.nullish().coalesce([]),
}),
[
{ field: "max_tokens", aliases: ["max_length"] },
{ field: "ban_eos_token", aliases: ["ignore_eos"] },
{ field: "stop", aliases: ["stop_sequence"] },
{ field: "banned_tokens", aliases: ["custom_token_bans"] },
],
)
.openapi({
description: "Generation options",
});
const TemperatureSamplerSchema = z.object({
temperature: z.number().gte(0).nullish().coalesce(1),
temperature_last: z.boolean().nullish().coalesce(false),
})
.openapi({
description: "Temperature options",
});
const AlphabetSamplerSchema = z.aliasedObject(
z.object({
top_k: z.number().gte(-1).transform((top_k) => top_k == -1 ? 0 : top_k)
.nullish().coalesce(0),
top_p: z.number().gte(0).lte(1).nullish().coalesce(1),
min_p: z.number().gte(0).lte(1).nullish().coalesce(0),
typical: z.number().gt(0).lte(1).nullish().coalesce(1),
nsigma: z.number().gte(0).nullish().coalesce(0),
}),
[{ field: "typical", aliases: ["typical_p"] }],
)
.openapi({
description: "Alphabet samplers",
});
const PenaltySamplerSchema = z.aliasedObject(
z.object({
frequency_penalty: z.number().gte(0).nullish().coalesce(0),
presence_penalty: z.number().gte(0).nullish().coalesce(0),
repetition_penalty: z.number().gt(0).nullish().coalesce(1)
.openapi({
description: "Aliases: rep_pen",
}),
penalty_range: z.number().nullish().coalesce(-1)
.openapi({
description:
"Aliases: repetition_range, repetition_penalty_range, rep_pen_range",
}),
}),
[
{ field: "repetition_penalty", aliases: ["rep_pen"] },
{
field: "penalty_range",
aliases: [
"repetition_range",
"repetition_penalty_range",
"rep_pen_range",
],
},
],
)
.openapi({
description: "Penalty samplers",
});
const DrySchema = z.aliasedObject(
z.object({
dry_multiplier: z.number().nullish().coalesce(0),
dry_base: z.number().nullish().coalesce(0),
dry_allowed_length: z.number().nullish().coalesce(0),
dry_sequence_breakers: z.union([
z.string()
.transform((str) => {
if (!str.startsWith("[")) {
str = `[${str}]`;
}
// Parse can fail, so return a default value if it does
try {
return JSON.parse(str);
} catch {
return [];
}
}),
z.array(z.string()),
])
.nullish().coalesce([]),
dry_range: z.number().nullish().coalesce(0)
.openapi({
description: "Aliases: dry_penalty_last_n",
}),
}),
[{ field: "dry_range", aliases: ["dry_penalty_last_n"] }],
)
.openapi({
description: "DRY options",
});
const XtcSchema = z.object({
xtc_probability: z.number().nullish().coalesce(0),
xtc_threshold: z.number().nullish().coalesce(0.1),
})
.openapi({
description: "XTC options",
});
const DynatempSchema = z.aliasedObject(
z.object({
max_temp: z.number().gte(0).nullish().coalesce(1)
.openapi({
description: "Aliases: dynatemp_high",
}),
min_temp: z.number().gte(0).nullish().coalesce(1)
.openapi({
description: "Aliases: dynatemp_low",
}),
temp_exponent: z.number().gte(0).nullish().coalesce(1)
.openapi({
description: "Aliases: dynatemp_exponent",
}),
}),
[
{ field: "max_temp", aliases: ["dynatemp_high"] },
{ field: "min_temp", aliases: ["dynatemp_low"] },
{ field: "temp_exponent", aliases: ["dynatemp_exponent"] },
],
)
.openapi({
description: "DynaTemp options",
});
const MirostatSchema = z.object({
mirostat_mode: z.number().nullish().coalesce(0),
mirostat_tau: z.number().nullish().coalesce(1),
mirostat_eta: z.number().nullish().coalesce(0),
})
.openapi({
description: "Mirostat options",
});
// Construct from aliased sampler requests
export const BaseSamplerRequest = GenerationOptionsSchema
.and(TemperatureSamplerSchema)
.and(AlphabetSamplerSchema)
.and(PenaltySamplerSchema)
.and(DrySchema)
.and(XtcSchema)
.and(DynatempSchema)
.and(MirostatSchema);
export type BaseSamplerRequest = z.infer<typeof BaseSamplerRequest>;