Graph & operators¶
The lazy Tensor graph and the operators it is built from. These are exposed at the top
level, so aneforge.graph.conv is reached as af.conv.
graph ¶
Lazy Tensor graph, op constructors, and nn helpers (conv, attention, GEGLU). The device-free frontend; _compile.py lowers it.
Tensor ¶
A node in the compute graph.
Source code in aneforge/graph.py
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 | |
inverse ¶
scaled_tanh ¶
threshold ¶
thresholded_relu ¶
clamped_relu ¶
min(beta, x) for x >= 0 else min(beta, alpha * x) (a leaky relu6).
sigmoid_hard ¶
Hard sigmoid: min(max(alpha * x + beta, 0), 1).
linear_activation ¶
alpha * x + beta (scalar affine, fused as one op).
greater ¶
Elementwise x > o -> a BOOL tensor (use as the cond of af.select).
Source code in aneforge/graph.py
prelu ¶
Per-channel PReLU: x if x>0 else alpha[c]*x. alpha: [C]; input rank>=3 [N,C,...].
Source code in aneforge/graph.py
reverse ¶
Reverse along axes (native reverse).
Source code in aneforge/graph.py
tile ¶
Repeat reps[i] times along each axis (native tile; factors of {2,3,4,8}).
Source code in aneforge/graph.py
reduce_log_sum_exp ¶
adds ¶
x + scalar as a fused scalar-add (the only fused way to inject a scalar offset; + needs two Tensors).
bmm_weight ¶
Batched matmul self [B,M,K] @ W [B,K,N], W a baked QUANTIZABLE weight (unlike @ with a _const,
which stays fp16) -- for per-expert projections, so compress= shrinks them.
Source code in aneforge/graph.py
linear ¶
x @ W.T (+ bias). W is [out, in] (PyTorch convention).
Source code in aneforge/graph.py
squeeze ¶
Remove size-1 dims at axes (native squeeze).
Source code in aneforge/graph.py
expand_dims ¶
Insert size-1 dims at axes (native expand_dims); axes index the OUTPUT rank.
Source code in aneforge/graph.py
flatten2d ¶
Collapse to 2-D about axis: [:axis] -> rows, [axis:] -> cols (native flatten2d).
Source code in aneforge/graph.py
slice_by_size ¶
Static per-axis slice x[begin[i]:begin[i]+size[i]] (native slice_by_size).
Source code in aneforge/graph.py
cumsum ¶
Cumulative sum along the last axis as x @ triu_ones (no native cumsum).
Source code in aneforge/graph.py
l1_norm ¶
log_sum ¶
sum_square ¶
l2_norm ¶
Per-axis L2-normalize x / sqrt(sum(x**2, axis) + eps) (built per-axis; MIL l2_norm is all-dims).
Source code in aneforge/graph.py
argmax ¶
Argmax along axis (keepdims). GlobalArgMinMax bridge (cut); 2D [C,W] only, indices fp16-encoded.
Source code in aneforge/graph.py
rms_norm ¶
RMSNorm over the last dim. gamma: a [D] array (baked) or a [1,D] Tensor (trainable).
Source code in aneforge/graph.py
layer_norm ¶
LayerNorm over the last dim (2D [M,D]). gamma/beta: [D] arrays (baked) or [1,D] Tensors (trainable; pass both).
Source code in aneforge/graph.py
channel_layer_norm ¶
LayerNorm over the CHANNEL axis of [N,C,1,S] (ANE transformer layout; no transpose). gamma/beta: [C].
Source code in aneforge/graph.py
group_norm ¶
GroupNorm over [1,C,H,W]. gamma/beta: [C] arrays (baked) or [1,C,1,1] Tensors (trainable; pass both).
Source code in aneforge/graph.py
avg_pool ¶
avg_pool(k: int, stride: int | None = None, pad: int = 0, ceil_mode: bool = False, exclude_pad: bool = False) -> 'Tensor'
Average pool. exclude_pad divides by the valid (non-pad) cell count (ONNX count_include_pad=0); default includes pad cells (divide by full kernel area).
Source code in aneforge/graph.py
upsample ¶
Nearest-neighbour upsample [N,C,H,W] -> [N,C,scaleH,scaleW].
input ¶
A graph input placeholder (fed to the Model in creation order). dtype: "fp16" or "uint8" (raw bytes, dequantised in-graph; see af.image_input).
max_abs declares a bound on |value| at runtime, which lets the optimizer keep a lossy variant
whose encoding range provably covers this graph (see af.compile(opt=1) and #155). It is a promise,
not a clamp: nothing enforces it at dispatch, and feeding larger values makes the bound wrong. Left
undeclared the bound is unknown, and the optimizer stays fail-closed.
Source code in aneforge/graph.py
image_input ¶
A uint8 image input dequantised to fp16 on-engine (cast -> mul(scale) -> add(bias)). scale/bias are scalar or length-C (NCHW per-channel, broadcast [1,C,1,1]).
Source code in aneforge/graph.py
conv ¶
conv(x: Tensor, weight, stride: int = 1, pad: 'int | tuple[int, int, int, int]' = 0, dilation: int = 1, groups: int = 1, bias=None) -> Tensor
2D conv. x: [N,Cin,H,W]; weight: [Cout, Cin/groups, kH, kW]; bias: [Cout]. pad is a scalar (symmetric) or a (top, bottom, left, right) tuple.
Source code in aneforge/graph.py
dynamic_conv ¶
dynamic_conv(x: Tensor, weight: Tensor, stride: int = 1, pad: int = 0, dilation: int = 1, groups: int = 1) -> Tensor
2D conv with a runtime-tensor weight (native dynamic-kernel path; hypernetworks/per-sample kernels). x: [1,Cin,H,W]; weight Tensor [Cout,Cin/g,kH,kW]; batch must be 1.
Source code in aneforge/graph.py
conv_transpose ¶
conv_transpose(x: Tensor, weight, stride: int = 1, pad: int = 0, dilation: int = 1, groups: int = 1, bias=None) -> Tensor
2D transposed conv (deconv). x: [N,Cin,H,W]; weight: [Cin,Cout,kH,kW] (PyTorch layout); bias: [Cout].
Source code in aneforge/graph.py
batch_norm ¶
Inference BatchNorm over [1,C,...] from running mean/var. gamma/beta/mean/var: [C].
Source code in aneforge/graph.py
maximum ¶
minimum ¶
concat ¶
Concatenate tensors along axis (e.g. UNet skip connections).
Source code in aneforge/graph.py
gather ¶
Gather slices along axis by STATIC integer indices via slice_by_size + concat (no native gather).
Source code in aneforge/graph.py
stack ¶
Stack equal-shaped tensors along a NEW axis (native stack), N inserted at axis.
Source code in aneforge/graph.py
split ¶
Split x into num_splits equal parts along axis (native split); axis size must divide evenly.
Source code in aneforge/graph.py
select ¶
Elementwise cond ? a : b (native select). cond is a BOOL tensor; a/b are fp16.
Source code in aneforge/graph.py
instance_norm ¶
InstanceNorm over [N,C,H,W] (per-(N,C)-slice spatial norm + per-channel affine). gamma/beta: [C].
Source code in aneforge/graph.py
local_response_norm ¶
local_response_norm(x: Tensor, size: int = 5, alpha: float = 0.0001, beta: float = 0.75, k: float = 1.0) -> Tensor
Cross-channel LRN over [N,C,H,W] via the fused native local_response_norm op (no cut; distinct from the af.lrn bridge).
Source code in aneforge/graph.py
einsum_native ¶
Restricted batched contraction via the native einsum op (distinct from af.einsum). Only 'nchw,nwhu->nchu' is reachable: a=[N,C,H,W], b=[N,W,H,U] -> [N,C,H,U].
Source code in aneforge/graph.py
space_to_depth ¶
Space-to-depth (native space_to_depth): [N,C,H,W] -> [N,C*bs*bs,H/bs,W/bs]. Fused (no cut).
Source code in aneforge/graph.py
depth_to_space ¶
Depth-to-space (native depth_to_space): [N,C*bs*bs,H,W] -> [N,C,H*bs,W*bs]. Fused (no cut).
Source code in aneforge/graph.py
crop ¶
Spatial crop of [N,C,H,W]: drop top/bottom rows, left/right cols (native crop, no cut).
Source code in aneforge/graph.py
resize_nearest_neighbor ¶
Nearest-neighbour resize of [N,C,H,W] to (target_h, target_w) (native, no cut).
Source code in aneforge/graph.py
resize_bilinear ¶
Bilinear resize of [N,C,H,W] to (target_h, target_w) (native, no cut). Half-pixel sampling by default.
Source code in aneforge/graph.py
upsample_bilinear ¶
Bilinear upsample of [N,C,H,W] by integer scale (native, no cut). Half-pixel sampling by default.
Source code in aneforge/graph.py
affine ¶
2-D affine warp of [N,C,H,W] to (output_h, output_w) (native affine, no cut). transform: [N,6] [a0,a1,a2,b0,b1,b2] in normalized [-1,1] coords.
Source code in aneforge/graph.py
pixel_shuffle ¶
Depth-to-space upscale (PyTorch nn.PixelShuffle): [N,C*r*r,H,W] -> [N,C,H*r,W*r]. Fused (no cut).
Source code in aneforge/graph.py
pixel_unshuffle ¶
Space-to-depth (PyTorch nn.PixelUnshuffle): [N,C,H*r,W*r] -> [N,C*r*r,H,W]. Fused (no cut).
Source code in aneforge/graph.py
space_to_channel ¶
Space-to-depth on the native SpaceToChannel layer (TF channel order): [N,C,H*r,W*r] -> [N,C*r*r,H,W]. Graph cut.
Source code in aneforge/graph.py
channel_to_space ¶
Depth-to-space on the native ChannelToSpace layer (TF channel order): [N,C*r*r,H,W] -> [N,C,H*r,W*r]. Graph cut.
Source code in aneforge/graph.py
space_to_batch ¶
Move spatial blocks into batch (native SpaceToBatch): [N,C,H,W] -> [N*bh*bw,C,H/bh,W/bw]. Graph cut (batch grows).
Source code in aneforge/graph.py
batch_to_space ¶
Move batch blocks back into space (native BatchToSpace, inverse of space_to_batch): [N*bh*bw,C,H,W] -> [N,C,H*bh,W*bw]. Graph cut; batch must divide bh*bw.
Source code in aneforge/graph.py
flatten ¶
Flatten on the native Flatten layer: collapse [C,H,W] to a 1-D vector. Graph cut.
Source code in aneforge/graph.py
input_view ¶
Contiguous view x[offset:offset+size] along Width (native InputView); x flattened to 1-D -> [size]. Graph cut.
Source code in aneforge/graph.py
dynamic_slice ¶
Runtime-parametric slice x[start:start+size] (native DynamicSlice). Graph cut; only verified variant is Width=4, size==2.
Source code in aneforge/graph.py
scaled_elementwise ¶
scale * (x OP z) (native ScaledElementWise). op in {Add,Mult,Min,Max}; equal-size inputs. Graph cut; Sub rejected, Mult ignores scale.
Source code in aneforge/graph.py
topk ¶
Top-k per row of a 2D input [C,W] (native TopK bridge, a cut). k in {3,4} is arch-gated and rejected.
Source code in aneforge/graph.py
sort ¶
Sort each row of a 2D input [C,W] along Width (native Sort bridge, a cut). return_indices gives fp16-encoded argsort indices.
Source code in aneforge/graph.py
cross_product ¶
3-vector cross product cross(a,b) (native CrossProduct layer). Both inputs length-3; returns (3,). Graph cut.
Source code in aneforge/graph.py
cross_correlation ¶
Valid (no-flip) cross-correlation of map x [H,W] with template [Th,Tw] (native CrossCorrelation): y[i,j] = sum x[i+u,j+v]*template[u,v] -> [H-Th+1, W-Tw+1]. Graph cut.
Source code in aneforge/graph.py
cost_volume ¶
L1 stereo/flow matching cost (native CostVolume). aux length-Wa, ref length-Wr (Wr>=Wa+R) -> (R+1,Wa) with cost[d,x]=|aux[x]-ref[x+d]|. Graph cut.
Source code in aneforge/graph.py
fps ¶
Furthest-point sampling: greedily pick k far-apart points (native FurthestPointSampling, L2 only). points [N,3] -> [k,3] centroids. Graph cut.
Source code in aneforge/graph.py
radius_search ¶
L2 ball-query membership (native RadiusSearch): 1 iff point within radius of centroid. points [N,3], centroids [Nc,3] -> [N,Nc] 0/1. Graph cut.
Source code in aneforge/graph.py
minmax_norm ¶
Min-max normalize (x-min)/(max-min+eps) over dimension (native MinMaxNormalization). x [1,C,H,W]; "Width"/"Height" only. Graph cut.
Source code in aneforge/graph.py
lrn ¶
Cross-channel LRN (AlexNet) on the native LocalResponseNormalization layer (Channel mode). x [1,C,H,W]; graph cut. Window is a clipped local channel window of size N=C. Arch-gated: C<=15 only.
Source code in aneforge/graph.py
mha ¶
Multi-head self-attention on x [S,D]. Weights [out,in]; biases [D] or None. mask, when given,
is an additive score bias broadcast to [H,S,S] and sliced along the query axis -- pass [1,S,S] (or
[S,S]) with -inf/-1e4 at padded key columns for a key-padding mask (lets a padded batch share one
program without pad tokens corrupting the real ones).
Source code in aneforge/graph.py
cross_attention ¶
cross_attention(x: Tensor, context: Tensor, Wq, Wk, Wv, Wo, n_heads: int, bq=None, bk=None, bv=None, bo=None) -> Tensor
Cross-attention: queries from x [S,D], keys/values from context [T,Dctx]. Wq:[D,D]; Wk,Wv:[D,Dctx]; Wo:[D,D].
Source code in aneforge/graph.py
sdpa ¶
sdpa(q: Tensor, k: Tensor, v: Tensor, scale: float | None = None, is_causal: bool = False, attn_mask: 'Tensor | None' = None) -> Tensor
Scaled-dot-product attention via the native fused-attention layer (ANECSDPALayerDesc) inside the reliable regime, else the fused decomposition. q/k/v: [1,heads,seq,d_head] fp16; native use is a graph cut. is_causal=True is native (causal mask on the 5th bottom).
Source code in aneforge/graph.py
geglu ¶
GEGLU FFN gate: split the [2*Dff,D] projection into value/gate halves; out = value * gelu(gate).