namespace
SushiAI::NN
- Declared in
include/SushiAI/nn/avg_pool2d.hpp
Contains
SushiAI::NN::AvgPool2dSushiAI::NN::Conv2dSushiAI::NN::CrossEntropyLossSushiAI::NN::FlattenSushiAI::NN::GELUSushiAI::NN::GlobalAvgPoolSushiAI::NN::ILayerSushiAI::NN::InitPolicySushiAI::NN::LayerAdapterSushiAI::NN::LayerEntrySushiAI::NN::LayerStackSushiAI::NN::LeakyReLUSushiAI::NN::LinearSushiAI::NN::MaxPool2dSushiAI::NN::ParameterSushiAI::NN::ParameterFieldSushiAI::NN::ParameterVisitorSushiAI::NN::ReLUSushiAI::NN::SequentialSushiAI::NN::SigmoidSushiAI::NN::TanhSushiAI::NN::UnflattenSushiAI::NN::Upsample2d
Enumerations
enum class NonlinearityThe activation a layer feeds, which sets its initialisation gain.
LINEARNo activation, or an identity one.
TANHHyperbolic tangent.
RELURectified linear unit; also GELU and SiLU in practice.
LEAKY_RELULeaky rectified linear unit.
SIGMOIDLogistic sigmoid.
enum class InitFamilyWhich variance rule to apply to weights.
KAIMINGScale by fan-in; the right default for a rectifier stack.
XAVIERScale by both fans; the right default for tanh or sigmoid.
enum class ParameterRoleWhat a parameter is for, which is what decides how it is initialised.
WEIGHTA weight matrix; gets a fan-scaled random initialisation.
BIASA bias vector; always starts at zero.
Typedefs
using SushiAI::NN::LayerFactory = std::unique_ptr<ILayer> (*)(const Shape& input, const Config::Value& attributes, std::string_view context)Builds one layer from its spec.
Parameters
inputThe per-sample shape entering it, batch axis excluded.
attributesThe layer's own keys; the factory rejects any it does not know.
contextWhat to call this layer in a diagnostic, e.g. "layer 3 'linear'".
Returns
The layer.
Variables
constexpr std::array< LayerEntry, 13 > LAYER_REGISTRY = { LayerEntry{"linear", &make_linear}, LayerEntry{"gelu", &make_gelu}, LayerEntry{"relu", &make_relu}, LayerEntry{"tanh", &make_tanh}, LayerEntry{"sigmoid", &make_sigmoid}, LayerEntry{"leaky_relu", &make_leaky_relu}, LayerEntry{"unflatten", &make_unflatten}, LayerEntry{"conv2d", &make_conv2d}, LayerEntry{"maxpool2d", &make_maxpool2d}, LayerEntry{"avgpool2d", &make_avgpool2d}, LayerEntry{"global_avgpool", &make_global_avgpool}, LayerEntry{"flatten", &make_flatten}, LayerEntry{"upsample", &make_upsample}, }Every layer type a model.json may name.
A file-scope constant table, in the shape of Ops::ALL and the fusion CATALOG.
template <typename T>
constexpr bool Module = Detail::IsModule<T>::valueConcept variable checking whether T defines fields() and forward().
Template parameters
TCandidate type checked against the module contract.
template <typename T>
constexpr bool ModuleContainer = Detail::IsModuleContainer<T>::valueConcept variable checking whether T supports visit_children.
Template parameters
TCandidate container type.
Functions
double calculate_gain(Nonlinearity nonlinearity, double negative_slope=0.01) noexceptThe variance-preserving gain for nonlinearity.
Parameters
nonlinearityWhich activation follows the layer.
negative_slopeThe leaky-ReLU slope; ignored otherwise.
Returns
The multiplicative gain; always positive.
void kaiming_normal(SushiBLAS::Engine &engine, SushiBLAS::Tensor &tensor, int64_t fan_in, double gain)Fills tensor from N(0, (gain / sqrt(fan_in))^2).
Parameters
engineThe engine whose Philox stream supplies the numbers.
tensorThe tensor to fill.
fan_inInputs feeding one output unit.
gainThe nonlinearity's gain.
void kaiming_uniform(SushiBLAS::Engine &engine, SushiBLAS::Tensor &tensor, int64_t fan_in, double gain)Fills tensor uniformly on [-b, b] with b = gain * sqrt(3 / fan_in).
Parameters
engineThe engine whose Philox stream supplies the numbers.
tensorThe tensor to fill.
fan_inInputs feeding one output unit.
gainThe nonlinearity's gain.
void xavier_normal(SushiBLAS::Engine &engine, SushiBLAS::Tensor &tensor, int64_t fan_in, int64_t fan_out, double gain)Fills tensor from N(0, (gain * sqrt(2 / (fan_in + fan_out)))^2).
Parameters
engineThe engine whose Philox stream supplies the numbers.
tensorThe tensor to fill.
fan_inInputs feeding one output unit.
fan_outOutputs one input unit feeds.
gainThe nonlinearity's gain.
void xavier_uniform(SushiBLAS::Engine &engine, SushiBLAS::Tensor &tensor, int64_t fan_in, int64_t fan_out, double gain)Fills tensor uniformly on [-a, a] with a = gain * sqrt(6 / (fan_in + fan_out)).
Parameters
engineThe engine whose Philox stream supplies the numbers.
tensorThe tensor to fill.
fan_inInputs feeding one output unit.
fan_outOutputs one input unit feeds.
gainThe nonlinearity's gain.
void zeros(SushiBLAS::Engine &engine, SushiBLAS::Tensor &tensor)Fills tensor with zeros.
Parameters
engineThe engine to record the fill on.
tensorThe tensor to zero.
template <typename M>
void initialize(SushiBLAS::Engine &engine, M &module, Graph::TensorPool &pool, const InitPolicy &policy={})Initializes every parameter of module and flushes the device.
Parameters
engineEngine managing parameter tensor memory.
moduleModel to initialize whose parameters were declared during trace.
poolTensor pool binding parameter values.
policyInitialization policy configuration.
Exceptions
ErrorIf a parameter tensor is not bound in pool.
std::unique_ptr< ILayer > make_linear(const Shape &input, const Config::Value &attributes, std::string_view context)Builds a Linear from "units" and an optional "use_bias".
Parameters
inputThe predecessor's per-sample shape; must be rank 1, [features].
contextWhat to call this layer in a diagnostic.
Exceptions
ErrorIf
inputis not rank 1, a key is unknown, "units" is absent, or it is not positive.
std::unique_ptr< ILayer > make_gelu(const Shape &input, const Config::Value &attributes, std::string_view context)Builds a GELU, which takes no keys at all.
Parameters
inputThe predecessor's per-sample shape; the activation preserves it.
attributesThe layer's keys; any key whatsoever is an error.
contextWhat to call this layer in a diagnostic.
Returns
The layer.
Exceptions
ErrorIf any key is present.
std::unique_ptr< ILayer > make_relu(const Shape &input, const Config::Value &attributes, std::string_view context)Builds a ReLU, which takes no key.
std::unique_ptr< ILayer > make_tanh(const Shape &input, const Config::Value &attributes, std::string_view context)Builds a Tanh, which takes no key.
std::unique_ptr< ILayer > make_sigmoid(const Shape &input, const Config::Value &attributes, std::string_view context)Builds a Sigmoid, which takes no key.
std::unique_ptr< ILayer > make_leaky_relu(const Shape &input, const Config::Value &attributes, std::string_view context)Builds a LeakyReLU from an optional "slope", 0.01 when absent.
std::unique_ptr< ILayer > make_unflatten(const Shape &input, const Config::Value &attributes, std::string_view context)Builds an Unflatten from "height", "width" and "channels", all required.
Parameters
inputThe predecessor's per-sample shape; must be [height * width * channels].
std::unique_ptr< ILayer > make_conv2d(const Shape &input, const Config::Value &attributes, std::string_view context)Builds a Conv2d from "filters" and "kernel", with optional "stride", "padding", "dilation" and "use_bias".
Parameters
inputThe predecessor's per-sample [H, W, C] shape; C becomes in_channels.
attributes"kernel", "stride", "padding" and "dilation" each hold an integer or a two-entry [h, w] array.
std::unique_ptr< ILayer > make_maxpool2d(const Shape &input, const Config::Value &attributes, std::string_view context)Builds a MaxPool2d from "kernel", with optional "stride" and "padding".
Parameters
inputThe predecessor's per-sample shape; must be rank 3, [H, W, C].
attributes"stride" defaults to "kernel"; each key holds an integer or a two-entry [h, w] array.
std::unique_ptr< ILayer > make_avgpool2d(const Shape &input, const Config::Value &attributes, std::string_view context)Builds an AvgPool2d from "kernel", with optional "stride" and "padding".
Parameters
inputThe predecessor's per-sample shape; must be rank 3, [H, W, C].
attributes"stride" defaults to "kernel"; each key holds an integer or a two-entry [h, w] array.
std::unique_ptr< ILayer > make_global_avgpool(const Shape &input, const Config::Value &attributes, std::string_view context)Builds a GlobalAvgPool, which takes no key.
Parameters
inputThe predecessor's per-sample shape; must be rank 3, [H, W, C].
std::unique_ptr< ILayer > make_flatten(const Shape &input, const Config::Value &attributes, std::string_view context)Builds a Flatten, which takes no key.
Parameters
inputThe predecessor's per-sample shape, of rank 1 or more.
std::unique_ptr< ILayer > make_upsample(const Shape &input, const Config::Value &attributes, std::string_view context)Builds an Upsample2d from an optional "scale", 2 when absent.
Parameters
inputThe predecessor's per-sample shape; must be rank 3, [H, W, C].
LayerStack build_layer_stack(const Config::ModelSpec &model)Builds the whole layer stack, chaining each layer's per-sample shape.
Parameters
modelParsed model architecture specification.
Returns
Constructed layer stack ready for tracing.
Exceptions
ErrorIf a layer type is unknown or attributes are invalid.
template <typename... Ls>
Sequential(Ls...) -> Sequential< Ls... >Deduces Sequential's layer pack from its constructor arguments.
template <typename M>
constexpr ParameterField< M > field(std::string_view name, Parameter M::*member) noexceptBuilds one entry of a layer's fields() list.
Parameters
nameThe parameter's name, e.g. "weight".
memberA pointer to the layer's member.
Returns
The field descriptor.
template <typename M, typename Fn>
void for_each_parameter(M &module, std::string_view prefix, Fn &&visitor)Traverses declared parameters of a module in deterministic order.
Template parameters
MModule or container type.
Parameters
moduleRoot module or layer instance to inspect.
prefixDotted prefix representing parameter hierarchy.
visitorCallback receiving parameter name and reference.
template <typename M, typename Fn>
void for_each_parameter(M &module, Fn &&visitor)Visits every declared parameter of module from the root.
Parameters
moduleThe model to walk.
visitorCalled as visitor(const std::string& name, Parameter&).
template <typename M>
std::size_t parameter_count(M &module)Counts module's declared parameters.
Parameters
moduleThe model to walk.
Returns
How many parameters the walk yields.
template <typename M>
int64_t parameter_elements(M &module)Sums the element counts of module's declared parameters.
Parameters
moduleThe model to walk.
Returns
How many learnable scalars the model holds.

