gradient.h#

namespace onnx_light

Alias that makes onnx-light headers compatible with code that references ONNX_LIGHT_NAMESPACE (the macro used in the standard onnx package).

Set to ONNX_LIGHT_NAMESPACE so both names resolve to the same namespace.

Symbol-visibility attribute for the public onnx-light C++ API.

Maps the upstream compatibility macro to onnx-light’s explicit proto ABI annotation. This keeps declarations from vendored ONNX headers visible when lib_onnx_proto uses hidden visibility by default.

Namespace alias so that ONNX C++ code (and consumers such as onnxruntime) that refers to the literal onnx namespace — rather than the ONNX_NAMESPACE macro — resolves to the onnx-light namespace. The standard onnx package lives in namespace onnx; onnx-light uses onnx_light (via ONNX_LIGHT_NAMESPACE), so this alias keeps onnx-light a true drop-in. It is only introduced when the onnx-light namespace differs from onnx.

namespace core
namespace gradient

Functions

FunctionProto GradientOfNodes(std::span<const NodeProto> nodes, std::span<const std::string> inputs, std::span<const TensorProto> initializers, std::span<const std::string> xs, const std::string &y, std::span<const std::string> zs, const GradRegistry &registry)#

Computes the gradient FunctionProto from a list of ONNX nodes.

Performs reverse-mode automatic differentiation over the given nodes and returns a FunctionProto that computes the partial derivatives of y with respect to each variable in xs.

The returned FunctionProto has:

  • inputs : xs values followed by zs values, then “dy” (the incoming gradient of y, typically ones_like(y) for a scalar loss).

  • outputs: one gradient tensor per element of xs, named “grad_<xs[i]>”.

Parameters:
  • nodes – The forward computation nodes in topological order.

  • inputs – Names of all graph inputs. Accepted for API completeness; unused by the current algorithm but reserved for future use (e.g. gradient pruning based on graph-input status).

  • initializers – Constant tensors embedded in the forward graph.

  • xs – Variable names to differentiate with respect to.

  • y – The output tensor name whose gradient is computed.

  • zs – Additional non-differentiable input variable names.

  • registry – Operator-to-GradFn map used for backward dispatch. Pass a populated registry (e.g. from onnx_gradient’s DefaultGradRegistry, or a custom one built with RegisterGradientFunction) to support specific operators.

Throws:
  • std::invalid_argument – if xs is empty, y is empty, or y cannot be reached from the given nodes.

  • std::runtime_error – if an op_type is not found in registry on the path from the inputs to y.

Returns:

A FunctionProto encoding the gradient computation.

FunctionProto GradientOfFunction(const FunctionProto &function, std::span<const std::string> xs, const std::string &y, std::span<const std::string> zs, const GradRegistry &registry)#

Computes the gradient FunctionProto from an existing FunctionProto.

The function is expected to take its initializers as regular inputs (i.e. the caller bakes model parameters into the function’s input list rather than embedding them as graph initializers). This is a common pattern when a model is expressed as a pure function for training purposes.

Parameters:
  • function – The forward computation as a FunctionProto.

  • xs – Variable names (among function inputs) to differentiate with respect to.

  • y – The output tensor name whose gradient is computed.

  • zs – Additional non-differentiable input variable names.

  • registry – Operator-to-GradFn map used for backward dispatch.

Throws:
  • std::invalid_argument – on invalid arguments (same as GradientOfNodes).

  • std::runtime_error – on operators not found in registry.

Returns:

A FunctionProto encoding the gradient computation.