omega-mv
Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.
Brought by drakestapleton
omega-mv / src / train / sgd_update.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note Scalar arguments and storage carry IEEE binary32 bits. Each multiply |
| 3 | note and add or subtract is a separate operation, as specified by tg_sgd.h. |
| 4 | om2 |
| 5 | |
| 6 | export fn tg_sgd_mode learning ulong momentum ulong to long |
| 7 | note Finiteness is independent of FPCR. Ordered binary32 comparisons |
| 8 | note use the caller's flush-to-zero mode, including signed subnormals. |
| 9 | if or ge and learning 0x7fffffff 0x7f800000 ge and momentum 0x7fffffff 0x7f800000 |
| 10 | ret -1 |
| 11 | end |
| 12 | rate = bitcast f32 uint learning |
| 13 | weight = bitcast f32 uint momentum |
| 14 | zero = bitcast f32 uint 0 |
| 15 | one = bitcast f32 uint 0x3f800000 |
| 16 | if or le rate zero or lt weight zero ge weight one |
| 17 | ret -1 |
| 18 | end |
| 19 | if eq weight zero |
| 20 | ret 0 |
| 21 | end |
| 22 | ret 1 |
| 23 | end |
| 24 | |
| 25 | export fn tg_update_velocity velocity edit uint * gradient view uint * bits ulong to void |
| 26 | requires eq len velocity len gradient |
| 27 | momentum = bitcast f32 uint bits |
| 28 | for i ulong len velocity |
| 29 | previous = bitcast f32 get velocity i |
| 30 | contribution = mul momentum previous |
| 31 | updated = add contribution bitcast f32 get gradient i |
| 32 | put velocity i bitcast uint updated |
| 33 | end |
| 34 | end |
| 35 | |
| 36 | export fn tg_update_params params edit uint * direction view uint * bits ulong to void |
| 37 | requires eq len params len direction |
| 38 | learning = bitcast f32 uint bits |
| 39 | for i ulong len params |
| 40 | value = bitcast f32 get params i |
| 41 | step = mul learning bitcast f32 get direction i |
| 42 | put params i bitcast uint sub value step |
| 43 | end |
| 44 | end |