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_NAMESPACEso 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_protouses hidden visibility by default.Namespace alias so that ONNX C++ code (and consumers such as onnxruntime) that refers to the literal
onnxnamespace — rather than theONNX_NAMESPACEmacro — resolves to the onnx-light namespace. The standard onnx package lives innamespace onnx; onnx-light usesonnx_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 fromonnx.-
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 ®istry)#
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
ywith respect to each variable inxs.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:
- 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 ®istry)#
Computes the gradient FunctionProto from an existing FunctionProto.
The
functionis 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
functioninputs) 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:
- Returns:
A FunctionProto encoding the gradient computation.
-
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 ®istry)#
-
namespace gradient
-
namespace core