main.bend
mlbend-generative@0.1.1.0 · 422 lines · 36 definitions
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
31import Base
32
33
34
35
36def mix32(+z: U32) -> U32:
37 +a = U32.xor(z, U32.shrn(z, 16n))
38 +b = (a * 2246822519 : U32)
39 +c = U32.xor(b, U32.shrn(b, 13n))
40 +e = (c * 3266489917 : U32)
41 U32.xor(e, U32.shrn(e, 16n))
42
43
44def unxor13(+y: U32) -> U32:
45 a = U32.xor(y, U32.shrn(y, 13n))
46 U32.xor(a, U32.shrn(y, 26n))
47
48
49
50
51def mix32_inv(+z: U32) -> U32:
52 +d = U32.xor(z, U32.shrn(z, 16n))
53 +c = (d * 2828982549 : U32)
54 +b = unxor13(c)
55 +a = (b * 3066638151 : U32)
56 U32.xor(a, U32.shrn(a, 16n))
57
58
59
60def hash_of(+s: Nat, +i: Nat) -> U32:
61 +k = (U32.from_nat(s) * 2654435769 : U32)
62 (k + (U32.from_nat(i) + 305419898 : U32) : U32)
63
64
65
66def uniform(+s: Nat, +i: Nat) -> F32:
67 h = mix32(hash_of(s, i))
68 m = ((U32.and(h, U32.from_nat(16777215n)) + 1 : U32) : U32)
69 (F32.from_nat(U32.to_nat(m)) / 16777216.0 : F32)
70
71
72
73def gauss_body(is_even: Bool, +r: F32, +ang: F32) -> F32:
74 match is_even:
75 case True{}:
76 (r * F32.cos(ang) : F32)
77 case False{}:
78 (r * F32.sin(ang) : F32)
79
80def gauss_at(+s: Nat, +j: Nat) -> F32:
81 +pair = Nat.sub(j, Nat.mod(j, 2n))
82 +ua = uniform(s, pair)
83 +ub = uniform(s, (1n + pair : Nat))
84 +r = F32.sqrt((0.0 - (2.0 * F32.log(ua) : F32) : F32))
85 gauss_body(Nat.is_eq(Nat.mod(j, 2n), 0n), r, (6.2831855 * ub : F32))
86
87
88
89def noise_from(+s: Nat, +left: Nat, +from: Nat) -> List<&2, F32>:
90 match left:
91 case 0n:
92 Nil{}
93 case 1n+rest:
94 Con{gauss_at(s, from), noise_from(s, rest, (1n + from : Nat))}
95
96
97def noise_of(+s: Nat, +m: Nat) -> List<&2, F32>:
98 noise_from(s, m, 0n)
99
100
101law noise_length:
102 for +s: Nat
103 for +m: Nat
104 for +from: Nat
105 {List.length(&2, F32, noise_from(s, m, from)) == m : Nat}
106
107
108def noise_length(s, m, from):
109 match m:
110 case 0n:
111 {==}
112 case 1n+rest:
113 %noise_length(s, rest, (1n + from : Nat)) :
114 {1n+List.length(&2, F32, noise_from(s, rest, (1n + from : Nat))) == 1n+_ : Nat}
115 {==}
116
117
118
119
120
121
122
123def cos_f(+t: Nat, +steps: Nat) -> F32:
124 +x = (((F32.from_nat(t) / F32.from_nat(steps) : F32) + 0.008 : F32) / 1.008 : F32)
125 +c = F32.cos((1.5707964 * x : F32))
126 (c * c : F32)
127
128
129def alpha_at(+t: Nat, +steps: Nat) -> F32:
130 (cos_f(t, steps) / cos_f(0n, steps) : F32)
131
132
133
134
135def beta_at(+t: Nat, +steps: Nat) -> F32:
136 F32.min((1.0 - (alpha_at(t, steps) / alpha_at(Nat.sub(t, 1n), steps) : F32) : F32), 0.99)
137
138
139
140def alphas_from(+steps: Nat, +left: Nat, +from: Nat) -> List<&2, F32>:
141 match left:
142 case 0n:
143 Nil{}
144 case 1n+rest:
145 Con{alpha_at(from, steps), alphas_from(steps, rest, (1n + from : Nat))}
146
147
148def alphas(+steps: Nat) -> List<&2, F32>:
149 alphas_from(steps, steps, 1n)
150
151
152law alphas_length:
153 for +t: Nat
154 for +left: Nat
155 for +from: Nat
156 {List.length(&2, F32, alphas_from(t, left, from)) == left : Nat}
157
158
159def alphas_length(t, left, from):
160 match left:
161 case 0n:
162 {==}
163 case 1n+rest:
164 %alphas_length(t, rest, (1n + from : Nat)) :
165 {1n+List.length(&2, F32, alphas_from(t, rest, (1n + from : Nat))) == 1n+_ : Nat}
166 {==}
167
168
169
170
171
172
173
174
175
176
177
178def lin_beta_at(+t: Nat, +steps: Nat) -> F32:
179 (0.0001 + ((0.2799 * (F32.from_nat(t) / F32.from_nat(steps) : F32)) : F32) : F32)
180
181
182
183
184def lin_alphas_from(+steps: Nat, +left: Nat, +from: Nat, +acc: F32) -> List<&2, F32>:
185 match left:
186 case 0n:
187 Nil{}
188 case 1n+rest:
189 +acc2 = (acc * (1.0 - lin_beta_at(from, steps) : F32) : F32)
190 Con{acc2, lin_alphas_from(steps, rest, (1n + from : Nat), acc2)}
191
192
193def lin_alphas(+steps: Nat) -> List<&2, F32>:
194 lin_alphas_from(steps, steps, 1n, 1.0)
195
196
197law lin_alphas_length:
198 for +t: Nat
199 for +left: Nat
200 for +from: Nat
201 for +acc: F32
202 {List.length(&2, F32, lin_alphas_from(t, left, from, acc)) == left : Nat}
203
204
205def lin_alphas_length(t, left, from, acc):
206 match left:
207 case 0n:
208 {==}
209 case 1n+rest:
210 %lin_alphas_length(t, rest, (1n + from : Nat),
211 (acc * (1.0 - lin_beta_at(from, t) : F32) : F32)) :
212 {1n+List.length(&2, F32, lin_alphas_from(t, rest, (1n + from : Nat),
213 (acc * (1.0 - lin_beta_at(from, t) : F32) : F32))) == 1n+_ : Nat}
214 {==}
215
216
217
218
219def freq_of(+p: Nat) -> F32:
220 match p:
221 case 0n:
222 0.001
223 case 1n:
224 0.01
225 case 2n:
226 0.1
227 case _:
228 1.0
229
230
231
232
233def temb_from(+t: Nat, +steps: Nat, +left: Nat, +p: Nat) -> List<&2, F32>:
234 match left:
235 case 0n:
236 Nil{}
237 case 1n+rest:
238 +tau = (F32.from_nat(t) / F32.from_nat(steps) : F32)
239 +f = freq_of(p)
240 Con{F32.sin((tau * f : F32)), Con{F32.cos((tau * f : F32)),
241 temb_from(t, steps, rest, (1n + p : Nat))}}
242
243
244def temb_of(+t: Nat, +steps: Nat, +k: Nat) -> List<&2, F32>:
245 temb_from(t, steps, k, 0n)
246
247
248law temb_length:
249 for +t: Nat
250 for +steps: Nat
251 for +k: Nat
252 for +p: Nat
253 {List.length(&2, F32, temb_from(t, steps, k, p)) == Nat.mul(k, 2n) : Nat}
254
255
256def temb_length(t, steps, k, p):
257 match k:
258 case 0n:
259 {==}
260 case 1n+rest:
261 %temb_length(t, steps, rest, (1n + p : Nat)) :
262 {1n+(1n+List.length(&2, F32, temb_from(t, steps, rest, (1n + p : Nat)))) == 1n+(1n+_) : Nat}
263 {==}
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279def groups_of(+n: Nat, +k: Nat, +skip: Nat) -> Nat:
280 match n:
281 case 0n:
282 0n
283 case 1n+m:
284 match skip:
285 case 0n:
286 (1n + groups_of(m, k, Nat.sub(k, 1n)) : Nat)
287 case 1n+r:
288 groups_of(m, k, r)
289
290
291
292
293def strided_from(-A: Data, xs: List<&2, A>, +k: Nat, +skip: Nat) -> List<&2, A>:
294 match xs:
295 case Nil{}:
296 Nil{}
297 case Con{h, t}:
298 match skip:
299 case 0n:
300 Con{h, strided_from(A, t, k, Nat.sub(k, 1n))}
301 case 1n+r:
302 strided_from(A, t, k, r)
303
304
305def strided(-A: Data, xs: List<&2, A>, +k: Nat) -> List<&2, A>:
306 strided_from(A, xs, k, Nat.sub(k, 1n))
307
308
309
310law strided_length:
311 for -A: Data
312 for +xs: List<&2, A>
313 for +k: Nat
314 for +skip: Nat
315 {List.length(&2, A, strided_from(A, xs, k, skip)) == groups_of(List.length(&2, A, xs), k, skip) : Nat}
316
317
318def strided_length(A, xs, k, skip):
319 match xs:
320 case Nil{}:
321 {==}
322 case Con{h, t}:
323 match skip:
324 case 0n:
325 %strided_length(A, t, k, Nat.sub(k, 1n)) :
326 {1n+List.length(&2, A, strided_from(A, t, k, Nat.sub(k, 1n))) == 1n+_ : Nat}
327 {==}
328 case 1n+r:
329 %strided_length(A, t, k, r) :
330 {List.length(&2, A, strided_from(A, t, k, r)) == _ : Nat}
331 {==}
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348def sigma_of(+abar: F32, +abar_prev: F32, +eta: F32) -> F32:
349 +above = F32.max(0.0, (1.0 - abar_prev : F32))
350 +below = F32.max(0.0, (1.0 - (abar / abar_prev : F32) : F32))
351 (eta * (F32.sqrt((above / (1.0 - abar : F32) : F32)) * F32.sqrt(below) : F32) : F32)
352
353
354
355
356
357
358type Step is Data:
359 Step{c0: F32, c1: F32, c2: F32, c3: F32, sig: F32}
360
361def step_of(+abar: F32, +abar_prev: F32, +eta: F32) -> Step:
362 +sig = sigma_of(abar, abar_prev, eta)
363 +c0 = (1.0 / F32.sqrt(abar) : F32)
364 +c1 = F32.sqrt(F32.max(0.0, (1.0 - abar : F32)))
365 +c2 = F32.sqrt(F32.max(0.0, abar_prev))
366 +c3 = F32.sqrt(F32.max(0.0, (1.0 - (abar_prev + (sig * sig : F32) : F32) : F32)))
367 Step{c0, c1, c2, c3, sig}
368
369
370def ddim_elem(+co: Step, +x: F32, +eps: F32, +z: F32) -> F32:
371 match co:
372 case Step{c0, c1, c2, c3, sig}:
373 +x0 = ((x - (c1 * eps : F32)) * c0 : F32)
374 (((x0 * c2 : F32) + (c3 * eps : F32) : F32) + (sig * z : F32) : F32)
375
376
377
378
379def head_or_zero(xs: List<&2, F32>) -> F32:
380 match xs:
381 case Nil{}:
382 0.0
383 case Con{h, t}:
384 h
385
386
387def tail_or_nil(xs: List<&2, F32>) -> List<&2, F32>:
388 match xs:
389 case Nil{}:
390 Nil{}
391 case Con{h, t}:
392 t
393
394
395
396
397
398def ddim(+co: Step, x: List<&2, F32>, +eps: List<&2, F32>, +z: List<&2, F32>) -> List<&2, F32>:
399 match x:
400 case Nil{}:
401 Nil{}
402 case Con{h, t}:
403 Con{ddim_elem(co, h, head_or_zero(eps), head_or_zero(z)),
404 ddim(co, t, tail_or_nil(eps), tail_or_nil(z))}
405
406
407law ddim_length:
408 for +co: Step
409 for +x: List<&2, F32>
410 for +eps: List<&2, F32>
411 for +z: List<&2, F32>
412 {List.length(&2, F32, ddim(co, x, eps, z)) == List.length(&2, F32, x) : Nat}
413
414
415def ddim_length(co, x, eps, z):
416 match x:
417 case Nil{}:
418 {==}
419 case Con{h, t}:
420 %ddim_length(co, t, tail_or_nil(eps), tail_or_nil(z)) :
421 {1n+List.length(&2, F32, ddim(co, t, tail_or_nil(eps), tail_or_nil(z))) == 1n+_ : Nat}
422 {==}