interplane.Get Omega
Read in your language

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

branch main Branches Tags History

omega-mv / src / autodiff / gradient_shape.om

52 lines · 1300 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note An M21 unbroadcast first removes excess leading axes, then sums expanded
3note singleton dimensions left to right with the axis retained. A step is
4note twice its axis plus one when the reduction keeps the dimension.
5om2
6
7export fn ad_unbroadcast_steps source view ulong * target view ulong * steps edit uchar 8 to long
8requires le len source 8
9requires 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
26end
27
28export fn ad_same_shape lhs view ulong * rhs view ulong * to ulong
29requires le len lhs 8
30requires 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
40end
41
42export fn ad_restore_axis shape view ulong * axis ulong restored edit ulong * to void
43requires le len shape 8
44requires 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
52end