Skip to content

Autograd hardened: bugs & inefficiencies fixed - #16

Merged
sourcepirate merged 2 commits into
mainfrom
opencode/issue15-20261001050920
Oct 2, 2026
Merged

sourcepirate merged 2 commits into
mainfrom
opencode/issue15-20261001050920

Conversation

@opencode-agent

@opencode-agent opencode-agent Bot commented Oct 1, 2026

Copy link
Copy Markdown
Contributor

Autograd robustness improvements completed across core engine:

neutro/autograd/tensor.py:18,33,37,42 - as_tensor handles numpy scalars robustly; Tensor.__init__ validates None, casts to float64 with minimal copy and contiguous layout; __hash__ uses identity to work with overridden __eq__ (array return); __bool__ raises ValueError for multi-element tensors (previously ambiguous bool(np.array) error), allows scalar tensors; __setitem__ clears stale grad; added dtype, __len__, __iter__ guards.

neutro/autograd/tape.py:6,10,15,22,34 - Stack safety: __exit__ validates top-of-stack and removes mismatched tape; watch accepts iterables and type-checks; _record_op defensive copy; gradient validates target/sources, normalizes single tensor, handles missing ops, wraps backward exceptions, pads/truncates mismatched grad lengths (preventing silent zip truncation), uses out-of-place grad = grad+ig to avoid aliasing, handles batched accumulation for shared inputs and broadcasted grads.

neutro/autograd/function.py:7,15 - _Ctx.save_for_backward isolated; Function.apply separates tensor positional and Tensor kwargs, tracks all_tensor_inputs, converts output via np.asarray, checks tape._watched via any, copies saved_data without mutating original ctx (converts remaining Tensor values to data), closure captures lists to avoid late binding, bw handles None returns, length mismatches (pads kwargs grads), calls broadcast_backward per input, returns list aligned with recorded inputs.

neutro/autograd/utils.py:4 - broadcast_backward handles scalar original shape, leading broadcast dims, keepdims expansion via building expanded shape and broadcast_to, raises informative error on mismatch; numeric_grad ensures scalar sum and correct dtype; gradclose coerces 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_shape helpers; _Add/_Sub removed unnecessary save_for_backward; _Pow handles k==0; _Matmul handles vector-vector g*b, vector-matrix batches via outer/matmul and generic _swap_last_two for batched ND; _Sum/_Mean/_Max use _expand_grad_to_shape supporting tuple axes, negative axes, keepdims True/False (fixes np.expand_dims tuple bug and np.ones memory waste); _Transpose validates permutation and normalizes negative axes; _Slice saves only shape/dtype/idx, uses np.add.at for duplicate fancy indices; _Relu saves bool mask; _Sigmoid saves view; _Tanh saves output t; _Sqrt saves out and clamps; _Exp saves out; _Log clamps; _Max divides ties equally and expands grad via helper; _Concatenate normalizes axis; _Tile robust to reps length mismatch (pads eff_x/reps_padded, splits iteratively, reshapes to original); _Clip handles scalar bounds; _Abs uses np.sign (0 at 0, fixes +1e-15 sign flip); _Repeat normalizes axis; _Maximum splits ties equally; added validation for reshape, tile reps, clip bounds, repeat repeats.

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 mismatched reps, duplicate slice indices, abs at 0, clip, max, maximum ties, var, nested tapes.

Closes #15

New%20session%20-%202026-10-01T05%3A09%3A19.611Z
opencode session  |  github run

Co-authored-by: sourcepirate <sourcepirate@users.noreply.github.com>
@opencode-agent opencode-agent Bot mentioned this pull request Oct 1, 2026
@sourcepirate

Copy link
Copy Markdown
Owner

/oc make it more readable. currently it looks code is very unreadable and convoluted.

Co-authored-by: sourcepirate <sourcepirate@users.noreply.github.com>
@opencode-agent

opencode-agent Bot commented Oct 1, 2026

Copy link
Copy Markdown
Contributor Author

Readability refactor completed while preserving hardened semantics:

neutro/autograd/utils.py:1 – Added module docstring, simplified broadcast_backward (leading-dim + keepdims loop with clear error), concise numeric_grad/gradclose.

neutro/autograd/tensor.py:1 –Grouped into sections (conversion / creation / magic / properties / ops), identity __hash__, __bool__ guard, sorted comparison helpers, centralized float64 handling.

neutro/autograd/tape.py:1 – Extracted _normalize_sources, _align_grad_lengths, _accumulate_grads, _collect_source_grads; cleaned __exit__ stack handling and watch iterable support; removed inline defensive-copy noise.

neutro/autograd/function.py:1 – Split into _split_tensor_args, _split_tensor_kwargs, _build_saved_copy, _make_backward_fn, _align_backward_grads; removed late-binding closure clutter and dense comments.

neutro/autograd/ops.py:1 – Sectioned helpers/arithmetic/matmul/reductions/shape/unary; documented _normalize_axis/_expand_grad_to_shape/_swap_last_two; rewrote _Matmul via _matmul_vec_mat_left/_matmul_mat_vec_right (loop-based batched handling, removed if False dead code); simplified _Tile iterative summing, _Max/_Maximum tie handling, and removed redundant copies/verbose inline comments.

Verified: tests/autograd 46 passed and full suite 411 passed; manual gradchecks for tuple-axis/negative-axis sum, batched matmul vector cases, mismatched reps tile, duplicate slice indices, abs/clip/max/maximum ties, var, nested tapes all pass.

New%20session%20-%202026-10-01T14%3A44%3A43.898Z
opencode session  |  github run

@sourcepirate
sourcepirate merged commit 1044454 into main Oct 2, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Make autograd robust.

1 participant