Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/omega-mv/blob/main/src/train/tg_sgd.om # 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 Sign in to star 1 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `omega-mv / src / train / tg_sgd.om` 152 lines ยท 4150 bytes Raw Blame History 1 note SPDX-License-Identifier: Apache-2.0 2 note tg_sgd.om -- Pure seeded OM-2 float32 SGD over a tg_store shadow. 3 note Composed with sgd_update.om and tg_store.om. 4 5 const TG_OP_SGD ulong 0x00444753 6 7 global tg_sgd_grad uint `1048576` 8 9 fn tg_load_u32_sh slot ulong is_opt ulong idx ulong to uint 10 p = mul idx 4 11 u = uint 0 12 if eq slot 0 13 if eq is_opt 0 14 u = uint get tg_sh0_params p 15 u = or u shl uint get tg_sh0_params add p 1 8 16 u = or u shl uint get tg_sh0_params add p 2 16 17 u = or u shl uint get tg_sh0_params add p 3 24 18 ret u 19 end 20 u = uint get tg_sh0_opt p 21 u = or u shl uint get tg_sh0_opt add p 1 8 22 u = or u shl uint get tg_sh0_opt add p 2 16 23 u = or u shl uint get tg_sh0_opt add p 3 24 24 ret u 25 end 26 if eq is_opt 0 27 u = uint get tg_sh1_params p 28 u = or u shl uint get tg_sh1_params add p 1 8 29 u = or u shl uint get tg_sh1_params add p 2 16 30 u = or u shl uint get tg_sh1_params add p 3 24 31 ret u 32 end 33 u = uint get tg_sh1_opt p 34 u = or u shl uint get tg_sh1_opt add p 1 8 35 u = or u shl uint get tg_sh1_opt add p 2 16 36 u = or u shl uint get tg_sh1_opt add p 3 24 37 ret u 38 end 39 40 fn tg_store_u32_sh slot ulong is_opt ulong idx ulong v uint to void 41 p = mul idx 4 42 if eq slot 0 43 if eq is_opt 0 44 put tg_sh0_params p uchar and v 255 45 put tg_sh0_params add p 1 uchar and shr v 8 255 46 put tg_sh0_params add p 2 uchar and shr v 16 255 47 put tg_sh0_params add p 3 uchar and shr v 24 255 48 ret 49 end 50 put tg_sh0_opt p uchar and v 255 51 put tg_sh0_opt add p 1 uchar and shr v 8 255 52 put tg_sh0_opt add p 2 uchar and shr v 16 255 53 put tg_sh0_opt add p 3 uchar and shr v 24 255 54 ret 55 end 56 if eq is_opt 0 57 put tg_sh1_params p uchar and v 255 58 put tg_sh1_params add p 1 uchar and shr v 8 255 59 put tg_sh1_params add p 2 uchar and shr v 16 255 60 put tg_sh1_params add p 3 uchar and shr v 24 255 61 ret 62 end 63 put tg_sh1_opt p uchar and v 255 64 put tg_sh1_opt add p 1 uchar and shr v 8 255 65 put tg_sh1_opt add p 2 uchar and shr v 16 255 66 put tg_sh1_opt add p 3 uchar and shr v 24 255 67 end 68 69 global tg_sgd_cfg ulong 4 70 71 fn tg_sgd_chunk first ulong cnt ulong to void 72 slot = get tg_sgd_cfg 0 73 pc = new uint 2048 0 74 vc = new uint 2048 0 75 gc = new uint 2048 0 76 for i ulong cnt 77 idx = add first i 78 put pc i tg_load_u32_sh slot 0 idx 79 put gc i get tg_sgd_grad idx 80 end 81 if ne get tg_sgd_cfg 3 0 82 for i ulong cnt 83 put vc i tg_load_u32_sh slot 1 add first i 84 end 85 tg_update_velocity part vc 0 cnt part gc 0 cnt get tg_sgd_cfg 2 86 tg_update_params part pc 0 cnt part vc 0 cnt get tg_sgd_cfg 1 87 for i ulong cnt 88 tg_store_u32_sh slot 1 add first i get vc i 89 end 90 else 91 tg_update_params part pc 0 cnt part gc 0 cnt get tg_sgd_cfg 1 92 end 93 for i ulong cnt 94 tg_store_u32_sh slot 0 add first i get pc i 95 end 96 end 97 98 fn tg_sgd_apply slot ulong n ulong lr_bits ulong mom_bits ulong use_v ulong to void 99 put tg_sgd_cfg 0 slot 100 put tg_sgd_cfg 1 lr_bits 101 put tg_sgd_cfg 2 mom_bits 102 put tg_sgd_cfg 3 use_v 103 first = ulong 0 104 while lt first n bound 1024 105 cnt = sub n first 106 if gt cnt 2048 107 cnt = 2048 108 end 109 tg_sgd_chunk first cnt 110 first = add first cnt 111 end 112 end 113 114 fn tg_sgd_step slot ulong n ulong lr_bits ulong mom_bits ulong grad_ref ulong to long 115 if or eq get tg_st_meta 0 0 or gt slot 1 or eq n 0 gt n `1048576` 116 ret TG_E_ARG 117 end 118 mode = tg_sgd_mode lr_bits mom_bits 119 if lt mode 0 120 ret TG_E_ARG 121 end 122 base = mul slot 8 123 bytes = mul n 4 124 if ne get tg_sh_meta add base 1 bytes 125 ret TG_E_ARG 126 end 127 if ne mode 0 128 if ne get tg_sh_meta add base 2 bytes 129 ret TG_E_ARG 130 end 131 end 132 refs = new ulong 9 0 133 nr = ulong 1 134 put refs 0 TG_BUF_PARAMS 135 put refs 1 0 136 put refs 2 bytes 137 if ne mode 0 138 put refs 3 TG_BUF_OPT 139 put refs 4 0 140 put refs 5 bytes 141 nr = 2 142 end 143 put refs mul nr 3 TG_BUF_EXTERNAL 144 put refs add mul nr 3 1 grad_ref 145 put refs add mul nr 3 2 bytes 146 r = tg_dispatch slot TG_OP_SGD or lr_bits shl mom_bits 32 grad_ref refs add nr 1 147 if ne r TG_OK 148 ret r 149 end 150 tg_sgd_apply slot n lr_bits mom_bits ulong mode 151 ret TG_OK 152 end