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 / autodiff / gradient_shape.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note An M21 unbroadcast first removes excess leading axes, then sums expanded |
| 3 | note singleton dimensions left to right with the axis retained. A step is |
| 4 | note twice its axis plus one when the reduction keeps the dimension. |
| 5 | om2 |
| 6 | |
| 7 | export fn ad_unbroadcast_steps source view ulong * target view ulong * steps edit uchar 8 to long |
| 8 | requires le len source 8 |
| 9 | requires le len target 8 |
| 10 | if lt len source len target |
| 11 | ret -1 |
| 12 | end |
| 13 | leading = sub len source len target |
| 14 | for i ulong leading |
| 15 | put steps i 0 |
| 16 | end |
| 17 | count = leading |
| 18 | for axis ulong len target |
| 19 | note MUT:UNBROADCAST |
| 20 | if and eq get target axis 1 ne get source add leading axis 1 |
| 21 | put steps count uchar add mul axis 2 1 |
| 22 | count = add count 1 |
| 23 | end |
| 24 | end |
| 25 | ret long count |
| 26 | end |
| 27 | |
| 28 | export fn ad_same_shape lhs view ulong * rhs view ulong * to ulong |
| 29 | requires le len lhs 8 |
| 30 | requires le len rhs 8 |
| 31 | if ne len lhs len rhs |
| 32 | ret 0 |
| 33 | end |
| 34 | for d ulong len lhs |
| 35 | if ne get lhs d get rhs d |
| 36 | ret 0 |
| 37 | end |
| 38 | end |
| 39 | ret 1 |
| 40 | end |
| 41 | |
| 42 | export fn ad_restore_axis shape view ulong * axis ulong restored edit ulong * to void |
| 43 | requires le len shape 8 |
| 44 | requires eq len shape len restored |
| 45 | for d ulong len shape |
| 46 | if eq d axis |
| 47 | put restored d 1 |
| 48 | else |
| 49 | put restored d get shape d |
| 50 | end |
| 51 | end |
| 52 | end |