grad_dispatcher.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

Typedefs

using GradFn = std::function<bool(const NodeProto &node, const std::string &output_grad, std::unordered_map<std::string, std::string> &grad_accum, int &counter, FunctionProto &func)>#

Defines the signature for a per-operator backward (gradient) function.

Parameters mirror those of ApplyBackward: node is the forward op, output_grad is the name of the gradient tensor flowing into this op’s output, grad_accum accumulates partial input gradients, counter generates unique names, and func receives the new backward nodes. Returns true on success.

using GradRegistry = std::unordered_map<std::pair<std::string, std::string>, GradFn, PairStringHash>#

Represents a mapping from (domain, op_type) pairs to their corresponding GradFn implementations. The empty string “” denotes the default ONNX operator domain.

Functions

const GradRegistry &DefaultGradRegistry()#

Returns a reference to the built-in gradient registry.

The registry contains backward rules for all natively supported operators. Callers who need a mutable copy should copy the returned registry and extend it via RegisterGradientFunction.

void RegisterGradientFunction(const std::string &domain, const std::string &op_type, GradFn fn, GradRegistry &registry)#

Registers a custom backward function for (domain, op_type) in registry.

Inserts or replaces the entry for the given key. Pass a copy of DefaultGradRegistry() to extend the built-in set while keeping the defaults. Use an empty string for domain to denote the default ONNX operator domain.

Parameters:
  • domain – The operator domain (e.g. “” for standard ONNX, “com.example” for custom).

  • op_type – The ONNX operator type name (e.g. “MyCustomOp”).

  • fn – The backward function implementing the gradient rule.

  • registry – The registry to insert into.

void ApplyBackward(const NodeProto &node, const std::unordered_map<std::string, std::string> &grad_table, std::unordered_map<std::string, std::string> &grad_accum, int &counter, FunctionProto &func, const GradRegistry &registry)#

Applies the backward rule for node using registry.

Looks up the output gradient in grad_table, then calls the registered backward function and accumulates the resulting input gradients into grad_accum. New nodes are appended to func. counter is used to generate unique intermediate names.

Raises an exception if the (domain, op_type) of node is not found in registry, as the whole gradient computation would be incorrect.

struct PairStringHash#
#include <grad_dispatcher.h>

Hash functor for std::pair<std::string, std::string> registry keys. Uses a FNV-inspired mixing to combine the two component hashes.

Public Functions

inline std::size_t operator()(const std::pair<std::string, std::string> &p) const noexcept#