interplane.Get Omega
Read in your language

omega-helix

This project has not described itself yet.

Brought by drakestapleton

branch main Branches Tags History

omega-helix / native / univ / ugrammar.om

332 lines · 4379 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note native/univ/ugrammar.om: Pure seeded-OM-2 deterministic finite-state grammar-constrained
3note token masking inside the universal sampler (ollm-univ gsamp).
4note Grammar modes: 1 = RFC-8259 json_obj, 2 = ascii_ident, 3 = csv_num.
5
6global ugr_st ulong 8
7global ugr_mk ulong *
8global ugr_lg f64 *
9global ugr_buf ulong *
10text u_gsamp "gsamp"
11
12fn ugr_allow_range lo ulong hi ulong to void
13v = get ug 0
14e = add hi 1
15if gt e v
16e = v
17end
18c = lo
19while lt c e bound 65536
20put ugr_mk c 1
21c = add c 1
22end
23end
24
25fn ugr_allow_ch ch ulong to void
26if lt ch get ug 0
27put ugr_mk ch 1
28end
29end
30
31fn ugr_mask_all mode ulong left ulong to void
32v = get ug 0
33for c ulong v
34put ugr_mk c 0
35end
36st = get ugr_st 0
37kl = get ugr_st 1
38vl = get ugr_st 2
39if eq mode 2
40if eq st 0
41ugr_allow_range 65 90
42ugr_allow_range 97 122
43ugr_allow_ch 95
44else
45ugr_allow_range 65 90
46ugr_allow_range 97 122
47ugr_allow_range 48 57
48ugr_allow_ch 95
49ugr_allow_ch 32
50end
51ret
52end
53if eq mode 3
54if eq st 0
55ugr_allow_range 48 57
56else
57ugr_allow_range 48 57
58if gt left 1
59ugr_allow_ch 44
60end
61end
62ret
63end
64note Grammar mode 1: RFC-8259 json_obj (min budget >= 7 tokens)
65if eq st 0
66ugr_allow_ch 123
67ret
68end
69if eq st 1
70ugr_allow_ch 34
71ret
72end
73if eq st 2
74if eq kl 0
75ugr_allow_range 97 122
76else
77if or ge kl 8 le left 4
78ugr_allow_ch 34
79else
80ugr_allow_range 97 122
81ugr_allow_range 48 57
82ugr_allow_ch 95
83ugr_allow_ch 34
84end
85end
86ret
87end
88if eq st 3
89ugr_allow_ch 58
90ret
91end
92if eq st 4
93ugr_allow_range 48 57
94if ge left 4
95ugr_allow_ch 34
96end
97ret
98end
99if eq st 5
100if or le left 2 ge vl 12
101ugr_allow_ch 34
102else
103ugr_allow_range 97 122
104ugr_allow_range 65 90
105ugr_allow_range 48 57
106ugr_allow_ch 95
107ugr_allow_ch 45
108ugr_allow_ch 32
109ugr_allow_ch 34
110end
111ret
112end
113if eq st 6
114if or le left 1 ge vl 6
115ugr_allow_ch 125
116else
117ugr_allow_range 48 57
118ugr_allow_ch 125
119if ge left 7
120ugr_allow_ch 44
121end
122end
123ret
124end
125if eq st 7
126if ge left 7
127ugr_allow_ch 44
128end
129ugr_allow_ch 125
130end
131end
132
133fn ugr_step_fsm mode ulong c ulong to void
134st = get ugr_st 0
135if eq mode 2
136if eq c 32
137put ugr_st 0 0
138else
139put ugr_st 0 1
140end
141ret
142end
143if eq mode 3
144if eq c 44
145put ugr_st 0 0
146else
147put ugr_st 0 1
148end
149ret
150end
151if eq st 0
152put ugr_st 0 1
153ret
154end
155if eq st 1
156put ugr_st 0 2
157put ugr_st 1 0
158ret
159end
160if eq st 2
161if eq c 34
162put ugr_st 0 3
163else
164put ugr_st 1 add get ugr_st 1 1
165end
166ret
167end
168if eq st 3
169put ugr_st 0 4
170ret
171end
172if eq st 4
173if eq c 34
174put ugr_st 0 5
175put ugr_st 2 0
176else
177if eq c 48
178put ugr_st 0 7
179else
180put ugr_st 0 6
181put ugr_st 2 1
182end
183end
184ret
185end
186if eq st 5
187if eq c 34
188put ugr_st 0 7
189else
190put ugr_st 2 add get ugr_st 2 1
191end
192ret
193end
194if eq st 6
195if eq c 125
196put ugr_st 0 8
197else
198if eq c 44
199put ugr_st 0 1
200else
201put ugr_st 2 add get ugr_st 2 1
202end
203end
204ret
205end
206if eq st 7
207if eq c 125
208put ugr_st 0 8
209else
210put ugr_st 0 1
211end
212end
213end
214
215fn ugr_pick_masked tm ulong st ulong to ulong
216v = get ug 0
217best = ulong 0
218bv = -1.0e300f64
219for c ulong v
220if eq get ugr_mk c 1
221lg = get ugr_lg c
222if gt lg bv
223bv = lg
224best = c
225end
226end
227end
228if eq tm 0
229ret best
230end
231invt = div 1000.0 f64 tm
232sm = 0.0f64
233for c2 ulong v
234if eq get ugr_mk c2 1
235w = ufexp mul sub get ugr_lg c2 bv invt
236put ugr_lg c2 w
237sm = add sm w
238else
239put ugr_lg c2 0.0f64
240end
241end
242u = mul mul f64 shr usmx st 11 1.1102230246251565e-16 sm
243acc = 0.0f64
244for c3 ulong v
245acc = add acc get ugr_lg c3
246if lt u acc
247ret c3
248end
249end
250ret best
251end
252
253fn ugr_copy_logits to void
254for z ulong get ug 0
255lv = get utmp z
256put ugr_lg z lv
257end
258end
259
260fn ugr_alloc nn ulong to void
261r = enlarge ugr_mk get ug 0
262if ne r 0
263exit 70
264end
265r = enlarge ugr_lg get ug 0
266if ne r 0
267exit 70
268end
269r = enlarge ugr_buf nn
270if ne r 0
271exit 70
272end
273end
274
275fn ugr_emit_out cnt ulong to void
276uputc 71
277uputc 32
278for j ulong cnt
279uputc get ugr_buf j
280end
281uputc 10
282uputc 83
283for j2 ulong cnt
284uputc 32
285uputu get ugr_buf j2
286end
287uputc 10
288uflush
289end
290
291note gsamp CK SEED N TEMPMILLI GRAMMAR_MODE
292fn ucmd_gsamp to void
293if ne ulong argcnt 7
294exit 2
295end
296uck_load 2
297st = uargu 3
298nn = uargu 4
299tm = uargu 5
300gm = uargu 6
301if or eq gm 0 gt gm 3
302exit 2
303end
304if gt nn get ug 1
305nn = get ug 1
306end
307if or lt get ug 0 126 and eq gm 1 lt nn 8
308exit 2
309end
310ugr_alloc nn
311put ugr_st 0 0
312put ugr_st 1 0
313put ugr_st 2 0
314cur = ulong 0
315cnt = ulong 0
316for s ulong nn
317if and eq gm 1 eq get ugr_st 0 8
318break
319end
320ustep_kv s cur
321if ne tm 0
322st = add st 11400714819323198485
323end
324ugr_copy_logits
325ugr_mask_all gm sub nn s
326cur = ugr_pick_masked tm st
327put ugr_buf cnt cur
328cnt = add cnt 1
329ugr_step_fsm gm cur
330end
331ugr_emit_out cnt
332end