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_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
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:
nodeis the forward op,output_gradis the name of the gradient tensor flowing into this op’s output,grad_accumaccumulates partial input gradients,countergenerates unique names, andfuncreceives the new backward nodes. Returns true on success.
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 ®istry)#
Registers a custom backward function for (
domain,op_type) inregistry.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
domainto 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 ®istry)#
Applies the backward rule for
nodeusingregistry.Looks up the output gradient in
grad_table, then calls the registered backward function and accumulates the resulting input gradients intograd_accum. New nodes are appended tofunc.counteris used to generate unique intermediate names.Raises an exception if the (domain, op_type) of
nodeis not found inregistry, 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.
-
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)>#
-
namespace gradient
-
namespace core