Repository navigation
Autograd hardened: bugs & inefficiencies fixed - #16
Conversation
Co-authored-by: sourcepirate <sourcepirate@users.noreply.github.com>
|
/oc make it more readable. currently it looks code is very unreadable and convoluted. |
Co-authored-by: sourcepirate <sourcepirate@users.noreply.github.com>
|
Readability refactor completed while preserving hardened semantics:
Verified: |

Autograd robustness improvements completed across core engine:
neutro/autograd/tensor.py:18,33,37,42-as_tensorhandles numpy scalars robustly;Tensor.__init__validatesNone, casts to float64 with minimal copy and contiguous layout;__hash__uses identity to work with overridden__eq__(array return);__bool__raisesValueErrorfor multi-element tensors (previously ambiguousbool(np.array)error), allows scalar tensors;__setitem__clears stalegrad; addeddtype,__len__,__iter__guards.neutro/autograd/tape.py:6,10,15,22,34- Stack safety:__exit__validates top-of-stack and removes mismatched tape;watchaccepts iterables and type-checks;_record_opdefensive copy;gradientvalidatestarget/sources, normalizes single tensor, handles missing ops, wraps backward exceptions, pads/truncates mismatched grad lengths (preventing silentziptruncation), uses out-of-placegrad = grad+igto avoid aliasing, handles batched accumulation for shared inputs and broadcasted grads.neutro/autograd/function.py:7,15-_Ctx.save_for_backwardisolated;Function.applyseparates tensor positional andTensorkwargs, tracksall_tensor_inputs, converts output vianp.asarray, checkstape._watchedviaany, copiessaved_datawithout mutating originalctx(converts remainingTensorvalues to data), closure captures lists to avoid late binding,bwhandlesNonereturns, length mismatches (pads kwargs grads), callsbroadcast_backwardper input, returns list aligned with recorded inputs.neutro/autograd/utils.py:4-broadcast_backwardhandles scalar original shape, leading broadcast dims,keepdimsexpansion via building expanded shape andbroadcast_to, raises informative error on mismatch;numeric_gradensures scalar sum and correct dtype;gradclosecoerces to array.neutro/autograd/ops.py:6,40,81,95,112,134,147,158,171,182,207,220,236,247,258,269,290,303,318,330,340,351,356- Extracted_normalize_axis/_expand_grad_to_shapehelpers;_Add/_Subremoved unnecessarysave_for_backward;_Powhandlesk==0;_Matmulhandles vector-vectorg*b, vector-matrix batches viaouter/matmuland generic_swap_last_twofor batched ND;_Sum/_Mean/_Maxuse_expand_grad_to_shapesupporting tuple axes, negative axes,keepdimsTrue/False (fixesnp.expand_dimstuple bug andnp.onesmemory waste);_Transposevalidates permutation and normalizes negative axes;_Slicesaves only shape/dtype/idx, usesnp.add.atfor duplicate fancy indices;_Relusaves bool mask;_Sigmoidsaves view;_Tanhsaves outputt;_Sqrtsavesoutand clamps;_Expsavesout;_Logclamps;_Maxdivides ties equally and expands grad via helper;_Concatenatenormalizes axis;_Tilerobust torepslength mismatch (padseff_x/reps_padded, splits iteratively, reshapes to original);_Cliphandles scalar bounds;_Absusesnp.sign(0 at 0, fixes+1e-15sign flip);_Repeatnormalizes axis;_Maximumsplits ties equally; added validation forreshape,tilereps,clipbounds,repeatrepeats.Verified with
tests/autograd(46 tests) and full suite (358+53 tests) plus manual gradchecks for tuple-axis sum/mean, negative axes, batched matmul, tile with mismatchedreps, duplicate slice indices,absat 0,clip,max,maximumties,var, nested tapes.Closes #15
opencode session | github run