Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Heuristic:1kbgz Tributary Dual Number Mismatch Prevention

From Leeroopedia
Knowledge Sources
Domains Automatic_Differentiation, Debugging
Last Updated 2026-10-14 09:15 GMT

Overview

Mixing dual-number and non-dual-number nodes in arithmetic operations raises `NotImplementedError`; all connected nodes must share the same `use_dual` setting.

Description

Tributary supports forward-mode automatic differentiation via dual numbers (tuples of `(value, derivative)`). Each Node has a `use_dual` flag that selects between standard and dual-number operator implementations. When binary or n-ary operators connect nodes, the code checks that all participating nodes share the same `use_dual` value. If they differ, a `NotImplementedError("Dual/Non-dual mismatch")` is raised. This is because dual-number arithmetic uses tuple-valued functions `(x[0], x[1])` while standard arithmetic uses scalar functions, and there is no automatic coercion between the two.

Usage

Use this heuristic when debugging `NotImplementedError: Dual/Non-dual mismatch` errors. This occurs when a dual-number node is connected to a non-dual node via arithmetic operators. Ensure all nodes in a computational chain have the same `use_dual` flag set at construction time.

The Insight (Rule of Thumb)

  • Action: Set `use_dual=True` consistently on all nodes in an autodiff graph, or `use_dual=False` on all nodes.
  • Value: Boolean flag — must be uniform across connected nodes.
  • Trade-off: Dual-number mode enables gradient computation but requires all nodes to participate; partial dual usage is not supported.
  • Compatibility: The `binary` and `n_ary` operator factory functions both enforce this check. The `unary` operator does not need the check since it only involves one node.

Reasoning

Dual numbers represent values as `(value, derivative)` tuples. Arithmetic operators like `Add`, `Sub`, `Mult`, `Div` have separate lambda implementations for scalar and dual modes. For example, multiplication in dual mode is `(x[0]*y[0], x[0]*y[1] + x[1]*y[0])` (product rule), while scalar mode is simply `x * y`. Mixing the two would cause runtime type errors (e.g., indexing into a scalar). The mismatch check is a guardrail that fails fast with a clear error message.

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment