namespace nbla

namespace nbla

Array interface class and its implementations such as CpuArray, CudaArray etc.

Synced array.

Solver interface class.

Parametric Functions.

Initializer.

Function interface class.

VATNoise.

Base class of unary operations.

Base class of binary operations.

Pooling.

Unpooling.

Unlink.

Transpose.

TopNError.

TanhShrink.

Tanh.

Swish.

SumPooling.

Sum.

Sub2.

Stack.

Split.

SoftSign.

SoftPlus.

SoftmaxCrossEntropy.

Softmax.

Slice.

Sink.

Sinc.

Sign.

Sigmoid cross entropy.

Sigmoid.

Shift.

SELU.

Round.

Reshape.

ReLU6.

ReLU.

ReduceSum.

ReduceMean.

RandomShift.

RandomFlip.

RandomCrop.

Randn.

Randint.

RandGamma.

RandBinomial.

RandBeta.

Rand.

R Sub Scalar.

RPow Scalar.

R Div Scalar.

Prod.

PReLU.

Pow Scalar.

Pow2Quantize.

Pow2.

OneHot.

NotEqualScalar.

NotEqual.

NmsDetection2d.

Mul Scalar.

Mul2.

Minimum Scalar.

minimum2

Min.

Mean Subtraction.

Mean.

Maximum Scalar.

maximum2

MaxPooling.

Max.

MatrixDiagPart.

MatrixDiag.

LogicalXorScalar.

LogicalXor.

LogicalOrScalar.

LogicalOr.

LogicalNot.

Logical_And Scalar.

LogicalAnd.

LogSigmoid.

Log.

LessScalar.

LessEqualScalar.

LessEqual.

Less.

LeakyReLU.

KLMultinomial.

INQConvolution.

INQAffine.

ImageAugmentation.

Identity.

Huber loss.

HardTanh.

HardSigmoid.

GreaterScalar.

GreaterEqualScalar.

GreaterEqual.

Greater.

GlobalAveragePooling.

Flip.

Exp.

EqualScalar.

Equal.

EpsilonInsensitiveLoss.

Embed.

ELU.

Dropout.

Div2.

DepthwiseDeconvolution.

DepthwiseConvolution.

cReLU

Constant.

ConfusionMatrix.

Concatenate.

ClipGradByValue.

CELU.

Ceil.

CategoricalCrossEntropy.

BroadcastTo.

BinaryWeightConvolution.

BinaryWeightAffine.

BinaryTanh.

Binary Sigmoid.

BinaryError.

Binary cross entropy.

Bc_Add2.

Batch Normalization.

BatchMatmul.

AveragePooling.

Add Scalar.

Squared error.

AbsoluteError.

Abs.

Exception throwing utilities.

Context.

Communicator interface class.

Common functions and variables.

Array creator and synchronizer.

Array classes are not directly used by users

Some items are not intentionally located in this header, maybe because they are uncategorized.

Typedefs

typedef Array::Ptr ArrayPtr
typedef const Array ConstArray
typedef shared_ptr<ConstArray> ConstArrayPtr
typedef vector<int64_t> Shape_t

Type of array shape and strides etc.

typedef int64_t Size_t

Type of size of array.

typedef shared_ptr<CgVariable> CgVariablePtr

shared_ptr typedef of CGVariable

typedef CgFunction::Ptr CgFunctionPtr
typedef shared_ptr<CommunicatorBackwardCallback> CommunicatorBackwardCallbackPtr
typedef function<void(const CgFunctionPtr &ptr)> function_hook_type
typedef shared_ptr<Event> EventPtr

Shared pointer of Event.

typedef vector<PadItem> PadList
using ChannelFirstAdaptorPtr = shared_ptr<ChannelFirstAdaptor>
typedef vector<Variable*> Variables

Variables as a vector of raw pointer.

typedef Function::Ptr FunctionPtr
typedef function<void(void)> update_hook_type
typedef shared_ptr<AllocatorMemory> AllocatorMemoryPtr
typedef shared_ptr<PhysicalMemory> PhysicalMemoryPtr
typedef vector<PhysicalMemoryPtr> VecPhysicalMemoryPtr
typedef std::chrono::time_point<std::chrono::high_resolution_clock> Tp
typedef NdArray::Ptr NdArrayPtr
typedef shared_ptr<SyncedArray> SyncedArrayPtr
using synced_array_callback_func_type = function<void(SyncedArrayPtr saptr, const SyncedArrayCallbackTag func_name, const dtypes dtype, const Context &ctx, const bool write_only, const bool first_creation, const bool off_recording)>

Type of callback function for get, cast, and clear of SyncedArray.

typedef Variable::Ptr VariablePtr

Enums

enum AsyncFlag

Values:

enumerator NONE
enumerator ASYNC
enumerator UNSAFE
enumerator OFFREC
enum class dtypes

ENUM for dtypes.

Compatible with numpy DTYPES. It allows us to use np.dtype in Python interface.

Values:

enumerator BOOL
enumerator BYTE
enumerator UBYTE
enumerator SHORT
enumerator USHORT
enumerator INT
enumerator UINT
enumerator LONG
enumerator ULONG
enumerator LONGLONG
enumerator ULONGLONG
enumerator FLOAT
enumerator DOUBLE
enumerator LONGDOUBLE
enumerator HALF
enum class error_code

Enum of error codes throwing in NNabla.

Note: Developers must add a line `CASE_ERROR_STRING({code name});` into get_error_string function in src/nbla/exception.cpp, if a new code is added.

Values:

enumerator unclassified
enumerator not_implemented
enumerator value
enumerator type
enumerator memory
enumerator io
enumerator os
enumerator target_specific
enumerator target_specific_async
enumerator runtime
enum class ResizeCoordTransformMode

Values:

enumerator HALF_PIXEL
enumerator PYTORCH_HALF_PIXEL
enumerator ALIGN_CORNERS
enumerator ASYMMETRIC
enumerator TF_HALF_PIXEL_FOR_NN
enumerator TF_CROP_AND_RESIZE
enum class ResizeNearestMode

Values:

enumerator ROUND_PREFER_FLOOR
enumerator ROUND_PREFER_CEIL
enumerator FLOOR
enumerator CEIL
enum DeviceMemoryState

Memory status on the device.

Values:

enumerator Locked

This memory will be used on device in future. Cannot clear it.

enumerator Unlocked

This memory is no longer used on device. Can clear it.

enum SyncedArrayCallbackTag

Get, cast, or clear.

Values:

enumerator GET
enumerator CAST
enumerator CLEAR

Functions

NBLA_API void synchronizer_default (Array *src, Array *dst, const int async_flags=AsyncFlag::NONE)

Synchronizer rely on Array::copy_from.

This should be used as a synchronizer between classes that are using the same device class like CpuArray-CpuCachedArray.

async_flags are not used in synchronizer_default.

inline Size_t compute_size_by_shape(const Shape_t &shape, Size_t axis = 0)

Compute array size by shape.

Parameters:

axis[in] size under given axis will be computed

inline Shape_t get_c_contiguous_strides(const Shape_t &shape)

Helper for getting strides of C contiguous memory arrangement.

template<typename T>
inline string string_join(const vector<T> &vec, const string &delim)

Join string with delimiter.

inline string byte_to_human_readable(long double byte)

size_t to byte strings that is readable by human.

template<typename T>
void hash_combine(size_t &seed, T const &v)

Hash combining function.

template<typename T>
vector<T*> as_pointer_array(const vector<shared_ptr<T>> &vec)
template<int index, typename ...Args>
auto get_from_variadic_args(Args&&... args) -> typename std::remove_reference<decltype(std::get<index>(std::make_tuple(args...)))>::type

Getter for variadic template arguments.

inline int nbla_setenv(const char *var_name, const char *new_value)
NBLA_REGISTER_COMMUNICATOR_HEADER(DataParallelCommunicator)
NBLA_REGISTER_COMMUNICATOR_HEADER(MultiProcessDataParallelCommunicator)
NBLA_API vector< CgVariablePtr > create_function_outputs (CgFunctionPtr cg_f, int n_outputs=-1, bool prohibit_clear_output=false)

Create CgVariable outputs.

Created CGVariables are held as weak_ptr by cg_f. The `need_grad` flags are automatically applied to created outputs.

NBLA_API vector< CgVariablePtr > connect (CgFunctionPtr cg_f, const vector< CgVariablePtr > &inputs, int n_outputs=1, vector< NdArrayPtr > inplace_outputs={}, bool execute=false)

Connect function to network.

Create outputs on-demand.

NBLA_API void steal_variable_from_to (CgVariablePtr from, CgVariablePtr to)

Steal some variable properties from `from` CgVariable to `to` in order to rewire previously constructed graphs.

It takes parent, need_grad flags, inplace flags, and the variable contents.

Parameters:
  • from[in] Its parent function is stolen by ‘to’.

  • to[inout] A variable ‘from’’s parent stolen to.

NBLA_API void forward_all (const vector< CgVariablePtr > variables, bool clear_buffer=false, bool clear_no_need_grad=false, function_hook_type function_pre_hook=nullptr, function_hook_type function_post_hook=nullptr)

Forward given variables in single inference Forward all given variables with shared fclosed flags.

vector<Variable*> to_variable_pointers(const vector<CgVariablePtr> &variables)
NBLA_API void c_activate_clear_called_flag_recorder ()
NBLA_API void c_deactivate_clear_called_flag_recorder ()
NBLA_API vector< vector< std::pair< bool, bool > > > c_get_input_clear_called_flags ()
NBLA_API vector< vector< std::pair< bool, bool > > > c_get_output_clear_called_flags ()
inline string get_array_key_from_context(const Context &ctx)
template<typename T>
dtypes get_dtype()

A function to get dtype enum by dtype of C++.

EX) dtypes dtype = get_dtype<T>::type;

GET_DTYPE_TEMPLATE_SPECIAL(unsigned char, UBYTE)
GET_DTYPE_TEMPLATE_SPECIAL(char, BYTE)
GET_DTYPE_TEMPLATE_SPECIAL(unsigned short, USHORT)
GET_DTYPE_TEMPLATE_SPECIAL(short, SHORT)
GET_DTYPE_TEMPLATE_SPECIAL(unsigned int, UINT)
GET_DTYPE_TEMPLATE_SPECIAL(int, INT)
GET_DTYPE_TEMPLATE_SPECIAL(unsigned long, ULONG)
GET_DTYPE_TEMPLATE_SPECIAL(long, LONG)
GET_DTYPE_TEMPLATE_SPECIAL(unsigned long long, ULONGLONG)
GET_DTYPE_TEMPLATE_SPECIAL(long long, LONGLONG)
GET_DTYPE_TEMPLATE_SPECIAL(float, FLOAT)
GET_DTYPE_TEMPLATE_SPECIAL(double, DOUBLE)
GET_DTYPE_TEMPLATE_SPECIAL(bool, BOOL)
GET_DTYPE_TEMPLATE_SPECIAL(long double, LONGDOUBLE)
GET_DTYPE_TEMPLATE_SPECIAL(nbla::Half, HALF)
inline string dtype_to_string(dtypes dtype)

Convert dtypes to string.

inline size_t sizeof_dtype(dtypes dtype)

Figure out the size of given dtype.

string get_error_string(error_code code)
template<typename T, typename ...Args>
string format_string(const string &format, T first, Args... rest)

String formatter.

inline string format_string(const string &format)

String formatter without format.

NBLA_DEFINE_TRANSFORM_UNARY (Abs, std::abs(x),(x<(T) 0) ? -dy :dy, false, true)
NBLA_DEFINE_TRANSFORM_BINARY (AbsoluteError, std::abs((x0 - x1)),((x0 - x1) >(T) 0) ? dy :-dy,((x0 - x1) >(T) 0) ? -dy :dy, false, false, true, true)
NBLA_DEFINE_TRANSFORM_UNARY (ACos, std::acos(x), -dy/std::sqrt(1 - x *x), false, true)
NBLA_DEFINE_TRANSFORM_UNARY (ACosh, std::acosh(x), dy/std::sqrt(x *x - 1), false, true)
NBLA_REGISTER_FUNCTION_HEADER(AdaptiveSeparableConvolution)
NBLA_REGISTER_FUNCTION_HEADER(Add2, bool)
NBLA_REGISTER_FUNCTION_HEADER(AddN)
NBLA_DEFINE_TRANSFORM_UNARY_1_INPLACE (AddScalar, x+(T) a0, dy, false, false, double, true)
NBLA_REGISTER_FUNCTION_HEADER(Affine, int)
NBLA_REGISTER_FUNCTION_HEADER(AffineGrid, const vector<int>&, bool)
NBLA_REGISTER_FUNCTION_HEADER(Arange, float, float, float)
NBLA_DEFINE_TRANSFORM_UNARY (ASin, std::asin(x), dy/std::sqrt(1 - x *x), false, true)
NBLA_DEFINE_TRANSFORM_UNARY (ASinh, std::asinh(x), dy/std::sqrt(x *x+1), false, true)
NBLA_REGISTER_FUNCTION_HEADER(Assign)
NBLA_DEFINE_TRANSFORM_UNARY (ATan, std::atan(x), dy/(1+x *x), false, true)
NBLA_DEFINE_TRANSFORM_BINARY (ATan2, std::atan2(x0, x1), dy *x1/(x0 *x0+x1 *x1), -dy *x0/(x0 *x0+x1 *x1), false, false, true, true)
NBLA_DEFINE_TRANSFORM_UNARY (ATanh, std::atanh(x), dy/(1 - x *x), false, true)
NBLA_REGISTER_FUNCTION_HEADER(AveragePooling, const vector<int>&, const vector<int>&, bool, const vector<int>&, bool, bool)
NBLA_REGISTER_FUNCTION_HEADER(BatchCholesky, bool)
NBLA_REGISTER_FUNCTION_HEADER(BatchDet)
NBLA_REGISTER_FUNCTION_HEADER(BatchInv)
NBLA_REGISTER_FUNCTION_HEADER(BatchLogdet)
NBLA_REGISTER_FUNCTION_HEADER(BatchMatmul, bool, bool)
NBLA_REGISTER_FUNCTION_HEADER(BatchNormalization, const vector<int>&, float, float, bool, bool, bool)
NBLA_DEFINE_TRANSFORM_BINARY_INPLACE (BcAdd2, x0+x1, dy, dy, false, false, false, false, true)
NBLA_REGISTER_FUNCTION_HEADER(BinaryConnectAffine, int, float)
NBLA_REGISTER_FUNCTION_HEADER(BinaryConnectConvolution, int, const vector<int>&, const vector<int>&, const vector<int>&, int, float)
NBLA_REGISTER_FUNCTION_HEADER(BinaryCrossEntropy)
NBLA_REGISTER_FUNCTION_HEADER(BinaryError)
NBLA_DEFINE_TRANSFORM_UNARY (BinarySigmoid,(x >(T) 0) ?(T) 1 :(T) 0,(std::abs(x) >=(T) 1) ?(T) 0 :dy *(T) 0.5, false, true)
NBLA_DEFINE_TRANSFORM_UNARY (BinaryTanh,(x >(T) 0) ?(T) 1 :(T) -1,(std::abs(x) >=(T) 1) ?(T) 0 :dy, false, true)
NBLA_REGISTER_FUNCTION_HEADER(BinaryWeightAffine, int, float)
NBLA_REGISTER_FUNCTION_HEADER(BinaryWeightConvolution, int, const vector<int>&, const vector<int>&, const vector<int>&, int, float)
NBLA_REGISTER_FUNCTION_HEADER(BitShift, const string&)
NBLA_REGISTER_FUNCTION_HEADER(BoolFill, float)
NBLA_REGISTER_FUNCTION_HEADER(BoolGather)
NBLA_REGISTER_FUNCTION_HEADER(BoolScatter)
inline Shape_t expand_shape(Shape_t source_shape, int target_ndim)
NBLA_REGISTER_FUNCTION_HEADER(Broadcast, const vector<int>&)
NBLA_REGISTER_FUNCTION_HEADER(BroadcastTo, int)
NBLA_API shared_ptr< Function > create_Callback (const Context &ctx, void *obj, int min_outputs, Callback::setup_callback_type s, Callback::forward_callback_type f, Callback::backward_callback_type b, Callback::cleanup_callback_type c, Callback::grad_depends_output_data_callback_type go, Callback::grad_depends_input_data_callback_type gi)
NBLA_REGISTER_FUNCTION_HEADER(CategoricalCrossEntropy, int)
NBLA_DEFINE_TRANSFORM_UNARY (Ceil, std::ceil(x), dy, false, false)
NBLA_REGISTER_FUNCTION_HEADER(CELU, double, int)
NBLA_REGISTER_FUNCTION_HEADER(ClipGradByNorm, float, const vector<int>&)
NBLA_REGISTER_FUNCTION_HEADER(ClipGradByValue)
NBLA_REGISTER_FUNCTION_HEADER(Concatenate, int)
NBLA_REGISTER_FUNCTION_HEADER(ConfusionMatrix, int)
NBLA_REGISTER_FUNCTION_HEADER(Constant, float, const vector<int>&)
NBLA_REGISTER_FUNCTION_HEADER(Convolution, int, const vector<int>&, const vector<int>&, const vector<int>&, int, bool)
NBLA_DEFINE_TRANSFORM_UNARY (Cos, std::cos(x), -dy *std::sin(x), false, true)
NBLA_DEFINE_TRANSFORM_UNARY (Cosh, std::cosh(x), dy *std::sinh(x), false, true)
NBLA_REGISTER_FUNCTION_HEADER(CReLU, int)
NBLA_REGISTER_FUNCTION_HEADER(CumProd, int, bool, bool)
NBLA_REGISTER_FUNCTION_HEADER(CumSum, int, bool, bool)
NBLA_REGISTER_FUNCTION_HEADER(Deconvolution, int, const vector<int>&, const vector<int>&, const vector<int>&, int, bool, const vector<int>&)
NBLA_REGISTER_FUNCTION_HEADER(DeformableConvolution, int, const vector<int>&, const vector<int>&, const vector<int>&, int, int, bool)
NBLA_REGISTER_FUNCTION_HEADER(DepthwiseConvolution, int, const vector<int>&, const vector<int>&, const vector<int>&, int)
NBLA_REGISTER_FUNCTION_HEADER(DepthwiseDeconvolution, int, const vector<int>&, const vector<int>&, const vector<int>&, int)
NBLA_REGISTER_FUNCTION_HEADER(DequantizeLinear)
NBLA_DEFINE_TRANSFORM_BINARY_INPLACE (Div2, x0/x1, dy/x1, -dy *x0/(x1 *x1), false, false, true, true, true)
NBLA_REGISTER_FUNCTION_HEADER(Dropout, double, int)
NBLA_REGISTER_FUNCTION_HEADER(Einsum, const string&)
NBLA_DEFINE_TRANSFORM_UNARY_1 (ELU, x >=(T) 0 ? x :(T) a0 *(std::exp(x) -(T) 1), x >=(T) 0 ? dy :dy *(T) a0 *std::exp(x), false, true, double)
NBLA_REGISTER_FUNCTION_HEADER(Embed)
NBLA_REGISTER_FUNCTION_HEADER(EpsilonInsensitiveLoss, float)
NBLA_DEFINE_TRANSFORM_BINARY_1 (EpsilonInsensitiveLoss,(std::abs(x0 - x1) >(T) a0) ?(std::abs((x0 - x1)) -(T) a0) :(T) 0,(x0 - x1) >(T) a0 ? dy :((x0 - x1)<(T) -a0) ? -dy :(T) 0,(x0 - x1) >(T) a0 ? -dy :((x0 - x1)<(T) -a0) ? dy :(T) 0, false, false, true, true, float)
NBLA_DEFINE_TRANSFORM_BINARY_NO_GRAD(Equal, x0 = =x1)
NBLA_DEFINE_TRANSFORM_UNARY_1_NO_GRAD(EqualScalar, x = =(T) a0, double)
NBLA_DEFINE_TRANSFORM_UNARY (Erf, std::erf(x), 2.0/std::sqrt(M_PI) *std::exp(-(x *x)) *dy, false, true)
NBLA_DEFINE_TRANSFORM_UNARY (Exp, std::exp(x), y *dy, true, false)
NBLA_REGISTER_FUNCTION_HEADER(EyeLike, int)
NBLA_REGISTER_FUNCTION_HEADER(FFT, int, bool)
NBLA_REGISTER_FUNCTION_HEADER(FixedPointQuantize, bool, int, float, bool)
NBLA_REGISTER_FUNCTION_HEADER(Flip, const vector<int>&)
NBLA_DEFINE_TRANSFORM_UNARY (Floor, std::floor(x), dy, false, false)
NBLA_REGISTER_FUNCTION_HEADER(FusedBatchNormalization, const vector<int>&, float, float, bool, const string&)
NBLA_REGISTER_FUNCTION_HEADER(FusedConvolution, int, const vector<int>&, const vector<int>&, const vector<int>&, int, bool, float, float, bool, const string&, const vector<float>&, const string&, float)
NBLA_REGISTER_FUNCTION_HEADER(Gather, int, int)
NBLA_REGISTER_FUNCTION_HEADER(GatherNd)
NBLA_REGISTER_FUNCTION_HEADER(GELU)
NBLA_DEFINE_TRANSFORM_UNARY (GELU,(x/2) *(1+std::tanh((std::sqrt((T)(2/M_PI)) *(x+(T) 0.044715 *std::pow(x, 3))))), dy *(0.5 *(1+std::tanh(std::sqrt((T)(2/M_PI)) *(x+(T) 0.044715 *std::pow(x, 3))))+0.5 *x *(1 - std::pow(std::tanh(std::sqrt((T)(2/M_PI)) *(x+(T) 0.044715 *std::pow(x, 3))), 2)) *std::sqrt((T)(2/M_PI)) *(1+0.134145 *std::pow(x, 2))), true, true)

Inputs:

  • N-D array.

Outputs:

  • N-D array.

NBLA_REGISTER_FUNCTION_HEADER(GlobalAveragePooling)
NBLA_DEFINE_TRANSFORM_BINARY_NO_GRAD (Greater, x0 > x1)
NBLA_DEFINE_TRANSFORM_BINARY_NO_GRAD (GreaterEqual, x0 >=x1)
NBLA_DEFINE_TRANSFORM_UNARY_1_NO_GRAD (GreaterEqualScalar, x >=(T) a0, double)
NBLA_DEFINE_TRANSFORM_UNARY_1_NO_GRAD (GreaterScalar, x >(T) a0, double)
NBLA_REGISTER_FUNCTION_HEADER(GroupNormalization, int, int, const vector<int>&, float, bool, bool)
NBLA_REGISTER_FUNCTION_HEADER(GRU, int, float, bool, bool)
NBLA_REGISTER_FUNCTION_HEADER(HardSigmoid)
NBLA_DEFINE_TRANSFORM_UNARY (HardSigmoid, x >(T) 2.5 ?(T) 1 :x<(T) -2.5 ?(T) 0 :(T) 0.2 *x+(T) 0.5, x<=(T) 2.5 &&(T) -2.5<=x ? dy *(T) 0.2 :(T) 0, false, true)
NBLA_REGISTER_FUNCTION_HEADER(HardTanh)
NBLA_DEFINE_TRANSFORM_UNARY (HardTanh, x >(T) 1 ?(T) 1 :x<(T) -1 ?(T) -1 :x,(T) -1<=x &&x<=(T) 1 ? dy :(T) 0, false, true)
NBLA_DEFINE_TRANSFORM_BINARY_1 (HuberLoss,(std::abs(x0 - x1)<(T) a0) ?(std::abs(x0 - x1) *std::abs(x0 - x1)) :((T) a0 *((T) 2 *std::abs(x0 - x1) -(T) a0)),(T) 2 *dy *(std::abs(x0 - x1)<(T) a0 ?(x0 - x1) :((x0 - x1) >(T) 0 ?(T) 1 :(T) -1) *(T) a0),(T) -2 *dy *(std::abs(x0 - x1)<(T) a0 ?(x0 - x1) :((x0 - x1) >(T) 0 ?(T) 1 :(T) -1) *(T) a0), false, false, true, true, float)
NBLA_REGISTER_FUNCTION_HEADER(Identity)
NBLA_REGISTER_FUNCTION_HEADER(IFFT, int, bool)
NBLA_REGISTER_FUNCTION_HEADER(ImageAugmentation, const vector<int>&, const vector<int>&, float, float, float, float, float, bool, bool, float, bool, float, float, bool, float, int)
NBLA_REGISTER_FUNCTION_HEADER(INQAffine, int, int, const vector<int>&, const string&, int)
NBLA_REGISTER_FUNCTION_HEADER(INQConvolution, int, const vector<int>&, const vector<int>&, const vector<int>&, int, int, const vector<int>&, const string&, int)
NBLA_REGISTER_FUNCTION_HEADER(InstanceNormalization, int, const vector<int>&, float, bool, bool)
NBLA_REGISTER_FUNCTION_HEADER(Interpolate, const vector<int>&, const string&, bool, bool, bool, bool)
NBLA_DEFINE_TRANSFORM_UNARY_NO_GRAD (IsInf, std::isinf(x) ?(T) 1 :(T) 0)

Test element-wise for Inf/-Inf.

Inputs:

  • N-D array.

Outputs:

  • N-D array.

Template Parameters:

T – Data type for computation.

NBLA_DEFINE_TRANSFORM_UNARY_NO_GRAD (IsNaN, std::isnan(x) ?(T) 1 :(T) 0)

Test element-wise for NaN.

Inputs:

  • N-D array.

Outputs:

  • N-D array.

Template Parameters:

T – Data type for computation.

NBLA_REGISTER_FUNCTION_HEADER(ISTFT, int, int, int, const string&, bool, const string&, bool)
NBLA_REGISTER_FUNCTION_HEADER(KLMultinomial, int)
NBLA_REGISTER_FUNCTION_HEADER(LayerNormalization, const vector<int>&, float, bool, bool)
NBLA_REGISTER_FUNCTION_HEADER(LeakyReLU, float, bool)
NBLA_DEFINE_TRANSFORM_BINARY_NO_GRAD (Less, x0< x1)
NBLA_DEFINE_TRANSFORM_BINARY_NO_GRAD (LessEqual, x0<=x1)
NBLA_DEFINE_TRANSFORM_UNARY_1_NO_GRAD (LessEqualScalar, x<=(T) a0, double)
NBLA_DEFINE_TRANSFORM_UNARY_1_NO_GRAD (LessScalar, x<(T) a0, double)
NBLA_REGISTER_FUNCTION_HEADER(Linspace, float, float, int)
NBLA_DEFINE_TRANSFORM_UNARY (Log, std::log(x), dy/x, false, true)
NBLA_REGISTER_FUNCTION_HEADER(LogSigmoid)
NBLA_DEFINE_TRANSFORM_UNARY (LogSigmoid, x >(T) 0 ? -std::log(std::exp(-x)+(T) 1) :x - std::log(std::exp(x)+(T) 1), dy/(std::exp(x)+(T) 1), false, true)
NBLA_REGISTER_FUNCTION_HEADER(LogSoftmax, int)
NBLA_DEFINE_TRANSFORM_BINARY_NO_GRAD (LogicalAnd, bool(x0) &bool(x1))
NBLA_DEFINE_TRANSFORM_UNARY_1_NO_GRAD (LogicalAndScalar, bool(x) &a0, bool)
NBLA_DEFINE_TRANSFORM_UNARY_NO_GRAD (LogicalNot, !bool(x))
NBLA_DEFINE_TRANSFORM_BINARY_NO_GRAD (LogicalOr, bool(x0)|bool(x1))
NBLA_DEFINE_TRANSFORM_UNARY_1_NO_GRAD (LogicalOrScalar, bool(x)|a0, bool)
NBLA_DEFINE_TRANSFORM_BINARY_NO_GRAD (LogicalXor, bool(x0) ^ bool(x1))
NBLA_DEFINE_TRANSFORM_UNARY_1_NO_GRAD (LogicalXorScalar, bool(x) ^ a0, bool)
NBLA_REGISTER_FUNCTION_HEADER(LSTM, int, float, bool, bool)
NBLA_REGISTER_FUNCTION_HEADER(MatrixDiag)
NBLA_REGISTER_FUNCTION_HEADER(MatrixDiagPart)
NBLA_REGISTER_FUNCTION_HEADER(Max, const vector<int>&, bool, bool, bool)
NBLA_REGISTER_FUNCTION_HEADER(MaxPooling, const vector<int>&, const vector<int>&, bool, const vector<int>&, bool)
NBLA_REGISTER_FUNCTION_HEADER(MaxPoolingBackward, const vector<int>&, const vector<int>&, bool, const vector<int>&, bool)
NBLA_DEFINE_TRANSFORM_BINARY (Maximum2,(x0 > x1) ? x0 :x1,(x0 > x1) *dy,(x0<=x1) *dy, false, false, true, true)
NBLA_DEFINE_TRANSFORM_UNARY_1 (MaximumScalar,(x >(T) a0) ? x :(T) a0,(x >(T) a0) ? dy :(T) 0, false, true, double)
NBLA_REGISTER_FUNCTION_HEADER(Mean, const vector<int>&, bool)
NBLA_REGISTER_FUNCTION_HEADER(MeanSubtraction, int, bool)
NBLA_REGISTER_FUNCTION_HEADER(Meshgrid, bool)
NBLA_REGISTER_FUNCTION_HEADER(Min, const vector<int>&, bool, bool, bool)
NBLA_REGISTER_FUNCTION_HEADER(MinMaxQuantize, float, bool, bool, bool, float)
NBLA_DEFINE_TRANSFORM_BINARY (Minimum2,(x0< x1) ? x0 :x1,(x0< x1) *dy,(x0 >=x1) *dy, false, false, true, true)
NBLA_DEFINE_TRANSFORM_UNARY_1 (MinimumScalar,(x<(T) a0) ? x :(T) a0,(x<(T) a0) ? dy :(T) 0, false, true, double)
NBLA_REGISTER_FUNCTION_HEADER(Mish)
NBLA_DEFINE_TRANSFORM_UNARY (Mish, x *std::tanh(std::log(std::exp(x)+(T) 1)),(dy *std::exp(x) *((T) 4 *(x+(T) 1)+(T) 4 *(std::exp((T) 2 *x))+std::exp((T) 3 *x)+std::exp(x) *((T) 4 *x+(T) 6)))/std::pow((T) 2 *std::exp(x)+std::exp((T) 2 *x)+(T) 2,(T) 2), false, true)

Inputs:

  • N-D array.

Outputs:

  • N-D array.

NBLA_REGISTER_FUNCTION_HEADER(Mod2, bool)
NBLA_DEFINE_TRANSFORM_BINARY_INPLACE (Mul2, x0 *x1, dy *x1, dy *x0, false, false, true, true, true)
NBLA_REGISTER_FUNCTION_HEADER(MulN)
NBLA_DEFINE_TRANSFORM_UNARY_1_INPLACE (MulScalar, x *(T) a0, dy *(T) a0, false, false, double, true)
NBLA_REGISTER_FUNCTION_HEADER(NmsDetection2d, float, float, bool)
NBLA_REGISTER_FUNCTION_HEADER(NonZero)
NBLA_REGISTER_FUNCTION_HEADER(Norm, float, const vector<int>&, bool)
NBLA_REGISTER_FUNCTION_HEADER(NormNormalization, float, const vector<int>&, float)
NBLA_DEFINE_TRANSFORM_BINARY_NO_GRAD (NotEqual, x0 !=x1)
NBLA_DEFINE_TRANSFORM_UNARY_1_NO_GRAD (NotEqualScalar, x !=(T) a0, double)
NBLA_REGISTER_FUNCTION_HEADER(OneHot, const vector<int>&)
NBLA_REGISTER_FUNCTION_HEADER(ONNXNonMaxSuppression, int, int, float, float)
NBLA_REGISTER_FUNCTION_HEADER(ONNXResize, const vector<float>&, const vector<float>&, const vector<int>&, const string&, const string&, float, int, float, const string&)
NBLA_REGISTER_FUNCTION_HEADER(PackPaddedSequence, bool)
NBLA_REGISTER_FUNCTION_HEADER(Pad, const vector<int>&, const string&, float)
NBLA_REGISTER_FUNCTION_HEADER(PadPackedSequence, bool, float, int)
NBLA_REGISTER_FUNCTION_HEADER(PatchCorrelation, const vector<int>&, const vector<int>&, const vector<int>&, const vector<int>&, const vector<int>&)
NBLA_DEFINE_TRANSFORM_BINARY_INPLACE (Pow2, std::pow(x0, x1), dy *x1 *std::pow(x0, x1 -(T) 1), dy *std::log(x0) *std::pow(x0, x1), false, false, true, true, true)
NBLA_REGISTER_FUNCTION_HEADER(Pow2Quantize, bool, bool, int, int, bool)
NBLA_DEFINE_TRANSFORM_UNARY_1_INPLACE (PowScalar, a0==0.5f ? std::sqrt(x) :std::pow(x,(T) a0), dy *(T) a0 *std::pow(x,(T) a0 -(T) 1), false, true, double, true)
NBLA_REGISTER_FUNCTION_HEADER(PReLU, int)
NBLA_REGISTER_FUNCTION_HEADER(Prod, const vector<int>&, bool)
NBLA_REGISTER_FUNCTION_HEADER(Prune, float)
NBLA_REGISTER_FUNCTION_HEADER(QuantizeLinear, const string&, bool, int)
NBLA_DEFINE_TRANSFORM_UNARY_1 (RDivScalar,(T) a0/x, dy *(-(T) a0/(x *x)), false, true, double)
NBLA_DEFINE_TRANSFORM_UNARY_1 (RPowScalar, std::pow((T) a0, x), dy *std::pow((T) a0, x) *std::log((T) a0), false, true, double)
NBLA_DEFINE_TRANSFORM_UNARY_1 (RSubScalar,(T) a0 - x, -dy, false, false, double)
NBLA_REGISTER_FUNCTION_HEADER(Rand, float, float, const vector<int>&, int)
NBLA_REGISTER_FUNCTION_HEADER(RandBeta, float, float, const vector<int>&, int)
NBLA_REGISTER_FUNCTION_HEADER(RandBinomial, int, float, const vector<int>&, int)
NBLA_REGISTER_FUNCTION_HEADER(RandGamma, float, float, const vector<int>&, int)
NBLA_REGISTER_FUNCTION_HEADER(Randint, int, int, const vector<int>&, int)
NBLA_REGISTER_FUNCTION_HEADER(Randn, float, float, const vector<int>&, int)
NBLA_REGISTER_FUNCTION_HEADER(RandomChoice, const vector<int>&, bool, int)
NBLA_REGISTER_FUNCTION_HEADER(RandomCrop, const vector<int>&, int, int)
NBLA_REGISTER_FUNCTION_HEADER(RandomErase, float, const vector<float>&, const vector<float>&, const vector<float>&, int, bool, bool, int, int, bool, bool)
NBLA_REGISTER_FUNCTION_HEADER(RandomFlip, const vector<int>&, int, int)
NBLA_REGISTER_FUNCTION_HEADER(RandomShift, const vector<int>&, const string&, float, int, int)
NBLA_REGISTER_FUNCTION_HEADER(ReduceMean)
NBLA_REGISTER_FUNCTION_HEADER(ReduceSum)
NBLA_REGISTER_FUNCTION_HEADER(ReLU, bool)
NBLA_REGISTER_FUNCTION_HEADER(ReLU6)
NBLA_DEFINE_TRANSFORM_UNARY (ReLU6, std::min(std::max(x,(T) 0),(T) 6),(T) 0< x &&x<(T) 6 ? dy :(T) 0, false, true)

ReLU6.

ReLU6 defined as

\[ y_i = \min(\max(x,0),6) \]

Inputs:

  • N-D array.

Outputs:

  • N-D array.

Template Parameters:

T – Data type for computation.

NBLA_DEFINE_TRANSFORM_UNARY_1 (ResetInf, std::isinf(x) ?(T) a0 :x, std::isinf(x) ?(T) 0 :dy, false, true, double)

Replace Inf/-Inf with a scalar value specified by val.

Inputs:

  • N-D array.

Outputs:

  • N-D array.

Parameters:

val – Value of the scalar.

Template Parameters:

T – Data type for computation.

NBLA_DEFINE_TRANSFORM_UNARY_1 (ResetNaN, std::isnan(x) ?(T) a0 :x, std::isnan(x) ?(T) 0 :dy, false, true, double)

Replace NaNs with a scalar value specified by val.

Inputs:

  • N-D array.

Outputs:

  • N-D array.

Parameters:

val – Value of the scalar.

Template Parameters:

T – Data type for computation.

NBLA_REGISTER_FUNCTION_HEADER(Reshape, const vector<int>&, bool)
NBLA_REGISTER_FUNCTION_HEADER(RNN, int, const string&, float, bool, bool)
NBLA_REGISTER_FUNCTION_HEADER(RoiAlign, const vector<int>&, const vector<float>&, int, bool)
NBLA_DEFINE_TRANSFORM_UNARY (Round, std::round(x), dy, false, false)
NBLA_REGISTER_FUNCTION_HEADER(ScatterAdd, int)
NBLA_REGISTER_FUNCTION_HEADER(ScatterNd, const vector<int>&, bool)
NBLA_REGISTER_FUNCTION_HEADER(SearchSorted, bool)
NBLA_REGISTER_FUNCTION_HEADER(SELU, double, double)
NBLA_REGISTER_FUNCTION_HEADER(Shape, int, int)
NBLA_REGISTER_FUNCTION_HEADER(Shift, const vector<int>&, const string&)
NBLA_DEFINE_TRANSFORM_UNARY (Sigmoid,(T) 1/((T) 1+std::exp(-x)), dy *y *((T) 1 - y), true, false)
NBLA_REGISTER_FUNCTION_HEADER(SigmoidCrossEntropy)
NBLA_REGISTER_FUNCTION_HEADER(Sign, float)
NBLA_DEFINE_TRANSFORM_UNARY_1 (Sign,(x >(T) 0) ?(T) 1 :((x<(T) 0) ?(T) -1 :(T) a0), dy, false, false, float)
NBLA_DEFINE_TRANSFORM_UNARY (Sin, std::sin(x), dy *std::cos(x), false, true)
NBLA_REGISTER_FUNCTION_HEADER(Sinc)
NBLA_DEFINE_TRANSFORM_UNARY (Sinc, x==(T) 0 ?(T) 1 :std::sin(x)/x, x==(T) 0 ?(T) 0 :dy *(std::cos(x) - std::sin(x)/x)/x, false, true)
NBLA_DEFINE_TRANSFORM_UNARY (Sinh, std::sinh(x), dy *std::cosh(x), false, true)
NBLA_REGISTER_FUNCTION_HEADER(Sink, bool)
NBLA_REGISTER_FUNCTION_HEADER(Slice, const vector<int>&, const vector<int>&, const vector<int>&)
NBLA_REGISTER_FUNCTION_HEADER(Softmax, int)
NBLA_REGISTER_FUNCTION_HEADER(SoftmaxCrossEntropy, int)
NBLA_DEFINE_TRANSFORM_UNARY_1 (SoftPlus, x >(T) 0 ? x+std::log(std::exp(-x *(T) a0)+(T) 1)/(T) a0 :(std::log(std::exp(x *(T) a0)+(T) 1))/(T) a0, dy/((T) 1+std::exp(-(T) a0 *x)), false, true, double)

SoftPlus.

SoftPlus defined as

\[ y_i = \frac{1}{\beta} * \log(\exp(\beta * x)+1) \]
Inputs:
  • N-D array. Outputs:

  • N-D array.

Template Parameters:

T – Data type for computation.

NBLA_REGISTER_FUNCTION_HEADER(SoftSign)
NBLA_DEFINE_TRANSFORM_UNARY (SoftSign, x/(1+std::abs(x)), dy/std::pow((1+std::abs(x)), 2), false, true)
NBLA_REGISTER_FUNCTION_HEADER(Sort, int, bool, bool, bool)
NBLA_REGISTER_FUNCTION_HEADER(SpectralNorm, int, int, float, bool, bool)
NBLA_REGISTER_FUNCTION_HEADER(Split, int)
NBLA_DEFINE_TRANSFORM_BINARY (SquaredError,(x0 - x1) *(x0 - x1),(T) 2 *dy *(x0 - x1),(T) -2 *dy *(x0 - x1), false, false, true, true)
NBLA_REGISTER_FUNCTION_HEADER(Stack, int)
NBLA_REGISTER_FUNCTION_HEADER(STFT, int, int, int, const string&, bool, const string&, bool)
NBLA_DEFINE_TRANSFORM_BINARY_INPLACE (Sub2, x0 - x1, dy, -dy, false, false, false, false, true)
NBLA_REGISTER_FUNCTION_HEADER(Sum, const vector<int>&, bool)
NBLA_REGISTER_FUNCTION_HEADER(SumPooling, const vector<int>&, const vector<int>&, bool, const vector<int>&, bool)
NBLA_DEFINE_TRANSFORM_UNARY (Swish, x/((T) 1+std::exp(-x)), dy *(y+((T) 1/((T) 1+std::exp(-x))) *((T) 1 - y)), true, true)
NBLA_REGISTER_FUNCTION_HEADER(SyncBatchNormalization, const shared_ptr<Communicator>&, const string&, const vector<int>&, float, float, bool)
NBLA_DEFINE_TRANSFORM_UNARY (Tan, std::tan(x), dy/std::pow(std::cos(x),(T) 2.), false, true)
NBLA_DEFINE_TRANSFORM_UNARY (Tanh, std::tanh(x), dy *((T) 1 - y *y), true, false)
NBLA_REGISTER_FUNCTION_HEADER(TanhShrink)
NBLA_DEFINE_TRANSFORM_UNARY (TanhShrink, x - std::tanh(x), dy *std::pow(std::tanh(x),(T) 2), false, true)
NBLA_REGISTER_FUNCTION_HEADER(TensorNormalization, const vector<int>&, float, bool, bool)
NBLA_REGISTER_FUNCTION_HEADER(Tile, const vector<int>&)
NBLA_REGISTER_FUNCTION_HEADER(TopKData, int, bool, bool, int, bool, bool)
NBLA_REGISTER_FUNCTION_HEADER(TopKGrad, int, bool, int)
NBLA_REGISTER_FUNCTION_HEADER(TopNError, int, int)
NBLA_REGISTER_FUNCTION_HEADER(Transpose, const vector<int>&)
NBLA_REGISTER_FUNCTION_HEADER(Trilu, int, bool)
NBLA_REGISTER_FUNCTION_HEADER(Unique, bool, int, bool, bool, bool, bool)
NBLA_REGISTER_FUNCTION_HEADER(Unpooling, const vector<int>&, bool)
template<typename T, typename BinaryOp>
void transform_binary(const Size_t size, const T *x0, const T *x1, T *y, BinaryOp op, const Size_t ndim, const Size_t *strides_x0, const Size_t *strides_x1, const Size_t *strides_y, const Size_t *shape_y)
template<typename T, typename BinaryOp>
void transform_binary_grad0(const Size_t size, const T *dy, const T *x0, const T *x1, const T *y, T *g0, const bool inplace, BinaryOp op, const Size_t ndim, const Size_t *strides_x0, const Size_t *strides_x1, const Size_t *strides_y, const Size_t *shape_y)
template<typename T, typename BinaryOp>
void transform_binary_grad1(const Size_t size, const T *dy, const T *x0, const T *x1, const T *y, T *g1, const bool inplace, BinaryOp op, const Size_t ndim, const Size_t *strides_x0, const Size_t *strides_x1, const Size_t *strides_y, const Size_t *shape_y)
template<typename T, typename UnaryOp>
void transform_unary(int size, const T *x, T *y, UnaryOp op)
template<typename T, typename UnaryOp, bool accum>
void transform_unary_grad(int size, const T *dy, const T *x, const T *y, T *g, const bool inplace, UnaryOp op)
template<typename T, bool accum = false>
inline void kernel_bool_gather(int D, int B, int nnz, T *sdata, const T *gdata, const T *mask)
template<typename T, bool accum = false, bool inplace = false>
inline void kernel_bool_scatter(int D, int B, int nnz, T *gdata, const T *sdata, const T *mask)
NBLA_API VariablePtr get_dropout_mask (VariablePtr dropout_inputs)

Danger.

Do not call this as a user interface.

Get the Variable holding a mask, meaning the externally accessible member variable of Dropout.

NBLA_API void set_dropout_mask (Variable *dropout_input, VariablePtr dropout_mask)

Danger.

Do not call this as a user interface.

Set the Variable holding a mask, meaning the externally accessible member variable of Dropout.

template<typename T>
void create_window(Variable *window, const string &window_type, const int window_size, const int fft_size, const Context &ctx)
NBLA_REGISTER_FUNCTION_HEADER(VATNoise, int, float)
NBLA_REGISTER_FUNCTION_HEADER(WarpByFlow)
NBLA_REGISTER_FUNCTION_HEADER(WarpByGrid, const string&, const string&, bool, bool)
NBLA_REGISTER_FUNCTION_HEADER(WeightNormalization, int, float)
NBLA_REGISTER_FUNCTION_HEADER(WeightStandardization, int, float)
NBLA_REGISTER_FUNCTION_HEADER(Where)
template<typename Item>
string print_function_items(vector<shared_ptr<Item>> items)
NBLA_API void set_function_pre_hook (const string &key, const function_hook_type &cb)

API.

NBLA_API void set_function_post_hook (const string &key, const function_hook_type &cb)
NBLA_API void unset_function_pre_hook (const string &key)
NBLA_API void unset_function_post_hook (const string &key)
NBLA_API void set_solver_pre_hook (const string &key, const update_hook_type &cb)

API.

NBLA_API void set_solver_post_hook (const string &key, const update_hook_type &cb)
NBLA_API void unset_solver_pre_hook (const string &key)
NBLA_API void unset_solver_post_hook (const string &key)
inline uint16_t float2halfbits(float fvalue)
inline float halfbits2float(uint16_t hbits)
struct NBLA_ALIGN (2) Half

Implementation of Half precision float emulating a builtin scalar type.

AOP_TYPE (+)
AOP_TYPE (-)
AOP_TYPE * AOP_TYPE (/);#define ROP_TYPE(OP, TYPE) #define IROP_TYPE(OP, TYPE) #define ROP(TYPE) ROP(unsigned char
ROP(char)
ROP(unsigned short)
ROP(short)
ROP(unsigned int)
ROP(int)
ROP(unsigned long)
ROP(long)
ROP(unsigned long long)
ROP(long long)
ROP(float)
ROP(double)
ROP(bool)
ROP(long double)
ROP_TYPE (<, Half)
ROP_TYPE (>, Half)
ROP_TYPE (<=, Half)
ROP_TYPE (>=, Half)
ROP_TYPE (!=, Half)
NBLA_API vector< NdArrayPtr > execute (FunctionPtr func, const vector< NdArrayPtr > &inputs, int n_outputs, vector< NdArrayPtr > outputs={})

Execute a function given NdArray instances as inputs.

Parameters:
  • func[in] A shared pointer of Function.

  • inputs[in] A vector of NdArray as function inputs. NdArrays are converted to data regions of Variables.

  • n_outputs[in] Number of function outputs.

  • outputs, This[inout] can be empty usually. Elements which are not nullptr will be used as in-place outputs.

NBLA_API void execute (FunctionPtr f, const Variables &inputs, const Variables &outputs)
NBLA_API void backward (FunctionPtr f, const Variables &inputs, const Variables &outputs, const vector< bool > &propagate_down, const vector< bool > &accum, bool with_setup=false)
NBLA_API void init_cpu ()

Initialize NNabla CPU features.

User usually does not need to call this function manually.

NBLA_API void clear_cpu_memory_cache ()

Clear CPU memory cache.

NBLA_API void print_cpu_memory_cache_map ()

Print CPU memory cache map.

NBLA_API vector< string > cpu_array_classes ()

Get CPU array classes.

NBLA_API void _cpu_set_array_classes (const vector< string > &a)

Set CPU array classes.

NBLA_API void cpu_device_synchronize (const string &device)
NBLA_API int cpu_get_device_count ()
NBLA_API vector< string > cpu_get_devices ()
NBLA_API float calc_normal_std_he_forward (int n_map_in, int n_map_out, int kernel_dim_product)
NBLA_API float calc_normal_std_he_backward (int n_map_in, int n_map_out, int kernel_dim_product)
NBLA_API float calc_normal_std_glorot (int n_map_in, int n_map_out, int kernel_dim_product)
NBLA_API float calc_uniform_lim_glorot (int n_map_in, int n_map_out, int kernel_dim_product)
CgVariablePtr make_parameter(Shape_t shape, Initializer *initializer, bool need_grad)
NBLA_API bool get_global_recompute ()
NBLA_API void c_set_global_recompute (const bool recompute)
NBLA_API bool c_get_global_recompute ()
NBLA_REGISTER_SOLVER_HEADER(AdaBelief, float, float, float, float, float, bool, bool, bool, bool)
NBLA_REGISTER_SOLVER_HEADER(AdaBound, float, float, float, float, float, float)
NBLA_REGISTER_SOLVER_HEADER(Adadelta, float, float, float)
NBLA_REGISTER_SOLVER_HEADER(Adagrad, float, float)
NBLA_REGISTER_SOLVER_HEADER(Adam, float, float, float, float)
NBLA_REGISTER_SOLVER_HEADER(Adamax, float, float, float, float)
NBLA_REGISTER_SOLVER_HEADER(AdamW, float, float, float, float, float)
NBLA_REGISTER_SOLVER_HEADER(AMSBound, float, float, float, float, float, float, bool)
NBLA_REGISTER_SOLVER_HEADER(AMSGRAD, float, float, float, float, bool)
template<typename T>
void clip_grad_by_norm_cpu(const Context &ctx, const shared_ptr<Variable> param, float clip_norm)
NBLA_REGISTER_SOLVER_HEADER(Lamb, float, float, float, float, float, float, bool)
NBLA_REGISTER_SOLVER_HEADER(Lars, float, float, float, float)
NBLA_REGISTER_SOLVER_HEADER(Lion, float, float, float)
template<typename T>
bool check_inf_grad_cpu(const Context &ctx, const shared_ptr<Variable> param)
template<typename T>
bool check_nan_grad_cpu(const Context &ctx, const shared_ptr<Variable> param)
template<typename T>
bool check_inf_or_nan_grad_cpu(const Context &ctx, const shared_ptr<Variable> param)
template<typename T>
void scale_grad_impl_cpu(const Context &ctx, const shared_ptr<Variable> param, float scale)
NBLA_REGISTER_SOLVER_HEADER(Momentum, float, float)
NBLA_REGISTER_SOLVER_HEADER(Nesterov, float, float)
NBLA_REGISTER_SOLVER_HEADER(RMSprop, float, float, float)
NBLA_REGISTER_SOLVER_HEADER(RMSpropGraves, float, float, float, float)
NBLA_REGISTER_SOLVER_HEADER(Sgd, float)
NBLA_REGISTER_SOLVER_HEADER(SgdW, float, float, float)
template<typename T>
void weight_decay_cpu(const Context &ctx, const shared_ptr<Variable> param, float decay_rate)
inline void *malloc(size_t size)
inline void free(void *p)
template<typename T, typename ...Args>
T *new_object(Args&&... args)

Allocate memory and call constructor of a new object.

template<typename T>
void delete_object(T *p)

Call destructor and deallocate memory of a object.

template<typename T>
T *new_array(size_t n)

Allocate memory and call constructor of a new array.

template<typename T>
void delete_array(T *p)

Call destructor and deallocate memory of an array.

inline void refine_axis(int &axis, int ndim)
inline void refine_axes(vector<int> &axes, int ndim)
template<typename T, bool MODULATED>
void modulated_deformable_im2col_cpu(const T *im, const T *offset, const T *mask, const int c_i, const vector<int> &shape, const vector<int> &k, const vector<int> &p, const vector<int> &s, const vector<int> &d, const int deformable_group, T *col)
template<typename T, bool MODULATED>
void modulated_deformable_col2im_cpu(const T *col, const T *offset, const T *mask, const int c_i, const vector<int> &shape, const vector<int> &k, const vector<int> &p, const vector<int> &s, const vector<int> &d, const int deformable_group, T *grad_im)
template<typename T, bool MODULATED>
void modulated_deformable_col2im_coord_cpu(const T *col, const T *im, const T *offset, const T *mask, const int c_i, const vector<int> &shape, const vector<int> &k, const vector<int> &p, const vector<int> &s, const vector<int> &d, const int deformable_group, T *grad_offset, T *grad_mask)
NBLA_API NdArrayPtr from_dlpack (DLManagedTensor *from)

NNabla borrows a DLPack Tensor as a NdArray.

NdArray is newly created shared the memory in DLPack and is returned.

NBLA_API void from_dlpack (DLManagedTensor *from, NdArray *to)

NNabla borrows a DLPack Tensor as a NdArray.

The passed NdArray is destroyed by this function. Into the NdArray, a array shared the memory in DLPack is newly set.

NBLA_API DLManagedTensor * to_dlpack (NdArray *array)

NNabla lends the head array in a NdArray as a DLPack Tensor.

NBLA_API DLManagedTensor * to_dlpack (NdArray *array, const dtypes dtype, const Context &ctx)

NNabla lends a array in NdArray as a DLPack Tensor according to given dtype and context.

NBLA_API void call_deleter (DLManagedTensor *dlp)

Call deleter in DLManagedTensor and then delete DLManagedTensor itself.

dtypes convert_dlpack_type_to_dtype(const DLDataType &dlp_type)

Convert DLDataType to Nnabla dtype.

template<typename T>
void fold_from_patches(const T *column_data, T *outmap_data, const int channels, const vector<int> &shape, const vector<int> &kernel, const vector<int> &padding, const vector<int> &stride, const vector<int> &dilation)
template<typename T, bool largest>
inline void top_k(const T *x, const size_t n, const size_t k, size_t *out)
template<typename T, bool largest>
inline void top_k_abs(const T *x, const size_t n, const size_t k, size_t *out)
template<typename T>
void unfold_to_patches(T const *sample_data, T *column_data, const int channels, const vector<int> &shape, const vector<int> &kernel, const vector<int> &padding, const vector<int> &stride, const vector<int> &dilation)
const string nbla_version(void)
const string nbla_author(void)
const string nbla_author_email(void)
const string nbla_build_number(void)
class CpuArray : public nbla::Array
#include <cpu_array.hpp>

Array for CPU.

Subclassed by nbla::CpuCachedArray

Public Functions

virtual void copy_from(const Array *src_array)

Copy from Array.

virtual void zero()

Fill all element with zero.

virtual void fill(float value)

Fill all element with given value.

class CpuCachedArray : public nbla::CpuArray
#include <cpu_array.hpp>

Cached CPU array.

class CpuDlpackArray : public nbla::DlpackArray
#include <cpu_dlpack_array.hpp>

CPU array with a borrwed memory pointer from other frameworks via DLPack.

Public Functions

virtual void copy_from(const Array *src_array)

Copy from Array.

virtual void zero()

Fill all element with zero.

virtual void fill(float value)

Fill all element with given value.

class DlpackArray : public nbla::Array
#include <dlpack_array.hpp>

Array with a borrwed memory pointer from other frameworks via DLPack.

Subclassed by nbla::CpuDlpackArray

Public Functions

DlpackArray(const Size_t size, dtypes dtype, const Context &ctx)

Constructor not to finish the construction of this class.

This special constructor is expected to be called from ArrayCreator::create in SyncedArray::get/cast. After the call, DlpackArray::borrow must be done to complete the construction.

virtual void copy_from(const Array *src_array)

Copy from Array.

virtual void zero()

Fill all element with zero.

virtual void fill(float value)

Fill all element with given value.

class Array : public std::enable_shared_from_this<Array>
#include <array.hpp>

An abstract class of Array interface.Shared pointer of NdArray.

This is extended to implement a new array class (see CpuArray, CudaArray etc.).

Subclassed by nbla::CpuArray, nbla::DlpackArray

Public Functions

template<typename T = void>
inline T *pointer()

Get object pointer.

template<typename T = void>
inline const T *const_pointer() const

Get constant object pointer.

inline AllocatorMemoryPtr memory() const

Get shared pointer of the AllocatorMemory.

inline Size_t offset() const

Get offset.

inline dtypes dtype() const

Return dtype.

inline Size_t size() const

Return size of descendant dimensions of specified axis.

inline Context context() const

Return context.

virtual void copy_from(const Array *src_array) = 0

Copy from Array.

virtual NBLA_API void zero ()=0

Fill all element with zero.

virtual NBLA_API void fill (float value)=0

Fill all element with given value.

virtual NBLA_API void set_event (EventPtr eptr)

Set an event.

virtual NBLA_API void wait_event (const Context ctx, const int async_flags=AsyncFlag::NONE)

Wait for the end of an event.

Parameters:
  • ctxContext where the event is waited for.

  • async_flags – .

virtual NBLA_API bool have_event ()

Check an event exist.

virtual NBLA_API Ptr getptr ()

Get shared_ptr of this object.

Public Static Functions

static Context filter_context(const Context &ctx)

Filter a Context into a minimal information to describe an Array.

class ArrayGroup
#include <array_registry.hpp>

Array classes which can be get/cast without copy.

Public Static Functions

static void add_group(const string &array_class, const string &group_name)

Register a new array.

static string get_group(const string &array_class)

Get the group name of an array.

class ArrayCreator
#include <array_registry.hpp>

ArrayCreator class this is never be instantiated.

Public Static Functions

static Array *create(const Size_t size, dtypes dtype, const Context &ctx, const AllocatorMemoryPtr mem = nullptr, const Size_t offset = 0)

Interface to create array.

static Context filter_context(const Context &ctx)

Filter an context into minimal info that describes the context.

static void add_creator(const string &name, Creator creator, FilterContext filter_context)

Register new creator.

class ArraySynchronizer
#include <array_registry.hpp>

ArraySynchronizer.

Public Static Functions

static void synchronize(const string &src_class, Array *src_array, const string &dst_class, Array *dst_array, const int async_flags = AsyncFlag::NONE)

Synchronize array.

static void add_synchronizer(const string &src_class, const string &dst_class, Synchronizer synchronizer)

Register new synchronizer.

class AutoForward
#include <auto_forward.hpp>

Singleton class for storing global context.

Public Functions

bool get_auto_forward() const

Get auto forward.

void set_auto_forward(bool autoforward)

Set current context.

class BackendBase
#include <backend_base.hpp>

Base class of singleton classes for storing some handles or configs for computation on a backend.

Subclassed by nbla::Cpu

Public Functions

virtual vector<string> array_classes() const = 0

Available array class list used in CPU Function implementations.

virtual void _set_array_classes(const vector<string> &a) = 0

Set array class list.

Note

Dangerous to call. End users shouldn’t call.

virtual void register_array_class(const string &name) = 0

Register array class to available list by name.

virtual shared_ptr<Allocator> caching_allocator() = 0

Get a caching allocator.

virtual shared_ptr<Allocator> naive_allocator() = 0

Get a no-cache allocator.

virtual void free_unused_host_caches() = 0

Free all unused host memory caches.

virtual void device_synchronize(const string &device) = 0

Synchronize host to device.

virtual void default_stream_synchronize(const string &device) = 0

Synchronize host to default stream of device.

virtual void create_lms_streams(int device = -1) = 0

Create non blockuing streams for data transfer.

class BackendUtils
#include <backend_registry.hpp>

BackendUtils class.

This class is never be instantiated.

Public Static Functions

static void add_backend(const string &backend_name, BackendGetter backend_getter)

Register new synchronizer.

static vector<string> array_classes(const Context ctx)

Call array_classes of the backend in a context.

static void _set_array_classes(const Context ctx, const vector<string> &a)

Call _set_array_classes of the backend in a context.

Note

Dangerous to call. End users shouldn’t call.

static void register_array_class(const Context ctx, const string &name)

Call register_array_class of the backend in a context.

static shared_ptr<Allocator> caching_allocator(const Context ctx)

Call caching_allocator of the backend in a context.

static shared_ptr<Allocator> naive_allocator(const Context ctx)

Call naive_allocator of the backend in a context.

static void free_unused_host_caches(const Context ctx)

Free all unused host memory caches.

static void device_synchronize(const Context ctx)

Call device_synchronize of the backend in a context.

static void default_stream_synchronize(const Context ctx)

Synchronize host to default stream of device.

static void create_lms_streams(const Context ctx)

Call create_lms_streams of the backend in a context.

class DestructorCallback
#include <common.hpp>

Scoped callback.

template<typename T>
class DataParallelCommunicator : public nbla::Communicator
#include <data_parallel_communicator.hpp>

DataParallelcommunicator interface which is extended to implement a new DataParallelcommunicator class.

DataParallelcommunicator exchanges gradients parameters or parameters itself.

Public Functions

DataParallelCommunicator(const Context &ctx)

Constructor takes at least context and parameters.

Parameters:

ctxContext

void add_context_and_parameters(const pair<Context, vector<pair<string, VariablePtr>>> &ctx_params)

Adding context and parameters communicated via this class.

Parameters:

cparams – pair<Context, vector of pair<name, VariablePtr>

void remove_context_parameters(const pair<Context, vector<string>> &ctx_keys)

Remove previously registered parameters by keys.

void clear_context_parameters()

Clear all parameters.

virtual void init()

Initall or initrank, depending multi-threads or multi-processes.

This function MUST be called after all parameters communicated are added by `add_context_and_parameters` method.

virtual void reduce(const vector<NdArrayPtr> &ndarray_list, int dst, bool division = false, bool inplace = false, const string &group = "world")

reduce over parameters added.

Parameters:
  • ndarray_list – Vector of NdArrayPtr.

  • dst – Destination rank.

  • division – Divide the reduced value.

  • inplace – Pack the arrays into one large array if false.

  • group – Name of a group.

virtual void reduce(NdArrayPtr ndarray, int dst, bool division = false, bool inplace = false, const string &group = "world")

reduce over parameters added.

Parameters:
  • data – NdArrayPtr.

  • dst – Destination rank.

  • division – Divide the reduced value.

  • inplace – Pack the arrays into one large array if false.

  • group – Name of a group.

virtual void allreduce(bool division = false, bool inplace = false)

allreduce over parameters added.

Deprecated. Use all_reduce.

Currently, allreduce is applied to gradient regions.

Parameters:
  • division – Divide the reduced value.

  • inplace – Pack the arrays into one large array if false.

  • group – Name of a group.

virtual void all_reduce(const vector<NdArrayPtr> &ndarray_list, bool division = false, bool inplace = false, const string &group = "world")

all_reduce over parameters added.

Parameters:
  • ndarray_list – Vector of NdArrayPtr

  • division – Divide the reduced value.

  • inplace – Pack the arrays into one large array if false.

  • group – Name of a group.

virtual void all_reduce(NdArrayPtr ndarray, bool division = false, bool inplace = false, const string &group = "world")

all_reduce over parameters added.

Parameters:
  • data – NdArrayPtr

  • division – Divide the reduced value.

  • inplace – Pack the arrays into one large array if false.

  • group – Name of a group.

virtual void reduce_scatter(const vector<NdArrayPtr> &ndarray_list, NdArrayPtr ndarray, bool division = false, const string &group = "world")

reducescatter.

Parameters:
  • ndarray_list – Vector of NdArrayPtr

  • ndarray – NdArrayPtr

  • division – Divide the reduced value.

  • group – Name of a group.

virtual void bcast(const vector<NdArrayPtr> &ndarray_list, int src, bool inplace = false, const string &group = "world")

broadcast.

Parameters:
  • ndarray_list – Vector of NdArrayPtr.

  • src – Source rank.

  • inplace – Pack the arrays into one large array if false.

  • group – Name of a group.

virtual void bcast(NdArrayPtr ndarray, int src, bool inplace = false, const string &group = "world")

broadcast.

Parameters:
  • data – NdArrayPtr.

  • src – Source rank.

  • inplace – Pack the arrays into one large array if false.

  • group – Name of a group.

virtual void all_gather(NdArrayPtr ndarray, const vector<NdArrayPtr> &ndarray_list, const string &group = "world")

all_gather.

Parameters:
  • ndarray – data to be sent.

  • ndarray_list – Vector of NdArrayPtr to receive data.

  • group – Name of a group.

virtual void reduce_async(bool division = false)

reduce asynchronously.

Parameters:

division – Divide the reduced value.

virtual void allreduce_async(bool division = false, bool inplace = false)

reduce asynchronously.

Parameters:
  • division – Divide the reduced value.

  • inplace – Pack the arrays into one large array if false.

virtual void reducescatter_async(bool division = false)

reducescatter asynchronously.

Parameters:

division – Divide the reduced value.

virtual void bcast_async()

broadcast asynchronously.

virtual void allgather_async()

allgather asynchronously.

vector<string> allowed_array_classes()

Get array classes that are allowed to be specified by Context.

template<typename T>
class MultiProcessDataParallelCommunicator : public nbla::Communicator
#include <multi_process_data_parallel_communicator.hpp>

MultiProcessDataParallelCommunicator interface which is extended to implement a new DataParallelcommunicator class.

MultiProcessDataParallelCommunicator exchanges gradients parameters or parameters itself.

Public Functions

MultiProcessDataParallelCommunicator(const Context &ctx)

Constructor takes at least context and parameters.

Parameters:

ctxContext

void add_context_and_parameters(const pair<Context, vector<pair<string, VariablePtr>>> &ctx_params)

Adding context and parameters communicated via this class.

Parameters:

cparams – pair<Context, vector of pair<name, VariablePtr>

void remove_context_parameters(const pair<Context, vector<string>> &ctx_keys)

Remove previously registered parameters by keys.

void clear_context_parameters()

Clear all parameters.

virtual void init()

Initall or initrank, depending multi-threads or multi-processes.

This function MUST be called after all parameters communicated are added by `add_context_and_parameters` method.

virtual string new_group(pair<string, vector<int>> name_ranks_pair)

Create group.

virtual unordered_map<string, vector<int>> list_groups()

List groups.

virtual vector<int> find_group(const string &group)

Find groups.

virtual void reduce(const vector<NdArrayPtr> &ndarray_list, int dst, bool division = false, bool inplace = false, const string &group = "world")

reduce over parameters added.

Parameters:
  • ndarray_list – Vector of NdArrayPtr.

  • dst – Destination rank.

  • division – Divide the reduced value.

  • inplace – Pack the arrays into one large array if false.

  • group – Name of a group.

virtual void reduce(NdArrayPtr ndarray, int dst, bool division = false, bool inplace = false, const string &group = "world")

reduce over parameters added.

Parameters:
  • data – NdArrayPtr.

  • dst – Destination rank.

  • division – Divide the reduced value.

  • inplace – Pack the arrays into one large array if false.

  • group – Name of a group.

virtual void allreduce(bool division = false, bool inplace = false)

allreduce over parameters added.

Deprecated. Use all_reduce.

Currently, allreduce is applied to gradient regions.

Parameters:
  • division – Divide the reduced value.

  • inplace – Pack the arrays into one large array if false.

  • group – Name of a group.

virtual void all_reduce(const vector<NdArrayPtr> &ndarray_list, bool division = false, bool inplace = false, const string &group = "world")

all_reduce over parameters added.

Parameters:
  • ndarray_list – Vector of NdArrayPtr

  • division – Divide the reduced value.

  • inplace – Pack the arrays into one large array if false.

  • group – Name of a group.

virtual void all_reduce(NdArrayPtr ndarray, bool division = false, bool inplace = false, const string &group = "world")

all_reduce over parameters added.

Parameters:
  • data – NdArrayPtr

  • division – Divide the reduced value.

  • inplace – Pack the arrays into one large array if false.

  • group – Name of a group.

virtual void reduce_scatter(const vector<NdArrayPtr> &ndarray_list, NdArrayPtr ndarray, bool division = false, const string &group = "world")

reducescatter.

Parameters:
  • ndarray_list – Vector of NdArrayPtr

  • ndarray – NdArrayPtr

  • division – Divide the reduced value.

  • group – Name of a group.

virtual void bcast(const vector<NdArrayPtr> &ndarray_list, int src, bool inplace = false, const string &group = "world")

broadcast.

Parameters:
  • ndarray_list – Vector of NdArrayPtr.

  • src – Source rank.

  • inplace – Pack the arrays into one large array if false.

  • group – Name of a group.

virtual void bcast(NdArrayPtr ndarray, int src, bool inplace = false, const string &group = "world")

broadcast.

Parameters:
  • data – NdArrayPtr.

  • src – Source rank.

  • inplace – Pack the arrays into one large array if false.

  • group – Name of a group.

virtual void all_gather(NdArrayPtr ndarray, const vector<NdArrayPtr> &ndarray_list, const string &group = "world")

all_gather.

Parameters:
  • ndarray – data to be sent.

  • ndarray_list – Vector of NdArrayPtr to receive data.

  • group – Name of a group.

virtual void reduce_async(bool division = false)

reduce asynchronously.

Parameters:

division – Divide the reduced value.

virtual void allreduce_async(bool division = false, bool inplace = false)

reduce asynchronously.

Parameters:
  • division – Divide the reduced value.

  • inplace – Pack the arrays into one large array if false.

virtual void reducescatter_async(bool division = false)

reducescatter asynchronously.

Parameters:

division – Divide the reduced value.

virtual void bcast_async()

broadcast asynchronously.

virtual void allgather_async()

allgather asynchronously.

vector<string> allowed_array_classes()

Get array classes that are allowed to be specified by Context.

class Communicator
#include <communicator.hpp>

Communicator interface which is extended to implement a new Communicator class.

Communicator exchanges gradients.

Subclassed by nbla::DataParallelCommunicator< T >, nbla::MultiProcessDataParallelCommunicator< T >

Public Functions

explicit Communicator(const Context &ctx)

Constructor takes at least context and parameters.

Parameters:

ctxContext

virtual ~Communicator() = 0

Name of Communicator class, usually class name.

void add_context_and_parameters(const pair<Context, vector<pair<string, VariablePtr>>> &ctx_params)

Add context and parameters.

Parameters:

pair – pair<Context, vector of pair<name, VariablePtr>

void remove_context_parameters(const pair<Context, vector<string>> &ctx_keys)

Remove previously registered parameters.

void clear_context_parameters()

Clear all parameters.

virtual void init()

Initall or initrank, depending multi-threads or multi-processes.

This function MUST be called after all parameters communicated are added by add_context_and_parameters method.

virtual void barrier()

Synchronize all processes in the specified group.

virtual void abort()

Abort all processes in the specified group.

virtual string new_group(pair<string, vector<int>> name_ranks_pair)

Create group.

virtual unordered_map<string, vector<int>> list_groups()

List groups.

virtual vector<int> find_group(const string &group)

Find groups.

void check_array_class(Context ctx, VariablePtr vp)

Check difference of the array classes.

Check difference between the array class of the context and that of the synced_array. If it differs, the error occurs.

virtual void reduce(const vector<NdArrayPtr> &ndarray_list, int dst, bool division = false, bool inplace = false, const string &group = "world")

reduce over parameters added.

Parameters:
  • ndarray_list – Vector of NdArrayPtr.

  • dst – Destination rank.

  • division – Divide the reduced value.

  • inplace – Pack the arrays into one large array if false.

  • group – Name of a group.

virtual void reduce(NdArrayPtr ndarray, int dst, bool division = false, bool inplace = false, const string &group = "world")

reduce over parameters added.

Parameters:
  • data – NdArrayPtr.

  • dst – Destination rank.

  • division – Divide the reduced value.

  • inplace – Pack the arrays into one large array if false.

  • group – Name of a group.

virtual void allreduce(bool division = false, bool inplace = false)

allreduce over parameters added.

Deprecated. Use all_reduce.

Currently, allreduce is applied to gradient regions.

Parameters:
  • division – Divide the reduced value.

  • inplace – Pack the arrays into one large array if false.

  • group – Name of a group.

virtual void all_reduce(const vector<NdArrayPtr> &ndarray_list, bool division = false, bool inplace = false, const string &group = "world")

all_reduce over parameters added.

Parameters:
  • ndarray_list – Vector of NdArrayPtr

  • division – Divide the reduced value.

  • inplace – Pack the arrays into one large array if false.

  • group – Name of a group.

virtual void all_reduce(NdArrayPtr ndarray, bool division = false, bool inplace = false, const string &group = "world")

all_reduce over parameters added.

Parameters:
  • data – NdArrayPtr

  • division – Divide the reduced value.

  • inplace – Pack the arrays into one large array if false.

  • group – Name of a group.

virtual CommunicatorBackwardCallbackPtr all_reduce_callback(const vector<NdArrayPtr> &ndarray_list, size_t pack_size, bool division = false, const string &group = "world", float scale_grad = 1.0, bool keep_dtype = false)

all_reduce over parameters added.

Parameters:
  • ndarray_list – Vector of NdArrayPtr

  • pack_size – The number of values contained in the packed data.

  • division – Divide the reduced value.

  • scale_grad – Scaling gradient before allreduce. See Python documentation for more details.

  • keep_dtype – Keep dtype of arrays unchanged. See Python documentation for more details.

virtual CommunicatorBackwardCallbackPtr all_reduce_callback(NdArrayPtr ndarray, size_t pack_size, bool division = false, const string &group = "world", float scale_grad = 1.0, bool keep_dtype = false)

all_reduce over parameters added.

Parameters:
  • ndarray – NdArrayPtr

  • pack_size – The number of values contained in the packed data.

  • division – Divide the reduced value.

  • scale_grad – Scaling gradient before allreduce. See Python documentation for more details.

  • keep_dtype – Keep dtype of arrays unchanged. See Python documentation for more details.

virtual void reduce_scatter(const vector<NdArrayPtr> &ndarray_list, NdArrayPtr ndarray, bool division = false, const string &group = "world")

reducescatter.

Parameters:
  • ndarray_list – Vector of NdArrayPtr

  • ndarray – NdArrayPtr

  • division – Divide the reduced value.

  • group – Name of a group.

virtual void bcast(const vector<NdArrayPtr> &ndarray_list, int src, bool inplace = false, const string &group = "world")

broadcast.

Parameters:
  • ndarray_list – Vector of NdArrayPtr.

  • src – Source rank.

  • inplace – Pack the arrays into one large array if false.

  • group – Name of a group.

virtual void bcast(NdArrayPtr ndarray, int src, bool inplace = false, const string &group = "world")

broadcast.

Parameters:
  • data – NdArrayPtr.

  • src – Source rank.

  • inplace – Pack the arrays into one large array if false.

  • group – Name of a group.

virtual void all_gather(NdArrayPtr ndarray, const vector<NdArrayPtr> &ndarray_list, const string &group = "world")

all_gather.

Parameters:
  • ndarray – data to be sent.

  • ndarray_list – Vector of NdArrayPtr to receive data.

  • group – Name of a group.

virtual void reduce_async(bool division = false)

reduce asynchronously.

Parameters:

division – Divide the reduced value.

virtual void allreduce_async(bool division = false, bool inplace = true)

reduce asynchronously.

Parameters:
  • division – Divide the reduced value.

  • inplace – Pack the arrays into one large array if false.

virtual void reducescatter_async(bool division = false)

reducescatter asynchronously.

Parameters:

division – Divide the reduced value.

virtual void bcast_async()

broadcast asynchronously.

virtual void allgather_async()

allgather asynchronously.

vector<string> allowed_array_classes()

Get array classes that are allowed to be specified by Context.

class GlobalClearBufferState
#include <computation_graph.hpp>

Clear buffer flags maintained in a global scope (per thread).

This is used to inform nbla::Function class which buffer flag is used when the function is called. It’s used in the forward & backward function in the nbla::CgVariable class.

Public Functions

unique_ptr<ScopedState> state(bool clear_buffer, bool clear_no_need_grad)

Set clear buffer flags globally until the life of the returned object ends.

Note that this doesn’t affect any decision of clearing buffers in the graph exeuction. It’s used to just inform anyone of the current clear buffer flag. Also, please keep in mind that the returned ScopedState instance shouldn’t be owned by any globally maintained instance because it maintains a raw pointer of a global singleton instance of this class (GlobalClearBufferState).

// Set a global clear buffer flag as true.
auto clear_buffer_state =
  SingletonManager::get<GlobalClearBufferState>()->state(true, false);

// The following will return true until clear_buffer_state is
// destroyed (at exiting this scope).
auto c = SingletonManager::get<GlobalClearBufferState>()->clear_buffer();
class CgFunction
#include <function.hpp>

Computation graph function.

A Function object is held in this object, and pointers to inputs and outputs also kept.

Public Functions

NBLA_API CgFunction(FunctionPtr func)

Ctor.

Parameters:

func[in] shared_ptr of Function.

NBLA_API ~CgFunction()

Dtor.

Erase all function_reference_ of inputs.

inline void set_inputs_(const vector<CgVariablePtr> &inputs)

Set inputs.

Note user shouldn’t call this directly.

Parameters:

inputs[in] Function inputs as CgVariables.

void setup()

Calling setup function of an Function object internally held.

inline CgVariablePtr output(int i)

Get a weak reference output as a shared reference by index or raise.

Parameters:

i[in] Output index.

NBLA_API void set_outputs (const vector< CgVariablePtr > &outputs)

Store outputs as weak references (weak_ptr).

Parameters:

outputs[in] Function outputs.

inline size_t num_inputs() const

Get number of inputs.

inline CgVariablePtr input(size_t i)

Get the i-th input.

inline size_t num_outputs() const

Get number of outputs.

struct CommunicatorBackwardCallback
#include <variable.hpp>

Callback functions during backward.

class FunctionHookWithObject
#include <variable.hpp>

Callback helper class for function callbacks during forward and backward class.

This is used from Python frontend.

class BaseCgVariable

Subclassed by nbla::CgVariable

Public Functions

inline virtual VariablePtr variable() final

Get variable reference held in this instance.

virtual NBLA_API void update_python_user_reference_counts (const int diff) final

Update the refernce counts from a Python user.

virtual void set_variable(VariablePtr var) final

Set variable reference.

class CgVariable : public nbla::BaseCgVariable
#include <variable.hpp>

Computation graph variable.

A Variable object is held in this object as a data container. In addition, a CGVariable object keeps information about the computation graph it belongs to. The information if such as the pointer to the parent function which creates this variable and some performance optimization clues.

Public Functions

NBLA_API CgVariable()

Create 0-shaped variable with no need_grad flag.

NBLA_API CgVariable(bool need_grad)

Create 0-shaped variable with need_grad option.

Parameters:

need_grad[in] Whether this variable requires gradient computation or not

NBLA_API CgVariable(Shape_t shape)

Create a variable by shape.

Parameters:

shape[in] Shape passed to Variable object held in the created instance.

NBLA_API CgVariable(Shape_t shape, bool need_grad)

Create a variable by shape with need_grad option.

Parameters:
  • shape[in] Shape passed to Variable object held in the created instance.

  • need_grad[in] Whether this variable requires gradient computation or not

NBLA_API CgVariable(VariablePtr var)

Create by a Variable instance.

Parameters:

var[in] Reference of an existing Variable object.

NBLA_API CgVariable(VariablePtr var, bool need_grad)

Create by a Variable instance.

Parameters:
  • var[in] Reference of an existing Variable object.

  • need_grad[in] Whether this variable requires gradient computation or not

inline bool need_grad() const

Get need grad flag.

inline bool need_grad_is_set() const

Check if need grad flag is set.

inline void set_need_grad(bool b)

Set need grad flag.

inline void unset_need_grad()

Unset need grad flag.

inline bool need_grad_state() const

Get need grad state flag.

inline bool need_grad_state_is_set() const

Check if need grad state is set.

inline void set_need_grad_state(bool b)

Set need grad state flag.

inline void unset_need_grad_state()

Unset need grad state flag.

inline bool recompute() const

Get recompute flag.

inline void set_recompute(bool b)

Set recompute flag.

inline bool prohibit_clear_data()

Get prohibit_clear_data_ flag.

inline void set_prohibit_clear_data(bool b)

Set prohibit_clear_data_ flag.

inline void set_parent(CgFunctionPtr func)

Set parent function.

Note

Users usually don’t use this directly. Used in connect function.

Parameters:

func[in] Function.

inline CgFunctionPtr parent()

Get parent function which produces outputs to this variable.

inline bool has_parent()

Query if a parent function is set.

inline int rank() const

Longest path from root variable.

Holds weak function references. https://stackoverflow.com/a/22110715

inline void set_rank_(int rank)

set rank.

Note

Users shouldn’t call this directly.

NBLA_API void forward (bool clear_buffer=false, bool clear_no_need_grad=false, unordered_set< CgFunctionPtr > *fclosed=nullptr, function_hook_type pre_callback=nullptr, function_hook_type post_callback=nullptr)

Forward propagation from root inputs to this variable.

The predecessor functions are executed in order of lower rank to higher rank until reaching this variable.

@seealso set_persistent() to prevent a specific variable to be cleared during forward propagation.

Parameters:
  • clear_buffer[in] Clear SyncedArray object of a variable never be used during the rest of forward propagation. This option significantly saves the memory consumption. This is not usually used in training phase because backward computation requires data computed during forward prop.

  • clear_need_grad[in] Clear the unreferenced variables with need_grad=False during forward propagation. True is usually used when calling this during training. This is ignored when clear_buffer=True.

  • fclosed[in] Set arbitrary fclosed flags to control forward computation. This is used for forward_all function.

NBLA_API void backward (NdArrayPtr grad=nullptr, bool clear_buffer=false, vector< CommunicatorBackwardCallbackPtr > communicator_callbacks={}, function_hook_type pre_callback=nullptr, function_hook_type post_callback=nullptr, const bool clear_initial_grad=false)

Performs a backward propagation.

starting from this variable until the root variable(s) is/are reached in the computation graph. The propagation will stop at a variable with need_grad=false. Backward propagation through predecessors of this variable.

@seealso set_persistent() to prevent a specific variable to be cleared during forward propagation.

Parameters:
  • grad[in] The backward error signal of this variable. if nullptr is set, its gradients are set as 1.

  • clear_buffer[in] Clears the no longer referenced variables during backpropagation to save memory.

  • communicator_callbacks – The callback functions invoked when 1) backward computation of each function is finished and 2) all backward computation is finished.

  • clear_initial_grad – If true, the input parameter, grad, will be cleared during backward propagation. This flag is only activated when grad is set.

void mark_need_setup()

Mark need_setup flag for all function references.

bool check_and_unmark_need_setup(CgFunctionPtr func)

Check need_setup signal, and unmark it.

inline bool allow_modify_data() const

Whether the data can be in-placed.

inline void set_allow_modify_data(bool allow)

Note

User shouldn’t call this directly.

inline void set_persistent(bool p)

Set persistent flag.

If it’s true, the variable data and grad are never cleared during forward or backward propagation with clear options. It is useful for visualization and debugging purposes.

Parameters:

p[in] Persistent flag.

inline bool persistent() const

Get persistent flag.

NBLA_API void clear_during_auto_forward ()

Clear the memory during forward propagation in auto-forward mode.

inline void set_name(string name)

Set variable name.

inline string name() const

Get variable name.

NBLA_API Ptr create_deep_copy (Context ctx, bool copy_grad=true)

Deepcopy method.

void visit_function_recursive(CgFunctionPtr func, unordered_set<CgFunctionPtr> &fclosed, const bool recomputation, function<void(CgFunctionPtr)> forward_callback)

Execute callback at functions in forward order in a graph.

void visit_function_backward(CgFunctionPtr func, function<void(CgFunctionPtr)> backward_callback, vector<CommunicatorBackwardCallbackPtr> communicator_callbacks)

Execute callback at functions in backward order in a graph.

class ClearCalledFlagRecorder
#include <variable.hpp>

ClearCalledFlagRecorder is a singleton class to record and collect the SyncedArray::clear_called flags during forward propagation.

Public Functions

bool is_activated()

Check if this recorder is activated.

void activate()

Activate recording clear flags.

void deactivate()

Deactivate recording clear flags and delete recorded flags.

void record(const CgFunctionPtr func)

Record clear flags from given function.

vector<vector<std::pair<bool, bool>>> get_recorded_input_clear_flags() const

Get recorded clear flags.

vector<vector<std::pair<bool, bool>>> get_recorded_output_clear_flags() const

Get recorded clear flags.

class Context
#include <context.hpp>

Context structure.

It will be used specifying device and array class etc.

Public Functions

explicit NBLA_API Context(const vector<string> &backend = {"cpu:float"}, const string &array_class = "CpuArray", const string &device_id = "0")

A compute backend descriptor passed to Function/Solver or NdArray class.

Parameters:
  • backend[in]

    A vector of backend description. A specific implementation of Function/Solver will be queried by each description, the first matched one is used. For each element, it describes the backend of computation and the data type config in a format of `<backend>:<data type

    config>`. If only `backend` is given (`:<data type configuration>` is omitted), the default data type config (`:float`)is automatically added.

  • array_class[in] Optional: A string expression of a preferred array class. Even if it is not specified an array class is chosen according to the default array class of an implementation of Function/Solver. Every Function/Solver class has a list of array classes that can be used as storage of the computation inputs and outputs. If the given array_class doesn’t match any of them, the default array class of the implementation will be used.

  • device_id[in] A string expression of device ID of the backend.

class Cpu : public nbla::BackendBase
#include <cpu.hpp>

Singleton class for storing some handles or configs for CPU Computation.

Public Functions

virtual vector<string> array_classes() const

Available array class list used in CPU Function implementations.

virtual void _set_array_classes(const vector<string> &a)

Set array class list.

Note

Dangerous to call. End users shouldn’t call.

virtual void register_array_class(const string &name)

Register array class to available list by name.

virtual shared_ptr<Allocator> caching_allocator()

Get a caching allocator.

virtual shared_ptr<Allocator> naive_allocator()

Get a no-cache allocator.

virtual void free_unused_host_caches()

Free all unused host memory caches.

virtual void device_synchronize(const string &device)

Synchronize host to device.

virtual void default_stream_synchronize(const string &device)

Synchronize host to default stream of device.

inline virtual void create_lms_streams(int device = -1)

Create non blockuing streams for data transfer.

Noting to do in CPU backend.

class Event
class Exception : public std::exception
#include <exception.hpp>

Exception class of NNabla.

Error codes are enumerated in enum class error_code you can find above. It is not expected that developers/users throw this exception directly. Instead, use NBLA_ERROR macro.

template<typename T>
struct nbits

Subclassed by nbla::float_bits< T >

template<>
struct nbits<Half>
template<>
struct nbits<float>
template<>
struct nbits<double>
template<typename T>
struct float_bits : public nbla::nbits<T>

Public Static Functions

template<typename U>
static inline nbits<U>::bit_t downconvert_to(bit_t bits)

Downconvert T-type bits into U-type bits.

union {
  float fval;
  uint32_t fbits;
};
float_value = 1;
uint16_t half_bits = float_bits<float>::downconvert_to<Half>(float_bits);
template<typename U>
static inline U upconvert_to(bit_t bits)

Upconvert T-type bits into U-type bits.

template<typename T>
class AdaptiveSeparableConvolution : public nbla::BaseFunction<>
#include <adaptive_separable_convolution.hpp>

Todo:

Write doc.

Inputs:

Outputs:

Public Functions

inline virtual shared_ptr<Function> copy() const

Copy another instance of Function with the same context.

inline virtual int min_inputs()

Get minimum number of inputs.

This is meant to be used in setup function with in_types which is used to get maximum number of inputs.

inline virtual int min_outputs()

Get minimum number of outputs.

This is meant to be used in setup function with out_types which is used to get max number of outputs.

inline virtual vector<dtypes> in_types()

Get input dtypes.

Last in_type will be used repeatedly if size of in_types is smaller than size of inputs

inline virtual vector<dtypes> out_types()

Get output dtypes.

Last out_type will be used repeatedly if size of out_types is smaller than size of outputs

inline virtual vector<string> allowed_array_classes()

Get array classes that are allowed to be specified by Context.

inline virtual string name()

Get function name in string.

inline virtual bool grad_depends_output_data(int i, int o) const

Dependency flag for checking if in-grad depends on out-data.

Checking if i-th input’ gradient computation requires o-th output’s data or not.

Note

If any of inputs requires an output variable data when computing its gradient, this function must be overridden to return appropriate boolean value. Otherwise, backward computation will be incorrect.

Parameters:
  • i[in] Input variable index.

  • o[in] Output variable index.

template<typename T>
class Add2 : public nbla::BaseFunction<bool>
#include <add2.hpp>

Elementwise add The function is defined as.

\[ y_i = x^{(0)}_i + x^{(1)}_i. \]

Inputs:

  • N-D array.

  • N-D array.

Outputs:

  • N-D array.

Template Parameters:

T – Data type for computation.

Public Functions

inline virtual shared_ptr<Function> copy() const

Copy another instance of Function with the same context.

inline virtual vector<dtypes> in_types()

Get input dtypes.

Last in_type will be used repeatedly if size of in_types is smaller than size of inputs

inline virtual vector<dtypes> out_types()

Get output dtypes.

Last out_type will be used repeatedly if size of out_types is smaller than size of outputs

inline virtual int min_inputs()

Get minimum number of inputs.

This is meant to be used in setup function with in_types which is used to get maximum number of inputs.

inline virtual int min_outputs()

Get minimum number of outputs.

This is meant to be used in setup function with out_types which is used to get max number of outputs.

inline virtual string name()

Get function name in string.

inline virtual vector<string> allowed_array_classes()

Get array classes that are allowed to be specified by Context.

inline virtual bool grad_depends_output_data(int i, int o) const

Dependency flag for checking if in-grad depends on out-data.

Checking if i-th input’ gradient computation requires o-th output’s data or not.

Note

If any of inputs requires an output variable data when computing its gradient, this function must be overridden to return appropriate boolean value. Otherwise, backward computation will be incorrect.

Parameters:
  • i[in] Input variable index.

  • o[in] Output variable index.

template<typename T>
class AddN : public nbla::BaseFunction<>
#include <add_n.hpp>

Elementwise add The function is defined as.

\[ y_i = x^{(0)}_i + . . . + x^{(n-1)}_i. \]

Inputs:

  • N-D arrays.

Outputs:

  • N-D array.

Template Parameters:

T – Data type for computation.

Public Functions

inline virtual shared_ptr<Function> copy() const

Copy another instance of Function with the same context.

inline virtual int min_inputs()

Get minimum number of inputs.

This is meant to be used in setup function with in_types which is used to get maximum number of inputs.

inline virtual int min_outputs()

Get minimum number of outputs.

This is meant to be used in setup function with out_types which is used to get max number of outputs.

inline virtual vector<dtypes> in_types()

Get input dtypes.

Last in_type will be used repeatedly if size of in_types is smaller than size of inputs

inline virtual vector<dtypes> out_types()

Get output dtypes.

Last out_type will be used repeatedly if size of out_types is smaller than size of outputs

inline virtual vector<string> allowed_array_classes()

Get array classes that are allowed to be specified by Context.

inline virtual string name()

Get function name in string.

inline virtual bool grad_depends_output_data(int i, int o) const

Dependency flag for checking if in-grad depends on out-data.

Checking if i-th input’ gradient computation requires o-th output’s data or not.

Note

If any of inputs requires an output variable data when computing its gradient, this function must be overridden to return appropriate boolean value. Otherwise, backward computation will be incorrect.

Parameters:
  • i[in] Input variable index.

  • o[in] Output variable index.

template<typename T>
class Affine : public nbla::BaseFunction<int>
#include <affine.hpp>

Affine also called as fully connected layer defined as.

\[ {\mathbf y} = {\mathbf A} {\mathbf x} + {\mathbf b}. \]

Inputs ( \(B\) is base_axis):

  • Input N-D array with shape ( \(M_0 \times ... \times M_{B-1} \times D_B \times ... \times D_N\)). Dimensions before and after base_axis are flattened as if it is a matrix.

  • Weight matrix with shape ( \((D_B \times ... \times D_N) \times L\))

  • (optional) Bias vector ( \(L\))

Outputs:

  • \((B + 1)\)-D array. ( \( M_0 \times ... \times M_{B-1} \times L \))

Template Parameters:

T – Data type for computation.

Param base_axis:

Base axis of Affine operation. Dimensions up to base_axis is treated as sample dimension.

Public Functions

inline virtual shared_ptr<Function> copy() const

Copy another instance of Function with the same context.

inline virtual vector<dtypes> in_types()

Get input dtypes.

Last in_type will be used repeatedly if size of in_types is smaller than size of inputs

inline virtual vector<dtypes> out_types()

Get output dtypes.

Last out_type will be used repeatedly if size of out_types is smaller than size of outputs

inline virtual int min_inputs()

Get minimum number of inputs.

This is meant to be used in setup function with in_types which is used to get maximum number of inputs.

inline virtual int min_outputs()

Get minimum number of outputs.

This is meant to be used in setup function with out_types which is used to get max number of outputs.

inline virtual string name()

Get function name in string.

inline virtual vector<string> allowed_array_classes()

Get array classes that are allowed to be specified by Context.

inline virtual bool grad_depends_output_data(int i, int o) const

Dependency flag for checking if in-grad depends on out-data.

Checking if i-th input’ gradient computation requires o-th output’s data or not.

Note

If any of inputs requires an output variable data when computing its gradient, this function must be overridden to return appropriate boolean value. Otherwise, backward computation will be incorrect.

Parameters:
  • i[in] Input variable index.

  • o[in] Output variable index.

template<typename T>
class AffineGrid : public nbla::BaseFunction<const vector<int>&, bool>
#include <affine_grid.hpp>

Inputs:

  • theta: N-D array with the shape ( \(B \times 2 \times 3\)), the sample-wise affine transformation matrix.

Outputs:

  • grid: N-D array with the shape ( \(B \times H \times W \times 2\)) for 2D and ( \(B \times D \times H \times W \times 3\)) for 3D. The last dimension of 2 is for (x, y) and of 3 for (x, y, z). The `gird` is used as the source grid for the warping.

Template Parameters:

T – Data type for computation.

Param size:

The grid size of ( \(H \times W\)) for 2D and ( \(D \times H \times W\)) for 3D.

Param align_corners:

If `True`, the top-left and bottom-right pixcels correspond to (-1, -1) and (1, 1) respectively since a pixel is located on the corner of a grid, and the target grid is normalized in [-1, 1]. If `False`, the normalized target grid in [-1, 1] is scaled by `size - 1 / size` according to the respective spatial size (e.g., \(H\) and \(W\)) before the transformation since a pixel is located on a center of a cell in a grid.

Public Functions

inline virtual shared_ptr<Function> copy() const

Copy another instance of Function with the same context.

inline virtual int min_inputs()

Get minimum number of inputs.

This is meant to be used in setup function with in_types which is used to get maximum number of inputs.

inline virtual int min_outputs()

Get minimum number of outputs.

This is meant to be used in setup function with out_types which is used to get max number of outputs.

inline virtual vector<dtypes> in_types()

Get input dtypes.

Last in_type will be used repeatedly if size of in_types is smaller than size of inputs

inline virtual vector<dtypes> out_types()

Get output dtypes.

Last out_type will be used repeatedly if size of out_types is smaller than size of outputs

inline virtual vector<string> allowed_array_classes()

Get array classes that are allowed to be specified by Context.

inline virtual string name()

Get function name in string.

inline virtual bool grad_depends_output_data(int i, int o) const

Dependency flag for checking if in-grad depends on out-data.

Checking if i-th input’ gradient computation requires o-th output’s data or not.

Note

If any of inputs requires an output variable data when computing its gradient, this function must be overridden to return appropriate boolean value. Otherwise, backward computation will be incorrect.

Parameters:
  • i[in] Input variable index.

  • o[in] Output variable index.

template<typename T>
class Arange : public nbla::BaseFunction<float, float, float>
#include <arange.hpp>

Generate a range of values within the half-open interval ``[start, stop)`` (the interval including start but excluding stop) with `step` increments.

Inputs:

  • none

Outputs:

  • 1-D array.

Template Parameters:

T – Data type for computation.

Param start:

Start value.

Param stop:

End value.

Param step:

Step value.

Public Functions

inline virtual shared_ptr<Function> copy() const

Copy another instance of Function with the same context.

inline virtual int min_inputs()

Get minimum number of inputs.

This is meant to be used in setup function with in_types which is used to get maximum number of inputs.

inline virtual int min_outputs()

Get minimum number of outputs.

This is meant to be used in setup function with out_types which is used to get max number of outputs.

inline virtual vector<dtypes> in_types()

Get input dtypes.

Last in_type will be used repeatedly if size of in_types is smaller than size of inputs

inline virtual vector<dtypes> out_types()

Get output dtypes.

Last out_type will be used repeatedly if size of out_types is smaller than size of outputs

inline virtual vector<string> allowed_array_classes()

Get array classes that are allowed to be specified by Context.

inline virtual string name()

Get function name in string.

inline virtual bool grad_depends_output_data(int i, int o) const

Dependency flag for checking if in-grad depends on out-data.

Checking if i-th input’ gradient computation requires o-th output’s data or not.

Note

If any of inputs requires an output variable data when computing its gradient, this function must be overridden to return appropriate boolean value. Otherwise, backward computation will be incorrect.

Parameters:
  • i[in] Input variable index.

  • o[in] Output variable index.

template<typename T>
class Assign : public nbla::BaseFunction<>
#include <assign.hpp>

Assign source array to destination array The function is defined as.

\[ y_i = x_i \]

Inputs:

  • destination N-D array

  • source N-D array

Outputs:

  • N-D array identical to source array

Public Functions

inline virtual shared_ptr<Function> copy() const

Copy another instance of Function with the same context.

inline virtual int min_inputs()

Get minimum number of inputs.

This is meant to be used in setup function with in_types which is used to get maximum number of inputs.

inline virtual int min_outputs()

Get minimum number of outputs.

This is meant to be used in setup function with out_types which is used to get max number of outputs.

inline virtual vector<dtypes> in_types()

Get input dtypes.

Last in_type will be used repeatedly if size of in_types is smaller than size of inputs

inline virtual vector<dtypes> out_types()

Get output dtypes.

Last out_type will be used repeatedly if size of out_types is smaller than size of outputs

inline virtual vector<string> allowed_array_classes()

Get array classes that are allowed to be specified by Context.

inline virtual string name()

Get function name in string.

inline virtual bool grad_depends_output_data(int i, int o) const

Dependency flag for checking if in-grad depends on out-data.

Checking if i-th input’ gradient computation requires o-th output’s data or not.

Note

If any of inputs requires an output variable data when computing its gradient, this function must be overridden to return appropriate boolean value. Otherwise, backward computation will be incorrect.

Parameters:
  • i[in] Input variable index.

  • o[in] Output variable index.

template<typename T>
class AveragePooling : public nbla::BasePooling<T, const vector<int>&, const vector<int>&, bool, const vector<int>&, bool, bool>
#include <average_pooling.hpp>

Average pooling operator.

Inputs:

  • N-D array that has more than two dimensions.

Outputs:

  • N-D array.

Note

Only 2D pooling supported so far.

Template Parameters:

T – Data type for computation.

Param kernel:

Shapes of kernel.

Param stride:

Subsampling factors of pooling.

Param ignore_border:

If false, a kernel overlapping border is also considered for output unlike convolution.

Param pad:

Border padding values of dimensions. Padding will be added both sides of the dimension.

Param including_pad:

If true, kernel size will be used for denominator of averaging even when the kernel overlaps border, otherwise the count of cells inside input tensor will be used.

Public Functions

inline virtual shared_ptr<Function> copy() const

Copy another instance of Function with the same context.

inline virtual string name()

Get function name in string.

inline virtual bool grad_depends_output_data(int i, int o) const

Dependency flag for checking if in-grad depends on out-data.

Checking if i-th input’ gradient computation requires o-th output’s data or not.

Note

If any of inputs requires an output variable data when computing its gradient, this function must be overridden to return appropriate boolean value. Otherwise, backward computation will be incorrect.

Parameters:
  • i[in] Input variable index.

  • o[in] Output variable index.

template<typename T>
class BatchCholesky : public nbla::BaseFunction<bool>
#include <batch_cholesky.hpp>

Todo:

Write doc.

Inputs:

Outputs:

Public Functions

inline virtual shared_ptr<Function> copy() const

Copy another instance of Function with the same context.

inline virtual int min_inputs()

Get minimum number of inputs.

This is meant to be used in setup function with in_types which is used to get maximum number of inputs.

inline virtual int min_outputs()

Get minimum number of outputs.

This is meant to be used in setup function with out_types which is used to get max number of outputs.

inline virtual vector<dtypes> in_types()

Get input dtypes.

Last in_type will be used repeatedly if size of in_types is smaller than size of inputs

inline virtual vector<dtypes> out_types()

Get output dtypes.

Last out_type will be used repeatedly if size of out_types is smaller than size of outputs

inline virtual vector<string> allowed_array_classes()

Get array classes that are allowed to be specified by Context.

inline virtual string name()

Get function name in string.

template<typename T>
class BatchDet : public nbla::BaseFunction<>
#include <batch_det.hpp>

Todo:

Write doc.

Inputs:

Outputs:

Public Functions

inline virtual shared_ptr<Function> copy() const

Copy another instance of Function with the same context.

inline virtual int min_inputs()

Get minimum number of inputs.

This is meant to be used in setup function with in_types which is used to get maximum number of inputs.

inline virtual int min_outputs()

Get minimum number of outputs.

This is meant to be used in setup function with out_types which is used to get max number of outputs.

inline virtual vector<dtypes> in_types()

Get input dtypes.

Last in_type will be used repeatedly if size of in_types is smaller than size of inputs

inline virtual vector<dtypes> out_types()

Get output dtypes.

Last out_type will be used repeatedly if size of out_types is smaller than size of outputs

inline virtual vector<string> allowed_array_classes()

Get array classes that are allowed to be specified by Context.

inline virtual string name()

Get function name in string.

inline virtual bool grad_depends_output_data(int i, int o) const

Dependency flag for checking if in-grad depends on out-data.

Checking if i-th input’ gradient computation requires o-th output’s data or not.

Note

If any of inputs requires an output variable data when computing its gradient, this function must be overridden to return appropriate boolean value. Otherwise, backward computation will be incorrect.

Parameters:
  • i[in] Input variable index.

  • o[in] Output variable index.

template<typename T>
class BatchInv : public nbla::BaseFunction<>
#include <batch_inv.hpp>

Invert source matrix The function is defined as.

\[ y = x^{-1} \]

Inputs:

  • N-D array (each element must be a square matrix)

Outputs:

  • N-D array

Public Functions

inline virtual shared_ptr<Function> copy() const

Copy another instance of Function with the same context.

inline virtual int min_inputs()

Get minimum number of inputs.

This is meant to be used in setup function with in_types which is used to get maximum number of inputs.

inline virtual int min_outputs()

Get minimum number of outputs.

This is meant to be used in setup function with out_types which is used to get max number of outputs.

inline virtual vector<dtypes> in_types()

Get input dtypes.

Last in_type will be used repeatedly if size of in_types is smaller than size of inputs

inline virtual vector<dtypes> out_types()

Get output dtypes.

Last out_type will be used repeatedly if size of out_types is smaller than size of outputs

inline virtual vector<string> allowed_array_classes()

Get array classes that are allowed to be specified by Context.

inline virtual string name()

Get function name in string.

inline virtual bool grad_depends_output_data(int i, int o) const

Dependency flag for checking if in-grad depends on out-data.

Checking if i-th input’ gradient computation requires o-th output’s data or not.

Note

If any of inputs requires an output variable data when computing its gradient, this function must be overridden to return appropriate boolean value. Otherwise, backward computation will be incorrect.

Parameters:
  • i[in] Input variable index.

  • o[in] Output variable index.

template<typename T>
class BatchLogdet : public nbla::BaseFunction<>
#include <batch_logdet.hpp>

Todo:

Write doc.

Inputs:

Outputs:

Public Functions

inline virtual shared_ptr<Function> copy() const

Copy another instance of Function with the same context.

inline virtual int min_inputs()

Get minimum number of inputs.

This is meant to be used in setup function with in_types which is used to get maximum number of inputs.

inline virtual int min_outputs()

Get minimum number of outputs.

This is meant to be used in setup function with out_types which is used to get max number of outputs.

inline virtual vector<dtypes> in_types()

Get input dtypes.

Last in_type will be used repeatedly if size of in_types is smaller than size of inputs

inline virtual vector<dtypes> out_types()

Get output dtypes.

Last out_type will be used repeatedly if size of out_types is smaller than size of outputs

inline virtual vector<string> allowed_array_classes()

Get array classes that are allowed to be specified by Context.

inline virtual string name()

Get function name in string.

inline virtual bool grad_depends_output_data(int i, int o) const

Dependency flag for checking if in-grad depends on out-data.

Checking if i-th input’ gradient computation requires o-th output’s data or not.

Note

If any of inputs requires an output variable data when computing its gradient, this function must be overridden to return appropriate boolean value. Otherwise, backward computation will be incorrect.

Parameters:
  • i[in] Input variable index.

  • o[in] Output variable index.

template<typename T>
class BatchMatmul : public nbla::BaseFunction<bool, bool>
#include <batch_matmul.hpp>

Batch matrix multiplication.

Two of batches of matrices are multiplied for each sample in a batch. A batch of matrices is composed as […, P, Q] where the last two dimensions compose matrix dimensions, and the first dimensions up to the third last dimension are considered as batch samples.

Inputs:

  • a: N-D array with >= 2-dim. The last two dimensions will be treated as a matrix.

  • b: N-D array with >= 2-dim. The last two dimensions will be treated as a matrix. The product of the size of 0-th dimension through the size of the third last dimension must be same as that of the input a.

Output:

  • y: Output of sample-wise matrix multiplication in a batch. When a is of a shape of [N, P, Q], b is of a shape of [N, Q, R], and transpose options are all False, the output will be a shape of [N, P, R].

Template Parameters:

T – Data type for computation.

Param transpose_a:

Transpose the last two axes of a in matrix multiplication.

Param transpose_b:

Transpose the last two axes of b in matrix multiplication.

Public Functions

inline virtual shared_ptr<Function> copy() const

Copy another instance of Function with the same context.

inline virtual vector<dtypes> in_types()

Get input dtypes.

Last in_type will be used repeatedly if size of in_types is smaller than size of inputs

inline virtual vector<dtypes> out_types()

Get output dtypes.

Last out_type will be used repeatedly if size of out_types is smaller than size of outputs

inline virtual int min_inputs()

Get minimum number of inputs.

This is meant to be used in setup function with in_types which is used to get maximum number of inputs.

inline virtual int min_outputs()

Get minimum number of outputs.

This is meant to be used in setup function with out_types which is used to get max number of outputs.

inline virtual string name()

Get function name in string.

inline virtual vector<string> allowed_array_classes()

Get array classes that are allowed to be specified by Context.

inline virtual bool grad_depends_output_data(int i, int o) const

Dependency flag for checking if in-grad depends on out-data.

Checking if i-th input’ gradient computation requires o-th output’s data or not.

Note

If any of inputs requires an output variable data when computing its gradient, this function must be overridden to return appropriate boolean value. Otherwise, backward computation will be incorrect.

Parameters:
  • i[in] Input variable index.

  • o[in] Output variable index.

template<typename T>
class BatchNormalization : public nbla::BaseFunction<const vector<int>&, float, float, bool, bool, bool>
#include <batch_normalization.hpp>

Batch normalization at training time defined as.

\[\begin{split} \begin{array}{lcl} \mu &=& \frac{1}{M} \sum x_i\\ \sigma^2 &=& \frac{1}{M} \left(\sum x_i - \mu\right)^2\\ \hat{x}_i &=& \frac{x_i - \mu}{\sqrt{\sigma^2 + \epsilon}} \\ y_i &=& \hat{x}_i \gamma + \beta. \end{array} \end{split}\]
In testing, mean and variance computed by moving average calculated during training are used.

Inputs:

  • N-D array of input.

  • N-D array of beta which is learned.

  • N-D array of gamma which is learned.

  • N-D array of running mean (modified during forward execution).

  • N-D array of running variance (modified during forward execution).

Outputs (1 or 3):

  • N-D array.

  • (Optional) N-D array of batch mean.

  • (Optional) N-D array of batch variance.

See also

Ioffe and Szegedy, Batch Normalization: Accelerating Deep Network Training by Reducing Internal Covariate Shift. https://arxiv.org/abs/1502.03167

Template Parameters:

T – Data type for computation.

Param axes:

Axes mean and variance are taken.

Param decay_rate:

Decay rate of running mean and variance.

Param eps:

Tiny value to avoid zero division by std.

Param batch_stat:

Use mini-batch statistics rather than running ones.

Subclassed by nbla::SyncBatchNormalization< T >

Public Functions

inline virtual shared_ptr<Function> copy() const

Copy another instance of Function with the same context.

inline virtual vector<dtypes> in_types()

Get input dtypes.

Last in_type will be used repeatedly if size of in_types is smaller than size of inputs

inline virtual vector<dtypes> out_types()

Get output dtypes.

Last out_type will be used repeatedly if size of out_types is smaller than size of outputs

inline virtual int min_inputs()

Get minimum number of inputs.

This is meant to be used in setup function with in_types which is used to get maximum number of inputs.

inline virtual int min_outputs()

Get minimum number of outputs.

This is meant to be used in setup function with out_types which is used to get max number of outputs.

inline virtual string name()

Get function name in string.

inline virtual vector<string> allowed_array_classes()

Get array classes that are allowed to be specified by Context.

inline virtual bool grad_depends_output_data(int i, int o) const

Dependency flag for checking if in-grad depends on out-data.

Checking if i-th input’ gradient computation requires o-th output’s data or not.

Note

If any of inputs requires an output variable data when computing its gradient, this function must be overridden to return appropriate boolean value. Otherwise, backward computation will be incorrect.

Parameters:
  • i[in] Input variable index.

  • o[in] Output variable index.

template<typename T>
class BinaryConnectAffine : public nbla::BaseFunction<int, float>
#include <binary_connect_affine.hpp>

BinaryConnectAffine BinaryConnect network version of an affine layer, using deterministic quantization to -1 and 1.

Reference: M. Courbariaux, Y. Bengio, and J.-P. David. “BinaryConnect:

Training Deep Neural Networks with binary weights during propagations.” Advances in Neural Information Processing Systems. 2015.

NOTES:

1) if you would like to share weights between some layers, please make sure to share the standard, floating value weights (input parameter #2) and not the binarized weights (input parameter #3)

2) Only after a call to forward() the weights and the binary weights are in sync, not after a call to backward(). If wanting to store the parameters of the network, remember to call forward() once before doing so, otherwise the weights and the binary weights will not be in sync.

\[ {\mathbf y} = {\mathbf A} {\mathbf x} + {\mathbf b}. \]

Inputs ( \(B\) is base_axis):

  • Input N-D array with shape ( \(M_0 \times ... \times M_{B-1} \times D_B \times ... \times D_N\)). Dimensions before and after base_axis are flattened as if it is a matrix.

  • Weight matrix with shape ( \((D_B \times ... \times D_N) \times L\))

  • Binarized weight matrix with shape ( \((D_B \times ... \times D_N) \times L\))

  • (optional) Bias vector ( \(L\))

Outputs:

  • \((B + 1)\)-D array. ( \( M_0 \times ... \times M_{B-1} \times L \))

Template Parameters:

T – Data type for computation.

Param base_axis:

Base axis of BinaryConnectAffine operation. Dimensions up to base_axis is treated as sample dimension.

Param quantize_zero_to:

Input value at zero is quantized to this value.

Public Functions

inline virtual shared_ptr<Function> copy() const

Copy another instance of Function with the same context.

inline virtual vector<dtypes> in_types()

Get input dtypes.

Last in_type will be used repeatedly if size of in_types is smaller than size of inputs

inline virtual vector<dtypes> out_types()

Get output dtypes.

Last out_type will be used repeatedly if size of out_types is smaller than size of outputs

inline virtual int min_inputs()

Get minimum number of inputs.

This is meant to be used in setup function with in_types which is used to get maximum number of inputs.

inline virtual int min_outputs()

Get minimum number of outputs.

This is meant to be used in setup function with out_types which is used to get max number of outputs.

inline virtual string name()

Get function name in string.

inline virtual vector<string> allowed_array_classes()

Get array classes that are allowed to be specified by Context.

inline virtual bool grad_depends_output_data(int i, int o) const

Dependency flag for checking if in-grad depends on out-data.

Checking if i-th input’ gradient computation requires o-th output’s data or not.

Note

If any of inputs requires an output variable data when computing its gradient, this function must be overridden to return appropriate boolean value. Otherwise, backward computation will be incorrect.

Parameters:
  • i[in] Input variable index.

  • o[in] Output variable index.

template<typename T>
class BinaryConnectConvolution : public nbla::BaseFunction<int, const vector<int>&, const vector<int>&, const vector<int>&, int, float>
#include <binary_connect_convolution.hpp>

N-D BinaryConnect Convolution with bias.

Reference: M. Courbariaux, Y. Bengio, and J.-P. David. “BinaryConnect:

Training Deep Neural Networks with binary weights during propagations.” Advances in Neural Information Processing Systems. 2015.

NOTES:

1) if you would like to share weights between some layers, please make sure to share the standard, floating value weights (input parameter #2) and not the binarized weights (input parameter #3)

2) Only after a call to forward() the weights and the binary weights are in sync, not after a call to backward(). If wanting to store the parameters of the network, remember to call forward() once before doing so, otherwise the weights and the binary weights will not be in sync.

Inputs ( \(B\) is base_axis):

  • Input \((B + 1 + N)\)-D array ( \(M_1 \times ... \times M_B \times C \times L_1 \times ... \times L_N\)).

  • Weight \((2 + N)\)-D array ( \(C' \times C \times K_1 \times ... \times K_N\)).

  • Binary Weight \((2 + N)\)-D array ( \(C' \times C \times K_1 \times ... \times K_N\)).

  • (optional) Bias vector ( \(C'\)).

Outputs:

  • \((B + 1 + N)\)-D array ( \( M_1 \times ... \times M_B \times C' \times L'_1 \times ... \times L'_N \)).

See also

For Dilated Convolution (a.k.a a trous), refer to:

Template Parameters:

T – Data type for computation.

Param base_axis:

Base axis of Convolution operation. Dimensions up to base_axis is treated as sample dimension.

Param pad:

Padding sizes for dimensions.

Param stride:

Stride sizes for dimensions.

Param dilation:

Dilation sizes for dimensions.

Param group:

Number of groups of channels. This makes connections across channels sparser by grouping connections along map direction.

Param quantize_zero_to:

Input value at zero is quantized to this value.

Public Functions

inline virtual shared_ptr<Function> copy() const

Copy another instance of Function with the same context.

inline virtual vector<dtypes> in_types()

Get input dtypes.

Last in_type will be used repeatedly if size of in_types is smaller than size of inputs

inline virtual vector<dtypes> out_types()

Get output dtypes.

Last out_type will be used repeatedly if size of out_types is smaller than size of outputs

inline virtual int min_inputs()

Get minimum number of inputs.

This is meant to be used in setup function with in_types which is used to get maximum number of inputs.

inline virtual int min_outputs()

Get minimum number of outputs.

This is meant to be used in setup function with out_types which is used to get max number of outputs.

inline virtual string name()

Get function name in string.

inline virtual vector<string> allowed_array_classes()

Get array classes that are allowed to be specified by Context.

inline virtual bool grad_depends_output_data(int i, int o) const

Dependency flag for checking if in-grad depends on out-data.

Checking if i-th input’ gradient computation requires o-th output’s data or not.

Note

If any of inputs requires an output variable data when computing its gradient, this function must be overridden to return appropriate boolean value. Otherwise, backward computation will be incorrect.

Parameters:
  • i[in] Input variable index.

  • o[in] Output variable index.

template<typename T>
class BinaryCrossEntropy : public nbla::BaseFunction<>
#include <binary_cross_entropy.hpp>

BinaryCrossEntropy calculate the element-wise cross entropy between the variable and label variables.

\[ y_i = - \left(x^{(1)}_i * \ln \left(x^{(0)}_i\right) + \left(1 - x^{(1)}_i\right) * \ln \left(1 - x^{(0)}_i\right)\right). \]

Inputs:

  • Probabilities N-D array. \(-\infty\) to \(\infty\).

  • Labels N-D array. Usually set as 0 or 1, but, unlike SigmoidCrossEntropy, it allows probability (0 to 1) as inputs and backpropagation can be done.

Outputs:

  • Element-wise losses N-D array.

Template Parameters:

T – Data type for computation.

Public Functions

inline virtual shared_ptr<Function> copy() const

Copy another instance of Function with the same context.

inline virtual vector<dtypes> in_types()

Get input dtypes.

Last in_type will be used repeatedly if size of in_types is smaller than size of inputs

inline virtual vector<dtypes> out_types()

Get output dtypes.

Last out_type will be used repeatedly if size of out_types is smaller than size of outputs

inline virtual int min_inputs()

Get minimum number of inputs.

This is meant to be used in setup function with in_types which is used to get maximum number of inputs.

inline virtual int min_outputs()

Get minimum number of outputs.

This is meant to be used in setup function with out_types which is used to get max number of outputs.

inline virtual string name()

Get function name in string.

inline virtual vector<string> allowed_array_classes()

Get array classes that are allowed to be specified by Context.

inline virtual bool grad_depends_output_data(int i, int o) const

Dependency flag for checking if in-grad depends on out-data.

Checking if i-th input’ gradient computation requires o-th output’s data or not.

Note

If any of inputs requires an output variable data when computing its gradient, this function must be overridden to return appropriate boolean value. Otherwise, backward computation will be incorrect.

Parameters:
  • i[in] Input variable index.

  • o[in] Output variable index.

template<typename T>
class BinaryError : public nbla::Function
#include <binary_error.hpp>

Elementwise binary error defined as.

\[\begin{split} y_i = \left \{ \begin{array}{l} 0 ((x^{(0)} \geq 0.5) = (x^{(1)} \geq 0.5)) \\ 1 ((x^{(0)} \geq 0.5) \neq (x^{(1)} \geq 0.5)) \end{array} \right. \end{split}\]

Inputs:

  • Probabilities N-D array. \(-\infty\) to \(\infty\).

  • Labels N-D array. Usually set as 0 or 1, but, it allows probability (0 to 1) as inputs.

Outputs:

  • Element-wise errors N-D array.

Template Parameters:

T – Data type for computation.

Public Functions

inline virtual shared_ptr<Function> copy() const

Copy another instance of Function with the same context.

inline virtual vector<dtypes> in_types()

Get input dtypes.

Last in_type will be used repeatedly if size of in_types is smaller than size of inputs

inline virtual vector<dtypes> out_types()

Get output dtypes.

Last out_type will be used repeatedly if size of out_types is smaller than size of outputs

inline virtual int min_inputs()

Get minimum number of inputs.

This is meant to be used in setup function with in_types which is used to get maximum number of inputs.

inline virtual int min_outputs()

Get minimum number of outputs.

This is meant to be used in setup function with out_types which is used to get max number of outputs.

inline virtual string name()

Get function name in string.

inline virtual vector<string> allowed_array_classes()

Get array classes that are allowed to be specified by Context.

inline virtual bool grad_depends_output_data(int i, int o) const

Dependency flag for checking if in-grad depends on out-data.

Checking if i-th input’ gradient computation requires o-th output’s data or not.

Note

If any of inputs requires an output variable data when computing its gradient, this function must be overridden to return appropriate boolean value. Otherwise, backward computation will be incorrect.

Parameters:
  • i[in] Input variable index.

  • o[in] Output variable index.

template<typename T>
class BinaryWeightAffine : public nbla::BaseFunction<int>
#include <binary_weight_affine.hpp>

Binary weight network version of an affine layer, using deterministic quantization to -1 and 1 (with scaling).

Reference: Rastegari, Mohammad, et al. “XNOR-Net: ImageNet Classification Using

Binary Convolutional Neural Networks.” arXiv preprint arXiv:1603.05279 (2016).

NOTES:

1) if you would like to share weights between some layers, please make sure to share the standard, floating value weights (input parameter #2) and not the binarized weights (input parameter #3)

2) Only after a call to forward() the weights and the binary weights are in sync, not after a call to backward(). If wanting to store the parameters of the network, remember to call forward() once before doing so, otherwise the weights and the binary weights will not be in sync.

\[ {\mathbf y} = {\mathbf A} {\mathbf x} + {\mathbf b}. \]

Inputs ( \(B\) is base_axis):

  • Input N-D array with shape ( \(M_0 \times ... \times M_{B-1} \times D_B \times ... \times D_N\)). Dimensions before and after base_axis are flattened as if it is a matrix.

  • Weight matrix with shape ( \((D_B \times ... \times D_N) \times L\))

  • Binarized weight matrix with shape ( \((D_B \times ... \times D_N) \times L\))

  • (optional) Bias vector ( \(L\))

Outputs:

  • \((B + 1)\)-D array. ( \( M_0 \times ... \times M_{B-1} \times L \))

Template Parameters:

T – Data type for computation.

Param base_axis:

Base axis of BinaryConnectAffine operation. Dimensions up to base_axis is treated as sample dimension.

Param quantize_zero_to:

Input value at zero is quantized to this value.

Public Functions

inline virtual shared_ptr<Function> copy() const

Copy another instance of Function with the same context.

inline virtual vector<dtypes> in_types()

Get input dtypes.

Last in_type will be used repeatedly if size of in_types is smaller than size of inputs

inline virtual vector<dtypes> out_types()

Get output dtypes.

Last out_type will be used repeatedly if size of out_types is smaller than size of outputs

inline virtual int min_inputs()

Get minimum number of inputs.

This is meant to be used in setup function with in_types which is used to get maximum number of inputs.

inline virtual int min_outputs()

Get minimum number of outputs.

This is meant to be used in setup function with out_types which is used to get max number of outputs.

inline virtual string name()

Get function name in string.

inline virtual vector<string> allowed_array_classes()

Get array classes that are allowed to be specified by Context.

inline virtual bool grad_depends_output_data(int i, int o) const

Dependency flag for checking if in-grad depends on out-data.

Checking if i-th input’ gradient computation requires o-th output’s data or not.

Note

If any of inputs requires an output variable data when computing its gradient, this function must be overridden to return appropriate boolean value. Otherwise, backward computation will be incorrect.

Parameters:
  • i[in] Input variable index.

  • o[in] Output variable index.

template<typename T>
class BinaryWeightConvolution : public nbla::BaseFunction<int, const vector<int>&, const vector<int>&, const vector<int>&, int, float>
#include <binary_weight_convolution.hpp>

N-D Binary Weight Convolution with bias.

Reference: Rastegari, Mohammad, et al. “XNOR-Net: ImageNet Classification Using

Binary Convolutional Neural Networks.” arXiv preprint arXiv:1603.05279 (2016).

NOTES:

1) if you would like to share weights between some layers, please make sure to share the standard, floating value weights (input parameter #2) and not the binarized weights (input parameter #3)

2) Only after a call to forward() the weights and the binary weights are in sync, not after a call to backward(). If wanting to store the parameters of the network, remember to call forward() once before doing so, otherwise the weights and the binary weights will not be in sync.

Inputs ( \(B\) is base_axis):

  • Input \((B + 1 + N)\)-D array ( \(M_1 \times ... \times M_B \times C \times L_1 \times ... \times L_N\)).

  • Weight \((2 + N)\)-D array ( \(C' \times C \times K_1 \times ... \times K_N\)).

  • Binary Weight \((2 + N)\)-D array ( \(C' \times C \times K_1 \times ... \times K_N\)).

  • Alpha \(1\)-D array ( \(C'\)).

  • (optional) Bias vector ( \(C'\)).

Outputs:

  • \((B + 1 + N)\)-D array ( \( M_1 \times ... \times M_B \times C' \times L'_1 \times ... \times L'_N \)).

See also

For Dilated Convolution (a.k.a a trous), refer to:

Template Parameters:

T – Data type for computation.

Param base_axis:

Base axis of Convolution operation. Dimensions up to base_axis is treated as sample dimension.

Param pad:

Padding sizes for dimensions.

Param stride:

Stride sizes for dimensions.

Param dilation:

Dilation sizes for dimensions.

Param group:

Number of groups of channels. This makes connections across channels sparser by grouping connections along map direction.

Param quantize_zero_to:

Input value at zero is quantized to this value.

Public Functions

inline virtual shared_ptr<Function> copy() const

Copy another instance of Function with the same context.

inline virtual vector<dtypes> in_types()

Get input dtypes.

Last in_type will be used repeatedly if size of in_types is smaller than size of inputs

inline virtual vector<dtypes> out_types()

Get output dtypes.

Last out_type will be used repeatedly if size of out_types is smaller than size of outputs

inline virtual int min_inputs()

Get minimum number of inputs.

This is meant to be used in setup function with in_types which is used to get maximum number of inputs.

inline virtual int min_outputs()

Get minimum number of outputs.

This is meant to be used in setup function with out_types which is used to get max number of outputs.

inline virtual string name()

Get function name in string.

inline virtual vector<string> allowed_array_classes()

Get array classes that are allowed to be specified by Context.

inline virtual bool grad_depends_output_data(int i, int o) const

Dependency flag for checking if in-grad depends on out-data.

Checking if i-th input’ gradient computation requires o-th output’s data or not.

Note

If any of inputs requires an output variable data when computing its gradient, this function must be overridden to return appropriate boolean value. Otherwise, backward computation will be incorrect.

Parameters:
  • i[in] Input variable index.

  • o[in] Output variable index.

template<typename T>
class BitShift : public nbla::BaseTransformBinary<>
#include <bit_shift.hpp>

Element-wise bit shift function.

Inputs:

  • x: A N-D array.

  • shift: A N-D array.

Outputs:

  • y: A N-D array.

Param direction:

Direction of bit shift.

Public Functions

inline virtual shared_ptr<Function> copy() const

Copy another instance of Function with the same context.

inline virtual int min_inputs()

Get minimum number of inputs.

This is meant to be used in setup function with in_types which is used to get maximum number of inputs.

inline virtual int min_outputs()

Get minimum number of outputs.

This is meant to be used in setup function with out_types which is used to get max number of outputs.

inline virtual vector<dtypes> in_types()

Get input dtypes.

Last in_type will be used repeatedly if size of in_types is smaller than size of inputs

inline virtual vector<dtypes> out_types()

Get output dtypes.

Last out_type will be used repeatedly if size of out_types is smaller than size of outputs

inline virtual vector<string> allowed_array_classes()

Get array classes that are allowed to be specified by Context.

inline virtual string name()

Get function name in string.

template<typename T>
class BoolFill : public nbla::BaseFunction<float>
#include <bool_fill.hpp>

Todo:

Write doc.

Inputs:

Outputs:

Public Functions

inline virtual shared_ptr<Function> copy() const

Copy another instance of Function with the same context.

inline virtual int min_inputs()

Get minimum number of inputs.

This is meant to be used in setup function with in_types which is used to get maximum number of inputs.

inline virtual int min_outputs()

Get minimum number of outputs.

This is meant to be used in setup function with out_types which is used to get max number of outputs.

inline virtual vector<dtypes> in_types()

Get input dtypes.

Last in_type will be used repeatedly if size of in_types is smaller than size of inputs

inline virtual vector<dtypes> out_types()

Get output dtypes.

Last out_type will be used repeatedly if size of out_types is smaller than size of outputs

inline virtual vector<string> allowed_array_classes()

Get array classes that are allowed to be specified by Context.

inline virtual string name()

Get function name in string.

template<typename T>
class BoolGather : public nbla::BaseFunction<>
#include <bool_gather.hpp>

Todo:

Write doc.

Inputs:

Outputs:

Public Functions

inline virtual shared_ptr<Function> copy() const

Copy another instance of Function with the same context.

inline virtual int min_inputs()

Get minimum number of inputs.

This is meant to be used in setup function with in_types which is used to get maximum number of inputs.

inline virtual int min_outputs()

Get minimum number of outputs.

This is meant to be used in setup function with out_types which is used to get max number of outputs.

inline virtual vector<dtypes> in_types()

Get input dtypes.

Last in_type will be used repeatedly if size of in_types is smaller than size of inputs

inline virtual vector<dtypes> out_types()

Get output dtypes.

Last out_type will be used repeatedly if size of out_types is smaller than size of outputs

inline virtual vector<string> allowed_array_classes()

Get array classes that are allowed to be specified by Context.

inline virtual string name()

Get function name in string.

inline virtual bool grad_depends_output_data(int i, int o) const

Dependency flag for checking if in-grad depends on out-data.

Checking if i-th input’ gradient computation requires o-th output’s data or not.

Note

If any of inputs requires an output variable data when computing its gradient, this function must be overridden to return appropriate boolean value. Otherwise, backward computation will be incorrect.

Parameters:
  • i[in] Input variable index.

  • o[in] Output variable index.

template<typename T>
class BoolScatter : public nbla::BaseFunction<>
#include <bool_scatter.hpp>

Todo:

Write doc.

Inputs:

Outputs:

Public Functions

inline virtual shared_ptr<Function> copy() const

Copy another instance of Function with the same context.

inline virtual int min_inputs()

Get minimum number of inputs.

This is meant to be used in setup function with in_types which is used to get maximum number of inputs.

inline virtual int min_outputs()

Get minimum number of outputs.

This is meant to be used in setup function with out_types which is used to get max number of outputs.

inline virtual vector<dtypes> in_types()

Get input dtypes.

Last in_type will be used repeatedly if size of in_types is smaller than size of inputs

inline virtual vector<dtypes> out_types()

Get output dtypes.

Last out_type will be used repeatedly if size of out_types is smaller than size of outputs

inline virtual vector<string> allowed_array_classes()

Get array classes that are allowed to be specified by Context.

inline virtual string name()

Get function name in string.

inline virtual int inplace_data(int i) const

Get in-place-level of i-th input variable’s data (see below).

0 (NOT_INPLACE): Not in-placed 1 (INPLACE_NOT_MODIFY): In-placed but not modified. 2 (INPLACE): In-placed and modified.

Note

If a subclass uses in-place computation, the function must override this function.

Parameters:

i[in] Input variable index.

Return values:

Returns – 0 by default.

inline virtual int inplace_data_with(int i) const

Get the output variable index where i-th variables’ data in-placed to.

Note

This is only valid if the i-th variable is in-placed. The maintainer of a sub-class function must override this function.

Parameters:

i[in] Input variable index.

template<typename T>
class Broadcast : public nbla::BaseFunction<const vector<int>&>
#include <broadcast.hpp>

Broadcast N-D array to shape.

Param shape:

The shape input array broadcasted to. Dimensions broadcasted in input array must be size one.

Public Functions

inline virtual shared_ptr<Function> copy() const

Copy another instance of Function with the same context.

inline virtual vector<dtypes> in_types()

Get input dtypes.

Last in_type will be used repeatedly if size of in_types is smaller than size of inputs

inline virtual vector<dtypes> out_types()

Get output dtypes.

Last out_type will be used repeatedly if size of out_types is smaller than size of outputs

inline virtual int min_inputs()

Get minimum number of inputs.

This is meant to be used in setup function with in_types which is used to get maximum number of inputs.

inline virtual int min_outputs()

Get minimum number of outputs.

This is meant to be used in setup function with out_types which is used to get max number of outputs.

inline virtual string name()

Get function name in string.

inline virtual vector<string> allowed_array_classes()

Get array classes that are allowed to be specified by Context.

inline virtual bool grad_depends_output_data(int i, int o) const

Dependency flag for checking if in-grad depends on out-data.

Checking if i-th input’ gradient computation requires o-th output’s data or not.

Note

If any of inputs requires an output variable data when computing its gradient, this function must be overridden to return appropriate boolean value. Otherwise, backward computation will be incorrect.

Parameters:
  • i[in] Input variable index.

  • o[in] Output variable index.

template<typename T>
class BroadcastTo : public nbla::BaseFunction<int>
#include <broadcast_to.hpp>

Todo:

PLACE HERE FUNCTION DOCUMENTATION.

Public Functions

inline virtual shared_ptr<Function> copy() const

Copy another instance of Function with the same context.

inline virtual vector<dtypes> in_types()

Get input dtypes.

Last in_type will be used repeatedly if size of in_types is smaller than size of inputs

inline virtual vector<dtypes> out_types()

Get output dtypes.

Last out_type will be used repeatedly if size of out_types is smaller than size of outputs

inline virtual int min_inputs()

Get minimum number of inputs.

This is meant to be used in setup function with in_types which is used to get maximum number of inputs.

inline virtual int min_outputs()

Get minimum number of outputs.

This is meant to be used in setup function with out_types which is used to get max number of outputs.

inline virtual string name()

Get function name in string.

inline virtual vector<string> allowed_array_classes()

Get array classes that are allowed to be specified by Context.

inline virtual bool grad_depends_output_data(int i, int o) const

Dependency flag for checking if in-grad depends on out-data.

Checking if i-th input’ gradient computation requires o-th output’s data or not.

Note

If any of inputs requires an output variable data when computing its gradient, this function must be overridden to return appropriate boolean value. Otherwise, backward computation will be incorrect.

Parameters:
  • i[in] Input variable index.

  • o[in] Output variable index.

class Callback : public nbla::BaseFunction<>
#include <callback.hpp>

A callback Function.

The callback functions of setup_impl, forward_impl and backward_mpl registered at initialization are called.

Public Functions

inline virtual shared_ptr<Function> copy() const

Copy another instance of Function with the same context.

inline virtual int min_inputs()

Get minimum number of inputs.

This is meant to be used in setup function with in_types which is used to get maximum number of inputs.

inline virtual int min_outputs()

Get minimum number of outputs.

This is meant to be used in setup function with out_types which is used to get max number of outputs.

inline virtual vector<dtypes> in_types()

Get input dtypes.

Last in_type will be used repeatedly if size of in_types is smaller than size of inputs

inline virtual vector<dtypes> out_types()

Get output dtypes.

Last out_type will be used repeatedly if size of out_types is smaller than size of outputs

inline virtual string name()

Get function name in string.

virtual vector<string> allowed_array_classes()

Get array classes that are allowed to be specified by Context.

inline virtual bool grad_depends_output_data(int i, int o) const

Dependency flag for checking if in-grad depends on out-data.

Checking if i-th input’ gradient computation requires o-th output’s data or not.

Note

If any of inputs requires an output variable data when computing its gradient, this function must be overridden to return appropriate boolean value. Otherwise, backward computation will be incorrect.

Parameters:
  • i[in] Input variable index.

  • o[in] Output variable index.

template<typename T, typename Tl = int>
class CategoricalCrossEntropy : public nbla::BaseFunction<int>
#include <categorical_cross_entropy.hpp>

CategoricalCrossEntropy calculate the element-wise cross entropy between the variables and the variables of a label given by a category index.

\[ y_i = -\ln \left( x^{(0)}_{i,x^{(1)}}\right) \]
along dimension specified by axis.

Inputs (i is axis normalization taken):

  • Probabilities N-D array. ( \(D_1 \times ... \times D_i \times ... \times D_N\))

  • Labels N-D array. ( \(D_1 \times ... \times 1 \times ... \times D_N\))

Outputs:

  • Element-wise losses N-D array. ( \(D_1 \times ... \times 1 \times ... \times D_N\))

Template Parameters:
  • T – Data type for computation and score variable.

  • Tl – Data type of label variable.

Param axis:

Axis normalization is taken.

Public Functions

inline virtual shared_ptr<Function> copy() const

Copy another instance of Function with the same context.

inline virtual vector<dtypes> in_types()

Get input dtypes.

Last in_type will be used repeatedly if size of in_types is smaller than size of inputs

inline virtual vector<dtypes> out_types()

Get output dtypes.

Last out_type will be used repeatedly if size of out_types is smaller than size of outputs

inline virtual int min_inputs()

Get minimum number of inputs.

This is meant to be used in setup function with in_types which is used to get maximum number of inputs.

inline virtual int min_outputs()

Get minimum number of outputs.

This is meant to be used in setup function with out_types which is used to get max number of outputs.

inline virtual string name()

Get function name in string.

inline virtual vector<string> allowed_array_classes()

Get array classes that are allowed to be specified by Context.

inline virtual bool grad_depends_output_data(int i, int o) const

Dependency flag for checking if in-grad depends on out-data.

Checking if i-th input’ gradient computation requires o-th output’s data or not.

Note

If any of inputs requires an output variable data when computing its gradient, this function must be overridden to return appropriate boolean value. Otherwise, backward computation will be incorrect.

Parameters:
  • i[in] Input variable index.

  • o[in] Output variable index.

template<typename T>
class CELU : public nbla::BaseFunction<double, int>
#include <celu.hpp>

Concatenated Exponential Linear Unit (CELU) concatenates ELU outputs of positive and negative inputs together at specified axis.

Inputs:

  • N-D array.

Outputs:

  • N-D array where axis dimension is doubled by concatenating.

Template Parameters:

T – Data type for computation.

Param axis:

The ELU activations of positive inputs and negative inputs are concatenated at axis.

Public Functions

inline virtual shared_ptr<Function> copy() const

Copy another instance of Function with the same context.

inline virtual vector<dtypes> in_types()

Get input dtypes.

Last in_type will be used repeatedly if size of in_types is smaller than size of inputs

inline virtual vector<dtypes> out_types()

Get output dtypes.

Last out_type will be used repeatedly if size of out_types is smaller than size of outputs

inline virtual int min_inputs()

Get minimum number of inputs.

This is meant to be used in setup function with in_types which is used to get maximum number of inputs.

inline virtual int min_outputs()

Get minimum number of outputs.

This is meant to be used in setup function with out_types which is used to get max number of outputs.

inline virtual string name()

Get function name in string.

inline virtual vector<string> allowed_array_classes()

Get array classes that are allowed to be specified by Context.

inline virtual bool grad_depends_output_data(int i, int o) const

Dependency flag for checking if in-grad depends on out-data.

Checking if i-th input’ gradient computation requires o-th output’s data or not.

Note

If any of inputs requires an output variable data when computing its gradient, this function must be overridden to return appropriate boolean value. Otherwise, backward computation will be incorrect.

Parameters:
  • i[in] Input variable index.

  • o[in] Output variable index.

template<typename T>
class ClipGradByNorm : public nbla::BaseFunction<float, const vector<int>&>
#include <clip_grad_by_norm.hpp>

ClipGradByNorm.

\[ g_x = clip\_norm \times \frac{g_y}{\|g_y\|_2} \]

Inputs:

  • N-D array.

Outputs:

  • N-D array.

Template Parameters:

T – Data type for computation.

Param clip_norm:

Value of the scalar to clip to the norm of input to `clip_norm` in the forward pass.

Param axes:

Axes to be reduced. If empty list is given, all dimensions are reduced to scalar. This is used in the forward pass.

Public Functions

inline virtual shared_ptr<Function> copy() const

Copy another instance of Function with the same context.

inline virtual vector<dtypes> in_types()

Get input dtypes.

Last in_type will be used repeatedly if size of in_types is smaller than size of inputs

inline virtual vector<dtypes> out_types()

Get output dtypes.

Last out_type will be used repeatedly if size of out_types is smaller than size of outputs

inline virtual int min_inputs()

Get minimum number of inputs.

This is meant to be used in setup function with in_types which is used to get maximum number of inputs.

inline virtual int min_outputs()

Get minimum number of outputs.

This is meant to be used in setup function with out_types which is used to get max number of outputs.

inline virtual string name()

Get function name in string.

inline virtual vector<string> allowed_array_classes()

Get array classes that are allowed to be specified by Context.

inline virtual bool grad_depends_output_data(int i, int o) const

Dependency flag for checking if in-grad depends on out-data.

Checking if i-th input’ gradient computation requires o-th output’s data or not.

Note

If any of inputs requires an output variable data when computing its gradient, this function must be overridden to return appropriate boolean value. Otherwise, backward computation will be incorrect.

Parameters:
  • i[in] Input variable index.

  • o[in] Output variable index.

template<typename T>
class ClipGradByValue : public nbla::BaseFunction<>
#include <clip_grad_by_value.hpp>

In forward pass, the function behaves the identity.

In backward pass,

\[\begin{split} \frac{\partial C}{\partial x} = \begin{cases} max & (\frac{\partial C}{\partial y} > max) \\ \frac{\partial C}{\partial y} & (otherwise) \\ min & (\frac{\partial C}{\partial y} < min) \end{cases}, \end{split}\]

Inputs:

  • N-D array.

Outputs:

  • N-D array.

Template Parameters:

T – Data type for computation.

Param min:

Value of the scalar to clip to this `min` if the input value is less than `min` in the forward pass.

Param max:

Value of the scalar to clip to this `max` if the input value is greater than `max` in the forward pass.

Public Functions

inline virtual shared_ptr<Function> copy() const

Copy another instance of Function with the same context.

inline virtual vector<dtypes> in_types()

Get input dtypes.

Last in_type will be used repeatedly if size of in_types is smaller than size of inputs

inline virtual vector<dtypes> out_types()

Get output dtypes.

Last out_type will be used repeatedly if size of out_types is smaller than size of outputs

inline virtual int min_inputs()

Get minimum number of inputs.

This is meant to be used in setup function with in_types which is used to get maximum number of inputs.

inline virtual int min_outputs()

Get minimum number of outputs.

This is meant to be used in setup function with out_types which is used to get max number of outputs.

inline virtual string name()

Get function name in string.

inline virtual vector<string> allowed_array_classes()

Get array classes that are allowed to be specified by Context.

inline virtual bool grad_depends_output_data(int i, int o) const

Dependency flag for checking if in-grad depends on out-data.

Checking if i-th input’ gradient computation requires o-th output’s data or not.

Note

If any of inputs requires an output variable data when computing its gradient, this function must be overridden to return appropriate boolean value. Otherwise, backward computation will be incorrect.

Parameters:
  • i[in] Input variable index.

  • o[in] Output variable index.

template<typename T>
class Concatenate : public nbla::BaseFunction<int>
#include <concatenate.hpp>

Concatenate a variable number of input arrays along specified axis.

Inputs: Input arrays must have the same shapes except specified axis.

  • N-D array.

  • N-D array.

Outputs:

  • N-D array.

Template Parameters:

T – Data type for computation.

Public Functions

inline virtual shared_ptr<Function> copy() const

Copy another instance of Function with the same context.

inline virtual vector<dtypes> in_types()

Get input dtypes.

Last in_type will be used repeatedly if size of in_types is smaller than size of inputs

inline virtual vector<dtypes> out_types()

Get output dtypes.

Last out_type will be used repeatedly if size of out_types is smaller than size of outputs

inline virtual int min_inputs()

Get minimum number of inputs.

This is meant to be used in setup function with in_types which is used to get maximum number of inputs.

inline virtual int min_outputs()

Get minimum number of outputs.

This is meant to be used in setup function with out_types which is used to get max number of outputs.

inline virtual string name()

Get function name in string.

inline virtual vector<string> allowed_array_classes()

Get array classes that are allowed to be specified by Context.

inline virtual bool grad_depends_output_data(int i, int o) const

Dependency flag for checking if in-grad depends on out-data.

Checking if i-th input’ gradient computation requires o-th output’s data or not.

Note

If any of inputs requires an output variable data when computing its gradient, this function must be overridden to return appropriate boolean value. Otherwise, backward computation will be incorrect.

Parameters:
  • i[in] Input variable index.

  • o[in] Output variable index.

template<typename T, typename T1>
class ConfusionMatrix : public nbla::Function
#include <confusion_matrix.hpp>

Confusion matrix.

The return value is already summed over samples.

Inputs (i is the axis on which the confusion matrix is calculated):

  • Probabilities N-D array. ( \(D_1 \times ... \times D_i \times ... \times D_N\))

  • Labels N-D array. ( \(D_1 \times ... \times 1 \times ... \times D_N\))

Outputs:

  • Confusion matrix 2-D array. Col index is estimated class. Row index is label class.

Template Parameters:
  • T – Data type for computation and score variable.

  • Tl – Data type of label variable.

Param axis:

Axis on which the confusion matrix is calculated.

Public Functions

inline virtual shared_ptr<Function> copy() const

Copy another instance of Function with the same context.

inline virtual vector<dtypes> in_types()

Get input dtypes.

Last in_type will be used repeatedly if size of in_types is smaller than size of inputs

inline virtual vector<dtypes> out_types()

Get output dtypes.

Last out_type will be used repeatedly if size of out_types is smaller than size of outputs

inline virtual int min_inputs()

Get minimum number of inputs.

This is meant to be used in setup function with in_types which is used to get maximum number of inputs.

inline virtual int min_outputs()

Get minimum number of outputs.

This is meant to be used in setup function with out_types which is used to get max number of outputs.

inline virtual string name()

Get function name in string.

inline virtual vector<string> allowed_array_classes()

Get array classes that are allowed to be specified by Context.

inline virtual bool grad_depends_output_data(int i, int o) const

Dependency flag for checking if in-grad depends on out-data.

Checking if i-th input’ gradient computation requires o-th output’s data or not.

Note

If any of inputs requires an output variable data when computing its gradient, this function must be overridden to return appropriate boolean value. Otherwise, backward computation will be incorrect.

Parameters:
  • i[in] Input variable index.

  • o[in] Output variable index.

template<typename T>
class Constant : public nbla::BaseFunction<float, const vector<int>&>
#include <constant.hpp>

Todo:

PLACE HERE FUNCTION DOCUMENTATION.

Public Functions

inline virtual shared_ptr<Function> copy() const

Copy another instance of Function with the same context.

inline virtual vector<dtypes> in_types()

Get input dtypes.

Last in_type will be used repeatedly if size of in_types is smaller than size of inputs

inline virtual vector<dtypes> out_types()

Get output dtypes.

Last out_type will be used repeatedly if size of out_types is smaller than size of outputs

inline virtual int min_inputs()

Get minimum number of inputs.

This is meant to be used in setup function with in_types which is used to get maximum number of inputs.

inline virtual int min_outputs()

Get minimum number of outputs.

This is meant to be used in setup function with out_types which is used to get max number of outputs.

inline virtual string name()

Get function name in string.

inline virtual vector<string> allowed_array_classes()

Get array classes that are allowed to be specified by Context.

inline virtual bool grad_depends_output_data(int i, int o) const

Dependency flag for checking if in-grad depends on out-data.

Checking if i-th input’ gradient computation requires o-th output’s data or not.

Note

If any of inputs requires an output variable data when computing its gradient, this function must be overridden to return appropriate boolean value. Otherwise, backward computation will be incorrect.

Parameters:
  • i[in] Input variable index.

  • o[in] Output variable index.

template<typename T>
class Convolution : public nbla::BaseFunction<int, const vector<int>&, const vector<int>&, const vector<int>&, int, bool>
#include <convolution.hpp>

N-D Convolution with bias.

Inputs ( \(B\) is base_axis):

  • Input \((B + 1 + N)\)-D array ( \(M_1 \times ... \times M_B \times C \times L_1 \times ... \times L_N\)).

  • Weight \((2 + N)\)-D array ( \(C' \times C \times K_1 \times ... \times K_N\)).

  • (optional) Bias vector ( \(C'\)).

Outputs:

  • \((B + 1 + N)\)-D array ( \( M_1 \times ... \times M_B \times C' \times L'_1 \times ... \times L'_N \)).

See also

For Dilated Convolution (a.k.a a trous), refer to:

Template Parameters:

T – Data type for computation.

Param base_axis:

Base axis of Convolution operation. Dimensions up to base_axis is treated as sample dimension.

Param pad:

Padding sizes for dimensions.

Param stride:

Stride sizes for dimensions.

Param dilation:

Dilation sizes for dimensions.

Param group:

Number of groups of channels. This makes connections across channels sparser by grouping connections along map direction.

Param channel_last:

If True, the last dimension is considered as channel dimension, a.k.a NHWC order.

Public Functions

inline virtual shared_ptr<Function> copy() const

Copy another instance of Function with the same context.

inline virtual vector<dtypes> in_types()

Get input dtypes.

Last in_type will be used repeatedly if size of in_types is smaller than size of inputs

inline virtual vector<dtypes> out_types()

Get output dtypes.

Last out_type will be used repeatedly if size of out_types is smaller than size of outputs

inline virtual int min_inputs()

Get minimum number of inputs.

This is meant to be used in setup function with in_types which is used to get maximum number of inputs.

inline virtual int min_outputs()

Get minimum number of outputs.

This is meant to be used in setup function with out_types which is used to get max number of outputs.

inline virtual string name()

Get function name in string.

inline virtual vector<string> allowed_array_classes()

Get array classes that are allowed to be specified by Context.

inline virtual bool grad_depends_output_data(int i, int o) const

Dependency flag for checking if in-grad depends on out-data.

Checking if i-th input’ gradient computation requires o-th output’s data or not.

Note

If any of inputs requires an output variable data when computing its gradient, this function must be overridden to return appropriate boolean value. Otherwise, backward computation will be incorrect.

Parameters:
  • i[in] Input variable index.

  • o[in] Output variable index.

template<typename T>
class CReLU : public nbla::BaseFunction<int>
#include <crelu.hpp>

Concatenated Rectified Linear Unit (CReLU) concatenates ReLU outputs of positive inputs and negative inputs together at specified axis.

Inputs:

  • N-D array.

Outputs:

  • N-D array where axis dimension is doubled by concatenating.

See also

Clevert et al. Fast and Accurate Deep Network Learning by Exponential Linear Units (ELUs). https://arxiv.org/abs/1511.07289

Template Parameters:

T – Data type for computation.

Param axis:

The ReLU activations of positive inputs and negative inputs are concatenated at axis.

Public Functions

inline virtual shared_ptr<Function> copy() const

Copy another instance of Function with the same context.

inline virtual int min_inputs()

Get minimum number of inputs.

This is meant to be used in setup function with in_types which is used to get maximum number of inputs.

inline virtual int min_outputs()

Get minimum number of outputs.

This is meant to be used in setup function with out_types which is used to get max number of outputs.

inline virtual vector<dtypes> in_types()

Get input dtypes.

Last in_type will be used repeatedly if size of in_types is smaller than size of inputs

inline virtual vector<dtypes> out_types()

Get output dtypes.

Last out_type will be used repeatedly if size of out_types is smaller than size of outputs

inline virtual string name()

Get function name in string.

inline virtual vector<string> allowed_array_classes()

Get array classes that are allowed to be specified by Context.

inline virtual bool grad_depends_output_data(int i, int o) const

Dependency flag for checking if in-grad depends on out-data.

Checking if i-th input’ gradient computation requires o-th output’s data or not.

Note

If any of inputs requires an output variable data when computing its gradient, this function must be overridden to return appropriate boolean value. Otherwise, backward computation will be incorrect.

Parameters:
  • i[in] Input variable index.

  • o[in] Output variable index.

template<typename T>
class CumProd : public nbla::BaseFunction<int, bool, bool>
#include <cumprod.hpp>

Performs cumulative product of a given array along a specific axis.

Given x = [x1, x2, x3] y = [x1, x1*x2, x1*x2*x3] if not exclusive and not reverse y = [1, x1, x1*x2] if exclusive and not reverse y = [x1*x2*x3, x2*x3, x3] if not exclusive and reverse y = [x2*x3, x3, 1] if exclusive and reverse

Inputs:

  • x: N-D array

  • (param) axis: int

  • (param) exclusive: bool

  • (param) reverse: bool

Outputs:

  • y: N-D array

Public Functions

inline virtual shared_ptr<Function> copy() const

Copy another instance of Function with the same context.

inline virtual int min_inputs()

Get minimum number of inputs.

This is meant to be used in setup function with in_types which is used to get maximum number of inputs.

inline virtual int min_outputs()

Get minimum number of outputs.

This is meant to be used in setup function with out_types which is used to get max number of outputs.

inline virtual vector<dtypes> in_types()

Get input dtypes.

Last in_type will be used repeatedly if size of in_types is smaller than size of inputs

inline virtual vector<dtypes> out_types()

Get output dtypes.

Last out_type will be used repeatedly if size of out_types is smaller than size of outputs

inline virtual vector<string> allowed_array_classes()

Get array classes that are allowed to be specified by Context.

inline virtual string name()

Get function name in string.

inline virtual bool grad_depends_output_data(int i, int o) const

Dependency flag for checking if in-grad depends on out-data.

Checking if i-th input’ gradient computation requires o-th output’s data or not.

Note

If any of inputs requires an output variable data when computing its gradient, this function must be overridden to return appropriate boolean value. Otherwise, backward computation will be incorrect.

Parameters:
  • i[in] Input variable index.

  • o[in] Output variable index.

template<typename T>
class CumSum : public nbla::BaseFunction<int, bool, bool>
#include <cumsum.hpp>

Performs cumulative summation of a given array along a specific axis.

Given x = [x1, x2, x3] y = [x1, x1+x2, x1+x2+x3] if not exclusive and not reverse y = [0, x1, x1+x2] if exclusive and not reverse y = [x1+x2+x3, x2+x3, x3] if not exclusive and reverse y = [x2+x3, x3, 0] if exclusive and reverse

Inputs:

  • x: N-D array

  • (param) axis: int

  • (param) exclusive: bool

  • (param) reverse: bool

Outputs:

  • y: N-D array

Public Functions

inline virtual shared_ptr<Function> copy() const

Copy another instance of Function with the same context.

inline virtual int min_inputs()

Get minimum number of inputs.

This is meant to be used in setup function with in_types which is used to get maximum number of inputs.

inline virtual int min_outputs()

Get minimum number of outputs.

This is meant to be used in setup function with out_types which is used to get max number of outputs.

inline virtual vector<dtypes> in_types()

Get input dtypes.

Last in_type will be used repeatedly if size of in_types is smaller than size of inputs

inline virtual vector<dtypes> out_types()

Get output dtypes.

Last out_type will be used repeatedly if size of out_types is smaller than size of outputs

inline virtual vector<string> allowed_array_classes()

Get array classes that are allowed to be specified by Context.

inline virtual string name()

Get function name in string.

inline virtual bool grad_depends_output_data(int i, int o) const

Dependency flag for checking if in-grad depends on out-data.

Checking if i-th input’ gradient computation requires o-th output’s data or not.

Note

If any of inputs requires an output variable data when computing its gradient, this function must be overridden to return appropriate boolean value. Otherwise, backward computation will be incorrect.

Parameters:
  • i[in] Input variable index.

  • o[in] Output variable index.

template<typename T>
class Deconvolution : public nbla::BaseFunction<int, const vector<int>&, const vector<int>&, const vector<int>&, int, bool, const vector<int>&>
#include <deconvolution.hpp>

N-D Deconvolution with bias operates backward convolution (derivative of output wrt input) plus channel-wise learned bias.

The weights must be given with the same format as in forward convolution, hence the number of input channels (can be seen as output channels of forward convolution) comes to the first dimension, and the second dimension has number of the output channels divided by group.

Inputs ( \(B\) is base_axis):

  • Input \((B + 1 + N)\)-D array ( \(M_1 \times ... \times M_B \times C \times L_1 \times ... \times L_N\)).

  • Weight \((2 + N)\)-D array ( \(C' \times C \times K_1 \times ... \times K_N\)).

  • (optional) Bias vector ( \(C'\)).

See also

Convolution for documentation of parameters.

See also

Deconvolution is introduced in Shelhamer et al., Fully Convolutional Networks for Semantic Segmentation. https://arxiv.org/abs/1605.06211

Public Functions

inline virtual shared_ptr<Function> copy() const

Copy another instance of Function with the same context.

inline virtual vector<dtypes> in_types()

Get input dtypes.

Last in_type will be used repeatedly if size of in_types is smaller than size of inputs

inline virtual vector<dtypes> out_types()

Get output dtypes.

Last out_type will be used repeatedly if size of out_types is smaller than size of outputs

inline virtual int min_inputs()

Get minimum number of inputs.

This is meant to be used in setup function with in_types which is used to get maximum number of inputs.

inline virtual int min_outputs()

Get minimum number of outputs.

This is meant to be used in setup function with out_types which is used to get max number of outputs.

inline virtual string name()

Get function name in string.

inline virtual vector<string> allowed_array_classes()

Get array classes that are allowed to be specified by Context.

inline virtual bool grad_depends_output_data(int i, int o) const

Dependency flag for checking if in-grad depends on out-data.

Checking if i-th input’ gradient computation requires o-th output’s data or not.

Note

If any of inputs requires an output variable data when computing its gradient, this function must be overridden to return appropriate boolean value. Otherwise, backward computation will be incorrect.

Parameters:
  • i[in] Input variable index.

  • o[in] Output variable index.

template<typename T>
class DeformableConvolution : public nbla::BaseFunction<int, const vector<int>&, const vector<int>&, const vector<int>&, int, int, bool>
#include <deformable_convolution.hpp>

Todo:

Write doc.

Inputs:

Outputs:

Public Functions

inline virtual shared_ptr<Function> copy() const

Copy another instance of Function with the same context.

inline virtual int min_inputs()

Get minimum number of inputs.

This is meant to be used in setup function with in_types which is used to get maximum number of inputs.

inline virtual int min_outputs()

Get minimum number of outputs.

This is meant to be used in setup function with out_types which is used to get max number of outputs.

inline virtual vector<dtypes> in_types()

Get input dtypes.

Last in_type will be used repeatedly if size of in_types is smaller than size of inputs

inline virtual vector<dtypes> out_types()

Get output dtypes.

Last out_type will be used repeatedly if size of out_types is smaller than size of outputs

inline virtual vector<string> allowed_array_classes()

Get array classes that are allowed to be specified by Context.

inline virtual string name()

Get function name in string.

inline virtual bool grad_depends_output_data(int i, int o) const

Dependency flag for checking if in-grad depends on out-data.

Checking if i-th input’ gradient computation requires o-th output’s data or not.

Note

If any of inputs requires an output variable data when computing its gradient, this function must be overridden to return appropriate boolean value. Otherwise, backward computation will be incorrect.

Parameters:
  • i[in] Input variable index.

  • o[in] Output variable index.

template<typename T>
class DepthwiseConvolution : public nbla::BaseFunction<int, const vector<int>&, const vector<int>&, const vector<int>&, int>
#include <depthwise_convolution.hpp>

N-D Depthwise Convolution with bias.

Inputs ( \(B\) is base_axis):

  • Input \((B + 1 + N)\)-D array ( \(M_1 \times ... \times M_B \times C \times L_1 \times ... \times L_N\)).

  • Weight \((1 + N)\)-D array ( \(C \times K_1 \times ... \times K_N\)).

  • (optional) Bias vector ( \(C\)).

Outputs:

  • \((B + 1 + N)\)-D array ( \( M_1 \times ... \times M_B \times C \times L'_1 \times ... \times L'_N \)).

See also

Reference:

Template Parameters:

T – Data type for computation.

Param base_axis:

Base axis of Convolution operation. Dimensions up to base_axis is treated as sample dimension.

Param pad:

Padding sizes for dimensions.

Param stride:

Stride sizes for dimensions.

Param dilation:

Dilation sizes for dimensions.

Param multiplier:

Number of output feature maps per input feature map.

Public Functions

inline virtual shared_ptr<Function> copy() const

Copy another instance of Function with the same context.

inline virtual vector<dtypes> in_types()

Get input dtypes.

Last in_type will be used repeatedly if size of in_types is smaller than size of inputs

inline virtual vector<dtypes> out_types()

Get output dtypes.

Last out_type will be used repeatedly if size of out_types is smaller than size of outputs

inline virtual int min_inputs()

Get minimum number of inputs.

This is meant to be used in setup function with in_types which is used to get maximum number of inputs.

inline virtual int min_outputs()

Get minimum number of outputs.

This is meant to be used in setup function with out_types which is used to get max number of outputs.

inline virtual string name()

Get function name in string.

inline virtual vector<string> allowed_array_classes()

Get array classes that are allowed to be specified by Context.

inline virtual bool grad_depends_output_data(int i, int o) const

Dependency flag for checking if in-grad depends on out-data.

Checking if i-th input’ gradient computation requires o-th output’s data or not.

Note

If any of inputs requires an output variable data when computing its gradient, this function must be overridden to return appropriate boolean value. Otherwise, backward computation will be incorrect.

Parameters:
  • i[in] Input variable index.

  • o[in] Output variable index.

template<typename T>
class DepthwiseDeconvolution : public nbla::BaseFunction<int, const vector<int>&, const vector<int>&, const vector<int>&, int>
#include <depthwise_deconvolution.hpp>

1-D and 2-D Depthwise Deconvolution with bias.

Inputs ( \(B\) is base_axis):

  • Input \((B + 1 + N)\)-D array ( \(M_1 \times ... \times M_B \times C \times L_1 \times ... \times L_N\)).

  • Weight \((1 + N)\)-D array ( \(C \times K_1 \times ... \times K_N\)).

  • (optional) Bias vector ( \(C\)).

Outputs:

  • \((B + 1 + N)\)-D array ( \( M_1 \times ... \times M_B \times C \times L'_1 \times ... \times L'_N \)).

Template Parameters:

T – Data type for computation.

Param base_axis:

Base axis of Convolution operation. Dimensions up to base_axis is treated as sample dimension.

Param pad:

Padding sizes for dimensions.

Param stride:

Stride sizes for dimensions.

Param dilation:

Dilation sizes for dimensions.

Param divisor:

Number of input feature maps per output feature map.

Public Functions

inline virtual shared_ptr<Function> copy() const

Copy another instance of Function with the same context.

inline virtual vector<dtypes> in_types()

Get input dtypes.

Last in_type will be used repeatedly if size of in_types is smaller than size of inputs

inline virtual vector<dtypes> out_types()

Get output dtypes.

Last out_type will be used repeatedly if size of out_types is smaller than size of outputs

inline virtual int min_inputs()

Get minimum number of inputs.

This is meant to be used in setup function with in_types which is used to get maximum number of inputs.

inline virtual int min_outputs()

Get minimum number of outputs.

This is meant to be used in setup function with out_types which is used to get max number of outputs.

inline virtual string name()

Get function name in string.

inline virtual vector<string> allowed_array_classes()

Get array classes that are allowed to be specified by Context.

inline virtual bool grad_depends_output_data(int i, int o) const

Dependency flag for checking if in-grad depends on out-data.

Checking if i-th input’ gradient computation requires o-th output’s data or not.

Note

If any of inputs requires an output variable data when computing its gradient, this function must be overridden to return appropriate boolean value. Otherwise, backward computation will be incorrect.

Parameters:
  • i[in] Input variable index.

  • o[in] Output variable index.

template<typename T>
class DequantizeLinear : public nbla::BaseFunction<>
#include <dequantize_linear.hpp>

Todo:

Write doc.

Inputs:

Outputs:

Public Functions

inline virtual shared_ptr<Function> copy() const

Copy another instance of Function with the same context.

inline virtual int min_inputs()

Get minimum number of inputs.

This is meant to be used in setup function with in_types which is used to get maximum number of inputs.

inline virtual int min_outputs()

Get minimum number of outputs.

This is meant to be used in setup function with out_types which is used to get max number of outputs.

inline virtual vector<dtypes> in_types()

Get input dtypes.

Last in_type will be used repeatedly if size of in_types is smaller than size of inputs

inline virtual vector<dtypes> out_types()

Get output dtypes.

Last out_type will be used repeatedly if size of out_types is smaller than size of outputs

inline virtual vector<string> allowed_array_classes()

Get array classes that are allowed to be specified by Context.

inline virtual string name()

Get function name in string.

inline virtual bool grad_depends_output_data(int i, int o) const

Dependency flag for checking if in-grad depends on out-data.

Checking if i-th input’ gradient computation requires o-th output’s data or not.

Note

If any of inputs requires an output variable data when computing its gradient, this function must be overridden to return appropriate boolean value. Otherwise, backward computation will be incorrect.

Parameters:
  • i[in] Input variable index.

  • o[in] Output variable index.

template<typename T>
class Dropout : public nbla::BaseFunction<double, int>
#include <dropout.hpp>

Dropout defined as.

\[\begin{split} y = \left\{ \begin{array}{ll} \frac{x}{1 - p} & \mbox{if } u > p \\ 0 & \mbox{otherwise} \end{array} \right. \end{split}\]
where u is generated from uniform dist in [0, 1].

Inputs:

  • N-D array.

Outputs:

  • N-D array with the same shape as input.

Template Parameters:

T – Data type for computation.

Param p:

\(p\) in definition.

Param seed:

Random seed

Public Functions

inline virtual shared_ptr<Function> copy() const

Copy another instance of Function with the same context.

inline virtual int min_inputs()

Get minimum number of inputs.

This is meant to be used in setup function with in_types which is used to get maximum number of inputs.

inline virtual int min_outputs()

Get minimum number of outputs.

This is meant to be used in setup function with out_types which is used to get max number of outputs.

inline virtual vector<dtypes> in_types()

Get input dtypes.

Last in_type will be used repeatedly if size of in_types is smaller than size of inputs

inline virtual vector<dtypes> out_types()

Get output dtypes.

Last out_type will be used repeatedly if size of out_types is smaller than size of outputs

inline virtual string name()

Get function name in string.

inline virtual vector<string> allowed_array_classes()

Get array classes that are allowed to be specified by Context.

inline virtual bool need_setup_recompute(int o) const

A flag for checking if setup_recompute() is needed.

Checking if o-th output’ data requires setup_recompute().

Note

setup_recompute() will skipped during forward execution if none of outputs requires setup_recompute().

Parameters:

o[in] Output variable index.

inline virtual bool grad_depends_output_data(int i, int o) const

Dependency flag for checking if in-grad depends on out-data.

Checking if i-th input’ gradient computation requires o-th output’s data or not.

Note

If any of inputs requires an output variable data when computing its gradient, this function must be overridden to return appropriate boolean value. Otherwise, backward computation will be incorrect.

Parameters:
  • i[in] Input variable index.

  • o[in] Output variable index.

template<typename T>
class Einsum : public nbla::BaseFunction<const string&>
#include <einsum.hpp>

Evaluates the Einstein summation convention on the inputs.

Inputs:

  • x: List of N-D array.

Outputs:

  • y: A N-D array.

Param equation:

A string that folllows Einstein summation convention.

Public Functions

inline virtual shared_ptr<Function> copy() const

Copy another instance of Function with the same context.

inline virtual int min_inputs()

Get minimum number of inputs.

This is meant to be used in setup function with in_types which is used to get maximum number of inputs.

inline virtual int min_outputs()

Get minimum number of outputs.

This is meant to be used in setup function with out_types which is used to get max number of outputs.

inline virtual vector<dtypes> in_types()

Get input dtypes.

Last in_type will be used repeatedly if size of in_types is smaller than size of inputs

inline virtual vector<dtypes> out_types()

Get output dtypes.

Last out_type will be used repeatedly if size of out_types is smaller than size of outputs

inline virtual vector<string> allowed_array_classes()

Get array classes that are allowed to be specified by Context.

inline virtual string name()

Get function name in string.

inline virtual bool grad_depends_output_data(int i, int o) const

Dependency flag for checking if in-grad depends on out-data.

Checking if i-th input’ gradient computation requires o-th output’s data or not.

Note

If any of inputs requires an output variable data when computing its gradient, this function must be overridden to return appropriate boolean value. Otherwise, backward computation will be incorrect.

Parameters:
  • i[in] Input variable index.

  • o[in] Output variable index.

template<typename T, typename T1>
class Embed : public nbla::BaseFunction<>
#include <embed.hpp>

Embed slices a matrix/tensor with indexing array/tensor.

Inputs:

  • Indexes with shape \((I_0, ..., I_N)\)

  • Weights with shape \((W_0, ..., W_M)\)

Outputs:

  • Output with shape \((I_0, ..., I_N, W_1, ..., W_M)\)

Template Parameters:
  • T – Index type (integer)

  • T1 – Value type (usually float)

Public Functions

inline virtual shared_ptr<Function> copy() const

Copy another instance of Function with the same context.

inline virtual vector<dtypes> in_types()

Get input dtypes.

Last in_type will be used repeatedly if size of in_types is smaller than size of inputs

inline virtual vector<dtypes> out_types()

Get output dtypes.

Last out_type will be used repeatedly if size of out_types is smaller than size of outputs

inline virtual int min_inputs()

Get minimum number of inputs.

This is meant to be used in setup function with in_types which is used to get maximum number of inputs.

inline virtual int min_outputs()

Get minimum number of outputs.

This is meant to be used in setup function with out_types which is used to get max number of outputs.

inline virtual string name()

Get function name in string.

inline virtual vector<string> allowed_array_classes()

Get array classes that are allowed to be specified by Context.

inline virtual bool grad_depends_output_data(int i, int o) const

Dependency flag for checking if in-grad depends on out-data.

Checking if i-th input’ gradient computation requires o-th output’s data or not.

Note

If any of inputs requires an output variable data when computing its gradient, this function must be overridden to return appropriate boolean value. Otherwise, backward computation will be incorrect.

Parameters:
  • i[in] Input variable index.

  • o[in] Output variable index.

template<typename T>
class EyeLike : public nbla::BaseFunction<int>
#include <eye_like.hpp>

Generate a 2-D array with ones on the diagonal, specified by `k`, and zeros elsewhere.

The shape of the output array is the same as the input array.

Inputs:

  • x: A 2-D array.

Outputs:

  • y: A 2-D array.

Param k:

Index of the diagonal. The default value 0 means the main diagonal, a positive value means an upper diagonal, and a negative value means a lower diagonal.

Public Functions

inline virtual shared_ptr<Function> copy() const

Copy another instance of Function with the same context.

inline virtual int min_inputs()

Get minimum number of inputs.

This is meant to be used in setup function with in_types which is used to get maximum number of inputs.

inline virtual int min_outputs()

Get minimum number of outputs.

This is meant to be used in setup function with out_types which is used to get max number of outputs.

inline virtual vector<dtypes> in_types()

Get input dtypes.

Last in_type will be used repeatedly if size of in_types is smaller than size of inputs

inline virtual vector<dtypes> out_types()

Get output dtypes.

Last out_type will be used repeatedly if size of out_types is smaller than size of outputs

inline virtual vector<string> allowed_array_classes()

Get array classes that are allowed to be specified by Context.

inline virtual string name()

Get function name in string.

inline virtual bool grad_depends_output_data(int i, int o) const

Dependency flag for checking if in-grad depends on out-data.

Checking if i-th input’ gradient computation requires o-th output’s data or not.

Note

If any of inputs requires an output variable data when computing its gradient, this function must be overridden to return appropriate boolean value. Otherwise, backward computation will be incorrect.

Parameters:
  • i[in] Input variable index.

  • o[in] Output variable index.

template<typename T>
class FFT : public nbla::BaseFunction<int, bool>
#include <fft.hpp>

Todo:

Write doc.

Inputs:

Outputs:

Public Functions

inline virtual shared_ptr<Function> copy() const

Copy another instance of Function with the same context.

inline virtual int min_inputs()

Get minimum number of inputs.

This is meant to be used in setup function with in_types which is used to get maximum number of inputs.

inline virtual int min_outputs()

Get minimum number of outputs.

This is meant to be used in setup function with out_types which is used to get max number of outputs.

inline virtual vector<dtypes> in_types()

Get input dtypes.

Last in_type will be used repeatedly if size of in_types is smaller than size of inputs

inline virtual vector<dtypes> out_types()

Get output dtypes.

Last out_type will be used repeatedly if size of out_types is smaller than size of outputs

inline virtual vector<string> allowed_array_classes()

Get array classes that are allowed to be specified by Context.

inline virtual string name()

Get function name in string.

inline virtual bool grad_depends_output_data(int i, int o) const

Dependency flag for checking if in-grad depends on out-data.

Checking if i-th input’ gradient computation requires o-th output’s data or not.

Note

If any of inputs requires an output variable data when computing its gradient, this function must be overridden to return appropriate boolean value. Otherwise, backward computation will be incorrect.

Parameters:
  • i[in] Input variable index.

  • o[in] Output variable index.

template<typename T>
class FixedPointQuantize : public nbla::BaseFunction<bool, int, float, bool>
#include <fixed_point_quantize.hpp>

FixedPointQuantize quantizes values in fixed-point number representation.

Template Parameters:

T – Data type for computation.

Param sign:

Indicate the signed number or the unsigned number. Default is true.

Param n:

Bit width used. Note that `sign` consumes one bit. \(n-1\) is used for number representation in `signed` case.

Param delta:

Step size.

Param quantize:

If true, quantize input, otherwise not.

Param ste_fine_grained:

If true, STE is not 1.

Public Functions

inline virtual shared_ptr<Function> copy() const

Copy another instance of Function with the same context.

inline virtual vector<dtypes> in_types()

Get input dtypes.

Last in_type will be used repeatedly if size of in_types is smaller than size of inputs

inline virtual vector<dtypes> out_types()

Get output dtypes.

Last out_type will be used repeatedly if size of out_types is smaller than size of outputs

inline virtual int min_inputs()

Get minimum number of inputs.

This is meant to be used in setup function with in_types which is used to get maximum number of inputs.

inline virtual int min_outputs()

Get minimum number of outputs.

This is meant to be used in setup function with out_types which is used to get max number of outputs.

inline virtual string name()

Get function name in string.

inline virtual vector<string> allowed_array_classes()

Get array classes that are allowed to be specified by Context.

inline virtual bool grad_depends_output_data(int i, int o) const

Dependency flag for checking if in-grad depends on out-data.

Checking if i-th input’ gradient computation requires o-th output’s data or not.

Note

If any of inputs requires an output variable data when computing its gradient, this function must be overridden to return appropriate boolean value. Otherwise, backward computation will be incorrect.

Parameters:
  • i[in] Input variable index.

  • o[in] Output variable index.

template<typename T>
class Flip : public nbla::BaseFunction<const vector<int>&>
#include <flip.hpp>

Flip reverses the order of elements of the specified dimension of an array.

Inputs:

  • N-D array.

Outputs:

  • N-D array.

Template Parameters:

T – Data type for computation.

Param axes:

The index of the dimension to reverse the order of the elements. Axis indexes take on values 0, 1, 2, and so on from the left. For example, to flip a 32 (W) by 24 (H) 100 RGB image (100,3,24,32) vertically and horizontally, specify (2,3).

Public Functions

inline virtual shared_ptr<Function> copy() const

Copy another instance of Function with the same context.

inline virtual vector<dtypes> in_types()

Get input dtypes.

Last in_type will be used repeatedly if size of in_types is smaller than size of inputs

inline virtual vector<dtypes> out_types()

Get output dtypes.

Last out_type will be used repeatedly if size of out_types is smaller than size of outputs

inline virtual int min_inputs()

Get minimum number of inputs.

This is meant to be used in setup function with in_types which is used to get maximum number of inputs.

inline virtual int min_outputs()

Get minimum number of outputs.

This is meant to be used in setup function with out_types which is used to get max number of outputs.

inline virtual string name()

Get function name in string.

inline virtual vector<string> allowed_array_classes()

Get array classes that are allowed to be specified by Context.

inline virtual bool grad_depends_output_data(int i, int o) const

Dependency flag for checking if in-grad depends on out-data.

Checking if i-th input’ gradient computation requires o-th output’s data or not.

Note

If any of inputs requires an output variable data when computing its gradient, this function must be overridden to return appropriate boolean value. Otherwise, backward computation will be incorrect.

Parameters:
  • i[in] Input variable index.

  • o[in] Output variable index.

template<typename T>
class FusedBatchNormalization : public nbla::BaseFunction<const vector<int>&, float, float, bool, const string&>
#include <fused_batch_normalization.hpp>

Batch normalization fused with add2 (adding a residual input) and activation.

This is an equivalent operation to the following (in Python), but is more computationally efficient:

h = F.BatchNormalization(x, beta, gamma, mean, variance, *opts)
y = F.ReLU(h + z)

Inputs:

  • N-D array of input.

  • N-D array of beta which is learned.

  • N-D array of gamma which is learned.

  • N-D array of running mean (modified during forward execution).

  • N-D array of running variance (modified during forward execution).

  • N-D array of N-D array of a residual input (optional). If not passed, the activation function will follow immediately after BN operation.

Outputs (1 or 3):

  • N-D array.

  • (Optional) N-D array of batch mean.

  • (Optional) N-D array of batch variance.

See also

Ioffe and Szegedy, Batch Normalization: Accelerating Deep Network Training by Reducing Internal Covariate Shift. https://arxiv.org/abs/1502.03167

Template Parameters:

T – Data type for computation.

Param axes:

Axes mean and variance are taken.

Param decay_rate:

Decay rate of running mean and variance.

Param eps:

Tiny value to avoid zero division by std.

Param batch_stat:

Use mini-batch statistics rather than running ones.

Param nonlinearity:

Activation chosen from (“relu”).

Public Functions

inline virtual shared_ptr<Function> copy() const

Copy another instance of Function with the same context.

inline virtual int min_inputs()

Get minimum number of inputs.

This is meant to be used in setup function with in_types which is used to get maximum number of inputs.

inline virtual int min_outputs()

Get minimum number of outputs.

This is meant to be used in setup function with out_types which is used to get max number of outputs.

inline virtual vector<string> allowed_array_classes()

Get array classes that are allowed to be specified by Context.

inline virtual vector<dtypes> in_types()

Get input dtypes.

Last in_type will be used repeatedly if size of in_types is smaller than size of inputs

inline virtual vector<dtypes> out_types()

Get output dtypes.

Last out_type will be used repeatedly if size of out_types is smaller than size of outputs

inline virtual string name()

Get function name in string.

inline virtual bool grad_depends_output_data(int i, int o) const

Dependency flag for checking if in-grad depends on out-data.

Checking if i-th input’ gradient computation requires o-th output’s data or not.

Note

If any of inputs requires an output variable data when computing its gradient, this function must be overridden to return appropriate boolean value. Otherwise, backward computation will be incorrect.

Parameters:
  • i[in] Input variable index.

  • o[in] Output variable index.

template<typename T>
class FusedConvolution : public nbla::BaseFunction<int, const vector<int>&, const vector<int>&, const vector<int>&, int, bool, float, float, bool, const string&, const vector<float>&, const string&, float>
#include <fused_convolution.hpp>

Todo:

Write doc.

Inputs:

Outputs:

Public Functions

inline virtual shared_ptr<Function> copy() const

Copy another instance of Function with the same context.

inline virtual int min_inputs()

Get minimum number of inputs.

This is meant to be used in setup function with in_types which is used to get maximum number of inputs.

inline virtual int min_outputs()

Get minimum number of outputs.

This is meant to be used in setup function with out_types which is used to get max number of outputs.

inline virtual vector<dtypes> in_types()

Get input dtypes.

Last in_type will be used repeatedly if size of in_types is smaller than size of inputs

inline virtual vector<dtypes> out_types()

Get output dtypes.

Last out_type will be used repeatedly if size of out_types is smaller than size of outputs

inline virtual vector<string> allowed_array_classes()

Get array classes that are allowed to be specified by Context.

inline virtual string name()

Get function name in string.

inline virtual bool grad_depends_output_data(int i, int o) const

Dependency flag for checking if in-grad depends on out-data.

Checking if i-th input’ gradient computation requires o-th output’s data or not.

Note

If any of inputs requires an output variable data when computing its gradient, this function must be overridden to return appropriate boolean value. Otherwise, backward computation will be incorrect.

Parameters:
  • i[in] Input variable index.

  • o[in] Output variable index.

template<typename T>
class Gather : public nbla::BaseFunction<int, int>
#include <gather.hpp>

Gather from the input data according to the index.

Given the input data \(X\) of \((D_{0}, \ldots, D_{N-1})\) shape and the indices \(IDX\) of \((I_{0}, \ldots, I_{M-1})\) shape, in case of `batch_dims = 0`, the gather outputs

\[\begin{split}\begin{eqnarray*} && Y[d_{0}, \ldots, d_{axis - 1}, i_{0}, \ldots, i_{M-1}, d_{axis + 1}, \ldots, d_{N-1}] = \\ && X[d_{0}, \ldots, d_{axis - 1}, IDX[i_{0}, \ldots, i_{M-1}], d_{axis + 1}, \ldots, d_{N-1}]. \end{eqnarray*}\end{split}\]

Generally, the gather ouptuts

\[\begin{split}\begin{eqnarray*} && Y[d_{0}, \ldots, d_{axis - 1}, i_{B}, \ldots, i_{M-1}, d_{axis + 1}, \ldots, d_{N-1}] = \\ && X[d_{0}, \ldots, d_{axis - 1}, IDX[i_{0}, \ldots, i_{B - 1}, i_{B} \ldots, i_{M-1}], d_{axis + 1}, \ldots d_{N-1}]. \end{eqnarray*}\end{split}\]

where \(B\) = `batch_dims`.

`x.shape[:batch_dims]` must be equal to `indices.shape[:batch_dims]`.

Output shape is `x.shape[:axis] + indices.shape[batch_dims:] + x.shape[axis + 1]`.

Inputs:

  • Data from which to gather.

  • Index with which to gather.

Outputs:

  • Gathered output.

Template Parameters:

T – Data type for computation.

Param axis:

Axis in the data to gather from. `axis` must be greater than or equal to `batch_dims`.

Param batch_dims:

The number of batch dimensions.

Public Functions

inline virtual shared_ptr<Function> copy() const

Copy another instance of Function with the same context.

inline virtual int min_inputs()

Get minimum number of inputs.

This is meant to be used in setup function with in_types which is used to get maximum number of inputs.

inline virtual int min_outputs()

Get minimum number of outputs.

This is meant to be used in setup function with out_types which is used to get max number of outputs.

inline virtual vector<dtypes> in_types()

Get input dtypes.

Last in_type will be used repeatedly if size of in_types is smaller than size of inputs

inline virtual vector<dtypes> out_types()

Get output dtypes.

Last out_type will be used repeatedly if size of out_types is smaller than size of outputs

inline virtual vector<string> allowed_array_classes()

Get array classes that are allowed to be specified by Context.

inline virtual string name()

Get function name in string.

inline virtual bool grad_depends_output_data(int i, int o) const

Dependency flag for checking if in-grad depends on out-data.

Checking if i-th input’ gradient computation requires o-th output’s data or not.

Note

If any of inputs requires an output variable data when computing its gradient, this function must be overridden to return appropriate boolean value. Otherwise, backward computation will be incorrect.

Parameters:
  • i[in] Input variable index.

  • o[in] Output variable index.

template<typename T>
class GatherNd : public nbla::BaseFunction<>
#include <gather_nd.hpp>

Gather elements or slices from `data` according to `indices`, which must be at least two-dimensional with the first dimension :math:`M` being less or equal to the :math:`N` dimensions of `data`.

Given `data` with shape :math:`(X_0, X_1, …, X_{N-1})` and indices with shape :math:`(M, Y_0, …, Y_{K-1})` output has shape :math:`(Y_0, …, Y_{K-1}, X_M, …, X_{N-1})`. If :math:`M == N`, output shape is simply :math:`(Y_0, …, Y_{K-1})`.

The forward of :func:`~nnabla.functions.gather_nd` is equivalent to the following Python code:

def gather_nd(data, index):
    import numpy as np
    tmp_index = index.reshape(index.shape[0], -1)
    tmp_index = (idx + (Ellipsis,) for idx in zip(*new_index))
    out_shape = index.shape[1:] + data.shape[index.shape[0]:]
    return np.vstack(data[idx] for idx in tmp_index).reshape(*out_shape)

Inputs:

  • N-D array `data`

  • N-D array `indices`

Outputs:

  • N-D array

Public Functions

inline virtual shared_ptr<Function> copy() const

Copy another instance of Function with the same context.

inline virtual int min_inputs()

Get minimum number of inputs.

This is meant to be used in setup function with in_types which is used to get maximum number of inputs.

inline virtual int min_outputs()

Get minimum number of outputs.

This is meant to be used in setup function with out_types which is used to get max number of outputs.

inline virtual vector<dtypes> in_types()

Get input dtypes.

Last in_type will be used repeatedly if size of in_types is smaller than size of inputs

inline virtual vector<dtypes> out_types()

Get output dtypes.

Last out_type will be used repeatedly if size of out_types is smaller than size of outputs

inline virtual vector<string> allowed_array_classes()

Get array classes that are allowed to be specified by Context.

inline virtual string name()

Get function name in string.

inline virtual bool grad_depends_output_data(int i, int o) const

Dependency flag for checking if in-grad depends on out-data.

Checking if i-th input’ gradient computation requires o-th output’s data or not.

Note

If any of inputs requires an output variable data when computing its gradient, this function must be overridden to return appropriate boolean value. Otherwise, backward computation will be incorrect.

Parameters:
  • i[in] Input variable index.

  • o[in] Output variable index.

template<typename T>
class GlobalAveragePooling : public nbla::BaseFunction<>
#include <global_average_pooling.hpp>

Global average pooling operator.

Inputs:

  • N-D array that has more than two dimensions.

Outputs:

  • N-D array.

Note

Only 2D pooling supported so far.

Template Parameters:

T – Data type for computation.

Param kernel:

Shapes of kernel.

Param stride:

Subsampling factors of pooling.

Param ignore_border:

If false, a kernel overlapping border is also considered for output unlike convolution.

Param pad:

Border padding values of dimensions. Padding will be added both sides of the dimension.

Public Functions

inline virtual shared_ptr<Function> copy() const

Copy another instance of Function with the same context.

inline virtual vector<dtypes> in_types()

Get input dtypes.

Last in_type will be used repeatedly if size of in_types is smaller than size of inputs

inline virtual vector<dtypes> out_types()

Get output dtypes.

Last out_type will be used repeatedly if size of out_types is smaller than size of outputs

inline virtual int min_inputs()

Get minimum number of inputs.

This is meant to be used in setup function with in_types which is used to get maximum number of inputs.

inline virtual int min_outputs()

Get minimum number of outputs.

This is meant to be used in setup function with out_types which is used to get max number of outputs.

inline virtual string name()

Get function name in string.

inline virtual vector<string> allowed_array_classes()

Get array classes that are allowed to be specified by Context.

inline virtual bool grad_depends_output_data(int i, int o) const

Dependency flag for checking if in-grad depends on out-data.

Checking if i-th input’ gradient computation requires o-th output’s data or not.

Note

If any of inputs requires an output variable data when computing its gradient, this function must be overridden to return appropriate boolean value. Otherwise, backward computation will be incorrect.

Parameters:
  • i[in] Input variable index.

  • o[in] Output variable index.

template<typename T>
class GroupNormalization : public nbla::BaseFunction<int, int, const vector<int>&, float, bool, bool>
#include <group_normalization.hpp>

Todo:

Write doc.

Inputs:

Outputs:

Public Functions

inline virtual shared_ptr<Function> copy() const

Copy another instance of Function with the same context.

inline virtual int min_inputs()

Get minimum number of inputs.

This is meant to be used in setup function with in_types which is used to get maximum number of inputs.

inline virtual int min_outputs()

Get minimum number of outputs.

This is meant to be used in setup function with out_types which is used to get max number of outputs.

inline virtual vector<dtypes> in_types()

Get input dtypes.

Last in_type will be used repeatedly if size of in_types is smaller than size of inputs

inline virtual vector<dtypes> out_types()

Get output dtypes.

Last out_type will be used repeatedly if size of out_types is smaller than size of outputs

inline virtual vector<string> allowed_array_classes()

Get array classes that are allowed to be specified by Context.

inline virtual string name()

Get function name in string.

inline virtual bool grad_depends_output_data(int i, int o) const

Dependency flag for checking if in-grad depends on out-data.

Checking if i-th input’ gradient computation requires o-th output’s data or not.

Note

If any of inputs requires an output variable data when computing its gradient, this function must be overridden to return appropriate boolean value. Otherwise, backward computation will be incorrect.

Parameters:
  • i[in] Input variable index.

  • o[in] Output variable index.

template<typename T>
class GRU : public nbla::BaseFunction<int, float, bool, bool>
#include <gru.hpp>

Todo:

Write doc.

Inputs:

Outputs:

Public Functions

inline virtual shared_ptr<Function> copy() const

Copy another instance of Function with the same context.

inline virtual int min_inputs()

Get minimum number of inputs.

This is meant to be used in setup function with in_types which is used to get maximum number of inputs.

inline virtual int min_outputs()

Get minimum number of outputs.

This is meant to be used in setup function with out_types which is used to get max number of outputs.

inline virtual vector<dtypes> in_types()

Get input dtypes.

Last in_type will be used repeatedly if size of in_types is smaller than size of inputs

inline virtual vector<dtypes> out_types()

Get output dtypes.

Last out_type will be used repeatedly if size of out_types is smaller than size of outputs

inline virtual vector<string> allowed_array_classes()

Get array classes that are allowed to be specified by Context.

inline virtual string name()

Get function name in string.

inline virtual bool grad_depends_output_data(int i, int o) const

Dependency flag for checking if in-grad depends on out-data.

Checking if i-th input’ gradient computation requires o-th output’s data or not.

Note

If any of inputs requires an output variable data when computing its gradient, this function must be overridden to return appropriate boolean value. Otherwise, backward computation will be incorrect.

Parameters:
  • i[in] Input variable index.

  • o[in] Output variable index.

template<typename T>
class Identity : public nbla::BaseFunction<>
#include <identity.hpp>

Identify outputs the input as-is.

There is no need to use this layer normally, but it can be inserted to identity certain locations of the network.

\[ y = x. \]

Inputs:

  • N-D array.

Outputs:

  • N-D array. The same variable as input.

Template Parameters:

T – Data type for computation.

Public Functions

inline virtual shared_ptr<Function> copy() const

Copy another instance of Function with the same context.

inline virtual int min_inputs()

Get minimum number of inputs.

This is meant to be used in setup function with in_types which is used to get maximum number of inputs.

inline virtual int min_outputs()

Get minimum number of outputs.

This is meant to be used in setup function with out_types which is used to get max number of outputs.

inline virtual vector<dtypes> in_types()

Get input dtypes.

Last in_type will be used repeatedly if size of in_types is smaller than size of inputs

inline virtual vector<dtypes> out_types()

Get output dtypes.

Last out_type will be used repeatedly if size of out_types is smaller than size of outputs

inline virtual string name()

Get function name in string.

inline virtual vector<string> allowed_array_classes()

Get array classes that are allowed to be specified by Context.

inline virtual bool grad_depends_output_data(int i, int o) const

Dependency flag for checking if in-grad depends on out-data.

Checking if i-th input’ gradient computation requires o-th output’s data or not.

Note

If any of inputs requires an output variable data when computing its gradient, this function must be overridden to return appropriate boolean value. Otherwise, backward computation will be incorrect.

Parameters:
  • i[in] Input variable index.

  • o[in] Output variable index.

template<typename T>
class IFFT : public nbla::BaseFunction<int, bool>
#include <ifft.hpp>

Todo:

Write doc.

Inputs:

Outputs:

Public Functions

inline virtual shared_ptr<Function> copy() const

Copy another instance of Function with the same context.

inline virtual int min_inputs()

Get minimum number of inputs.

This is meant to be used in setup function with in_types which is used to get maximum number of inputs.

inline virtual int min_outputs()

Get minimum number of outputs.

This is meant to be used in setup function with out_types which is used to get max number of outputs.

inline virtual vector<dtypes> in_types()

Get input dtypes.

Last in_type will be used repeatedly if size of in_types is smaller than size of inputs

inline virtual vector<dtypes> out_types()

Get output dtypes.

Last out_type will be used repeatedly if size of out_types is smaller than size of outputs

inline virtual vector<string> allowed_array_classes()

Get array classes that are allowed to be specified by Context.

inline virtual string name()

Get function name in string.

inline virtual bool grad_depends_output_data(int i, int o) const

Dependency flag for checking if in-grad depends on out-data.

Checking if i-th input’ gradient computation requires o-th output’s data or not.

Note

If any of inputs requires an output variable data when computing its gradient, this function must be overridden to return appropriate boolean value. Otherwise, backward computation will be incorrect.

Parameters:
  • i[in] Input variable index.

  • o[in] Output variable index.

template<typename T>
class ImageAugmentation : public nbla::BaseFunction<const vector<int>&, const vector<int>&, float, float, float, float, float, bool, bool, float, bool, float, float, bool, float, int>
#include <image_augmentation.hpp>

ImageAugmentation randomly alters the input image.

Inputs:

  • N-D array of three or more dimensions

Outputs:

  • N-D array.

Template Parameters:

T – Data type for computation and score variable.

Param shape:

The output image data size.

Param pad:

Border padding values for each spatial axis (height and width). Padding will be added both sides of the dimension.

Param min_scale:

The minimum scale ratio when randomly scaling the image. For example, to scale down to 0.8 times the size of the original image, specify “0.8”. To not apply random scaling, set both min_scale and max_scale to “1.0”.

Param max_scale:

The maximum scale ratio when randomly scaling the image. For example, to scale down to 2 times the size of the original image, specify “2.0”.

Param angle:

The rotation angle range in radians when randomly rotating the image. The image is randomly rotated in the -Angle to +Angle range. For example, to rotate in a +-15 degree range, specify “0.26” (15 degrees/360 degrees * 2PI). To not apply random rotation, specify “0.0”.

Param aspect_ratio:

The aspect ratio range when randomly deforming the image. For example, to deform aspect ratio of image from 1:1.3 to 1.3:1, specify “1.3”. To not apply random deforming, specify “1.0”.

Param distortion:

The distortion range when randomly distorting the image. To not apply distortion, specify “0.0”.

Param flip_lr:

Whether to randomly flip the image horizontally at 50% probability.

Param flip_ud:

Whether to randomly flip the image vertically at 50% probability.

Param brightness:

The range of values to randomly add to the brightness. A random value in the -Brightness to +Brightness range is added to the brightness. For example, to vary the brightness in the -0.05 to +0.05 range, specify “0.05”. To not apply random addition to brightness, specify “0.0”.

Param brightness_each:

Whether to apply the random addition to brightness (as specified by brightness) to each color channel. True: brightness is added based on a different random number for each channel. False: brightness is added based on a random number common to all channels.

Param contrast:

The range in which to randomly very the image contrast. The contrast is varied in the 1/Contrast times to Contrast times range. The output brightness is equal to (input - contrast_center) * contrast + contrast_center. For example, to vary the contrast in the 0.91 times to 1.1 times range, specify “1.1”. To not apply random contrast variation, specify “1.0”.

Param contrast_center:

Intensity center used for applying contrast.

Param contrast_each:

Whether to apply the random contrast variation (as specified by contrast) to each color channel. True: contrast is varied based on a different random number for each channel. False: contrast is varied based on a random number common to all channels.

Param noise:

Sigma of normal random number to be added.

Param seed:

Random seed.

Public Functions

inline virtual shared_ptr<Function> copy() const

Copy another instance of Function with the same context.

inline virtual vector<dtypes> in_types()

Get input dtypes.

Last in_type will be used repeatedly if size of in_types is smaller than size of inputs

inline virtual vector<dtypes> out_types()

Get output dtypes.

Last out_type will be used repeatedly if size of out_types is smaller than size of outputs

inline virtual int min_inputs()

Get minimum number of inputs.

This is meant to be used in setup function with in_types which is used to get maximum number of inputs.

inline virtual int min_outputs()

Get minimum number of outputs.

This is meant to be used in setup function with out_types which is used to get max number of outputs.

inline virtual string name()

Get function name in string.

inline virtual vector<string> allowed_array_classes()

Get array classes that are allowed to be specified by Context.

inline virtual bool need_setup_recompute(int o) const

A flag for checking if setup_recompute() is needed.

Checking if o-th output’ data requires setup_recompute().

Note

setup_recompute() will skipped during forward execution if none of outputs requires setup_recompute().

Parameters:

o[in] Output variable index.

inline virtual bool grad_depends_output_data(int i, int o) const

Dependency flag for checking if in-grad depends on out-data.

Checking if i-th input’ gradient computation requires o-th output’s data or not.

Note

If any of inputs requires an output variable data when computing its gradient, this function must be overridden to return appropriate boolean value. Otherwise, backward computation will be incorrect.

Parameters:
  • i[in] Input variable index.

  • o[in] Output variable index.

template<typename T, typename T1>
class INQAffine : public nbla::BaseFunction<int, int, const vector<int>&, const string&, int>
#include <inq_affine.hpp>

This function implements an INQ affine layer.

During training, the weights are sequentially quantized to power-of-two values, which allows the training of a multiplierless network.

Using `inq_iterations`, one can specify after how many forward passes half of the learnable weights are fixed and quantized to powers-of-two. After reaching the last value in `inq_iterations`, all weights are fixed.

Please note that the weights are quantized in the forward pass. Therefore, in order to make sure that we only have power-of-two values, one needs to do a final call to `forward` as the weights might have been updated by the solver.

For more details, please refer to the reference.

Reference: Zhou A, Yao A, Guo Y, Xu L, Chen Y. Incremental network quantization: Towards lossless CNNs with low-precision weights. https://arxiv.org/abs/1702.03044

Inputs ( \(B\) is base_axis):

  • Input N-D array with shape ( \(M_0 \times ... \times M_{B-1} \times D_B \times ... \times D_N\)). Dimensions before and after base_axis are flattened as if it is a matrix.

  • Weight matrix with shape ( \((D_B \times ... \times D_N) \times L\))

  • Indicator matrix with shape ( \((D_B \times ... \times D_N) \times L\)) where `0` indicates learnable weights and `1` indicates fixed weights

  • (optional) Bias vector ( \(L\))

Outputs:

  • \((B + 1)\)-D array. ( \( M_0 \times ... \times M_{B-1} \times L \))

Template Parameters:

T – Data type for computation.

Param base_axis:

Base axis of BinaryConnectAffine operation. Dimensions up to base_axis is treated as sample dimension.

Param num_bits:

Number of bits per weight. Needs to be >= 2.

Param inq_iterations:

Vector of integer values which give after how many forward passes we fix 50% of the learnable weights.

Param selection_algorithm:

Chooses algorithm for selection of weights that we want to fix (“largest_abs” … fix weights with largest absolute value, “random” … fix all learnable weights randomly with a probability of 50%)

Param seed:

Random seed

Public Functions

inline virtual shared_ptr<Function> copy() const

Copy another instance of Function with the same context.

inline virtual vector<dtypes> in_types()

Get input dtypes.

Last in_type will be used repeatedly if size of in_types is smaller than size of inputs

inline virtual vector<dtypes> out_types()

Get output dtypes.

Last out_type will be used repeatedly if size of out_types is smaller than size of outputs

inline virtual int min_inputs()

Get minimum number of inputs.

This is meant to be used in setup function with in_types which is used to get maximum number of inputs.

inline virtual int min_outputs()

Get minimum number of outputs.

This is meant to be used in setup function with out_types which is used to get max number of outputs.

inline virtual string name()

Get function name in string.

inline virtual vector<string> allowed_array_classes()

Get array classes that are allowed to be specified by Context.

inline virtual bool grad_depends_output_data(int i, int o) const

Dependency flag for checking if in-grad depends on out-data.

Checking if i-th input’ gradient computation requires o-th output’s data or not.

Note

If any of inputs requires an output variable data when computing its gradient, this function must be overridden to return appropriate boolean value. Otherwise, backward computation will be incorrect.

Parameters:
  • i[in] Input variable index.

  • o[in] Output variable index.

template<typename T, typename T1>
class INQConvolution : public nbla::BaseFunction<int, const vector<int>&, const vector<int>&, const vector<int>&, int, int, const vector<int>&, const string&, int>
#include <inq_convolution.hpp>

This function implements an INQ convolution layer.

During training, the weights are sequentially quantized to power-of-two values, which allows the training of a multiplierless network.

Using `inq_iterations`, one can specify after how many forward passes half of the learnable weights are fixed and quantized to powers-of-two. After reaching the last value in `inq_iterations`, all weights are fixed.

Please note that the weights are quantized in the forward pass. Therefore, in order to make sure that we only have power-of-two values, one needs to do a final call to `forward` as the weights might have been updated by the solver.

For more details, please refer to the reference.

Reference: Zhou A, Yao A, Guo Y, Xu L, Chen Y. Incremental network quantization: Towards lossless CNNs with low-precision weights. https://arxiv.org/abs/1702.03044

Inputs ( \(B\) is base_axis):

  • Input \((B + 1 + N)\)-D array ( \(M_1 \times ... \times M_B \times C \times L_1 \times ... \times L_N\)).

  • Weight \((2 + N)\)-D array ( \(C' \times C \times K_1 \times ... \times K_N\)).

  • Indicator \((2 + N)\)-D array with shape ( \(C' \times C \times K_1 \times ... \times K_N\)) where `0` indicates learnable weights and `1` indicates fixed weights

  • (optional) Bias vector ( \(C'\)).

Outputs:

  • \((B + 1 + N)\)-D array ( \( M_1 \times ... \times M_B \times C' \times L'_1 \times ... \times L'_N \)).

Template Parameters:

T – Data type for computation.

Param base_axis:

Base axis of Convolution operation. Dimensions up to base_axis is treated as sample dimension.

Param pad:

Padding sizes for dimensions.

Param stride:

Stride sizes for dimensions.

Param dilation:

Dilation sizes for dimensions.

Param group:

Number of groups of channels. This makes connections across channels sparser by grouping connections along map direction.

Param num_bits:

Number of bits per weight. Needs to be >= 2.

Param inq_iterations:

Vector of integer values which give after how many forward passes we fix 50% of the learnable weights.

Param selection_algorithm:

Chooses algorithm for selection of weights that we want to fix (“largest_abs” … fix weights with largest absolute value, “random” … fix all learnable weights randomly with a probability of 50%)

Param seed:

Random seed

Public Functions

inline virtual shared_ptr<Function> copy() const

Copy another instance of Function with the same context.

inline virtual vector<dtypes> in_types()

Get input dtypes.

Last in_type will be used repeatedly if size of in_types is smaller than size of inputs

inline virtual vector<dtypes> out_types()

Get output dtypes.

Last out_type will be used repeatedly if size of out_types is smaller than size of outputs

inline virtual int min_inputs()

Get minimum number of inputs.

This is meant to be used in setup function with in_types which is used to get maximum number of inputs.

inline virtual int min_outputs()

Get minimum number of outputs.

This is meant to be used in setup function with out_types which is used to get max number of outputs.

inline virtual string name()

Get function name in string.

inline virtual vector<string> allowed_array_classes()

Get array classes that are allowed to be specified by Context.

inline virtual bool grad_depends_output_data(int i, int o) const

Dependency flag for checking if in-grad depends on out-data.

Checking if i-th input’ gradient computation requires o-th output’s data or not.

Note

If any of inputs requires an output variable data when computing its gradient, this function must be overridden to return appropriate boolean value. Otherwise, backward computation will be incorrect.

Parameters:
  • i[in] Input variable index.

  • o[in] Output variable index.

template<typename T>
class InstanceNormalization : public nbla::BaseFunction<int, const vector<int>&, float, bool, bool>
#include <instance_normalization.hpp>

Todo:

Write doc.

Inputs:

Outputs:

Public Functions

inline virtual shared_ptr<Function> copy() const

Copy another instance of Function with the same context.

inline virtual int min_inputs()

Get minimum number of inputs.

This is meant to be used in setup function with in_types which is used to get maximum number of inputs.

inline virtual int min_outputs()

Get minimum number of outputs.

This is meant to be used in setup function with out_types which is used to get max number of outputs.

inline virtual vector<dtypes> in_types()

Get input dtypes.

Last in_type will be used repeatedly if size of in_types is smaller than size of inputs

inline virtual vector<dtypes> out_types()

Get output dtypes.

Last out_type will be used repeatedly if size of out_types is smaller than size of outputs

inline virtual vector<string> allowed_array_classes()

Get array classes that are allowed to be specified by Context.

inline virtual string name()

Get function name in string.

inline virtual bool grad_depends_output_data(int i, int o) const

Dependency flag for checking if in-grad depends on out-data.

Checking if i-th input’ gradient computation requires o-th output’s data or not.

Note

If any of inputs requires an output variable data when computing its gradient, this function must be overridden to return appropriate boolean value. Otherwise, backward computation will be incorrect.

Parameters:
  • i[in] Input variable index.

  • o[in] Output variable index.

template<typename T>
class Interpolate : public nbla::BaseFunction<const vector<int>&, const string&, bool, bool, bool, bool>
#include <interpolate.hpp>

Resize an ND array with interpolation.

The last output_size.size() dimensions of the input are considered as the spatial dimensions.

Inputs:

  • x: N-D array with an arbitrary number of dimensions.

Outputs:

  • y: N-D array. The shape are the same as the input except that the last dimensions are replaced with the output_size.

Template Parameters:

T – Data type for computation.

Param output_size:

The Spatial sizes resized to.

Param mode:

Interpolation mode chosen from linear or nearest.

Param align_corners:

If true, the corner pixels are aligned to preserve the values of the input corner pixels.

Param half_pixel:

If true, in the coordinate transformation, 0.5 is added to the output coordinate and 0.5 is subtracted from the input coordinate after scaling. Default is `False`. This option is only applicable to the `mode` being linear.

Param half_pixel_for_nn:

This is a special argument to support the backward-compatibility of the nearest neighbor interpolation. Default is `False`.

Public Functions

inline virtual shared_ptr<Function> copy() const

Copy another instance of Function with the same context.

inline virtual int min_inputs()

Get minimum number of inputs.

This is meant to be used in setup function with in_types which is used to get maximum number of inputs.

inline virtual int min_outputs()

Get minimum number of outputs.

This is meant to be used in setup function with out_types which is used to get max number of outputs.

inline virtual vector<dtypes> in_types()

Get input dtypes.

Last in_type will be used repeatedly if size of in_types is smaller than size of inputs

inline virtual vector<dtypes> out_types()

Get output dtypes.

Last out_type will be used repeatedly if size of out_types is smaller than size of outputs

inline virtual vector<string> allowed_array_classes()

Get array classes that are allowed to be specified by Context.

inline virtual string name()

Get function name in string.

inline virtual bool grad_depends_output_data(int i, int o) const

Dependency flag for checking if in-grad depends on out-data.

Checking if i-th input’ gradient computation requires o-th output’s data or not.

Note

If any of inputs requires an output variable data when computing its gradient, this function must be overridden to return appropriate boolean value. Otherwise, backward computation will be incorrect.

Parameters:
  • i[in] Input variable index.

  • o[in] Output variable index.

template<typename T>
class STFT : public nbla::BaseFunction<int, int, int, const string&, bool, const string&, bool>
#include <stft.hpp>

Todo:

Write doc.

Inputs:

Outputs:

Public Functions

inline virtual shared_ptr<Function> copy() const

Copy another instance of Function with the same context.

inline virtual int min_inputs()

Get minimum number of inputs.

This is meant to be used in setup function with in_types which is used to get maximum number of inputs.

inline virtual int min_outputs()

Get minimum number of outputs.

This is meant to be used in setup function with out_types which is used to get max number of outputs.

inline virtual vector<dtypes> in_types()

Get input dtypes.

Last in_type will be used repeatedly if size of in_types is smaller than size of inputs

inline virtual vector<dtypes> out_types()

Get output dtypes.

Last out_type will be used repeatedly if size of out_types is smaller than size of outputs

inline virtual vector<string> allowed_array_classes()

Get array classes that are allowed to be specified by Context.

inline virtual string name()

Get function name in string.

inline virtual bool grad_depends_output_data(int i, int o) const

Dependency flag for checking if in-grad depends on out-data.

Checking if i-th input’ gradient computation requires o-th output’s data or not.

Note

If any of inputs requires an output variable data when computing its gradient, this function must be overridden to return appropriate boolean value. Otherwise, backward computation will be incorrect.

Parameters:
  • i[in] Input variable index.

  • o[in] Output variable index.

template<typename T>
class ISTFT : public nbla::BaseFunction<int, int, int, const string&, bool, const string&, bool>
#include <istft.hpp>

Todo:

Write doc.

Inputs:

Outputs:

Public Functions

inline virtual shared_ptr<Function> copy() const

Copy another instance of Function with the same context.

inline virtual int min_inputs()

Get minimum number of inputs.

This is meant to be used in setup function with in_types which is used to get maximum number of inputs.

inline virtual int min_outputs()

Get minimum number of outputs.

This is meant to be used in setup function with out_types which is used to get max number of outputs.

inline virtual vector<dtypes> in_types()

Get input dtypes.

Last in_type will be used repeatedly if size of in_types is smaller than size of inputs

inline virtual vector<dtypes> out_types()

Get output dtypes.

Last out_type will be used repeatedly if size of out_types is smaller than size of outputs

inline virtual vector<string> allowed_array_classes()

Get array classes that are allowed to be specified by Context.

inline virtual string name()

Get function name in string.

inline virtual bool grad_depends_output_data(int i, int o) const

Dependency flag for checking if in-grad depends on out-data.

Checking if i-th input’ gradient computation requires o-th output’s data or not.

Note

If any of inputs requires an output variable data when computing its gradient, this function must be overridden to return appropriate boolean value. Otherwise, backward computation will be incorrect.

Parameters:
  • i[in] Input variable index.

  • o[in] Output variable index.

template<typename T>
class KLMultinomial : public nbla::BaseFunction<int>
#include <kl_multinomial.hpp>

Kullback Leibler Divergence for Multinomial Distributions.

Inputs:

  • categorical distribution p

  • categorical distribution q

Outputs:

  • kullback leibler divergence k(p, q).

Template Parameters:

T – Data type for computation.

Public Functions

inline virtual shared_ptr<Function> copy() const

Copy another instance of Function with the same context.

inline virtual vector<dtypes> in_types()

Get input dtypes.

Last in_type will be used repeatedly if size of in_types is smaller than size of inputs

inline virtual vector<dtypes> out_types()

Get output dtypes.

Last out_type will be used repeatedly if size of out_types is smaller than size of outputs

inline virtual int min_inputs()

Get minimum number of inputs.

This is meant to be used in setup function with in_types which is used to get maximum number of inputs.

inline virtual int min_outputs()

Get minimum number of outputs.

This is meant to be used in setup function with out_types which is used to get max number of outputs.

inline virtual string name()

Get function name in string.

inline virtual vector<string> allowed_array_classes()

Get array classes that are allowed to be specified by Context.

inline virtual bool grad_depends_output_data(int i, int o) const

Dependency flag for checking if in-grad depends on out-data.

Checking if i-th input’ gradient computation requires o-th output’s data or not.

Note

If any of inputs requires an output variable data when computing its gradient, this function must be overridden to return appropriate boolean value. Otherwise, backward computation will be incorrect.

Parameters:
  • i[in] Input variable index.

  • o[in] Output variable index.

template<typename T>
class LayerNormalization : public nbla::BaseFunction<const vector<int>&, float, bool, bool>
#include <layer_normalization.hpp>

Todo:

Write doc.

Inputs:

Outputs:

Public Functions

inline virtual shared_ptr<Function> copy() const

Copy another instance of Function with the same context.

inline virtual int min_inputs()

Get minimum number of inputs.

This is meant to be used in setup function with in_types which is used to get maximum number of inputs.

inline virtual int min_outputs()

Get minimum number of outputs.

This is meant to be used in setup function with out_types which is used to get max number of outputs.

inline virtual vector<dtypes> in_types()

Get input dtypes.

Last in_type will be used repeatedly if size of in_types is smaller than size of inputs

inline virtual vector<dtypes> out_types()

Get output dtypes.

Last out_type will be used repeatedly if size of out_types is smaller than size of outputs

inline virtual vector<string> allowed_array_classes()

Get array classes that are allowed to be specified by Context.

inline virtual string name()

Get function name in string.

inline virtual bool grad_depends_output_data(int i, int o) const

Dependency flag for checking if in-grad depends on out-data.

Checking if i-th input’ gradient computation requires o-th output’s data or not.

Note

If any of inputs requires an output variable data when computing its gradient, this function must be overridden to return appropriate boolean value. Otherwise, backward computation will be incorrect.

Parameters:
  • i[in] Input variable index.

  • o[in] Output variable index.

template<typename T>
class LeakyReLU : public nbla::BaseFunction<float, bool>
#include <leaky_relu.hpp>

Leaky Rectified Linear Unit (LeakyReLU) defined as.

\[\begin{split} y_i= \left\{ \begin{array}{ll} x_i & \text{if } x_i > 0 \\ alpha * x_i & otherwise \end{array}\right.. \end{split}\]

Inputs:

  • N-D array.

Outputs:

  • N-D array.

Template Parameters:

T – Data type for computation.

Param alpha:

Leakage parameter.

Param inplace:

This option is obsolete and ignored. Output is never in-placed with input.

Public Functions

inline virtual shared_ptr<Function> copy() const

Copy another instance of Function with the same context.

inline virtual int min_inputs()

Get minimum number of inputs.

This is meant to be used in setup function with in_types which is used to get maximum number of inputs.

inline virtual int min_outputs()

Get minimum number of outputs.

This is meant to be used in setup function with out_types which is used to get max number of outputs.

inline virtual vector<dtypes> in_types()

Get input dtypes.

Last in_type will be used repeatedly if size of in_types is smaller than size of inputs

inline virtual vector<dtypes> out_types()

Get output dtypes.

Last out_type will be used repeatedly if size of out_types is smaller than size of outputs

inline virtual string name()

Get function name in string.

inline virtual vector<string> allowed_array_classes()

Get array classes that are allowed to be specified by Context.

inline virtual bool grad_depends_output_data(int i, int o) const

Dependency flag for checking if in-grad depends on out-data.

Checking if i-th input’ gradient computation requires o-th output’s data or not.

Note

If any of inputs requires an output variable data when computing its gradient, this function must be overridden to return appropriate boolean value. Otherwise, backward computation will be incorrect.

Parameters:
  • i[in] Input variable index.

  • o[in] Output variable index.

template<typename T>
class Linspace : public nbla::BaseFunction<float, float, int>
#include <linspace.hpp>

Generate a one-dimensional vector/tensor of size `num` whose values are evenly spaced from `start` to `end`, inclusive.

Inputs:

  • none

Outputs:

  • 1-D array.

Template Parameters:

T – Data type for computation.

Param start:

Start value.

Param stop:

End value.

Param num:

Size of the constructed vector/tensor.

Public Functions

inline virtual shared_ptr<Function> copy() const

Copy another instance of Function with the same context.

inline virtual int min_inputs()

Get minimum number of inputs.

This is meant to be used in setup function with in_types which is used to get maximum number of inputs.

inline virtual int min_outputs()

Get minimum number of outputs.

This is meant to be used in setup function with out_types which is used to get max number of outputs.

inline virtual vector<dtypes> in_types()

Get input dtypes.

Last in_type will be used repeatedly if size of in_types is smaller than size of inputs

inline virtual vector<dtypes> out_types()

Get output dtypes.

Last out_type will be used repeatedly if size of out_types is smaller than size of outputs

inline virtual vector<string> allowed_array_classes()

Get array classes that are allowed to be specified by Context.

inline virtual string name()

Get function name in string.

inline virtual bool grad_depends_output_data(int i, int o) const

Dependency flag for checking if in-grad depends on out-data.

Checking if i-th input’ gradient computation requires o-th output’s data or not.

Note

If any of inputs requires an output variable data when computing its gradient, this function must be overridden to return appropriate boolean value. Otherwise, backward computation will be incorrect.

Parameters:
  • i[in] Input variable index.

  • o[in] Output variable index.

template<typename T>
class LogSoftmax : public nbla::BaseFunction<int>
#include <log_softmax.hpp>

Softmax normalization defined as.

\[ y_i = x_i - \log(\sum_j \exp(x_j)) \]
along dimension specified by axis.

Inputs:

  • N-D array.

Outputs:

  • N-D array with the same shape as input.

Template Parameters:

T – Data type for computation.

Param axis:

Axis normalization is taken.

Public Functions

inline virtual shared_ptr<Function> copy() const

Copy another instance of Function with the same context.

inline virtual int min_inputs()

Get minimum number of inputs.

This is meant to be used in setup function with in_types which is used to get maximum number of inputs.

inline virtual int min_outputs()

Get minimum number of outputs.

This is meant to be used in setup function with out_types which is used to get max number of outputs.

inline virtual vector<dtypes> in_types()

Get input dtypes.

Last in_type will be used repeatedly if size of in_types is smaller than size of inputs

inline virtual vector<dtypes> out_types()

Get output dtypes.

Last out_type will be used repeatedly if size of out_types is smaller than size of outputs

inline virtual vector<string> allowed_array_classes()

Get array classes that are allowed to be specified by Context.

inline virtual string name()

Get function name in string.

inline virtual bool grad_depends_output_data(int i, int o) const

Dependency flag for checking if in-grad depends on out-data.

Checking if i-th input’ gradient computation requires o-th output’s data or not.

Note

If any of inputs requires an output variable data when computing its gradient, this function must be overridden to return appropriate boolean value. Otherwise, backward computation will be incorrect.

Parameters:
  • i[in] Input variable index.

  • o[in] Output variable index.

template<typename T>
class LSTM : public nbla::BaseFunction<int, float, bool, bool>
#include <lstm.hpp>

Todo:

Write doc.

Inputs:

Outputs:

Public Functions

inline virtual shared_ptr<Function> copy() const

Copy another instance of Function with the same context.

inline virtual int min_inputs()

Get minimum number of inputs.

This is meant to be used in setup function with in_types which is used to get maximum number of inputs.

inline virtual int min_outputs()

Get minimum number of outputs.

This is meant to be used in setup function with out_types which is used to get max number of outputs.

inline virtual vector<dtypes> in_types()

Get input dtypes.

Last in_type will be used repeatedly if size of in_types is smaller than size of inputs

inline virtual vector<dtypes> out_types()

Get output dtypes.

Last out_type will be used repeatedly if size of out_types is smaller than size of outputs

inline virtual vector<string> allowed_array_classes()

Get array classes that are allowed to be specified by Context.

inline virtual string name()

Get function name in string.

inline virtual bool grad_depends_output_data(int i, int o) const

Dependency flag for checking if in-grad depends on out-data.

Checking if i-th input’ gradient computation requires o-th output’s data or not.

Note

If any of inputs requires an output variable data when computing its gradient, this function must be overridden to return appropriate boolean value. Otherwise, backward computation will be incorrect.

Parameters:
  • i[in] Input variable index.

  • o[in] Output variable index.

template<typename T>
class MatrixDiag : public nbla::BaseFunction<>
#include <matrix_diag.hpp>

Todo:

PLACE HERE FUNCTION DOCUMENTATION.

Public Functions

inline virtual shared_ptr<Function> copy() const

Copy another instance of Function with the same context.

inline virtual vector<dtypes> in_types()

Get input dtypes.

Last in_type will be used repeatedly if size of in_types is smaller than size of inputs

inline virtual vector<dtypes> out_types()

Get output dtypes.

Last out_type will be used repeatedly if size of out_types is smaller than size of outputs

inline virtual int min_inputs()

Get minimum number of inputs.

This is meant to be used in setup function with in_types which is used to get maximum number of inputs.

inline virtual int min_outputs()

Get minimum number of outputs.

This is meant to be used in setup function with out_types which is used to get max number of outputs.

inline virtual string name()

Get function name in string.

inline virtual vector<string> allowed_array_classes()

Get array classes that are allowed to be specified by Context.

inline virtual bool grad_depends_output_data(int i, int o) const

Dependency flag for checking if in-grad depends on out-data.

Checking if i-th input’ gradient computation requires o-th output’s data or not.

Note

If any of inputs requires an output variable data when computing its gradient, this function must be overridden to return appropriate boolean value. Otherwise, backward computation will be incorrect.

Parameters:
  • i[in] Input variable index.

  • o[in] Output variable index.

template<typename T>
class MatrixDiagPart : public nbla::BaseFunction<>
#include <matrix_diag_part.hpp>

Todo:

PLACE HERE FUNCTION DOCUMENTATION.

Public Functions

inline virtual shared_ptr<Function> copy() const

Copy another instance of Function with the same context.

inline virtual vector<dtypes> in_types()

Get input dtypes.

Last in_type will be used repeatedly if size of in_types is smaller than size of inputs

inline virtual vector<dtypes> out_types()

Get output dtypes.

Last out_type will be used repeatedly if size of out_types is smaller than size of outputs

inline virtual int min_inputs()

Get minimum number of inputs.

This is meant to be used in setup function with in_types which is used to get maximum number of inputs.

inline virtual int min_outputs()

Get minimum number of outputs.

This is meant to be used in setup function with out_types which is used to get max number of outputs.

inline virtual string name()

Get function name in string.

inline virtual vector<string> allowed_array_classes()

Get array classes that are allowed to be specified by Context.

inline virtual bool grad_depends_output_data(int i, int o) const

Dependency flag for checking if in-grad depends on out-data.

Checking if i-th input’ gradient computation requires o-th output’s data or not.

Note

If any of inputs requires an output variable data when computing its gradient, this function must be overridden to return appropriate boolean value. Otherwise, backward computation will be incorrect.

Parameters:
  • i[in] Input variable index.

  • o[in] Output variable index.

template<typename T>
class Max : public nbla::Sum<T>
#include <max.hpp>

Reduction along axes with max operation.

Template Parameters:

T – Data type for computation.

Param axes:

A list of axes to be reduced.

Param keep_dims:

Flag whether the reduced axes are kept.

Subclassed by nbla::Min< T >

Public Functions

inline virtual shared_ptr<Function> copy() const

Copy another instance of Function with the same context.

inline virtual string name()

Get function name in string.

inline virtual bool grad_depends_output_data(int i, int o) const

Dependency flag for checking if in-grad depends on out-data.

Checking if i-th input’ gradient computation requires o-th output’s data or not.

Note

If any of inputs requires an output variable data when computing its gradient, this function must be overridden to return appropriate boolean value. Otherwise, backward computation will be incorrect.

Parameters:
  • i[in] Input variable index.

  • o[in] Output variable index.

inline virtual bool auto_grad_depends_output_data(int i, int o) const

Dependency flag to check if in-grad depends on out-data for auto grad.

Checking if i-th input’s gradient computation requires o-th output’s data or not when auto grad (nnabla.grad). This method is refered in auto-forward mode to clear memory consumption dynamically.

Note

If the grad dependencies of the output are different between Function::backward and auto grad, this function must be overridden to return appropriate boolean value. Otherwise, auto grad computation will be incorrect.

Parameters:
  • i[in] Input variable index.

  • o[in] Output variable index.

template<typename T>
class MaxPooling : public nbla::BasePooling<T, const vector<int>&, const vector<int>&, bool, const vector<int>&, bool>
#include <max_pooling.hpp>

Max pooling operator.

Inputs:

  • N-D array that has more than two dimensions.

Outputs:

  • N-D array.

Note

Only 2D pooling supported so far.

Template Parameters:

T – Data type for computation.

Param kernel:

Shapes of kernel.

Param stride:

Subsampling factors of pooling.

Param ignore_border:

If false, a kernel overlapping border is also considered for output unlike convolution.

Param pad:

Border padding values of dimensions. Padding will be added both sides of the dimension.

Public Functions

inline virtual shared_ptr<Function> copy() const

Copy another instance of Function with the same context.

inline virtual string name()

Get function name in string.

inline virtual bool grad_depends_output_data(int i, int o) const

Dependency flag for checking if in-grad depends on out-data.

Checking if i-th input’ gradient computation requires o-th output’s data or not.

Note

If any of inputs requires an output variable data when computing its gradient, this function must be overridden to return appropriate boolean value. Otherwise, backward computation will be incorrect.

Parameters:
  • i[in] Input variable index.

  • o[in] Output variable index.

template<typename T>
class MaxPoolingBackward : public nbla::BaseFunction<const vector<int>&, const vector<int>&, bool, const vector<int>&, bool>
#include <max_pooling_backward.hpp>

Todo:

Write doc.

Inputs:

Outputs:

Public Functions

inline virtual shared_ptr<Function> copy() const

Copy another instance of Function with the same context.

inline virtual int min_inputs()

Get minimum number of inputs.

This is meant to be used in setup function with in_types which is used to get maximum number of inputs.

inline virtual int min_outputs()

Get minimum number of outputs.

This is meant to be used in setup function with out_types which is used to get max number of outputs.

inline virtual vector<dtypes> in_types()

Get input dtypes.

Last in_type will be used repeatedly if size of in_types is smaller than size of inputs

inline virtual vector<dtypes> out_types()

Get output dtypes.

Last out_type will be used repeatedly if size of out_types is smaller than size of outputs

inline virtual vector<string> allowed_array_classes()

Get array classes that are allowed to be specified by Context.

inline virtual string name()

Get function name in string.

inline virtual bool grad_depends_output_data(int i, int o) const

Dependency flag for checking if in-grad depends on out-data.

Checking if i-th input’ gradient computation requires o-th output’s data or not.

Note

If any of inputs requires an output variable data when computing its gradient, this function must be overridden to return appropriate boolean value. Otherwise, backward computation will be incorrect.

Parameters:
  • i[in] Input variable index.

  • o[in] Output variable index.

template<typename T>
class Mean : public nbla::Sum<T>
#include <mean.hpp>

Reduction along axes with mean operation.

Template Parameters:

T – Data type for computation.

Param axes:

A list of axes to be reduced.

Param keep_dims:

Flag whether the reduced axes are kept.

Public Functions

inline virtual shared_ptr<Function> copy() const

Copy another instance of Function with the same context.

inline virtual string name()

Get function name in string.

inline virtual bool grad_depends_output_data(int i, int o) const

Dependency flag for checking if in-grad depends on out-data.

Checking if i-th input’ gradient computation requires o-th output’s data or not.

Note

If any of inputs requires an output variable data when computing its gradient, this function must be overridden to return appropriate boolean value. Otherwise, backward computation will be incorrect.

Parameters:
  • i[in] Input variable index.

  • o[in] Output variable index.

template<typename T>
class MeanSubtraction : public nbla::BaseFunction<int, bool>
#include <mean_subtraction.hpp>

MeanSubtraction normalizes input to mean 0.

Using this as a preprocess has the effect of improving accuracy in image classification and the like.

At training time defined as

\[\begin{split} \begin{array}{lcl} \mu &=& \frac{1}{M} \sum x_i \\ rm &=& ({\rm decay\_rate}) rm + (1 - {\rm decay\_rate}) \mu \\ y_i &=& x_i - rm \end{array} \end{split}\]

At validation time defined as

\[ y_i = x_i - rm \]

Inputs:

  • N-D array of input.

  • N-D array of running mean (modified during forward execution).

  • Scalar of num of iteration of running mean (modified during forward execution).

Outputs:

  • N-D array.

Note

The backward performs an approximated differentiation that takes into account only the latest mini-batch.

Template Parameters:

T – Data type for computation.

Param base_axis:

Base axis of mean subtraction operation. Dimensions up to base_axis is treated as sample dimension.

Param update_running_mean:

Update running mean during forward execution.

Public Functions

inline virtual shared_ptr<Function> copy() const

Copy another instance of Function with the same context.

inline virtual vector<dtypes> in_types()

Get input dtypes.

Last in_type will be used repeatedly if size of in_types is smaller than size of inputs

inline virtual vector<dtypes> out_types()

Get output dtypes.

Last out_type will be used repeatedly if size of out_types is smaller than size of outputs

inline virtual int min_inputs()

Get minimum number of inputs.

This is meant to be used in setup function with in_types which is used to get maximum number of inputs.

inline virtual int min_outputs()

Get minimum number of outputs.

This is meant to be used in setup function with out_types which is used to get max number of outputs.

inline virtual string name()

Get function name in string.

inline virtual vector<string> allowed_array_classes()

Get array classes that are allowed to be specified by Context.

inline virtual bool grad_depends_output_data(int i, int o) const

Dependency flag for checking if in-grad depends on out-data.

Checking if i-th input’ gradient computation requires o-th output’s data or not.

Note

If any of inputs requires an output variable data when computing its gradient, this function must be overridden to return appropriate boolean value. Otherwise, backward computation will be incorrect.

Parameters:
  • i[in] Input variable index.

  • o[in] Output variable index.

template<typename T>
class Meshgrid : public nbla::BaseFunction<bool>
#include <meshgrid.hpp>

Given N 1-D arrays, this function returns N-D coordinate arrays for vectorized evaluations on an N-D grid.

Inputs:

  • N-D array.

  • N-D array.

Outputs:

  • N-D array.

  • N-D array.

Param ij_indexing:

(bool); If True, it sets matrix (‘ij’) indexing of output. Default is False (Cartesian (‘xy’) indexing).

Public Functions

inline virtual shared_ptr<Function> copy() const

Copy another instance of Function with the same context.

inline virtual int min_inputs()

Get minimum number of inputs.

This is meant to be used in setup function with in_types which is used to get maximum number of inputs.

inline virtual int min_outputs()

Get minimum number of outputs.

This is meant to be used in setup function with out_types which is used to get max number of outputs.

inline virtual vector<dtypes> in_types()

Get input dtypes.

Last in_type will be used repeatedly if size of in_types is smaller than size of inputs

inline virtual vector<dtypes> out_types()

Get output dtypes.

Last out_type will be used repeatedly if size of out_types is smaller than size of outputs

inline virtual vector<string> allowed_array_classes()

Get array classes that are allowed to be specified by Context.

inline virtual string name()

Get function name in string.

inline virtual bool grad_depends_output_data(int i, int o) const

Dependency flag for checking if in-grad depends on out-data.

Checking if i-th input’ gradient computation requires o-th output’s data or not.

Note

If any of inputs requires an output variable data when computing its gradient, this function must be overridden to return appropriate boolean value. Otherwise, backward computation will be incorrect.

Parameters:
  • i[in] Input variable index.

  • o[in] Output variable index.

template<typename T>
class Min : public nbla::Max<T>
#include <min.hpp>

Reduction along axes with min operation.

Template Parameters:

T – Data type for computation.

Param axes:

A list of axes to be reduced.

Param keep_dims:

Flag whether the reduced axes are kept.

Public Functions

inline virtual shared_ptr<Function> copy() const

Copy another instance of Function with the same context.

inline virtual string name()

Get function name in string.

inline virtual bool grad_depends_output_data(int i, int o) const

Dependency flag for checking if in-grad depends on out-data.

Checking if i-th input’ gradient computation requires o-th output’s data or not.

Note

If any of inputs requires an output variable data when computing its gradient, this function must be overridden to return appropriate boolean value. Otherwise, backward computation will be incorrect.

Parameters:
  • i[in] Input variable index.

  • o[in] Output variable index.

inline virtual bool auto_grad_depends_output_data(int i, int o) const

Dependency flag to check if in-grad depends on out-data for auto grad.

Checking if i-th input’s gradient computation requires o-th output’s data or not when auto grad (nnabla.grad). This method is refered in auto-forward mode to clear memory consumption dynamically.

Note

If the grad dependencies of the output are different between Function::backward and auto grad, this function must be overridden to return appropriate boolean value. Otherwise, auto grad computation will be incorrect.

Parameters:
  • i[in] Input variable index.

  • o[in] Output variable index.

template<typename T>
class MinMaxQuantize : public nbla::BaseFunction<float, bool, bool, bool, float>
#include <min_max_quantize.hpp>

MinMaxQuantize quantizes values in integer representation.

Inputs:

  • N-D array of input

  • N-D array of minimum quantization range (modified during forward execution)

  • N-D array of maximum quantization range (modified during forward execution)

  • N-D array of minimum quantization level

  • N-D array of maximum quantization level execution)

Template Parameters:

T – Data type for computation.

Param decay:

Decay rate for the exponential moving average.

Param x_min_max:

Use the min and max of x to compute quantization ranges.

Param ema:

Use the exponential moving average for the min and max quantization. ranges.

Param ste_fine_grained:

Straight Through Estimator is fine-grained or not.

Public Functions

inline virtual shared_ptr<Function> copy() const

Copy another instance of Function with the same context.

inline virtual int min_inputs()

Get minimum number of inputs.

This is meant to be used in setup function with in_types which is used to get maximum number of inputs.

inline virtual int min_outputs()

Get minimum number of outputs.

This is meant to be used in setup function with out_types which is used to get max number of outputs.

inline virtual vector<dtypes> in_types()

Get input dtypes.

Last in_type will be used repeatedly if size of in_types is smaller than size of inputs

inline virtual vector<dtypes> out_types()

Get output dtypes.

Last out_type will be used repeatedly if size of out_types is smaller than size of outputs

inline virtual vector<string> allowed_array_classes()

Get array classes that are allowed to be specified by Context.

inline virtual string name()

Get function name in string.

inline virtual bool grad_depends_output_data(int i, int o) const

Dependency flag for checking if in-grad depends on out-data.

Checking if i-th input’ gradient computation requires o-th output’s data or not.

Note

If any of inputs requires an output variable data when computing its gradient, this function must be overridden to return appropriate boolean value. Otherwise, backward computation will be incorrect.

Parameters:
  • i[in] Input variable index.

  • o[in] Output variable index.

template<typename T>
class Mod2 : public nbla::BaseTransformBinary<>
#include <mod2.hpp>

Element-wise remainder function.

If fmod is True, this operator behaves like numpy.fmod, otherwise it behaves like numpy.mod.

.. math::

y_i = \text{mod} (x_i)

Inputs:

  • x0: A N-D array.

  • x1: A N-D array.

Outputs:

  • y: A N-D array.

Param fmod:

If True, this operator behaves like numpy.fmod, otherwise it behaves like numpy.mod.

Public Functions

inline virtual shared_ptr<Function> copy() const

Copy another instance of Function with the same context.

inline virtual int min_inputs()

Get minimum number of inputs.

This is meant to be used in setup function with in_types which is used to get maximum number of inputs.

inline virtual int min_outputs()

Get minimum number of outputs.

This is meant to be used in setup function with out_types which is used to get max number of outputs.

inline virtual vector<dtypes> in_types()

Get input dtypes.

Last in_type will be used repeatedly if size of in_types is smaller than size of inputs

inline virtual vector<dtypes> out_types()

Get output dtypes.

Last out_type will be used repeatedly if size of out_types is smaller than size of outputs

inline virtual vector<string> allowed_array_classes()

Get array classes that are allowed to be specified by Context.

inline virtual string name()

Get function name in string.

template<typename T>
class MulN : public nbla::BaseFunction<>
#include <mul_n.hpp>

Elementwise multiplication defined as.

\[ y_i = x^{(0)}_i \cdot \cdot \cdot x^{(n-1)}_i. \]

Inputs:

  • N-D arrays.

Outputs:

  • N-D array.

Template Parameters:

T – Data type for computation.

Public Functions

inline virtual shared_ptr<Function> copy() const

Copy another instance of Function with the same context.

inline virtual int min_inputs()

Get minimum number of inputs.

This is meant to be used in setup function with in_types which is used to get maximum number of inputs.

inline virtual int min_outputs()

Get minimum number of outputs.

This is meant to be used in setup function with out_types which is used to get max number of outputs.

inline virtual vector<dtypes> in_types()

Get input dtypes.

Last in_type will be used repeatedly if size of in_types is smaller than size of inputs

inline virtual vector<dtypes> out_types()

Get output dtypes.

Last out_type will be used repeatedly if size of out_types is smaller than size of outputs

inline virtual vector<string> allowed_array_classes()

Get array classes that are allowed to be specified by Context.

inline virtual string name()

Get function name in string.

inline virtual bool grad_depends_output_data(int i, int o) const

Dependency flag for checking if in-grad depends on out-data.

Checking if i-th input’ gradient computation requires o-th output’s data or not.

Note

If any of inputs requires an output variable data when computing its gradient, this function must be overridden to return appropriate boolean value. Otherwise, backward computation will be incorrect.

Parameters:
  • i[in] Input variable index.

  • o[in] Output variable index.

template<typename T>
class NmsDetection2d : public nbla::BaseFunction<float, float, bool>
#include <nms_detection2d.hpp>

Non-Maximum Suppression (NMS) to 2D Object detector output.

The full description of this function can be found at `functions.yaml` or a docstring of Python function `F.nms_detection2d`.

Inputs:

  • N-D array.

Outputs:

  • N-D array.

Template Parameters:

T – Data type for computation.

Param thresh:

Detection score threshold.

Param nms:

IoU threshold for Non-maximum suppression (NMS).

Param nms_per_class:

If true, NMS is applied for each class.

Public Functions

inline virtual shared_ptr<Function> copy() const

Copy another instance of Function with the same context.

inline virtual vector<dtypes> in_types()

Get input dtypes.

Last in_type will be used repeatedly if size of in_types is smaller than size of inputs

inline virtual vector<dtypes> out_types()

Get output dtypes.

Last out_type will be used repeatedly if size of out_types is smaller than size of outputs

inline virtual int min_inputs()

Get minimum number of inputs.

This is meant to be used in setup function with in_types which is used to get maximum number of inputs.

inline virtual int min_outputs()

Get minimum number of outputs.

This is meant to be used in setup function with out_types which is used to get max number of outputs.

inline virtual string name()

Get function name in string.

inline virtual vector<string> allowed_array_classes()

Get array classes that are allowed to be specified by Context.

inline virtual bool grad_depends_output_data(int i, int o) const

Dependency flag for checking if in-grad depends on out-data.

Checking if i-th input’ gradient computation requires o-th output’s data or not.

Note

If any of inputs requires an output variable data when computing its gradient, this function must be overridden to return appropriate boolean value. Otherwise, backward computation will be incorrect.

Parameters:
  • i[in] Input variable index.

  • o[in] Output variable index.

template<typename T>
class NonZero : public nbla::BaseFunction<>
#include <nonzero.hpp>

Find indices of non-zero elements.

NonZero behaves similar to NonZero Operator in ONNX.

Inputs:

  • x: N-D array.

Outputs:

  • y: N-D array indices.

Template Parameters:

T – Data type for computation.

Public Functions

inline virtual shared_ptr<Function> copy() const

Copy another instance of Function with the same context.

inline virtual int min_inputs()

Get minimum number of inputs.

This is meant to be used in setup function with in_types which is used to get maximum number of inputs.

inline virtual int min_outputs()

Get minimum number of outputs.

This is meant to be used in setup function with out_types which is used to get max number of outputs.

inline virtual vector<dtypes> in_types()

Get input dtypes.

Last in_type will be used repeatedly if size of in_types is smaller than size of inputs

inline virtual vector<dtypes> out_types()

Get output dtypes.

Last out_type will be used repeatedly if size of out_types is smaller than size of outputs

inline virtual vector<string> allowed_array_classes()

Get array classes that are allowed to be specified by Context.

inline virtual string name()

Get function name in string.

inline virtual bool grad_depends_output_data(int i, int o) const

Dependency flag for checking if in-grad depends on out-data.

Checking if i-th input’ gradient computation requires o-th output’s data or not.

Note

If any of inputs requires an output variable data when computing its gradient, this function must be overridden to return appropriate boolean value. Otherwise, backward computation will be incorrect.

Parameters:
  • i[in] Input variable index.

  • o[in] Output variable index.

template<typename T>
class Norm : public nbla::BaseFunction<float, const vector<int>&, bool>
#include <norm.hpp>

Todo:

Write doc.

Inputs:

Outputs:

Public Functions

inline virtual shared_ptr<Function> copy() const

Copy another instance of Function with the same context.

inline virtual int min_inputs()

Get minimum number of inputs.

This is meant to be used in setup function with in_types which is used to get maximum number of inputs.

inline virtual int min_outputs()

Get minimum number of outputs.

This is meant to be used in setup function with out_types which is used to get max number of outputs.

inline virtual vector<dtypes> in_types()

Get input dtypes.

Last in_type will be used repeatedly if size of in_types is smaller than size of inputs

inline virtual vector<dtypes> out_types()

Get output dtypes.

Last out_type will be used repeatedly if size of out_types is smaller than size of outputs

inline virtual vector<string> allowed_array_classes()

Get array classes that are allowed to be specified by Context.

inline virtual string name()

Get function name in string.

inline virtual bool grad_depends_output_data(int i, int o) const

Dependency flag for checking if in-grad depends on out-data.

Checking if i-th input’ gradient computation requires o-th output’s data or not.

Note

If any of inputs requires an output variable data when computing its gradient, this function must be overridden to return appropriate boolean value. Otherwise, backward computation will be incorrect.

Parameters:
  • i[in] Input variable index.

  • o[in] Output variable index.

template<typename T>
class NormNormalization : public nbla::BaseFunction<float, const vector<int>&, float>
#include <norm_normalization.hpp>

Todo:

Write doc.

Inputs:

Outputs:

Public Functions

inline virtual shared_ptr<Function> copy() const

Copy another instance of Function with the same context.

inline virtual int min_inputs()

Get minimum number of inputs.

This is meant to be used in setup function with in_types which is used to get maximum number of inputs.

inline virtual int min_outputs()

Get minimum number of outputs.

This is meant to be used in setup function with out_types which is used to get max number of outputs.

inline virtual vector<dtypes> in_types()

Get input dtypes.

Last in_type will be used repeatedly if size of in_types is smaller than size of inputs

inline virtual vector<dtypes> out_types()

Get output dtypes.

Last out_type will be used repeatedly if size of out_types is smaller than size of outputs

inline virtual vector<string> allowed_array_classes()

Get array classes that are allowed to be specified by Context.

inline virtual string name()

Get function name in string.

inline virtual bool grad_depends_output_data(int i, int o) const

Dependency flag for checking if in-grad depends on out-data.

Checking if i-th input’ gradient computation requires o-th output’s data or not.

Note

If any of inputs requires an output variable data when computing its gradient, this function must be overridden to return appropriate boolean value. Otherwise, backward computation will be incorrect.

Parameters:
  • i[in] Input variable index.

  • o[in] Output variable index.

template<typename T, typename T1>
class OneHot : public nbla::BaseFunction<const vector<int>&>
#include <one_hot.hpp>

OneHot creates one-hot vector based on input indices.

Inputs:

  • N-D array

Outputs:

  • N-D array

Template Parameters:
  • T – Index type (integer)

  • T1 – Value type (usually float)

Public Functions

inline virtual shared_ptr<Function> copy() const

Copy another instance of Function with the same context.

inline virtual vector<dtypes> in_types()

Get input dtypes.

Last in_type will be used repeatedly if size of in_types is smaller than size of inputs

inline virtual vector<dtypes> out_types()

Get output dtypes.

Last out_type will be used repeatedly if size of out_types is smaller than size of outputs

inline virtual int min_inputs()

Get minimum number of inputs.

This is meant to be used in setup function with in_types which is used to get maximum number of inputs.

inline virtual int min_outputs()

Get minimum number of outputs.

This is meant to be used in setup function with out_types which is used to get max number of outputs.

inline virtual string name()

Get function name in string.

inline virtual vector<string> allowed_array_classes()

Get array classes that are allowed to be specified by Context.

inline virtual bool grad_depends_output_data(int i, int o) const

Dependency flag for checking if in-grad depends on out-data.

Checking if i-th input’ gradient computation requires o-th output’s data or not.

Note

If any of inputs requires an output variable data when computing its gradient, this function must be overridden to return appropriate boolean value. Otherwise, backward computation will be incorrect.

Parameters:
  • i[in] Input variable index.

  • o[in] Output variable index.

template<typename T>
class ONNXNonMaxSuppression : public nbla::BaseFunction<int, int, float, float>
#include <onnx_non_max_suppression.hpp>

Non-Maximum Suppression (NMS) to 2D Object detector output.

The full description of this function can be found at `functions.yaml` or a docstring of Python function `F.onnx_non_max_suppression`.

Inputs:

  • 3-D bounding boxes array.

  • 3-D scores array.

Outputs:

  • 2-D selected indices array.

Template Parameters:

T – Data type for computation.

Param max_output_boxes_per_class:

The maximum number of boxes selected per batch per class.

Param iou_threshold:

IoU threshold for Non-maximum suppression (NMS).

Param score_threshold:

Detection score threshold.

Public Functions

inline virtual shared_ptr<Function> copy() const

Copy another instance of Function with the same context.

inline virtual int min_inputs()

Get minimum number of inputs.

This is meant to be used in setup function with in_types which is used to get maximum number of inputs.

inline virtual int min_outputs()

Get minimum number of outputs.

This is meant to be used in setup function with out_types which is used to get max number of outputs.

inline virtual vector<dtypes> in_types()

Get input dtypes.

Last in_type will be used repeatedly if size of in_types is smaller than size of inputs

inline virtual vector<dtypes> out_types()

Get output dtypes.

Last out_type will be used repeatedly if size of out_types is smaller than size of outputs

inline virtual vector<string> allowed_array_classes()

Get array classes that are allowed to be specified by Context.

inline virtual string name()

Get function name in string.

inline virtual bool grad_depends_output_data(int i, int o) const

Dependency flag for checking if in-grad depends on out-data.

Checking if i-th input’ gradient computation requires o-th output’s data or not.

Note

If any of inputs requires an output variable data when computing its gradient, this function must be overridden to return appropriate boolean value. Otherwise, backward computation will be incorrect.

Parameters:
  • i[in] Input variable index.

  • o[in] Output variable index.

struct ResizeOption
template<typename T>
class ONNXResize : public nbla::BaseFunction<const vector<float>&, const vector<float>&, const vector<int>&, const string&, const string&, float, int, float, const string&>
#include <onnx_resize.hpp>

Resize an ND array with interpolation.

This function provides a compatible interface to ONNX Resize.

Inputs:

  • x: N-D array.

Outputs:

  • y: N-D array.

Template Parameters:

T – Data type for computation.

Param roi:

RoIs for tf_crop_and_resize.

Param scales:

Scale factors along axes.

Param sizes:

Output size.

Param mode:

Interpolation mode chosen from (‘nearest’|’linear’|’cubic’).

Param coordinate_transformation_mode:

How to transform coordinates in the resized tensor to coordinates in the original tensor.

Param cubic_coeff_a:

The coefficient for cubic interpolation.

Param exclude_outside:

Whether to set coefficients to zero when a sampling location is outside the input tensor.

Param extrapolation_value:

An extrapolation value used when a sampling location is outside the input tensor at tf_crop_and_resize mode.

Param nearest_mode:

Rounding mode for nearest-neighbor interpolation.

Public Functions

inline virtual shared_ptr<Function> copy() const

Copy another instance of Function with the same context.

inline virtual int min_inputs()

Get minimum number of inputs.

This is meant to be used in setup function with in_types which is used to get maximum number of inputs.

inline virtual int min_outputs()

Get minimum number of outputs.

This is meant to be used in setup function with out_types which is used to get max number of outputs.

inline virtual vector<dtypes> in_types()

Get input dtypes.

Last in_type will be used repeatedly if size of in_types is smaller than size of inputs

inline virtual vector<dtypes> out_types()

Get output dtypes.

Last out_type will be used repeatedly if size of out_types is smaller than size of outputs

inline virtual vector<string> allowed_array_classes()

Get array classes that are allowed to be specified by Context.

inline virtual string name()

Get function name in string.

inline virtual bool grad_depends_output_data(int i, int o) const

Dependency flag for checking if in-grad depends on out-data.

Checking if i-th input’ gradient computation requires o-th output’s data or not.

Note

If any of inputs requires an output variable data when computing its gradient, this function must be overridden to return appropriate boolean value. Otherwise, backward computation will be incorrect.

Parameters:
  • i[in] Input variable index.

  • o[in] Output variable index.

template<typename T>
class PackPaddedSequence : public nbla::BaseFunction<bool>
#include <pack_padded_sequence.hpp>

Pack a padded variable-length sequences.

This method packs a padded variable-length sequences.

Inputs:

  • Padded sequence of ( \(T \times B \times *\)) or ( \(B \times T \times *\)) shape.

  • Sequence length for each batch and always resides in CPU.

Outputs:

  • Packed sequence of ( \(N\), \(*\)) shape.

  • Batch size for each time and always resides in CPU.

Template Parameters:

T – Data type for computation.

Param batch_first:

Packed sequence is of ( \(T\), \(B\), \(*\)) shape if False, otherwise ( \(B\), \(T\), \(*\)).

Public Functions

inline virtual shared_ptr<Function> copy() const

Copy another instance of Function with the same context.

inline virtual int min_inputs()

Get minimum number of inputs.

This is meant to be used in setup function with in_types which is used to get maximum number of inputs.

inline virtual int min_outputs()

Get minimum number of outputs.

This is meant to be used in setup function with out_types which is used to get max number of outputs.

inline virtual vector<dtypes> in_types()

Get input dtypes.

Last in_type will be used repeatedly if size of in_types is smaller than size of inputs

inline virtual vector<dtypes> out_types()

Get output dtypes.

Last out_type will be used repeatedly if size of out_types is smaller than size of outputs

inline virtual vector<string> allowed_array_classes()

Get array classes that are allowed to be specified by Context.

inline virtual string name()

Get function name in string.

inline virtual bool grad_depends_output_data(int i, int o) const

Dependency flag for checking if in-grad depends on out-data.

Checking if i-th input’ gradient computation requires o-th output’s data or not.

Note

If any of inputs requires an output variable data when computing its gradient, this function must be overridden to return appropriate boolean value. Otherwise, backward computation will be incorrect.

Parameters:
  • i[in] Input variable index.

  • o[in] Output variable index.

struct PadItem
#include <pad.hpp>

Pad the input N-D array `x` over the number dimensions given by half the length of the `pad_width` iterable, where every two values in `pad_width` determine the before and after pad size of an axis starting from the last (innermost) dimension and continuing towards the first (outermost) dimension.

The `pad_width` iterable must hold an even number of positive values which may cover all or fewer dimensions of the input variable `x`.

Padding is performed according to the requested `mode`. If pad mode is “constant” then all padded elements are set to `constant_value`. If pad mode is “reflect” then the padded alements are populated with the reflection of the vector mirrored on the first and last values of the vector along each axis.

Inputs:

  • x: N-D array variable.

  • pad_width: Vector with an even number of padding values.

  • mode: Padding mode string, either ‘constant’ or ‘reflect’.

  • constant_value - Fill value if mode is ‘constant’.

Outputs:

  • Padded N-D array with the same number of dimensions as the input.

Template Parameters:

T – Data type for computation.

template<typename T>
class Pad : public nbla::BaseFunction<const vector<int>&, const string&, float>

Public Functions

inline virtual shared_ptr<Function> copy() const

Copy another instance of Function with the same context.

inline virtual int min_inputs()

Get minimum number of inputs.

This is meant to be used in setup function with in_types which is used to get maximum number of inputs.

inline virtual int min_outputs()

Get minimum number of outputs.

This is meant to be used in setup function with out_types which is used to get max number of outputs.

inline virtual vector<dtypes> in_types()

Get input dtypes.

Last in_type will be used repeatedly if size of in_types is smaller than size of inputs

inline virtual vector<dtypes> out_types()

Get output dtypes.

Last out_type will be used repeatedly if size of out_types is smaller than size of outputs

inline virtual vector<string> allowed_array_classes()

Get array classes that are allowed to be specified by Context.

inline virtual string name()

Get function name in string.

inline virtual bool grad_depends_output_data(int i, int o) const

Dependency flag for checking if in-grad depends on out-data.

Checking if i-th input’ gradient computation requires o-th output’s data or not.

Note

If any of inputs requires an output variable data when computing its gradient, this function must be overridden to return appropriate boolean value. Otherwise, backward computation will be incorrect.

Parameters:
  • i[in] Input variable index.

  • o[in] Output variable index.

template<typename T>
class PadPackedSequence : public nbla::BaseFunction<bool, float, int>
#include <pad_packed_sequence.hpp>

Pad packed sequence.

This method unpacks the packed sequqnce and pad it, the inverse operation of :func:`pack_padded_sequence`.

Inputs:

  • Packed sequence of ( \(N\), \(*\)) shape.

  • Batch size for each time and always resides in CPU.

Outputs:

  • Padded sequence of ( \(T \times B \times *\)) or ( \(B \times T \times *\)) shape.

  • Sequence length for each batch and always resides in CPU.

Template Parameters:

T – Data type for computation.

Param batch_first:

Padded sequence is of ( \(T\), \(B\), \(*\)) shape if False, otherwise ( \(B\), \(T\), \(*\)).

Param padding_value:

Padding value.

Param total_length:

If not -1, the outputs are padded up to the `total_length`. If the `total_length` is less than the max length in the `sequences`, the error is thrown.

Public Functions

inline virtual shared_ptr<Function> copy() const

Copy another instance of Function with the same context.

inline virtual int min_inputs()

Get minimum number of inputs.

This is meant to be used in setup function with in_types which is used to get maximum number of inputs.

inline virtual int min_outputs()

Get minimum number of outputs.

This is meant to be used in setup function with out_types which is used to get max number of outputs.

inline virtual vector<dtypes> in_types()

Get input dtypes.

Last in_type will be used repeatedly if size of in_types is smaller than size of inputs

inline virtual vector<dtypes> out_types()

Get output dtypes.

Last out_type will be used repeatedly if size of out_types is smaller than size of outputs

inline virtual vector<string> allowed_array_classes()

Get array classes that are allowed to be specified by Context.

inline virtual string name()

Get function name in string.

inline virtual bool grad_depends_output_data(int i, int o) const

Dependency flag for checking if in-grad depends on out-data.

Checking if i-th input’ gradient computation requires o-th output’s data or not.

Note

If any of inputs requires an output variable data when computing its gradient, this function must be overridden to return appropriate boolean value. Otherwise, backward computation will be incorrect.

Parameters:
  • i[in] Input variable index.

  • o[in] Output variable index.

template<typename T>
class PatchCorrelation : public nbla::BaseFunction<const vector<int>&, const vector<int>&, const vector<int>&, const vector<int>&, const vector<int>&>
#include <patch_correlation.hpp>

PatchConvolution.

Inputs:

  • Input N-D array with shape (N, H, W, C).

  • Input N-D array with shape (N, H, W, C).

Outputs:

  • N-D array with shape (N, C_y, C_x, Ho, Wo)

Template Parameters:

T – Data type for computation.

Param patch:

Height and width of the correlation patch.

Param shift:

Maximum vertical and horizontal displacement of patches.

Param patch_step:

Vertical and horizontal patch increments.

Param shift_step:

Vertical and horizontal shift increments.

Param padding:

Top, bottom, left and right padding extent.

Public Functions

inline virtual shared_ptr<Function> copy() const

Copy another instance of Function with the same context.

inline virtual int min_inputs()

Get minimum number of inputs.

This is meant to be used in setup function with in_types which is used to get maximum number of inputs.

inline virtual int min_outputs()

Get minimum number of outputs.

This is meant to be used in setup function with out_types which is used to get max number of outputs.

inline virtual vector<dtypes> in_types()

Get input dtypes.

Last in_type will be used repeatedly if size of in_types is smaller than size of inputs

inline virtual vector<dtypes> out_types()

Get output dtypes.

Last out_type will be used repeatedly if size of out_types is smaller than size of outputs

inline virtual vector<string> allowed_array_classes()

Get array classes that are allowed to be specified by Context.

inline virtual string name()

Get function name in string.

inline virtual bool grad_depends_output_data(int i, int o) const

Dependency flag for checking if in-grad depends on out-data.

Checking if i-th input’ gradient computation requires o-th output’s data or not.

Note

If any of inputs requires an output variable data when computing its gradient, this function must be overridden to return appropriate boolean value. Otherwise, backward computation will be incorrect.

Parameters:
  • i[in] Input variable index.

  • o[in] Output variable index.

template<typename T>
class Pow2Quantize : public nbla::BaseFunction<bool, bool, int, int, bool>
#include <pow2_quantize.hpp>

This function quantizes values in the power of 2 number representation, in other words, it is linear (uniform) quantization in :math:`log_2` domain.

Template Parameters:

T – Data type for computation.

Param sign:

Indicate the signed number or the unsigned number. Default is true.

Param with_zero:

Indicate using zero as a quantized value. Default is true. Note that `zero` consumes one bit.

Param n:

Bit width used. Note that `sign` consumes one bit. \(n-1\) is used for number representation in `signed` case.

Param m:

\(2^m\) is the upper bound of the dynamic range and \(-2^m\) is the lower bound, \(m \in \mathcal{Z}\).

Param quantize:

If true, quantize input, otherwise not.

Param ste_fine_grained:

If true, STE is not 1.

Public Functions

inline virtual shared_ptr<Function> copy() const

Copy another instance of Function with the same context.

inline virtual vector<dtypes> in_types()

Get input dtypes.

Last in_type will be used repeatedly if size of in_types is smaller than size of inputs

inline virtual vector<dtypes> out_types()

Get output dtypes.

Last out_type will be used repeatedly if size of out_types is smaller than size of outputs

inline virtual int min_inputs()

Get minimum number of inputs.

This is meant to be used in setup function with in_types which is used to get maximum number of inputs.

inline virtual int min_outputs()

Get minimum number of outputs.

This is meant to be used in setup function with out_types which is used to get max number of outputs.

inline virtual string name()

Get function name in string.

inline virtual vector<string> allowed_array_classes()

Get array classes that are allowed to be specified by Context.

inline virtual bool grad_depends_output_data(int i, int o) const

Dependency flag for checking if in-grad depends on out-data.

Checking if i-th input’ gradient computation requires o-th output’s data or not.

Note

If any of inputs requires an output variable data when computing its gradient, this function must be overridden to return appropriate boolean value. Otherwise, backward computation will be incorrect.

Parameters:
  • i[in] Input variable index.

  • o[in] Output variable index.

template<typename T>
class PReLU : public nbla::BaseFunction<int>
#include <prelu.hpp>

Parametrized Rectified Linear Unit function defined as.

\[ y_i = \max(0, x_i) + \alpha \min(0, x_i) \]
where negative slope \( \alpha \) is learned and can vary across channels (an axis specified with base_axis).

Inputs:

  • N-D array

  • Scalar or a vector with size of channel axis (base_axis).

Outputs:

  • N-D array.

See also

He et.al, Delving Deep into Rectifiers: Surpassing Human-Level Performance on ImageNet Classification. https://arxiv.org/abs/1502.01852

Template Parameters:

T – Data type for computation.

Param base_axis:

Valid if negative slope is a vector. The negative slope vector size and input shape of base_axis must match.

Public Functions

inline virtual shared_ptr<Function> copy() const

Copy another instance of Function with the same context.

inline virtual vector<dtypes> in_types()

Get input dtypes.

Last in_type will be used repeatedly if size of in_types is smaller than size of inputs

inline virtual vector<dtypes> out_types()

Get output dtypes.

Last out_type will be used repeatedly if size of out_types is smaller than size of outputs

inline virtual int min_inputs()

Get minimum number of inputs.

This is meant to be used in setup function with in_types which is used to get maximum number of inputs.

inline virtual int min_outputs()

Get minimum number of outputs.

This is meant to be used in setup function with out_types which is used to get max number of outputs.

inline virtual string name()

Get function name in string.

inline virtual vector<string> allowed_array_classes()

Get array classes that are allowed to be specified by Context.

inline virtual bool grad_depends_output_data(int i, int o) const

Dependency flag for checking if in-grad depends on out-data.

Checking if i-th input’ gradient computation requires o-th output’s data or not.

Note

If any of inputs requires an output variable data when computing its gradient, this function must be overridden to return appropriate boolean value. Otherwise, backward computation will be incorrect.

Parameters:
  • i[in] Input variable index.

  • o[in] Output variable index.

template<typename T>
class Prod : public nbla::Sum<T>
#include <prod.hpp>

Reduction along axes with product operation.

Note

Backward computation is not accurate in a zero value input.

Template Parameters:

T – Data type for computation.

Param axes:

A list of axes to be reduced.

Param keep_dims:

Flag whether the reduced axes are kept.

Public Functions

inline virtual shared_ptr<Function> copy() const

Copy another instance of Function with the same context.

inline virtual string name()

Get function name in string.

inline virtual bool grad_depends_output_data(int i, int o) const

Dependency flag for checking if in-grad depends on out-data.

Checking if i-th input’ gradient computation requires o-th output’s data or not.

Note

If any of inputs requires an output variable data when computing its gradient, this function must be overridden to return appropriate boolean value. Otherwise, backward computation will be incorrect.

Parameters:
  • i[in] Input variable index.

  • o[in] Output variable index.

template<typename T>
class Prune : public nbla::BaseFunction<float>
#include <prune.hpp>

Prune function defined as.

\[\begin{split} q_i = \left \{ \begin{array}{ll} 0 & abs(x_i) < threshold \\ x_i & otherwise \end{array} \right. \end{split}\]

\( threshold \) is determined by `threshold = np.sort(np.abs(x))[int((x.size - 1) * rate)]`.

Inputs:

  • N-D array. Outputs:

  • N-D array.

Template Parameters:

T – Data type for computation.

Param rate:

Rate for pruning. The rage is [0.0, 1.0] and 0.9 rate is default.

Public Functions

inline virtual shared_ptr<Function> copy() const

Copy another instance of Function with the same context.

inline virtual int min_inputs()

Get minimum number of inputs.

This is meant to be used in setup function with in_types which is used to get maximum number of inputs.

inline virtual int min_outputs()

Get minimum number of outputs.

This is meant to be used in setup function with out_types which is used to get max number of outputs.

inline virtual vector<dtypes> in_types()

Get input dtypes.

Last in_type will be used repeatedly if size of in_types is smaller than size of inputs

inline virtual vector<dtypes> out_types()

Get output dtypes.

Last out_type will be used repeatedly if size of out_types is smaller than size of outputs

inline virtual vector<string> allowed_array_classes()

Get array classes that are allowed to be specified by Context.

inline virtual string name()

Get function name in string.

inline virtual bool grad_depends_output_data(int i, int o) const

Dependency flag for checking if in-grad depends on out-data.

Checking if i-th input’ gradient computation requires o-th output’s data or not.

Note

If any of inputs requires an output variable data when computing its gradient, this function must be overridden to return appropriate boolean value. Otherwise, backward computation will be incorrect.

Parameters:
  • i[in] Input variable index.

  • o[in] Output variable index.

template<typename T>
class QuantizeLinear : public nbla::BaseFunction<const string&, bool, int>
#include <quantize_linear.hpp>

Todo:

Write doc.

Inputs:

Outputs:

Public Functions

inline virtual shared_ptr<Function> copy() const

Copy another instance of Function with the same context.

inline virtual int min_inputs()

Get minimum number of inputs.

This is meant to be used in setup function with in_types which is used to get maximum number of inputs.

inline virtual int min_outputs()

Get minimum number of outputs.

This is meant to be used in setup function with out_types which is used to get max number of outputs.

inline virtual vector<dtypes> in_types()

Get input dtypes.

Last in_type will be used repeatedly if size of in_types is smaller than size of inputs

inline virtual vector<dtypes> out_types()

Get output dtypes.

Last out_type will be used repeatedly if size of out_types is smaller than size of outputs

inline virtual vector<string> allowed_array_classes()

Get array classes that are allowed to be specified by Context.

inline virtual string name()

Get function name in string.

inline virtual bool grad_depends_output_data(int i, int o) const

Dependency flag for checking if in-grad depends on out-data.

Checking if i-th input’ gradient computation requires o-th output’s data or not.

Note

If any of inputs requires an output variable data when computing its gradient, this function must be overridden to return appropriate boolean value. Otherwise, backward computation will be incorrect.

Parameters:
  • i[in] Input variable index.

  • o[in] Output variable index.

template<typename T>
class Rand : public nbla::BaseFunction<float, float, const vector<int>&, int>
#include <rand.hpp>

Todo:

PLACE HERE FUNCTION DOCUMENTATION.

Public Functions

inline virtual shared_ptr<Function> copy() const

Copy another instance of Function with the same context.

inline virtual vector<dtypes> in_types()

Get input dtypes.

Last in_type will be used repeatedly if size of in_types is smaller than size of inputs

inline virtual vector<dtypes> out_types()

Get output dtypes.

Last out_type will be used repeatedly if size of out_types is smaller than size of outputs

inline virtual int min_inputs()

Get minimum number of inputs.

This is meant to be used in setup function with in_types which is used to get maximum number of inputs.

inline virtual int min_outputs()

Get minimum number of outputs.

This is meant to be used in setup function with out_types which is used to get max number of outputs.

inline virtual string name()

Get function name in string.

inline virtual vector<string> allowed_array_classes()

Get array classes that are allowed to be specified by Context.

inline virtual bool need_setup_recompute(int o) const

A flag for checking if setup_recompute() is needed.

Checking if o-th output’ data requires setup_recompute().

Note

setup_recompute() will skipped during forward execution if none of outputs requires setup_recompute().

Parameters:

o[in] Output variable index.

inline virtual bool grad_depends_output_data(int i, int o) const

Dependency flag for checking if in-grad depends on out-data.

Checking if i-th input’ gradient computation requires o-th output’s data or not.

Note

If any of inputs requires an output variable data when computing its gradient, this function must be overridden to return appropriate boolean value. Otherwise, backward computation will be incorrect.

Parameters:
  • i[in] Input variable index.

  • o[in] Output variable index.

template<typename T>
class RandBeta : public nbla::BaseFunction<float, float, const vector<int>&, int>
#include <rand_beta.hpp>

Todo:

Write doc.

Inputs:

Outputs:

Public Functions

inline virtual shared_ptr<Function> copy() const

Copy another instance of Function with the same context.

inline virtual int min_inputs()

Get minimum number of inputs.

This is meant to be used in setup function with in_types which is used to get maximum number of inputs.

inline virtual int min_outputs()

Get minimum number of outputs.

This is meant to be used in setup function with out_types which is used to get max number of outputs.

inline virtual vector<dtypes> in_types()

Get input dtypes.

Last in_type will be used repeatedly if size of in_types is smaller than size of inputs

inline virtual vector<dtypes> out_types()

Get output dtypes.

Last out_type will be used repeatedly if size of out_types is smaller than size of outputs

inline virtual vector<string> allowed_array_classes()

Get array classes that are allowed to be specified by Context.

inline virtual string name()

Get function name in string.

inline virtual bool need_setup_recompute(int o) const

A flag for checking if setup_recompute() is needed.

Checking if o-th output’ data requires setup_recompute().

Note

setup_recompute() will skipped during forward execution if none of outputs requires setup_recompute().

Parameters:

o[in] Output variable index.

inline virtual bool grad_depends_output_data(int i, int o) const

Dependency flag for checking if in-grad depends on out-data.

Checking if i-th input’ gradient computation requires o-th output’s data or not.

Note

If any of inputs requires an output variable data when computing its gradient, this function must be overridden to return appropriate boolean value. Otherwise, backward computation will be incorrect.

Parameters:
  • i[in] Input variable index.

  • o[in] Output variable index.

template<typename T>
class RandBinomial : public nbla::BaseFunction<int, float, const vector<int>&, int>
#include <rand_binomial.hpp>

Todo:

Write doc.

Inputs:

Outputs:

Public Functions

inline virtual shared_ptr<Function> copy() const

Copy another instance of Function with the same context.

inline virtual int min_inputs()

Get minimum number of inputs.

This is meant to be used in setup function with in_types which is used to get maximum number of inputs.

inline virtual int min_outputs()

Get minimum number of outputs.

This is meant to be used in setup function with out_types which is used to get max number of outputs.

inline virtual vector<dtypes> in_types()

Get input dtypes.

Last in_type will be used repeatedly if size of in_types is smaller than size of inputs

inline virtual vector<dtypes> out_types()

Get output dtypes.

Last out_type will be used repeatedly if size of out_types is smaller than size of outputs

inline virtual vector<string> allowed_array_classes()

Get array classes that are allowed to be specified by Context.

inline virtual string name()

Get function name in string.

inline virtual bool need_setup_recompute(int o) const

A flag for checking if setup_recompute() is needed.

Checking if o-th output’ data requires setup_recompute().

Note

setup_recompute() will skipped during forward execution if none of outputs requires setup_recompute().

Parameters:

o[in] Output variable index.

inline virtual bool grad_depends_output_data(int i, int o) const

Dependency flag for checking if in-grad depends on out-data.

Checking if i-th input’ gradient computation requires o-th output’s data or not.

Note

If any of inputs requires an output variable data when computing its gradient, this function must be overridden to return appropriate boolean value. Otherwise, backward computation will be incorrect.

Parameters:
  • i[in] Input variable index.

  • o[in] Output variable index.

template<typename T>
class RandGamma : public nbla::BaseFunction<float, float, const vector<int>&, int>
#include <rand_gamma.hpp>

Todo:

Write doc.

Inputs:

Outputs:

Public Functions

inline virtual shared_ptr<Function> copy() const

Copy another instance of Function with the same context.

inline virtual int min_inputs()

Get minimum number of inputs.

This is meant to be used in setup function with in_types which is used to get maximum number of inputs.

inline virtual int min_outputs()

Get minimum number of outputs.

This is meant to be used in setup function with out_types which is used to get max number of outputs.

inline virtual vector<dtypes> in_types()

Get input dtypes.

Last in_type will be used repeatedly if size of in_types is smaller than size of inputs

inline virtual vector<dtypes> out_types()

Get output dtypes.

Last out_type will be used repeatedly if size of out_types is smaller than size of outputs

inline virtual vector<string> allowed_array_classes()

Get array classes that are allowed to be specified by Context.

inline virtual string name()

Get function name in string.

inline virtual bool need_setup_recompute(int o) const

A flag for checking if setup_recompute() is needed.

Checking if o-th output’ data requires setup_recompute().

Note

setup_recompute() will skipped during forward execution if none of outputs requires setup_recompute().

Parameters:

o[in] Output variable index.

inline virtual bool grad_depends_output_data(int i, int o) const

Dependency flag for checking if in-grad depends on out-data.

Checking if i-th input’ gradient computation requires o-th output’s data or not.

Note

If any of inputs requires an output variable data when computing its gradient, this function must be overridden to return appropriate boolean value. Otherwise, backward computation will be incorrect.

Parameters:
  • i[in] Input variable index.

  • o[in] Output variable index.

template<typename T>
class Randint : public nbla::BaseFunction<int, int, const vector<int>&, int>
#include <randint.hpp>

Todo:

PLACE HERE FUNCTION DOCUMENTATION.

Public Functions

inline virtual shared_ptr<Function> copy() const

Copy another instance of Function with the same context.

inline virtual vector<dtypes> in_types()

Get input dtypes.

Last in_type will be used repeatedly if size of in_types is smaller than size of inputs

inline virtual vector<dtypes> out_types()

Get output dtypes.

Last out_type will be used repeatedly if size of out_types is smaller than size of outputs

inline virtual int min_inputs()

Get minimum number of inputs.

This is meant to be used in setup function with in_types which is used to get maximum number of inputs.

inline virtual int min_outputs()

Get minimum number of outputs.

This is meant to be used in setup function with out_types which is used to get max number of outputs.

inline virtual string name()

Get function name in string.

inline virtual vector<string> allowed_array_classes()

Get array classes that are allowed to be specified by Context.

inline virtual bool need_setup_recompute(int o) const

A flag for checking if setup_recompute() is needed.

Checking if o-th output’ data requires setup_recompute().

Note

setup_recompute() will skipped during forward execution if none of outputs requires setup_recompute().

Parameters:

o[in] Output variable index.

inline virtual bool grad_depends_output_data(int i, int o) const

Dependency flag for checking if in-grad depends on out-data.

Checking if i-th input’ gradient computation requires o-th output’s data or not.

Note

If any of inputs requires an output variable data when computing its gradient, this function must be overridden to return appropriate boolean value. Otherwise, backward computation will be incorrect.

Parameters:
  • i[in] Input variable index.

  • o[in] Output variable index.

template<typename T>
class Randn : public nbla::BaseFunction<float, float, const vector<int>&, int>
#include <randn.hpp>

Todo:

PLACE HERE FUNCTION DOCUMENTATION.

Public Functions

inline virtual shared_ptr<Function> copy() const

Copy another instance of Function with the same context.

inline virtual vector<dtypes> in_types()

Get input dtypes.

Last in_type will be used repeatedly if size of in_types is smaller than size of inputs

inline virtual vector<dtypes> out_types()

Get output dtypes.

Last out_type will be used repeatedly if size of out_types is smaller than size of outputs

inline virtual int min_inputs()

Get minimum number of inputs.

This is meant to be used in setup function with in_types which is used to get maximum number of inputs.

inline virtual int min_outputs()

Get minimum number of outputs.

This is meant to be used in setup function with out_types which is used to get max number of outputs.

inline virtual string name()

Get function name in string.

inline virtual vector<string> allowed_array_classes()

Get array classes that are allowed to be specified by Context.

inline virtual bool need_setup_recompute(int o) const

A flag for checking if setup_recompute() is needed.

Checking if o-th output’ data requires setup_recompute().

Note

setup_recompute() will skipped during forward execution if none of outputs requires setup_recompute().

Parameters:

o[in] Output variable index.

inline virtual bool grad_depends_output_data(int i, int o) const

Dependency flag for checking if in-grad depends on out-data.

Checking if i-th input’ gradient computation requires o-th output’s data or not.

Note

If any of inputs requires an output variable data when computing its gradient, this function must be overridden to return appropriate boolean value. Otherwise, backward computation will be incorrect.

Parameters:
  • i[in] Input variable index.

  • o[in] Output variable index.

template<typename T>
class RandomChoice : public nbla::BaseFunction<const vector<int>&, bool, int>
#include <random_choice.hpp>

Generate random samples from population `x` with selection probabilities determined by the relative weights `w`.

The number of samples to draw is given by the product of `shape`s dimensions, and the samples are returned with the given `shape`. By default, samples are drawn with replacement, i.e. selection of a specific population member is solely determined by its associated weight. Sampling without replacement, where any population member may be drawn only once, is used if `replace` is set to False.

For both `x` and `w` the innermost dimension corresponds to the individual populations and their weights from which samples are returned with the requested `shape` following all outermost dimensions of the input.

import nnabla as nn
import nnabla.functions as F
import numpy as np
nn.set_auto_forward(True)

# x holds two populations
x = nn.Variable.from_numpy_array(np.array([[11, 22, 33], [110, 220, 330]]))
# w holds the weights for each population
w = nn.Variable.from_numpy_array(np.array([[10, 20, 70], [70, 20, 10]]))

# draw one sample from each population
y = F.random_choice(x, w)  # y.shape => (2, 1)

# draw 12 samples with shape (3, 4) from each population
y = F.random_choice(x, w, shape=(3, 4))  # y.shape => (2, 3, 4)

Note that weights must not be less than zero and for each population the sum of weights must be greater than zero. Additionally, sampling without replacement requires that the number of non-zero weights is not less than the number of samples to be drawn. These conditions are verified in “cpu” computation context but not when using “cuda” or “cudnn” acceleration (this would require additional device synchronization steps penalizing performance).

Random sampling from an implicit array of index values (like categorical or multinomial) can be realized with input `x` constructed as indices.

w = nn.Variable.from_numpy_array(np.array([1, 2, 3, 2, 1]))
y = F.random_choice(F.arange(0, 5), w)

Inputs:

  • x: N-D array from which a random sample is generated.

  • w: N-D array of associated weights of elements in `x`.

Outputs:

  • N-D array

Template Parameters:

T – Data type for computation.

Param shape:

Number and shape of generated samples.

Param replace:

Whether sampling is with or without replacement.

Param seed:

Random seed.

Public Functions

inline virtual shared_ptr<Function> copy() const

Copy another instance of Function with the same context.

inline virtual int min_inputs()

Get minimum number of inputs.

This is meant to be used in setup function with in_types which is used to get maximum number of inputs.

inline virtual int min_outputs()

Get minimum number of outputs.

This is meant to be used in setup function with out_types which is used to get max number of outputs.

inline virtual vector<dtypes> in_types()

Get input dtypes.

Last in_type will be used repeatedly if size of in_types is smaller than size of inputs

inline virtual vector<dtypes> out_types()

Get output dtypes.

Last out_type will be used repeatedly if size of out_types is smaller than size of outputs

inline virtual vector<string> allowed_array_classes()

Get array classes that are allowed to be specified by Context.

inline virtual string name()

Get function name in string.

inline virtual bool need_setup_recompute(int o) const

A flag for checking if setup_recompute() is needed.

Checking if o-th output’ data requires setup_recompute().

Note

setup_recompute() will skipped during forward execution if none of outputs requires setup_recompute().

Parameters:

o[in] Output variable index.

inline virtual bool grad_depends_output_data(int i, int o) const

Dependency flag for checking if in-grad depends on out-data.

Checking if i-th input’ gradient computation requires o-th output’s data or not.

Note

If any of inputs requires an output variable data when computing its gradient, this function must be overridden to return appropriate boolean value. Otherwise, backward computation will be incorrect.

Parameters:
  • i[in] Input variable index.

  • o[in] Output variable index.

template<typename T>
class RandomCrop : public nbla::BaseFunction<const vector<int>&, int, int>
#include <random_crop.hpp>

RandomCrop randomly extracts a portion of an array.

Inputs:

  • N-D array.

Outputs:

  • N-D array.

Template Parameters:

T – Data type for computation.

Param shape:

The data size to extract. For example, to randomly extract a portion of the image (3,48,48) from a 3,64,64 image, specify (3,48,48).

Public Functions

inline virtual shared_ptr<Function> copy() const

Copy another instance of Function with the same context.

inline virtual vector<dtypes> in_types()

Get input dtypes.

Last in_type will be used repeatedly if size of in_types is smaller than size of inputs

inline virtual vector<dtypes> out_types()

Get output dtypes.

Last out_type will be used repeatedly if size of out_types is smaller than size of outputs

inline virtual int min_inputs()

Get minimum number of inputs.

This is meant to be used in setup function with in_types which is used to get maximum number of inputs.

inline virtual int min_outputs()

Get minimum number of outputs.

This is meant to be used in setup function with out_types which is used to get max number of outputs.

inline virtual string name()

Get function name in string.

inline virtual vector<string> allowed_array_classes()

Get array classes that are allowed to be specified by Context.

inline virtual bool need_setup_recompute(int o) const

A flag for checking if setup_recompute() is needed.

Checking if o-th output’ data requires setup_recompute().

Note

setup_recompute() will skipped during forward execution if none of outputs requires setup_recompute().

Parameters:

o[in] Output variable index.

inline virtual bool grad_depends_output_data(int i, int o) const

Dependency flag for checking if in-grad depends on out-data.

Checking if i-th input’ gradient computation requires o-th output’s data or not.

Note

If any of inputs requires an output variable data when computing its gradient, this function must be overridden to return appropriate boolean value. Otherwise, backward computation will be incorrect.

Parameters:
  • i[in] Input variable index.

  • o[in] Output variable index.

template<typename T>
class RandomErase : public nbla::BaseFunction<float, const vector<float>&, const vector<float>&, const vector<float>&, int, bool, bool, int, int, bool, bool>
#include <random_erase.hpp>

RandomErase randomly erase the inputs and replace with random values.

Inputs:

  • N-D array of three or more dimensions

Outputs:

  • N-D array.

Template Parameters:

T – Data type for computation.

Param prob:

Probability to erase.

Param area_ratios:

Low and high of the area ratio range.

Param aspect_ratios:

Low and high of the aspect ratios range.

Param replacements:

Low and high of the replacement value range.

Param n:

Max number of patches to be erased.

Param share:

Use a same bounding box randomly picked over the feature dimension when being True. Default is False.

Param inplace:

This option is obsolete and ignored. Output is never in-placed with input.

Param base_axis:

Param seed:

Random seed. When -1, seed is sampled from global random number generator.

Param channel_last:

If True, the last dimension is considered as channel dimension, a.k.a NHWC order.

Param ste_fine_grained:

Straight Through Estimator is fine-grained or not. Default

Public Functions

inline virtual shared_ptr<Function> copy() const

Copy another instance of Function with the same context.

inline virtual int min_inputs()

Get minimum number of inputs.

This is meant to be used in setup function with in_types which is used to get maximum number of inputs.

inline virtual int min_outputs()

Get minimum number of outputs.

This is meant to be used in setup function with out_types which is used to get max number of outputs.

inline virtual vector<dtypes> in_types()

Get input dtypes.

Last in_type will be used repeatedly if size of in_types is smaller than size of inputs

inline virtual vector<dtypes> out_types()

Get output dtypes.

Last out_type will be used repeatedly if size of out_types is smaller than size of outputs

inline virtual vector<string> allowed_array_classes()

Get array classes that are allowed to be specified by Context.

inline virtual string name()

Get function name in string.

inline virtual bool need_setup_recompute(int o) const

A flag for checking if setup_recompute() is needed.

Checking if o-th output’ data requires setup_recompute().

Note

setup_recompute() will skipped during forward execution if none of outputs requires setup_recompute().

Parameters:

o[in] Output variable index.

inline virtual bool grad_depends_output_data(int i, int o) const

Dependency flag for checking if in-grad depends on out-data.

Checking if i-th input’ gradient computation requires o-th output’s data or not.

Note

If any of inputs requires an output variable data when computing its gradient, this function must be overridden to return appropriate boolean value. Otherwise, backward computation will be incorrect.

Parameters:
  • i[in] Input variable index.

  • o[in] Output variable index.

template<typename T>
class RandomFlip : public nbla::BaseFunction<const vector<int>&, int, int>
#include <random_flip.hpp>

RandomFlip reverses the order of elements of the specified dimension of an array at 50% probability.

Inputs:

  • N-D array.

Outputs:

  • N-D array.

Template Parameters:

T – Data type for computation.

Param axes:

The index of the axis to reverse the order of the elements. Axis indexes take on values 0, 1, 2, and so on from the left. For example, to flip a 32 (W) by 24 (H) 100 RGB images (100, 3,24,32) vertically and horizontally at random, specify (2,3).

Public Functions

inline virtual shared_ptr<Function> copy() const

Copy another instance of Function with the same context.

inline virtual vector<dtypes> in_types()

Get input dtypes.

Last in_type will be used repeatedly if size of in_types is smaller than size of inputs

inline virtual vector<dtypes> out_types()

Get output dtypes.

Last out_type will be used repeatedly if size of out_types is smaller than size of outputs

inline virtual int min_inputs()

Get minimum number of inputs.

This is meant to be used in setup function with in_types which is used to get maximum number of inputs.

inline virtual int min_outputs()

Get minimum number of outputs.

This is meant to be used in setup function with out_types which is used to get max number of outputs.

inline virtual string name()

Get function name in string.

inline virtual vector<string> allowed_array_classes()

Get array classes that are allowed to be specified by Context.

inline virtual bool need_setup_recompute(int o) const

A flag for checking if setup_recompute() is needed.

Checking if o-th output’ data requires setup_recompute().

Note

setup_recompute() will skipped during forward execution if none of outputs requires setup_recompute().

Parameters:

o[in] Output variable index.

inline virtual bool grad_depends_output_data(int i, int o) const

Dependency flag for checking if in-grad depends on out-data.

Checking if i-th input’ gradient computation requires o-th output’s data or not.

Note

If any of inputs requires an output variable data when computing its gradient, this function must be overridden to return appropriate boolean value. Otherwise, backward computation will be incorrect.

Parameters:
  • i[in] Input variable index.

  • o[in] Output variable index.

template<typename T>
class RandomShift : public nbla::BaseFunction<const vector<int>&, const string&, float, int, int>
#include <random_shift.hpp>

RandomShift randomly shifts the array elements within the specified range.

Inputs:

  • N-D array.

Outputs:

  • N-D array.

Template Parameters:

T – Data type for computation.

Param shift:

Max absolute amount to shift elements. For example, to shift image data horizontally by +-2 pixels and vertically by +-3 pixels, specify (3,2).

Param border_mode:

Specify how to process the ends of arrays whose values will be undetermined as a result of shifting. nearest: The data at the ends of the original array is copied and used. reflect: Original data reflected at the ends of the original array is used.

Public Functions

inline virtual shared_ptr<Function> copy() const

Copy another instance of Function with the same context.

inline virtual vector<dtypes> in_types()

Get input dtypes.

Last in_type will be used repeatedly if size of in_types is smaller than size of inputs

inline virtual vector<dtypes> out_types()

Get output dtypes.

Last out_type will be used repeatedly if size of out_types is smaller than size of outputs

inline virtual int min_inputs()

Get minimum number of inputs.

This is meant to be used in setup function with in_types which is used to get maximum number of inputs.

inline virtual int min_outputs()

Get minimum number of outputs.

This is meant to be used in setup function with out_types which is used to get max number of outputs.

inline virtual string name()

Get function name in string.

inline virtual vector<string> allowed_array_classes()

Get array classes that are allowed to be specified by Context.

inline virtual bool need_setup_recompute(int o) const

A flag for checking if setup_recompute() is needed.

Checking if o-th output’ data requires setup_recompute().

Note

setup_recompute() will skipped during forward execution if none of outputs requires setup_recompute().

Parameters:

o[in] Output variable index.

inline virtual bool grad_depends_output_data(int i, int o) const

Dependency flag for checking if in-grad depends on out-data.

Checking if i-th input’ gradient computation requires o-th output’s data or not.

Note

If any of inputs requires an output variable data when computing its gradient, this function must be overridden to return appropriate boolean value. Otherwise, backward computation will be incorrect.

Parameters:
  • i[in] Input variable index.

  • o[in] Output variable index.

template<typename T>
class ReduceMean : public nbla::BaseFunction<>
#include <reduce_mean.hpp>

Reduction with mean along all dimensions defined as.

\[ y = \frac{1}{N} \sum_{i,j,...} x_{i,j,...}. \]

Inputs:

  • N-D array. Outputs:

  • Scalar (0-shape array).

where N is size of the input array.

Template Parameters:

T – Data type for computation.

Public Functions

inline virtual shared_ptr<Function> copy() const

Copy another instance of Function with the same context.

inline virtual vector<dtypes> in_types()

Get input dtypes.

Last in_type will be used repeatedly if size of in_types is smaller than size of inputs

inline virtual vector<dtypes> out_types()

Get output dtypes.

Last out_type will be used repeatedly if size of out_types is smaller than size of outputs

inline virtual int min_inputs()

Get minimum number of inputs.

This is meant to be used in setup function with in_types which is used to get maximum number of inputs.

inline virtual int min_outputs()

Get minimum number of outputs.

This is meant to be used in setup function with out_types which is used to get max number of outputs.

inline virtual string name()

Get function name in string.

inline virtual vector<string> allowed_array_classes()

Get array classes that are allowed to be specified by Context.

inline virtual bool grad_depends_output_data(int i, int o) const

Dependency flag for checking if in-grad depends on out-data.

Checking if i-th input’ gradient computation requires o-th output’s data or not.

Note

If any of inputs requires an output variable data when computing its gradient, this function must be overridden to return appropriate boolean value. Otherwise, backward computation will be incorrect.

Parameters:
  • i[in] Input variable index.

  • o[in] Output variable index.

template<typename T>
class ReduceSum : public nbla::BaseFunction<>
#include <reduce_sum.hpp>

Reduction with summation along all dimensions defined as.

\[ y = \sum_{i,j,...} x_{i,j,...}. \]

Inputs:

  • N-D array.

Outputs:

  • Scalar (0-shape array).

Template Parameters:

T – Data type for computation.

Public Functions

inline virtual shared_ptr<Function> copy() const

Copy another instance of Function with the same context.

inline virtual vector<dtypes> in_types()

Get input dtypes.

Last in_type will be used repeatedly if size of in_types is smaller than size of inputs

inline virtual vector<dtypes> out_types()

Get output dtypes.

Last out_type will be used repeatedly if size of out_types is smaller than size of outputs

inline virtual int min_inputs()

Get minimum number of inputs.

This is meant to be used in setup function with in_types which is used to get maximum number of inputs.

inline virtual int min_outputs()

Get minimum number of outputs.

This is meant to be used in setup function with out_types which is used to get max number of outputs.

inline virtual string name()

Get function name in string.

inline virtual vector<string> allowed_array_classes()

Get array classes that are allowed to be specified by Context.

inline virtual bool grad_depends_output_data(int i, int o) const

Dependency flag for checking if in-grad depends on out-data.

Checking if i-th input’ gradient computation requires o-th output’s data or not.

Note

If any of inputs requires an output variable data when computing its gradient, this function must be overridden to return appropriate boolean value. Otherwise, backward computation will be incorrect.

Parameters:
  • i[in] Input variable index.

  • o[in] Output variable index.

template<typename T>
class ReLU : public nbla::BaseFunction<bool>
#include <relu.hpp>

Rectified Linear Unit (ReLU) defined as.

\[ y_i = \max (0, x_i). \]

Inputs:

  • N-D array.

Outputs:

  • N-D array.

Template Parameters:

T – Data type for computation.

Param inplace:

This option is obsolete and ignored. Output is never in-placed with input.

Public Functions

inline virtual shared_ptr<Function> copy() const

Copy another instance of Function with the same context.

inline virtual int min_inputs()

Get minimum number of inputs.

This is meant to be used in setup function with in_types which is used to get maximum number of inputs.

inline virtual int min_outputs()

Get minimum number of outputs.

This is meant to be used in setup function with out_types which is used to get max number of outputs.

inline virtual vector<dtypes> in_types()

Get input dtypes.

Last in_type will be used repeatedly if size of in_types is smaller than size of inputs

inline virtual vector<dtypes> out_types()

Get output dtypes.

Last out_type will be used repeatedly if size of out_types is smaller than size of outputs

inline virtual string name()

Get function name in string.

inline virtual vector<string> allowed_array_classes()

Get array classes that are allowed to be specified by Context.

inline virtual bool grad_depends_output_data(int i, int o) const

Dependency flag for checking if in-grad depends on out-data.

Checking if i-th input’ gradient computation requires o-th output’s data or not.

Note

If any of inputs requires an output variable data when computing its gradient, this function must be overridden to return appropriate boolean value. Otherwise, backward computation will be incorrect.

Parameters:
  • i[in] Input variable index.

  • o[in] Output variable index.

template<typename T>
class Reshape : public nbla::BaseFunction<const vector<int>&, bool>
#include <reshape.hpp>

Reshaping copy of an input variable.

Inputs:

  • N-D array.

Outputs:

  • N-D array.

Template Parameters:

T – Data type for computation.

Param shape:

Target shape.

Param inplace:

The output array is will be shared with the input array if true.

Public Functions

inline virtual shared_ptr<Function> copy() const

Copy another instance of Function with the same context.

inline virtual vector<dtypes> in_types()

Get input dtypes.

Last in_type will be used repeatedly if size of in_types is smaller than size of inputs

inline virtual vector<dtypes> out_types()

Get output dtypes.

Last out_type will be used repeatedly if size of out_types is smaller than size of outputs

inline virtual int min_inputs()

Get minimum number of inputs.

This is meant to be used in setup function with in_types which is used to get maximum number of inputs.

inline virtual int min_outputs()

Get minimum number of outputs.

This is meant to be used in setup function with out_types which is used to get max number of outputs.

inline virtual string name()

Get function name in string.

inline virtual vector<string> allowed_array_classes()

Get array classes that are allowed to be specified by Context.

inline virtual bool grad_depends_output_data(int i, int o) const

Dependency flag for checking if in-grad depends on out-data.

Checking if i-th input’ gradient computation requires o-th output’s data or not.

Note

If any of inputs requires an output variable data when computing its gradient, this function must be overridden to return appropriate boolean value. Otherwise, backward computation will be incorrect.

Parameters:
  • i[in] Input variable index.

  • o[in] Output variable index.

inline virtual int inplace_data(int i) const

Get in-place-level of i-th input variable’s data (see below).

0 (NOT_INPLACE): Not in-placed 1 (INPLACE_NOT_MODIFY): In-placed but not modified. 2 (INPLACE): In-placed and modified.

Note

If a subclass uses in-place computation, the function must override this function.

Parameters:

i[in] Input variable index.

Return values:

Returns – 0 by default.

inline virtual int inplace_data_with(int i) const

Get the output variable index where i-th variables’ data in-placed to.

Note

This is only valid if the i-th variable is in-placed. The maintainer of a sub-class function must override this function.

Parameters:

i[in] Input variable index.

template<typename T>
class RNN : public nbla::BaseFunction<int, const string&, float, bool, bool>
#include <rnn.hpp>

Todo:

Write doc.

Inputs:

Outputs:

Public Functions

inline virtual shared_ptr<Function> copy() const

Copy another instance of Function with the same context.

inline virtual int min_inputs()

Get minimum number of inputs.

This is meant to be used in setup function with in_types which is used to get maximum number of inputs.

inline virtual int min_outputs()

Get minimum number of outputs.

This is meant to be used in setup function with out_types which is used to get max number of outputs.

inline virtual vector<dtypes> in_types()

Get input dtypes.

Last in_type will be used repeatedly if size of in_types is smaller than size of inputs

inline virtual vector<dtypes> out_types()

Get output dtypes.

Last out_type will be used repeatedly if size of out_types is smaller than size of outputs

inline virtual vector<string> allowed_array_classes()

Get array classes that are allowed to be specified by Context.

inline virtual string name()

Get function name in string.

inline virtual bool grad_depends_output_data(int i, int o) const

Dependency flag for checking if in-grad depends on out-data.

Checking if i-th input’ gradient computation requires o-th output’s data or not.

Note

If any of inputs requires an output variable data when computing its gradient, this function must be overridden to return appropriate boolean value. Otherwise, backward computation will be incorrect.

Parameters:
  • i[in] Input variable index.

  • o[in] Output variable index.

template<typename T>
class RoiAlign : public nbla::BaseFunction<const vector<int>&, const vector<float>&, int, bool>
#include <roi_align.hpp>

Map Regions of Interest (RoI) defined by bounding `boxes` to features of `output_size` height and width using bilinear interpolation with `sampling_ratio` points in the interpolation grid.

Inputs:

  • Input array with shape \((N, H, W, C)\) or \((N, C, H, W)\).

  • Boxes array with shape \((K, 5)\) containing box coordinates in (b, x1, y1, x2, y2) format where b is the batch index.

Outputs:

  • N-D array with shape \((K, C, output\_size[0], output\_size[1])\) or \((K, output\_size[0], output\_size[1], C)\).

See also

Reference:

Template Parameters:

T – Data type for computation.

Param output_size:

The height and width of the output feature maps.

Param spatial_scale:

Scaling factor from box to input coordinates, as (x, y).

Param sampling_ratio:

The number of sampling points used for interpolation.

Param channel_last:

Whethe last dimension is considered as channel dimension.

Public Functions

inline virtual shared_ptr<Function> copy() const

Copy another instance of Function with the same context.

inline virtual int min_inputs()

Get minimum number of inputs.

This is meant to be used in setup function with in_types which is used to get maximum number of inputs.

inline virtual int min_outputs()

Get minimum number of outputs.

This is meant to be used in setup function with out_types which is used to get max number of outputs.

inline virtual vector<dtypes> in_types()

Get input dtypes.

Last in_type will be used repeatedly if size of in_types is smaller than size of inputs

inline virtual vector<dtypes> out_types()

Get output dtypes.

Last out_type will be used repeatedly if size of out_types is smaller than size of outputs

inline virtual vector<string> allowed_array_classes()

Get array classes that are allowed to be specified by Context.

inline virtual string name()

Get function name in string.

template<typename T>
class ScatterAdd : public nbla::BaseFunction<int>
#include <scatter_add.hpp>

Add all values from `x1` into the `x0` according to index specified by `indices`.

This function adds `x1` into the copy of `x0` and outputs the copy. The original `x0` will not be changed. `x0`, `indices` and `x1` must have same number of dimensions.

The forward of :func:`~nnabla.functions.scatter_add` is equivalent to:

def scatter_add(x0, indices, x1, axis):
    # Assuming each input is 3 dimensional
    import numpy as np
    output = np.copy(x0)
    for i in range(indices.shape[0]):
        for j in range(indices.shape[1]):
            for k in range(indices.shape[2]):
                if axis == 0:
                    output[indices[i][j][k]][j][k] += x1[i][j][k]
                elif axis == 1:
                    output[i][indices[i][j][k]][k] += x1[i][j][k]
                elif axis == 2:
                    output[i][j][indices[i][j][k]] += x1[i][j][k]
    return output

inputs:
  x0:
    doc: N-D array which the data is added to its copy.
  indices:
    doc: N-D array scatter indices. The size of each dimension must be equal
        or smaller than that of x0 except for the specified axis. The value
        of indices must be smaller than the size of specified axis' dimension
        of x0. The size of each dimension must be equal or smaller than that
        of x1. Indices must not be negative.
  x1:
    doc: N-D array which is scattered and added to x0.
arguments:
  axis:
    doc: Axis along which to index. The axis must not exceed the inputs'
        dimension.
    type: int64
    default: 0
outputs:
  y:
    doc: N-D array which contains the result of scatter addition. The shape is
        same as x0.

Inputs:

  • N-D array x0

  • N-D array scatter indices

  • N-D array x1

Outputs:

  • N-D array of shape same as x0

Param axis:

Axis along which to index

Public Functions

inline virtual shared_ptr<Function> copy() const

Copy another instance of Function with the same context.

inline virtual int min_inputs()

Get minimum number of inputs.

This is meant to be used in setup function with in_types which is used to get maximum number of inputs.

inline virtual int min_outputs()

Get minimum number of outputs.

This is meant to be used in setup function with out_types which is used to get max number of outputs.

inline virtual vector<dtypes> in_types()

Get input dtypes.

Last in_type will be used repeatedly if size of in_types is smaller than size of inputs

inline virtual vector<dtypes> out_types()

Get output dtypes.

Last out_type will be used repeatedly if size of out_types is smaller than size of outputs

inline virtual vector<string> allowed_array_classes()

Get array classes that are allowed to be specified by Context.

inline virtual string name()

Get function name in string.

inline virtual bool grad_depends_output_data(int i, int o) const

Dependency flag for checking if in-grad depends on out-data.

Checking if i-th input’ gradient computation requires o-th output’s data or not.

Note

If any of inputs requires an output variable data when computing its gradient, this function must be overridden to return appropriate boolean value. Otherwise, backward computation will be incorrect.

Parameters:
  • i[in] Input variable index.

  • o[in] Output variable index.

template<typename T>
class ScatterNd : public nbla::BaseFunction<const vector<int>&, bool>
#include <scatter_nd.hpp>

Scatter `data` into a new array of given `shape` according to `indices`.

This operation is the inverse of :func:`~nnabla.functions.gather_nd`.

The forward of :func:`~nnabla.functions.scatter_nd` is equivalent to:

def scatter_nd(data, indices, shape):
    import numpy as np
    if isinstance(indices, np.ndarray)
        indices = indices.tolist()
    result = np.zeros(shape, dtype=data.dtype)
    result[indices] = data
    return result

Examples:

  inputs:
    data:
      doc: N-D array input data.
    indices:
      doc: N-D array scatter indices.
  arguments:
    shape:
      doc: Shape of output variable.
      type: repeated int64
  outputs:
    y:
      doc: N-D array of given `shape`.

Inputs:

  • N-D array input data

  • N-D array scatter indices

Outputs:

  • N-D array of given `shape`

Param shape:

Shape of output variable.

Public Functions

inline virtual shared_ptr<Function> copy() const

Copy another instance of Function with the same context.

inline virtual int min_inputs()

Get minimum number of inputs.

This is meant to be used in setup function with in_types which is used to get maximum number of inputs.

inline virtual int min_outputs()

Get minimum number of outputs.

This is meant to be used in setup function with out_types which is used to get max number of outputs.

inline virtual vector<dtypes> in_types()

Get input dtypes.

Last in_type will be used repeatedly if size of in_types is smaller than size of inputs

inline virtual vector<dtypes> out_types()

Get output dtypes.

Last out_type will be used repeatedly if size of out_types is smaller than size of outputs

inline virtual vector<string> allowed_array_classes()

Get array classes that are allowed to be specified by Context.

inline virtual string name()

Get function name in string.

inline virtual int inplace_data(int i) const

Get in-place-level of i-th input variable’s data (see below).

0 (NOT_INPLACE): Not in-placed 1 (INPLACE_NOT_MODIFY): In-placed but not modified. 2 (INPLACE): In-placed and modified.

Note

If a subclass uses in-place computation, the function must override this function.

Parameters:

i[in] Input variable index.

Return values:

Returns – 0 by default.

inline virtual int inplace_data_with(int i) const

Get the output variable index where i-th variables’ data in-placed to.

Note

This is only valid if the i-th variable is in-placed. The maintainer of a sub-class function must override this function.

Parameters:

i[in] Input variable index.

inline virtual bool grad_depends_output_data(int i, int o) const

Dependency flag for checking if in-grad depends on out-data.

Checking if i-th input’ gradient computation requires o-th output’s data or not.

Note

If any of inputs requires an output variable data when computing its gradient, this function must be overridden to return appropriate boolean value. Otherwise, backward computation will be incorrect.

Parameters:
  • i[in] Input variable index.

  • o[in] Output variable index.

template<typename T>
class SearchSorted : public nbla::BaseFunction<bool>
#include <searchsorted.hpp>

SearchSorted searches for the index of values in the innermost dimension in sorted_sequence such that after inserting values in the sorted_sequence it remains sorted.

Inputs:

  • sorted_sequence: N-D array

  • values: N-D array

Outputs:

  • N-D array with the same shape as values

Param right:

if true last index is considered in case of repeated elements

Public Functions

inline virtual shared_ptr<Function> copy() const

Copy another instance of Function with the same context.

inline virtual int min_inputs()

Get minimum number of inputs.

This is meant to be used in setup function with in_types which is used to get maximum number of inputs.

inline virtual int min_outputs()

Get minimum number of outputs.

This is meant to be used in setup function with out_types which is used to get max number of outputs.

inline virtual vector<dtypes> in_types()

Get input dtypes.

Last in_type will be used repeatedly if size of in_types is smaller than size of inputs

inline virtual vector<dtypes> out_types()

Get output dtypes.

Last out_type will be used repeatedly if size of out_types is smaller than size of outputs

inline virtual vector<string> allowed_array_classes()

Get array classes that are allowed to be specified by Context.

inline virtual string name()

Get function name in string.

inline virtual bool grad_depends_output_data(int i, int o) const

Dependency flag for checking if in-grad depends on out-data.

Checking if i-th input’ gradient computation requires o-th output’s data or not.

Note

If any of inputs requires an output variable data when computing its gradient, this function must be overridden to return appropriate boolean value. Otherwise, backward computation will be incorrect.

Parameters:
  • i[in] Input variable index.

  • o[in] Output variable index.

template<typename T>
class SELU : public nbla::BaseFunction<double, double>
#include <selu.hpp>

The Scaled Exponential Linear Unit (SELU) function by Klambauer et al.

(2017), defined as

\[\begin{split} y_i= \lambda \left\{ \begin{array}{ll} x_i & (x > 0)\\ \alpha (\exp(x_i) - 1) & (x \leq 0) \end{array} \right.. \end{split}\]

The coefficients :math:`\lambda` and :math:`\alpha` default to the following values :math:`\lambda_{01}` and :math:`\alpha_{01}`, respectively, provided by Klambauer et al. (2017):

\[\begin{split} \begin{array}{lll} \lambda_{01} &=& \left( 1 - \operatorname{erfc}\left( \frac{1}{\sqrt{2}} \right) \sqrt{e} \right) \sqrt{2 \pi} \\ && \left( 2 \operatorname{erfc} \left( \sqrt{2} \right) e^2 + \pi \operatorname{erfc}\left( \frac{1}{\sqrt{2}} \right)^2 e \right. \\ && \left. - 2(2 + \pi) \operatorname{erfc} \left( \frac{1}{\sqrt{2}} \right) \sqrt{e} + \pi + 2 \right)^{-1/2} \\ &\approx& 1.0507 \\ \alpha_{01} &=& - \frac {\sqrt {\frac {2}{\pi}}} {\operatorname{erfc} \left( \frac{1}{\sqrt{2}} \right) \exp \left(\frac {1} {2} \right) - 1} \\ &\approx& 1.67326 \end{array} \end{split}\]

Inputs:

  • N-D array.

Outputs:

  • N-D array with the same shape as input.

See also

Klambauer, G., Unterthiner, T., Mayr, A., & Hochreiter, S. (2017). Self-Normalizing Neural Networks. In Advances in Neural Information Processing Systems (NIPS). https://arxiv.org/abs/1706.02515

Template Parameters:

T – Data type for computation.

Param scale:

The coefficient \(\lambda\) in the definition.

Param alpha:

The coefficient \(\alpha\) in the definition.

Public Functions

inline virtual shared_ptr<Function> copy() const

Copy another instance of Function with the same context.

inline virtual vector<dtypes> in_types()

Get input dtypes.

Last in_type will be used repeatedly if size of in_types is smaller than size of inputs

inline virtual vector<dtypes> out_types()

Get output dtypes.

Last out_type will be used repeatedly if size of out_types is smaller than size of outputs

inline virtual int min_inputs()

Get minimum number of inputs.

This is meant to be used in setup function with in_types which is used to get maximum number of inputs.

inline virtual int min_outputs()

Get minimum number of outputs.

This is meant to be used in setup function with out_types which is used to get max number of outputs.

inline virtual string name()

Get function name in string.

inline virtual vector<string> allowed_array_classes()

Get array classes that are allowed to be specified by Context.

inline virtual bool grad_depends_output_data(int i, int o) const

Dependency flag for checking if in-grad depends on out-data.

Checking if i-th input’ gradient computation requires o-th output’s data or not.

Note

If any of inputs requires an output variable data when computing its gradient, this function must be overridden to return appropriate boolean value. Otherwise, backward computation will be incorrect.

Parameters:
  • i[in] Input variable index.

  • o[in] Output variable index.

template<typename T>
class Shape : public nbla::BaseFunction<int, int>
#include <shape.hpp>

Shape Get the shape of specified tensor.

Inputs:

  • N-D array.

Outputs:

  • 1-D array.

Template Parameters:

T – Data type for computation.

Param start:

The start index of slice of shape array. If start axis is omit, the slice starts from 0.

Param end:

The end index of slice of shape array. If the end axis is omitted, the axes upto the last one will be included.

Public Functions

inline virtual shared_ptr<Function> copy() const

Copy another instance of Function with the same context.

inline virtual int min_inputs()

Get minimum number of inputs.

This is meant to be used in setup function with in_types which is used to get maximum number of inputs.

inline virtual int min_outputs()

Get minimum number of outputs.

This is meant to be used in setup function with out_types which is used to get max number of outputs.

inline virtual vector<dtypes> in_types()

Get input dtypes.

Last in_type will be used repeatedly if size of in_types is smaller than size of inputs

inline virtual vector<dtypes> out_types()

Get output dtypes.

Last out_type will be used repeatedly if size of out_types is smaller than size of outputs

inline virtual vector<string> allowed_array_classes()

Get array classes that are allowed to be specified by Context.

inline virtual string name()

Get function name in string.

template<typename T>
class Shift : public nbla::BaseFunction<const vector<int>&, const string&>
#include <shift.hpp>

Shift shifts the array elements by the specified amount.

Inputs:

  • N-D array.

Outputs:

  • N-D array.

Template Parameters:

T – Data type for computation.

Param shift:

The amount to shift elements. For example, to shift image data to the right by 2 pixels and up 3 pixels, specify (-3,2).

Param border_mode:

Specify how to process the ends of arrays whose values will be undetermined as a result of shifting. nearest: The data at the ends of the original array is copied and used. reflect: Original data reflected at the ends of the original array is used.

Public Functions

inline virtual shared_ptr<Function> copy() const

Copy another instance of Function with the same context.

inline virtual vector<dtypes> in_types()

Get input dtypes.

Last in_type will be used repeatedly if size of in_types is smaller than size of inputs

inline virtual vector<dtypes> out_types()

Get output dtypes.

Last out_type will be used repeatedly if size of out_types is smaller than size of outputs

inline virtual int min_inputs()

Get minimum number of inputs.

This is meant to be used in setup function with in_types which is used to get maximum number of inputs.

inline virtual int min_outputs()

Get minimum number of outputs.

This is meant to be used in setup function with out_types which is used to get max number of outputs.

inline virtual string name()

Get function name in string.

inline virtual vector<string> allowed_array_classes()

Get array classes that are allowed to be specified by Context.

inline virtual bool grad_depends_output_data(int i, int o) const

Dependency flag for checking if in-grad depends on out-data.

Checking if i-th input’ gradient computation requires o-th output’s data or not.

Note

If any of inputs requires an output variable data when computing its gradient, this function must be overridden to return appropriate boolean value. Otherwise, backward computation will be incorrect.

Parameters:
  • i[in] Input variable index.

  • o[in] Output variable index.

template<typename T, typename Tl>
class SigmoidCrossEntropy : public nbla::BaseFunction<>
#include <sigmoid_cross_entropy.hpp>

SigmoidCrossEntropy calculate the element-wise cross entropy between the variable and label variables with Sigmoid.

\[ y_i = - \left(x^{(1)}_i * \ln \left(\sigma \left(x^{(0)}_i \right)\right) + \left(1 - x^{(1)}_i\right) * \ln \left(1 - \sigma \left(x^{(0)}_i \right)\right)\right) \]
where \(\sigma(s)=\frac{1}{1+\exp(-s)}\).

SigmoidCrossEntropy is equivalent to Sigmoid+BinaryCrossEntropy, but computing them at once has the effect of reducing computational error.

Inputs:

  • Scores N-D array. The value lies in \([-\infty, \infty]\).

  • Labels N-D array. Only 0 or 1 value is allowed.

Outputs:

  • Element-wise losses N-D array.

Template Parameters:

T – Data type for computation.

Public Functions

inline virtual shared_ptr<Function> copy() const

Copy another instance of Function with the same context.

inline virtual vector<dtypes> in_types()

Get input dtypes.

Last in_type will be used repeatedly if size of in_types is smaller than size of inputs

inline virtual vector<dtypes> out_types()

Get output dtypes.

Last out_type will be used repeatedly if size of out_types is smaller than size of outputs

inline virtual int min_inputs()

Get minimum number of inputs.

This is meant to be used in setup function with in_types which is used to get maximum number of inputs.

inline virtual int min_outputs()

Get minimum number of outputs.

This is meant to be used in setup function with out_types which is used to get max number of outputs.

inline virtual string name()

Get function name in string.

inline virtual vector<string> allowed_array_classes()

Get array classes that are allowed to be specified by Context.

inline virtual bool grad_depends_output_data(int i, int o) const

Dependency flag for checking if in-grad depends on out-data.

Checking if i-th input’ gradient computation requires o-th output’s data or not.

Note

If any of inputs requires an output variable data when computing its gradient, this function must be overridden to return appropriate boolean value. Otherwise, backward computation will be incorrect.

Parameters:
  • i[in] Input variable index.

  • o[in] Output variable index.

template<typename T>
class Sink : public nbla::BaseFunction<bool>
#include <sink.hpp>

Todo:

PLACE HERE FUNCTION DOCUMENTATION.

Public Functions

inline virtual shared_ptr<Function> copy() const

Copy another instance of Function with the same context.

inline virtual vector<dtypes> in_types()

Get input dtypes.

Last in_type will be used repeatedly if size of in_types is smaller than size of inputs

inline virtual vector<dtypes> out_types()

Get output dtypes.

Last out_type will be used repeatedly if size of out_types is smaller than size of outputs

inline virtual int min_inputs()

Get minimum number of inputs.

This is meant to be used in setup function with in_types which is used to get maximum number of inputs.

inline virtual int min_outputs()

Get minimum number of outputs.

This is meant to be used in setup function with out_types which is used to get max number of outputs.

inline virtual string name()

Get function name in string.

inline virtual vector<string> allowed_array_classes()

Get array classes that are allowed to be specified by Context.

inline virtual bool prohibit_clear_input_buffers() const

A flag for preventing that the graph engine clears buffers of input variables even if clear_buffer is true and condition mets.

inline virtual bool prohibit_zero_input_grad() const

A flag for preventing that the graph engine sets input gradient buffer as 0 even when accum is true.

inline virtual bool grad_depends_output_data(int i, int o) const

Dependency flag for checking if in-grad depends on out-data.

Checking if i-th input’ gradient computation requires o-th output’s data or not.

Note

If any of inputs requires an output variable data when computing its gradient, this function must be overridden to return appropriate boolean value. Otherwise, backward computation will be incorrect.

Parameters:
  • i[in] Input variable index.

  • o[in] Output variable index.

template<typename T>
class Slice : public nbla::BaseFunction<const vector<int>&, const vector<int>&, const vector<int>&>
#include <slice.hpp>

Slice arrays along specified axis.

Inputs:

  • N-D array.

Outputs:

  • M-D array.

Slice input tensor. y = x[start[0]:stop[0]:step[0], start[1]:stop[1]:step[1], …]

Template Parameters:

T – Data type for computation.

Public Functions

inline virtual shared_ptr<Function> copy() const

Copy another instance of Function with the same context.

inline virtual vector<dtypes> in_types()

Get input dtypes.

Last in_type will be used repeatedly if size of in_types is smaller than size of inputs

inline virtual vector<dtypes> out_types()

Get output dtypes.

Last out_type will be used repeatedly if size of out_types is smaller than size of outputs

inline virtual int min_inputs()

Get minimum number of inputs.

This is meant to be used in setup function with in_types which is used to get maximum number of inputs.

inline virtual int min_outputs()

Get minimum number of outputs.

This is meant to be used in setup function with out_types which is used to get max number of outputs.

inline virtual string name()

Get function name in string.

inline virtual vector<string> allowed_array_classes()

Get array classes that are allowed to be specified by Context.

inline virtual bool grad_depends_output_data(int i, int o) const

Dependency flag for checking if in-grad depends on out-data.

Checking if i-th input’ gradient computation requires o-th output’s data or not.

Note

If any of inputs requires an output variable data when computing its gradient, this function must be overridden to return appropriate boolean value. Otherwise, backward computation will be incorrect.

Parameters:
  • i[in] Input variable index.

  • o[in] Output variable index.

template<typename T>
class Softmax : public nbla::BaseFunction<int>
#include <softmax.hpp>

Softmax normalization defined as.

\[ y_i = \frac{\exp(x_i)}{\sum_j exp(x_j)} \]
along dimension specified by axis.

Inputs:

  • N-D array.

Outputs:

  • N-D array with the same shape as input.

Template Parameters:

T – Data type for computation.

Param axis:

Axis normalization is taken.

Public Functions

inline virtual shared_ptr<Function> copy() const

Copy another instance of Function with the same context.

inline virtual vector<dtypes> in_types()

Get input dtypes.

Last in_type will be used repeatedly if size of in_types is smaller than size of inputs

inline virtual vector<dtypes> out_types()

Get output dtypes.

Last out_type will be used repeatedly if size of out_types is smaller than size of outputs

inline virtual int min_inputs()

Get minimum number of inputs.

This is meant to be used in setup function with in_types which is used to get maximum number of inputs.

inline virtual int min_outputs()

Get minimum number of outputs.

This is meant to be used in setup function with out_types which is used to get max number of outputs.

inline virtual string name()

Get function name in string.

inline virtual vector<string> allowed_array_classes()

Get array classes that are allowed to be specified by Context.

inline virtual bool grad_depends_output_data(int i, int o) const

Dependency flag for checking if in-grad depends on out-data.

Checking if i-th input’ gradient computation requires o-th output’s data or not.

Note

If any of inputs requires an output variable data when computing its gradient, this function must be overridden to return appropriate boolean value. Otherwise, backward computation will be incorrect.

Parameters:
  • i[in] Input variable index.

  • o[in] Output variable index.

template<typename T, typename Tl = int>
class SoftmaxCrossEntropy : public nbla::BaseFunction<int>
#include <softmax_cross_entropy.hpp>

SoftmaxCrossEntropy calculate the element-wise cross entropy between the variables and the variables of a label given by a category index with Softmax normalization.

\[ y_{j} = -\ln \left(\frac{\exp(x_{t_j,j})}{\sum_{i'} exp(x_{i'j})}\right) \]
along dimension specified by axis.

SoftmaxCrossEntropy is equivalent to Softmax+CategoricalCrossEntropy, but computing them at once has the effect of reducing computational error.

Inputs (i is axis normalization taken):

  • Scores N-D array. ( \(D_1 \times ... \times D_i \times ... \times D_N\))

  • Labels N-D array. ( \(D_1 \times ... \times 1 \times ... \times D_N\))

Outputs:

  • Element-wise losses N-D array. ( \(D_1 \times ... \times 1 \times ... \times D_N\))

Template Parameters:
  • T – Data type for computation and score variable.

  • Tl – Data type of label variable.

Param axis:

Axis normalization is taken.

Public Functions

inline virtual shared_ptr<Function> copy() const

Copy another instance of Function with the same context.

inline virtual vector<dtypes> in_types()

Get input dtypes.

Last in_type will be used repeatedly if size of in_types is smaller than size of inputs

inline virtual vector<dtypes> out_types()

Get output dtypes.

Last out_type will be used repeatedly if size of out_types is smaller than size of outputs

inline virtual int min_inputs()

Get minimum number of inputs.

This is meant to be used in setup function with in_types which is used to get maximum number of inputs.

inline virtual int min_outputs()

Get minimum number of outputs.

This is meant to be used in setup function with out_types which is used to get max number of outputs.

inline virtual string name()

Get function name in string.

inline virtual vector<string> allowed_array_classes()

Get array classes that are allowed to be specified by Context.

inline virtual bool grad_depends_output_data(int i, int o) const

Dependency flag for checking if in-grad depends on out-data.

Checking if i-th input’ gradient computation requires o-th output’s data or not.

Note

If any of inputs requires an output variable data when computing its gradient, this function must be overridden to return appropriate boolean value. Otherwise, backward computation will be incorrect.

Parameters:
  • i[in] Input variable index.

  • o[in] Output variable index.

template<typename T>
class Sort : public nbla::BaseFunction<int, bool, bool, bool>
#include <sort.hpp>

Sorts the elements of `x` along a given `axis` in ascending order by value.

A negative `axis` counts from the last dimension of `x`, so the default of -1 sorts along the last dimension. If `reverse` is True, then the elements are soreted in descending order.

If `with_index` is True, result is a tuple ``(sorted, indices)`` or only ``indices`` if `only_index` is True. Setting `only_index` to True implies that `with_index` is also True.

Inputs:

  • N-D array

Outputs:

  • one or two N-D arrays

Template Parameters:

T – Data type for computation.

Param axis:

Axis along which to sort.

Param reverse:

Sort in descending order.

Param with_index:

Return sorted values and index.

Param only_index:

Return only the sort index.

Public Functions

inline virtual shared_ptr<Function> copy() const

Copy another instance of Function with the same context.

inline virtual int min_inputs()

Get minimum number of inputs.

This is meant to be used in setup function with in_types which is used to get maximum number of inputs.

inline virtual int min_outputs()

Get minimum number of outputs.

This is meant to be used in setup function with out_types which is used to get max number of outputs.

inline virtual vector<dtypes> in_types()

Get input dtypes.

Last in_type will be used repeatedly if size of in_types is smaller than size of inputs

inline virtual vector<dtypes> out_types()

Get output dtypes.

Last out_type will be used repeatedly if size of out_types is smaller than size of outputs

inline virtual vector<string> allowed_array_classes()

Get array classes that are allowed to be specified by Context.

inline virtual string name()

Get function name in string.

inline virtual bool grad_depends_output_data(int i, int o) const

Dependency flag for checking if in-grad depends on out-data.

Checking if i-th input’ gradient computation requires o-th output’s data or not.

Note

If any of inputs requires an output variable data when computing its gradient, this function must be overridden to return appropriate boolean value. Otherwise, backward computation will be incorrect.

Parameters:
  • i[in] Input variable index.

  • o[in] Output variable index.

template<typename T>
class SpectralNorm : public nbla::BaseFunction<int, int, float, bool, bool>
#include <spectral_norm.hpp>

Spectral Normalization.

\[ W_{sn} = \frac{W}{\sigma(W)}. \]

Inputs:

  • N-D array of learnable weights. This is normally network parameter.

  • 1-D array of singular vector. When `test == false`, the data region will be updated during forward calculation.

Outputs:

  • Spectrally normalized \(W_{sn}\) with the same shape as \(W\).

See also

Takeru Miyato, Toshiki Kataoka, Masanori Koyama, Yuichi Yoshida, “Spectral

Normalization for Generative Adversarial Networks”, International Conference on Learning Representations. 2018.

Template Parameters:

T – Data type for computation.

Param dim:

Output dimension. If the dimension is not 0, then the specified dimension becomes the most-left dimension by transposing.

Param itr:

Number of power iterations.

Param eps:

Epsilon for the normalization. This `eps` is added before taking the sqrt in the norm computation.

Param test:

When in `true`, `u` will not be updated.

Public Functions

inline virtual shared_ptr<Function> copy() const

Copy another instance of Function with the same context.

inline virtual int min_inputs()

Get minimum number of inputs.

This is meant to be used in setup function with in_types which is used to get maximum number of inputs.

inline virtual int min_outputs()

Get minimum number of outputs.

This is meant to be used in setup function with out_types which is used to get max number of outputs.

inline virtual vector<dtypes> in_types()

Get input dtypes.

Last in_type will be used repeatedly if size of in_types is smaller than size of inputs

inline virtual vector<dtypes> out_types()

Get output dtypes.

Last out_type will be used repeatedly if size of out_types is smaller than size of outputs

inline virtual vector<string> allowed_array_classes()

Get array classes that are allowed to be specified by Context.

inline virtual string name()

Get function name in string.

inline virtual bool grad_depends_output_data(int i, int o) const

Dependency flag for checking if in-grad depends on out-data.

Checking if i-th input’ gradient computation requires o-th output’s data or not.

Note

If any of inputs requires an output variable data when computing its gradient, this function must be overridden to return appropriate boolean value. Otherwise, backward computation will be incorrect.

Parameters:
  • i[in] Input variable index.

  • o[in] Output variable index.

inline virtual bool need_setup_recompute(int o) const

A flag for checking if setup_recompute() is needed.

Checking if o-th output’ data requires setup_recompute().

Note

setup_recompute() will skipped during forward execution if none of outputs requires setup_recompute().

Parameters:

o[in] Output variable index.

template<typename T>
class Split : public nbla::BaseFunction<int>
#include <split.hpp>

Split arrays at axis.

Inputs:

  • N-D array.

Outputs: outputs must have the same shapes.

  • list of N-D array.

Template Parameters:

T – Data type for computation.

Param axis:

Axis along which to split.

Public Functions

inline virtual shared_ptr<Function> copy() const

Copy another instance of Function with the same context.

inline virtual vector<dtypes> in_types()

Get input dtypes.

Last in_type will be used repeatedly if size of in_types is smaller than size of inputs

inline virtual vector<dtypes> out_types()

Get output dtypes.

Last out_type will be used repeatedly if size of out_types is smaller than size of outputs

inline virtual int min_inputs()

Get minimum number of inputs.

This is meant to be used in setup function with in_types which is used to get maximum number of inputs.

inline virtual int min_outputs()

Get minimum number of outputs.

This is meant to be used in setup function with out_types which is used to get max number of outputs.

inline virtual string name()

Get function name in string.

inline virtual vector<string> allowed_array_classes()

Get array classes that are allowed to be specified by Context.

inline virtual bool grad_depends_output_data(int i, int o) const

Dependency flag for checking if in-grad depends on out-data.

Checking if i-th input’ gradient computation requires o-th output’s data or not.

Note

If any of inputs requires an output variable data when computing its gradient, this function must be overridden to return appropriate boolean value. Otherwise, backward computation will be incorrect.

Parameters:
  • i[in] Input variable index.

  • o[in] Output variable index.

template<typename T>
class Stack : public nbla::BaseFunction<int>
#include <stack.hpp>

Stack joins two or more arrays on a new axis.

The sizes of all the arrays to be stacked must be the same. Unlike Concatenate, which joins arrays on an existing axis, Stack joins arrays on a new axis.

Inputs:

  • list of N-D arrays.

Outputs:

  • N-D array.

Template Parameters:

T – Data type for computation.

Param axis:

The axis on which to concatenate arrays. Axis indexes take on values 0, 1, 2, and so on from the left. For example, to stack four (3,28,28) inputs on the second axis, specify 1. In this case, the output size will be (3,4,28,28).

Public Functions

inline virtual shared_ptr<Function> copy() const

Copy another instance of Function with the same context.

inline virtual vector<dtypes> in_types()

Get input dtypes.

Last in_type will be used repeatedly if size of in_types is smaller than size of inputs

inline virtual vector<dtypes> out_types()

Get output dtypes.

Last out_type will be used repeatedly if size of out_types is smaller than size of outputs

inline virtual int min_inputs()

Get minimum number of inputs.

This is meant to be used in setup function with in_types which is used to get maximum number of inputs.

inline virtual int min_outputs()

Get minimum number of outputs.

This is meant to be used in setup function with out_types which is used to get max number of outputs.

inline virtual string name()

Get function name in string.

inline virtual vector<string> allowed_array_classes()

Get array classes that are allowed to be specified by Context.

inline virtual bool grad_depends_output_data(int i, int o) const

Dependency flag for checking if in-grad depends on out-data.

Checking if i-th input’ gradient computation requires o-th output’s data or not.

Note

If any of inputs requires an output variable data when computing its gradient, this function must be overridden to return appropriate boolean value. Otherwise, backward computation will be incorrect.

Parameters:
  • i[in] Input variable index.

  • o[in] Output variable index.

template<typename T>
class Sum : public nbla::BaseFunction<const vector<int>&, bool>
#include <sum.hpp>

Reduction along axes with sum operation.

Template Parameters:

T – Data type for computation.

Param axes:

A list of axes to be reduced.

Param keep_dims:

Flag whether the reduced axes are kept.

Subclassed by nbla::Max< T >, nbla::Mean< T >, nbla::Prod< T >

Public Functions

inline virtual shared_ptr<Function> copy() const

Copy another instance of Function with the same context.

inline virtual vector<dtypes> in_types()

Get input dtypes.

Last in_type will be used repeatedly if size of in_types is smaller than size of inputs

inline virtual vector<dtypes> out_types()

Get output dtypes.

Last out_type will be used repeatedly if size of out_types is smaller than size of outputs

inline virtual int min_inputs()

Get minimum number of inputs.

This is meant to be used in setup function with in_types which is used to get maximum number of inputs.

inline virtual int min_outputs()

Get minimum number of outputs.

This is meant to be used in setup function with out_types which is used to get max number of outputs.

inline virtual string name()

Get function name in string.

inline virtual vector<string> allowed_array_classes()

Get array classes that are allowed to be specified by Context.

inline virtual bool grad_depends_output_data(int i, int o) const

Dependency flag for checking if in-grad depends on out-data.

Checking if i-th input’ gradient computation requires o-th output’s data or not.

Note

If any of inputs requires an output variable data when computing its gradient, this function must be overridden to return appropriate boolean value. Otherwise, backward computation will be incorrect.

Parameters:
  • i[in] Input variable index.

  • o[in] Output variable index.

template<typename T>
class SumPooling : public nbla::BasePooling<T, const vector<int>&, const vector<int>&, bool, const vector<int>&, bool>
#include <sum_pooling.hpp>

Sum pooling operator.

Inputs:

  • N-D array that has more than two dimensions.

Outputs:

  • N-D array.

Note

Only 2D pooling supported so far.

Template Parameters:

T – Data type for computation.

Param kernel:

Shapes of kernel.

Param stride:

Subsampling factors of pooling.

Param ignore_border:

If false, a kernel overlapping border is also considered for output unlike convolution.

Param pad:

Border padding values of dimensions. Padding will be added both sides of the dimension.

Public Functions

inline virtual shared_ptr<Function> copy() const

Copy another instance of Function with the same context.

inline virtual string name()

Get function name in string.

inline virtual bool grad_depends_output_data(int i, int o) const

Dependency flag for checking if in-grad depends on out-data.

Checking if i-th input’ gradient computation requires o-th output’s data or not.

Note

If any of inputs requires an output variable data when computing its gradient, this function must be overridden to return appropriate boolean value. Otherwise, backward computation will be incorrect.

Parameters:
  • i[in] Input variable index.

  • o[in] Output variable index.

template<typename T>
class SyncBatchNormalization : public nbla::BatchNormalization<T>
#include <sync_batch_normalization.hpp>

Batch normalization with sync between other processes at training time defined as.

\[\begin{split} \begin{array}{lcl} \mu &=& \frac{1}{M} \sum x_i\\ \sigma^2 &=& \frac{1}{M} \left(\sum x_i - \mu\right)^2\\ \hat{x}_i &=& \frac{x_i - \mu}{\sqrt{\sigma^2 + \epsilon}} \\ y_i &=& \hat{x}_i \gamma + \beta. \end{array} \end{split}\]

Inputs:

  • N-D array of input.

  • N-D array of beta which is learned.

  • N-D array of gamma which is learned.

  • N-D array of running mean (modified during forward execution).

  • N-D array of running variance (modified during forward execution).

Outputs (1 or 3):

  • N-D array.

  • (Optional) N-D array of batch mean.

  • (Optional) N-D array of batch variance.

See also

Implementing Synchronized Multi-GPU Batch Normalization https://hangzhang.org/PyTorch-Encoding/notes/syncbn.html

Template Parameters:

T – Data type for computation.

Param comm:

The communicator

Param group:

The name of the communicator group

Param axes:

Axes mean and variance are taken.

Param decay_rate:

Decay rate of running mean and variance.

Param eps:

Tiny value to avoid zero division by std.

Public Functions

inline virtual shared_ptr<Function> copy() const override

Copy another instance of Function with the same context.

inline virtual string name() override

Get function name in string.

inline virtual bool grad_depends_output_data(int i, int o) const

Dependency flag for checking if in-grad depends on out-data.

Checking if i-th input’ gradient computation requires o-th output’s data or not.

Note

If any of inputs requires an output variable data when computing its gradient, this function must be overridden to return appropriate boolean value. Otherwise, backward computation will be incorrect.

Parameters:
  • i[in] Input variable index.

  • o[in] Output variable index.

class BatchNormalizationInOutAdapter
template<typename T>
class TensorNormalization : public nbla::BaseFunction<const vector<int>&, float, bool, bool>
#include <tensor_normalization.hpp>

Todo:

Write doc.

Inputs:

Outputs:

Public Functions

inline virtual shared_ptr<Function> copy() const

Copy another instance of Function with the same context.

inline virtual int min_inputs()

Get minimum number of inputs.

This is meant to be used in setup function with in_types which is used to get maximum number of inputs.

inline virtual int min_outputs()

Get minimum number of outputs.

This is meant to be used in setup function with out_types which is used to get max number of outputs.

inline virtual vector<dtypes> in_types()

Get input dtypes.

Last in_type will be used repeatedly if size of in_types is smaller than size of inputs

inline virtual vector<dtypes> out_types()

Get output dtypes.

Last out_type will be used repeatedly if size of out_types is smaller than size of outputs

inline virtual vector<string> allowed_array_classes()

Get array classes that are allowed to be specified by Context.

inline virtual string name()

Get function name in string.

inline virtual bool grad_depends_output_data(int i, int o) const

Dependency flag for checking if in-grad depends on out-data.

Checking if i-th input’ gradient computation requires o-th output’s data or not.

Note

If any of inputs requires an output variable data when computing its gradient, this function must be overridden to return appropriate boolean value. Otherwise, backward computation will be incorrect.

Parameters:
  • i[in] Input variable index.

  • o[in] Output variable index.

template<typename T>
class Tile : public nbla::BaseFunction<const vector<int>&>
#include <tile.hpp>

Forward input `x` repeated the number of times given by `reps`.

If `reps` is a sequence, the output has dimension of ``d = max(len(reps), x.ndim)`` and either `x` is promoted to be d-dimensional by prepending new axes or `reps` is promoted to x.ndim by prepending 1’s.

Inputs:

  • N-D array

Outputs:

  • N-D array

Param reps:

Repetitions of input `x` along each axis.

Public Functions

inline virtual shared_ptr<Function> copy() const

Copy another instance of Function with the same context.

inline virtual int min_inputs()

Get minimum number of inputs.

This is meant to be used in setup function with in_types which is used to get maximum number of inputs.

inline virtual int min_outputs()

Get minimum number of outputs.

This is meant to be used in setup function with out_types which is used to get max number of outputs.

inline virtual vector<dtypes> in_types()

Get input dtypes.

Last in_type will be used repeatedly if size of in_types is smaller than size of inputs

inline virtual vector<dtypes> out_types()

Get output dtypes.

Last out_type will be used repeatedly if size of out_types is smaller than size of outputs

inline virtual vector<string> allowed_array_classes()

Get array classes that are allowed to be specified by Context.

inline virtual string name()

Get function name in string.

inline virtual bool grad_depends_output_data(int i, int o) const

Dependency flag for checking if in-grad depends on out-data.

Checking if i-th input’ gradient computation requires o-th output’s data or not.

Note

If any of inputs requires an output variable data when computing its gradient, this function must be overridden to return appropriate boolean value. Otherwise, backward computation will be incorrect.

Parameters:
  • i[in] Input variable index.

  • o[in] Output variable index.

template<typename T>
class TopKData : public nbla::BaseFunction<int, bool, bool, int, bool, bool>
#include <top_k_data.hpp>

Select the `k` largest values from each sample in `x` to propagate unmodified and set all other values to 0.

If `abs` is True, the `k` largest values are selected by magnitude. If `reduce` is True (the default), all feature dimensions are reduced to a single dimension of size `k` that propagates only the `k` largest values. Otherwise, if `reduce` is False, input and output dimensions are identical. Dimensions before `base_axis` are treated as number of sample dimensions and `k` values get selected from all elements of a sample (dimensions from `base_axis`) regardless of shape.

Inputs:

  • N-D array

Outputs (1 or 2):

  • N-D array.

  • (Optional) N-D array of top-k indices.

Template Parameters:

T – Data type for computation.

Param k:

Number of largest data values to propagate.

Param abs:

Determine largest data values by magnitude.

Param reduce:

Reduce feature size to one dimension of size `k`.

Param base_axis:

First dimension of the sample shape.

Param largest:

Whether to select the `k` largest or smallest values.

Param with_index:

Return top-k values and indices.

Public Functions

inline virtual shared_ptr<Function> copy() const

Copy another instance of Function with the same context.

inline virtual int min_inputs()

Get minimum number of inputs.

This is meant to be used in setup function with in_types which is used to get maximum number of inputs.

inline virtual int min_outputs()

Get minimum number of outputs.

This is meant to be used in setup function with out_types which is used to get max number of outputs.

inline virtual vector<dtypes> in_types()

Get input dtypes.

Last in_type will be used repeatedly if size of in_types is smaller than size of inputs

inline virtual vector<dtypes> out_types()

Get output dtypes.

Last out_type will be used repeatedly if size of out_types is smaller than size of outputs

inline virtual vector<string> allowed_array_classes()

Get array classes that are allowed to be specified by Context.

inline virtual string name()

Get function name in string.

inline virtual bool grad_depends_output_data(int i, int o) const

Dependency flag for checking if in-grad depends on out-data.

Checking if i-th input’ gradient computation requires o-th output’s data or not.

Note

If any of inputs requires an output variable data when computing its gradient, this function must be overridden to return appropriate boolean value. Otherwise, backward computation will be incorrect.

Parameters:
  • i[in] Input variable index.

  • o[in] Output variable index.

template<typename T>
class TopKGrad : public nbla::BaseFunction<int, bool, int>
#include <top_k_grad.hpp>

Select the `k` largest gradients for each sample in `x` to back-propagate unmodified and set all other gradients to 0.

If `abs` is True, the `k` largest gradients are selected by magnitude. Dimensions before `base_axis` are treated as number of sample dimensions and `k` gradients get selected from all gradients of a sample (dimensions from `base_axis`) regardless of shape.

Inputs:

  • N-D array

Outputs:

  • N-D array with same shape and data as `x`.

Template Parameters:

T – Data type for computation.

Param k:

Number of largest gradients to back-propagate.

Param abs:

Determine largest gradients by magnitude.

Param base_axis:

First dimension of the sample shape.

Public Functions

inline virtual shared_ptr<Function> copy() const

Copy another instance of Function with the same context.

inline virtual int min_inputs()

Get minimum number of inputs.

This is meant to be used in setup function with in_types which is used to get maximum number of inputs.

inline virtual int min_outputs()

Get minimum number of outputs.

This is meant to be used in setup function with out_types which is used to get max number of outputs.

inline virtual vector<dtypes> in_types()

Get input dtypes.

Last in_type will be used repeatedly if size of in_types is smaller than size of inputs

inline virtual vector<dtypes> out_types()

Get output dtypes.

Last out_type will be used repeatedly if size of out_types is smaller than size of outputs

inline virtual vector<string> allowed_array_classes()

Get array classes that are allowed to be specified by Context.

inline virtual string name()

Get function name in string.

inline virtual bool grad_depends_output_data(int i, int o) const

Dependency flag for checking if in-grad depends on out-data.

Checking if i-th input’ gradient computation requires o-th output’s data or not.

Note

If any of inputs requires an output variable data when computing its gradient, this function must be overridden to return appropriate boolean value. Otherwise, backward computation will be incorrect.

Parameters:
  • i[in] Input variable index.

  • o[in] Output variable index.

template<typename T, typename T1>
class TopNError : public nbla::BaseFunction<int, int>
#include <top_n_error.hpp>

Top N error along dimension specified by axis.

\[\begin{split} y_i = \left \{ \begin{array}{l} 1, &(x_i \text{ is not within Nth place}) \\ 0, &(x_i \text{ is within Nth place}) \end{array} \right. \end{split}\]

Inputs (i is the axis on which the top N error is calculated):

  • Probabilities N-D array. ( \(D_1 \times ... \times D_i \times ... \times D_N\))

  • Labels N-D array. ( \(D_1 \times ... \times 1 \times ... \times D_N\))

Outputs:

  • Element-wise error N-D array. ( \(D_1 \times ... \times 1 \times ... \times D_N\))

Template Parameters:
  • T – Data type for computation and score variable.

  • Tl – Data type of label variable.

Param axis:

Axis on which the top N error is calculated.

Public Functions

inline virtual shared_ptr<Function> copy() const

Copy another instance of Function with the same context.

inline virtual vector<dtypes> in_types()

Get input dtypes.

Last in_type will be used repeatedly if size of in_types is smaller than size of inputs

inline virtual vector<dtypes> out_types()

Get output dtypes.

Last out_type will be used repeatedly if size of out_types is smaller than size of outputs

inline virtual int min_inputs()

Get minimum number of inputs.

This is meant to be used in setup function with in_types which is used to get maximum number of inputs.

inline virtual int min_outputs()

Get minimum number of outputs.

This is meant to be used in setup function with out_types which is used to get max number of outputs.

inline virtual string name()

Get function name in string.

inline virtual vector<string> allowed_array_classes()

Get array classes that are allowed to be specified by Context.

inline virtual bool grad_depends_output_data(int i, int o) const

Dependency flag for checking if in-grad depends on out-data.

Checking if i-th input’ gradient computation requires o-th output’s data or not.

Note

If any of inputs requires an output variable data when computing its gradient, this function must be overridden to return appropriate boolean value. Otherwise, backward computation will be incorrect.

Parameters:
  • i[in] Input variable index.

  • o[in] Output variable index.

template<typename T>
class Transpose : public nbla::BaseFunction<const vector<int>&>
#include <transpose.hpp>

Transpose The function for tensor dimension conversion.

Inputs:

  • N-D array.

Outputs:

  • N-D array.

Template Parameters:

T – Data type for computation.

Param axes:

Transposing index

Public Functions

inline virtual shared_ptr<Function> copy() const

Copy another instance of Function with the same context.

inline virtual vector<dtypes> in_types()

Get input dtypes.

Last in_type will be used repeatedly if size of in_types is smaller than size of inputs

inline virtual vector<dtypes> out_types()

Get output dtypes.

Last out_type will be used repeatedly if size of out_types is smaller than size of outputs

inline virtual int min_inputs()

Get minimum number of inputs.

This is meant to be used in setup function with in_types which is used to get maximum number of inputs.

inline virtual int min_outputs()

Get minimum number of outputs.

This is meant to be used in setup function with out_types which is used to get max number of outputs.

inline virtual string name()

Get function name in string.

inline virtual vector<string> allowed_array_classes()

Get array classes that are allowed to be specified by Context.

inline virtual bool grad_depends_output_data(int i, int o) const

Dependency flag for checking if in-grad depends on out-data.

Checking if i-th input’ gradient computation requires o-th output’s data or not.

Note

If any of inputs requires an output variable data when computing its gradient, this function must be overridden to return appropriate boolean value. Otherwise, backward computation will be incorrect.

Parameters:
  • i[in] Input variable index.

  • o[in] Output variable index.

template<typename T>
class Trilu : public nbla::BaseFunction<int, bool>

Public Functions

inline virtual shared_ptr<Function> copy() const

Copy another instance of Function with the same context.

inline virtual vector<dtypes> in_types()

Get input dtypes.

Last in_type will be used repeatedly if size of in_types is smaller than size of inputs

inline virtual vector<dtypes> out_types()

Get output dtypes.

Last out_type will be used repeatedly if size of out_types is smaller than size of outputs

inline virtual int min_inputs()

Get minimum number of inputs.

This is meant to be used in setup function with in_types which is used to get maximum number of inputs.

inline virtual int min_outputs()

Get minimum number of outputs.

This is meant to be used in setup function with out_types which is used to get max number of outputs.

inline virtual string name()

Get function name in string.

inline virtual vector<string> allowed_array_classes()

Get array classes that are allowed to be specified by Context.

inline virtual bool grad_depends_output_data(int i, int o) const

Dependency flag for checking if in-grad depends on out-data.

Checking if i-th input’ gradient computation requires o-th output’s data or not.

Note

If any of inputs requires an output variable data when computing its gradient, this function must be overridden to return appropriate boolean value. Otherwise, backward computation will be incorrect.

Parameters:
  • i[in] Input variable index.

  • o[in] Output variable index.

template<typename T>
class Unique : public nbla::BaseFunction<bool, int, bool, bool, bool, bool>
#include <unique.hpp>

Find the unique elements of input array.

Inputs:

  • x: A N-D array.

Outputs:

  • y: A N-D array.

  • indices: A 1-D array. It’s indices of `y` elements first occurance in `x`. If `flatten` is True, it contains indices to flattend input array `x`. If `flatten` is False and `axis` is specified, it contains indices to input array `x` on `axis`.

  • inverse_indices: A 1-D array. It’s indices of `x` elements corresponding to `y`. If `flatten` is True, it contains indices to output array `y`. If `flatten` is False and `axis` is specified, it contains indices to output array `y` on `axis`.

  • counts: A 1-D array. It’s the count of each element of ‘y’ in input array `x`.

Param flatten:

If True, unique values of the flatten input array are returned.

Param axis:

If flatten is True and axis is specified, unique slices along axis are returned.

Param sorted:

If True, unique values/slices sorted in ascending order are returned.

Param with_index:

If True, `indices` is returned.

Param with_inverse:

If True, `inverse_indices` is returned.

Param with_counts:

`counts` is returned.

Public Functions

inline virtual shared_ptr<Function> copy() const

Copy another instance of Function with the same context.

inline virtual int min_inputs()

Get minimum number of inputs.

This is meant to be used in setup function with in_types which is used to get maximum number of inputs.

inline virtual int min_outputs()

Get minimum number of outputs.

This is meant to be used in setup function with out_types which is used to get max number of outputs.

inline virtual vector<dtypes> in_types()

Get input dtypes.

Last in_type will be used repeatedly if size of in_types is smaller than size of inputs

inline virtual vector<dtypes> out_types()

Get output dtypes.

Last out_type will be used repeatedly if size of out_types is smaller than size of outputs

inline virtual vector<string> allowed_array_classes()

Get array classes that are allowed to be specified by Context.

inline virtual string name()

Get function name in string.

template<typename T>
class Unlink : public nbla::BaseFunction<>
#include <unlink.hpp>

Unlink Layer identical forward and zero backward.

Inputs:

  • N-D array.

Outputs:

  • N-D array.

Template Parameters:

T – Data type for computation.

Public Functions

inline virtual shared_ptr<Function> copy() const

Copy another instance of Function with the same context.

inline virtual vector<dtypes> in_types()

Get input dtypes.

Last in_type will be used repeatedly if size of in_types is smaller than size of inputs

inline virtual vector<dtypes> out_types()

Get output dtypes.

Last out_type will be used repeatedly if size of out_types is smaller than size of outputs

inline virtual int min_inputs()

Get minimum number of inputs.

This is meant to be used in setup function with in_types which is used to get maximum number of inputs.

inline virtual int min_outputs()

Get minimum number of outputs.

This is meant to be used in setup function with out_types which is used to get max number of outputs.

inline virtual string name()

Get function name in string.

inline virtual vector<string> allowed_array_classes()

Get array classes that are allowed to be specified by Context.

inline virtual int inplace_data(int i) const

Get in-place-level of i-th input variable’s data (see below).

0 (NOT_INPLACE): Not in-placed 1 (INPLACE_NOT_MODIFY): In-placed but not modified. 2 (INPLACE): In-placed and modified.

Note

If a subclass uses in-place computation, the function must override this function.

Parameters:

i[in] Input variable index.

Return values:

Returns – 0 by default.

inline virtual int inplace_data_with(int i) const

Get the output variable index where i-th variables’ data in-placed to.

Note

This is only valid if the i-th variable is in-placed. The maintainer of a sub-class function must override this function.

Parameters:

i[in] Input variable index.

inline virtual bool grad_depends_output_data(int i, int o) const

Dependency flag for checking if in-grad depends on out-data.

Checking if i-th input’ gradient computation requires o-th output’s data or not.

Note

If any of inputs requires an output variable data when computing its gradient, this function must be overridden to return appropriate boolean value. Otherwise, backward computation will be incorrect.

Parameters:
  • i[in] Input variable index.

  • o[in] Output variable index.

template<typename T>
class Unpooling : public nbla::BaseFunction<const vector<int>&, bool>
#include <unpooling.hpp>

Unpooling function which upsample a feature map with an integer factor for each dimension.

The vector size of the kernel parameter corresponds to the number of dimensions of the unpooling operation. e.g) If (N, M, L, H, W)-shaped tensor is given as an input and kernel is (K, K), unpooling operation is applied to the last two dimensions, then the resulting output size will be (N, M, L, KH, KW).

Inputs:

  • N-d array.

Outputs:

  • N-d array.

Template Parameters:

T – Data type for computation.

Param kernel:

A list of upscaling factor of image.

Public Functions

inline virtual shared_ptr<Function> copy() const

Copy another instance of Function with the same context.

inline virtual vector<dtypes> in_types()

Get input dtypes.

Last in_type will be used repeatedly if size of in_types is smaller than size of inputs

inline virtual vector<dtypes> out_types()

Get output dtypes.

Last out_type will be used repeatedly if size of out_types is smaller than size of outputs

inline virtual int min_inputs()

Get minimum number of inputs.

This is meant to be used in setup function with in_types which is used to get maximum number of inputs.

inline virtual int min_outputs()

Get minimum number of outputs.

This is meant to be used in setup function with out_types which is used to get max number of outputs.

inline virtual string name()

Get function name in string.

inline virtual vector<string> allowed_array_classes()

Get array classes that are allowed to be specified by Context.

inline virtual bool grad_depends_output_data(int i, int o) const

Dependency flag for checking if in-grad depends on out-data.

Checking if i-th input’ gradient computation requires o-th output’s data or not.

Note

If any of inputs requires an output variable data when computing its gradient, this function must be overridden to return appropriate boolean value. Otherwise, backward computation will be incorrect.

Parameters:
  • i[in] Input variable index.

  • o[in] Output variable index.

struct PoolingConfiguration
#include <base_pooling.hpp>

Get output shape of pooling from input configuration.

template<typename T, class ...Args>
class BasePooling : public nbla::BaseFunction<Args...>
#include <base_pooling.hpp>

Base class for pooling functions AveragePooling, SumPooling and MaxPooling.

Inputs:

  • N-D array that has more than two dimensions.

Outputs:

  • N-D array.

Note

Only 2D pooling supported so far.

Template Parameters:

T – Data type for computation.

Param kernel:

Shapes of kernel.

Param stride:

Subsampling factors of pooling.

Param ignore_border:

If false, a kernel overlapping border is also considered for output unlike convolution.

Param pad:

Border padding values of dimensions. Padding will be added both sides of the dimension.

Public Functions

inline virtual shared_ptr<Function> copy() const

Copy another instance of Function with the same context.

inline virtual vector<dtypes> in_types()

Get input dtypes.

Last in_type will be used repeatedly if size of in_types is smaller than size of inputs

inline virtual vector<dtypes> out_types()

Get output dtypes.

Last out_type will be used repeatedly if size of out_types is smaller than size of outputs

inline virtual int min_inputs()

Get minimum number of inputs.

This is meant to be used in setup function with in_types which is used to get maximum number of inputs.

inline virtual int min_outputs()

Get minimum number of outputs.

This is meant to be used in setup function with out_types which is used to get max number of outputs.

inline virtual string name()

Get function name in string.

inline virtual vector<string> allowed_array_classes()

Get array classes that are allowed to be specified by Context.

template<typename ...Args>
class BaseTransformBinary : public nbla::BaseFunction<Args...>
#include <base_transform_binary.hpp>

Todo:

PLACE HERE FUNCTION DOCUMENTATION.

Public Functions

inline virtual int min_inputs()

Get minimum number of inputs.

This is meant to be used in setup function with in_types which is used to get maximum number of inputs.

inline virtual int min_outputs()

Get minimum number of outputs.

This is meant to be used in setup function with out_types which is used to get max number of outputs.

inline virtual int inplace_data(int i) const

Get in-place-level of i-th input variable’s data (see below).

0 (NOT_INPLACE): Not in-placed 1 (INPLACE_NOT_MODIFY): In-placed but not modified. 2 (INPLACE): In-placed and modified.

Note

If a subclass uses in-place computation, the function must override this function.

Parameters:

i[in] Input variable index.

Return values:

Returns – 0 by default.

inline virtual int inplace_data_with(int i) const

Get the output variable index where i-th variables’ data in-placed to.

Note

This is only valid if the i-th variable is in-placed. The maintainer of a sub-class function must override this function.

Parameters:

i[in] Input variable index.

template<typename T, typename BinaryOp, typename ...Args>
class TransformBinary : public nbla::BaseTransformBinary<Args...>

Public Functions

inline virtual vector<dtypes> in_types()

Get input dtypes.

Last in_type will be used repeatedly if size of in_types is smaller than size of inputs

inline virtual vector<dtypes> out_types()

Get output dtypes.

Last out_type will be used repeatedly if size of out_types is smaller than size of outputs

inline virtual vector<string> allowed_array_classes()

Get array classes that are allowed to be specified by Context.

class BaseBinaryOp
template<typename ...Args>
class BaseTransformUnary : public nbla::BaseFunction<Args...>
#include <base_transform_unary.hpp>

Todo:

PLACE HERE FUNCTION DOCUMENTATION.

Public Functions

inline virtual int min_inputs()

Get minimum number of inputs.

This is meant to be used in setup function with in_types which is used to get maximum number of inputs.

inline virtual int min_outputs()

Get minimum number of outputs.

This is meant to be used in setup function with out_types which is used to get max number of outputs.

inline virtual int inplace_data(int i) const

Get in-place-level of i-th input variable’s data (see below).

0 (NOT_INPLACE): Not in-placed 1 (INPLACE_NOT_MODIFY): In-placed but not modified. 2 (INPLACE): In-placed and modified.

Note

If a subclass uses in-place computation, the function must override this function.

Parameters:

i[in] Input variable index.

Return values:

Returns – 0 by default.

inline virtual int inplace_data_with(int i) const

Get the output variable index where i-th variables’ data in-placed to.

Note

This is only valid if the i-th variable is in-placed. The maintainer of a sub-class function must override this function.

Parameters:

i[in] Input variable index.

template<typename T, typename UnaryOp, typename ...Args>
class TransformUnary : public nbla::BaseTransformUnary<Args...>

Public Functions

inline virtual vector<dtypes> in_types()

Get input dtypes.

Last in_type will be used repeatedly if size of in_types is smaller than size of inputs

inline virtual vector<dtypes> out_types()

Get output dtypes.

Last out_type will be used repeatedly if size of out_types is smaller than size of outputs

inline virtual vector<string> allowed_array_classes()

Get array classes that are allowed to be specified by Context.

class BaseUnaryOp
class ChannelFirstAdaptor
#include <channel_first_adaptor.hpp>

This class can be used to transform a variable memory format to channel-first.

Typical use case is a transformation from channel-last to channel-first memory format in convolutional neural network.

For example, let an input variable shape (32, 128, 128, 3), batch_axis == [0] and channel_axis == 3 (channel-last). Then this adaptor converts the shape and memory format to (32, 3, 128, 128) by applying transpose function.

Possible use case is a force use of channel-first implementation (layer functions) in a channel-last network by sandwiching channel-first implementation with this adaptor. The conceptual work flow (forward prop) is following. chennal-last layer -> forward_pre -> channel-first layer -> forward_post -> channel-last layer (Variebles are omitted and the same for backward prop)

Public Functions

NBLA_API void setup (Variable *input_pre, Variable *output_pre, Variable *input_post, Variable *output_post, const Shape_t &shape, const vector< int > &batch_axis, const int channel_axis, const Context &ctx)

Setup the adaptor.

This method must be called before the use of `forward_(pre|post)` or `backward_(pre|post)` methods. Setting up of transpose functions (`(pre|post)_transpose_`) are performed internally.

Parameters:
  • input_preVariable pointer for `pre_transpose_` input.

  • output_preVariable pointer for `pre_transpose_` output.

  • input_postVariable pointer for `post_transpose_` input.

  • output_postVariable pointer for `post_transpose_` output.

  • shapeShape of original input. This should be the same as the shape of `input_pre`.

  • batch_axis – List of integer corresponding to batch or outer axis. Each axis must be in range of [0, ndim).

  • channel_axis – An integer corresponding to channel axis. This axis must be in range of [0, ndim).

  • ctx – A compute backend descriptor.

NBLA_API void convert_to_channel_first (Variable *input, Variable *output)

Transform variable memory format to channel-first.

Parameters:
  • input – `pre_tranpose_` input.

  • output – `pre_tranpose_` output.

NBLA_API void convert_from_channel_first (Variable *input, Variable *output)

Transform variable memory format from channel-first to original one.

Parameters:
  • input – `post_tranpose_` input.

  • output – `post_tranpose_` output.

NBLA_API void convert_to_channel_first_backward (Variable *input, Variable *output, const bool propagate_down, const bool accum)

Backward execution for `forward_pre`.

Parameters:
  • input – `pre_tranpose_` input.

  • output – `pre_tranpose_` output.

  • propagate_down – Flag whether or not to perform backward propagation.

  • accum – Flag whether or not to accumulate grad.

NBLA_API void convert_from_channel_first_backward (Variable *input, Variable *output, const bool propagate_down, const bool accum)

Backward execution for `forward_post`.

Parameters:
  • input – `post_tranpose_` input.

  • output – `post_tranpose_` output.

  • propagate_down – Flag whether or not to perform backward propagation.

  • accum – Flag whether or not to accumulate grad.

Public Static Functions

static NBLA_API bool need_adaptor (const Shape_t &shape, const vector< int > &batch_axis, const int channel_axis)

Check wheather this adaptor is needed for the input.

Returns `false` when the input is already channel-first format, otherwise `true`. The definition of channel-first memory format is when all of the following conditions are met. * `batch_axis` must be contiguous to the beginning of the shape. * `channel_axis` must be right after the last axis of `batch_axis`.

Examples of channel-first * ndim: 4, batch_axis: [0], channel_axis: 1 * ndim: 2, batch_axis: [0], channel_axis: 1 * ndim: 2, batch_axis: [], channel_axis: 0 * ndim: 1, batch_axis: [], channel_axis: 0 * ndim: 6, batch_axis: [0, 1, 2], channel_axis: 3 * ndim: 6, batch_axis: [1, 0, 2], channel_axis: 3

Examples of non channel-first * ndim: 4, batch_axis: [0], channel_axis: 3 * ndim: 4, batch_axis: [0], channel_axis: 2 * ndim: 4, batch_axis: [1], channel_axis: 0 * ndim: 2, batch_axis: [], channel_axis: 1 * ndim: 6, batch_axis: [0, 1, 3], channel_axis: 2

Parameters:
  • shapeShape of original input.

  • batch_axis – List of integer corresponding to batch or outer axis. Each axis must be unique and in range of [0, ndim).

  • channel_axis – An integer corresponding to channel axis. This axis must be in range of [0, ndim).

Returns:

true

Returns:

false

template<typename T>
class VATNoise : public nbla::BaseFunction<int, float>
#include <vat_noise.hpp>

VAT Noise This layer is specialized for getting noise for virtual adversarial training.

In the backward process, the input noise will be replaced with the output gradient.

Inputs:

  • noise n

Outputs:

  • eps_scaled noise eps_n

See also

Miyato et.al, Distributional Smoothing with Virtual Adversarial Training https://arxiv.org/abs/1507.00677

Template Parameters:

T – Data type for computation.

Public Functions

inline virtual shared_ptr<Function> copy() const

Copy another instance of Function with the same context.

inline virtual vector<dtypes> in_types()

Get input dtypes.

Last in_type will be used repeatedly if size of in_types is smaller than size of inputs

inline virtual vector<dtypes> out_types()

Get output dtypes.

Last out_type will be used repeatedly if size of out_types is smaller than size of outputs

inline virtual int min_inputs()

Get minimum number of inputs.

This is meant to be used in setup function with in_types which is used to get maximum number of inputs.

inline virtual int min_outputs()

Get minimum number of outputs.

This is meant to be used in setup function with out_types which is used to get max number of outputs.

inline virtual string name()

Get function name in string.

inline virtual vector<string> allowed_array_classes()

Get array classes that are allowed to be specified by Context.

inline virtual bool grad_depends_output_data(int i, int o) const

Dependency flag for checking if in-grad depends on out-data.

Checking if i-th input’ gradient computation requires o-th output’s data or not.

Note

If any of inputs requires an output variable data when computing its gradient, this function must be overridden to return appropriate boolean value. Otherwise, backward computation will be incorrect.

Parameters:
  • i[in] Input variable index.

  • o[in] Output variable index.

template<typename T>
class WarpByFlow : public nbla::BaseFunction<>
#include <warp_by_flow.hpp>

Transform the image(s) *data* by *flow* field(s) of offset vectors such that each output pixel corresponds to the input image pixel at the offset location given by horizontal and vertical flow values.

Both *data* and flow* are 4-D variables (in “NCHW” layout) with identical shape except the *flow* channel dimension (which is always 2).

.. math:: output_{n,c,y,x} = data_{n,c,y’,x’} \text{ with } y’ = y - flow_{n,1,y,x} \text{ and } x’ = x - flow_{n,0,y,x}

The output pixel values at :math:`y’` and :math:`x’` locations are obtained by bilinear interpolating between the 4 closest pixels of the input image. Pixel values outside of the input image are implicitly padded with the value of the closest boundary pixel.

Inputs:

  • Input image data with shape `(N, Channels, Height, Width)`.

  • Flow field vectors with shape `(N, 2, Height, Width)`.

Outputs:

  • Transformed image data with shape `(N, Channels, Height, Width)`.

Public Functions

inline virtual shared_ptr<Function> copy() const

Copy another instance of Function with the same context.

inline virtual int min_inputs()

Get minimum number of inputs.

This is meant to be used in setup function with in_types which is used to get maximum number of inputs.

inline virtual int min_outputs()

Get minimum number of outputs.

This is meant to be used in setup function with out_types which is used to get max number of outputs.

inline virtual vector<dtypes> in_types()

Get input dtypes.

Last in_type will be used repeatedly if size of in_types is smaller than size of inputs

inline virtual vector<dtypes> out_types()

Get output dtypes.

Last out_type will be used repeatedly if size of out_types is smaller than size of outputs

inline virtual vector<string> allowed_array_classes()

Get array classes that are allowed to be specified by Context.

inline virtual string name()

Get function name in string.

inline virtual bool grad_depends_output_data(int i, int o) const

Dependency flag for checking if in-grad depends on out-data.

Checking if i-th input’ gradient computation requires o-th output’s data or not.

Note

If any of inputs requires an output variable data when computing its gradient, this function must be overridden to return appropriate boolean value. Otherwise, backward computation will be incorrect.

Parameters:
  • i[in] Input variable index.

  • o[in] Output variable index.

template<typename T>
class WarpByGrid : public nbla::BaseFunction<const string&, const string&, bool, bool>
#include <warp_by_grid.hpp>

Inputs:

  • x: Input data to be warped with the shape ( \(B \times C \times H_{in} \times W_{in}\)) for 2D and ( \(B \times C \times D_{in} \times H_{in} \times W_{in}\)) for 3D.

  • grid: Grid warping the input data with the shape ( \(B \times H_{out} \times W_{out} \times 2\)) for 2D and ( \(B \times D_{out} \times H_{out} \times W_{out} \times 3\)) for 3D. The last dimension of 2 is for (x, y) or (x, y, z).

Outputs:

  • y: Output data warped by the grid.

Template Parameters:

T – Data type for computation.

Param mode:

Interpolation mode, linear or nearest.

Param padding_mode:

Padding mode when the grid value is outside [-1, 1]. If this is “zero”, 0 is used for padding. “reflect” uses the values reflected at the ends of the original input data like the mirror. “repeat” used the values at the ends of the original input data.

Param align_corners:

The target grid normalized in [-1, 1] is scaled by `size - 1 / size` according to the respective spatial size (e.g., \(H\) and \(W\)) before the transformation if this is `False`. If this is `True`, the top-left and bottom-right pixcels correspond to (-1, -1) and (1, 1) respectively.

Param channel_last:

If True, the last dimension is considered as channel dimension, a.k.a NHWC order.

Public Functions

inline virtual shared_ptr<Function> copy() const

Copy another instance of Function with the same context.

inline virtual int min_inputs()

Get minimum number of inputs.

This is meant to be used in setup function with in_types which is used to get maximum number of inputs.

inline virtual int min_outputs()

Get minimum number of outputs.

This is meant to be used in setup function with out_types which is used to get max number of outputs.

inline virtual vector<dtypes> in_types()

Get input dtypes.

Last in_type will be used repeatedly if size of in_types is smaller than size of inputs

inline virtual vector<dtypes> out_types()

Get output dtypes.

Last out_type will be used repeatedly if size of out_types is smaller than size of outputs

inline virtual vector<string> allowed_array_classes()

Get array classes that are allowed to be specified by Context.

inline virtual string name()

Get function name in string.

inline virtual bool grad_depends_output_data(int i, int o) const

Dependency flag for checking if in-grad depends on out-data.

Checking if i-th input’ gradient computation requires o-th output’s data or not.

Note

If any of inputs requires an output variable data when computing its gradient, this function must be overridden to return appropriate boolean value. Otherwise, backward computation will be incorrect.

Parameters:
  • i[in] Input variable index.

  • o[in] Output variable index.

template<typename T>
class WeightNormalization : public nbla::BaseFunction<int, float>
#include <weight_normalization.hpp>

Todo:

Write doc.

Inputs:

Outputs:

Public Functions

inline virtual shared_ptr<Function> copy() const

Copy another instance of Function with the same context.

inline virtual int min_inputs()

Get minimum number of inputs.

This is meant to be used in setup function with in_types which is used to get maximum number of inputs.

inline virtual int min_outputs()

Get minimum number of outputs.

This is meant to be used in setup function with out_types which is used to get max number of outputs.

inline virtual vector<dtypes> in_types()

Get input dtypes.

Last in_type will be used repeatedly if size of in_types is smaller than size of inputs

inline virtual vector<dtypes> out_types()

Get output dtypes.

Last out_type will be used repeatedly if size of out_types is smaller than size of outputs

inline virtual vector<string> allowed_array_classes()

Get array classes that are allowed to be specified by Context.

inline virtual string name()

Get function name in string.

inline virtual bool grad_depends_output_data(int i, int o) const

Dependency flag for checking if in-grad depends on out-data.

Checking if i-th input’ gradient computation requires o-th output’s data or not.

Note

If any of inputs requires an output variable data when computing its gradient, this function must be overridden to return appropriate boolean value. Otherwise, backward computation will be incorrect.

Parameters:
  • i[in] Input variable index.

  • o[in] Output variable index.

template<typename T>
class WeightStandardization : public nbla::BaseFunction<int, float>
#include <weight_standardization.hpp>

Todo:

Write doc.

Inputs:

Outputs:

Public Functions

inline virtual shared_ptr<Function> copy() const

Copy another instance of Function with the same context.

inline virtual int min_inputs()

Get minimum number of inputs.

This is meant to be used in setup function with in_types which is used to get maximum number of inputs.

inline virtual int min_outputs()

Get minimum number of outputs.

This is meant to be used in setup function with out_types which is used to get max number of outputs.

inline virtual vector<dtypes> in_types()

Get input dtypes.

Last in_type will be used repeatedly if size of in_types is smaller than size of inputs

inline virtual vector<dtypes> out_types()

Get output dtypes.

Last out_type will be used repeatedly if size of out_types is smaller than size of outputs

inline virtual vector<string> allowed_array_classes()

Get array classes that are allowed to be specified by Context.

inline virtual string name()

Get function name in string.

inline virtual bool grad_depends_output_data(int i, int o) const

Dependency flag for checking if in-grad depends on out-data.

Checking if i-th input’ gradient computation requires o-th output’s data or not.

Note

If any of inputs requires an output variable data when computing its gradient, this function must be overridden to return appropriate boolean value. Otherwise, backward computation will be incorrect.

Parameters:
  • i[in] Input variable index.

  • o[in] Output variable index.

template<typename T>
class Where : public nbla::BaseFunction<>
#include <where.hpp>

Return elements, either from x_true or x_false, depending on condition.

Inputs:

  • N-D array. When condition[i] is true, yield x_true[i], otherwise x_false[i] for all i.

  • N-D array.

  • N-D array.

Outputs:

  • N-d array.

Public Functions

inline virtual shared_ptr<Function> copy() const

Copy another instance of Function with the same context.

inline virtual int min_inputs()

Get minimum number of inputs.

This is meant to be used in setup function with in_types which is used to get maximum number of inputs.

inline virtual int min_outputs()

Get minimum number of outputs.

This is meant to be used in setup function with out_types which is used to get max number of outputs.

inline virtual vector<dtypes> in_types()

Get input dtypes.

Last in_type will be used repeatedly if size of in_types is smaller than size of inputs

inline virtual vector<dtypes> out_types()

Get output dtypes.

Last out_type will be used repeatedly if size of out_types is smaller than size of outputs

inline virtual vector<string> allowed_array_classes()

Get array classes that are allowed to be specified by Context.

inline virtual string name()

Get function name in string.

inline virtual bool grad_depends_output_data(int i, int o) const

Dependency flag for checking if in-grad depends on out-data.

Checking if i-th input’ gradient computation requires o-th output’s data or not.

Note

If any of inputs requires an output variable data when computing its gradient, this function must be overridden to return appropriate boolean value. Otherwise, backward computation will be incorrect.

Parameters:
  • i[in] Input variable index.

  • o[in] Output variable index.

class Function
#include <function.hpp>

An interface for the units of computation.

This is extended to implement a new Function which implements forward computation (forward() function) of the function

\[ {\mathbf y} = f({\mathbf x}) \]

and backward computation (backward() function)

\[ \Delta {\mathbf x} += \Delta {\mathbf y} \cdot \nabla_{\mathbf x} {\mathbf y} \]

where \(\Delta {\mathbf x}\) and \(\Delta {\mathbf y}\) are backpropagation error (gradient) of the input and output variable propagated through backward computations of descendant of computation graph, and \(\nabla_{\mathbf x} {\mathbf y}\) is a Jacobian matrix of the function. Note that propagated error is not substituted but accumulated to the gradient of the input variable because we assume \({\mathbf x}\) can be used more than once by other functions. In the above example, the number of each of input and output variable is one, but Function can take multiple inputs and outputs.

Subclassed by nbla::BaseFunction< int, bool, bool, bool >, nbla::BaseFunction< float, const vector< float > &, const vector< float > &, const vector< float > &, int, bool, bool, int, int, bool, bool >, nbla::BaseFunction< const vector< int > &, int, int >, nbla::BaseFunction< const vector< int > &, bool, int >, nbla::BaseFunction< float, float, int >, nbla::BaseFunction< const vector< int > &, float, bool, bool >, nbla::BaseFunction< bool, int, float, bool >, nbla::BaseFunction< int, const vector< int > &, const vector< int > &, const vector< int > &, int, bool, const vector< int > & >, nbla::BaseFunction< int, const vector< int > &, const vector< int > &, const vector< int > &, int, bool >, nbla::BaseFunction< int, float, const vector< int > &, int >, nbla::BaseFunction< float, const vector< int > & >, nbla::BaseFunction< int, const vector< int > &, const vector< int > &, const vector< int > &, int, float >, nbla::BaseFunction< float, const vector< int > &, float >, nbla::BaseFunction< const vector< int > &, const vector< int > &, float, float, float, float, float, bool, bool, float, bool, float, float, bool, float, int >, nbla::BaseFunction< int, const vector< int > &, const vector< int > &, const vector< int > &, int >, nbla::BaseFunction<>, nbla::BaseFunction< bool, int, bool, bool, bool, bool >, nbla::BaseFunction< int, bool, bool, int, bool, bool >, nbla::BaseFunction< const vector< int > & >, nbla::BaseFunction< int, int, float, bool, bool >, nbla::BaseFunction< int, int >, nbla::BaseFunction< float >, nbla::BaseFunction< float, float, float >, nbla::BaseFunction< const vector< int > &, const vector< float > &, int, bool >, nbla::BaseFunction< const vector< int > &, float, float, bool, bool, bool >, nbla::BaseFunction< int >, nbla::BaseFunction< int, int, float, float >, nbla::BaseFunction< int, const vector< int > &, const vector< int > &, const vector< int > &, int, int, const vector< int > &, const string &, int >, nbla::BaseFunction< int, bool >, nbla::BaseFunction< const string & >, nbla::BaseFunction< double, int >, nbla::BaseFunction< bool, bool, int, int, bool >, nbla::BaseFunction< int, const vector< int > &, const vector< int > &, const vector< int > &, int, bool, float, float, bool, const string &, const vector< float > &, const string &, float >, nbla::BaseFunction< const vector< int > &, float, float, bool, const string & >, nbla::BaseFunction< const vector< int > &, bool >, nbla::BaseFunction< const vector< int > &, const vector< int > &, const vector< int > &, const vector< int > &, const vector< int > & >, nbla::BaseFunction< float, float, bool >, nbla::BaseFunction< int, bool, bool >, nbla::BaseFunction< double, double >, nbla::BaseFunction< int, const vector< int > &, float, bool, bool >, nbla::BaseFunction< int, float >, nbla::BaseFunction< int, bool, int >, nbla::BaseFunction< const vector< int > &, const string & >, nbla::BaseFunction< const vector< int > &, const string &, float >, nbla::BaseFunction< const vector< int > &, const vector< int > &, const vector< int > & >, nbla::BaseFunction< int, const string &, float, bool, bool >, nbla::BaseFunction< int, float, bool, bool >, nbla::BaseFunction< Args… >, nbla::BaseFunction< const vector< int > &, const string &, float, int, int >, nbla::BaseFunction< int, int, const vector< int > &, int >, nbla::BaseFunction< float, const vector< int > &, bool >, nbla::BaseFunction< float, bool >, nbla::BaseFunction< int, int, const vector< int > &, const string &, int >, nbla::BaseFunction< bool >, nbla::BaseFunction< const string &, bool, int >, nbla::BaseFunction< float, bool, bool, bool, float >, nbla::BaseFunction< const vector< int > &, const string &, bool, bool, bool, bool >, nbla::BaseFunction< int, int, int, const string &, bool, const string &, bool >, nbla::BaseFunction< const string &, const string &, bool, bool >, nbla::BaseFunction< float, float, const vector< int > &, int >, nbla::BaseFunction< const vector< float > &, const vector< float > &, const vector< int > &, const string &, const string &, float, int, float, const string & >, nbla::BaseFunction< bool, float, int >, nbla::BaseFunction< const vector< int > &, const vector< int > &, bool, const vector< int > &, bool >, nbla::BaseFunction< int, const vector< int > &, const vector< int > &, const vector< int > &, int, int, bool >, nbla::BaseFunction< int, int, const vector< int > &, float, bool, bool >, nbla::BaseFunction< bool, bool >, nbla::BaseFunction< Args >, nbla::BinaryError< T >, nbla::ConfusionMatrix< T, T1 >

Public Functions

explicit Function(const Context &ctx)

Copying and storing Context.

void setup(const Variables &inputs, const Variables &outputs)

Setting up function.

This must be called before the Function instance is used, and will do:

  • Determine Array class used according to the return of Function::allowed_array_classes and the given Context.

  • Type and shape check.

  • Calling Function::setup_impl.

  • Pre-allocate memory to prevent locking in asynchronous execution in CUDA etc.

Parameters:
  • inputs – vector of Variable*

  • outputs – vector of Variable*

void forward(const Variables &inputs, const Variables &outputs)

Compute forwardprop and store results into outputs’ data.

Checking shapes before calling forward_impl() which must be implemented in a derived function.

See also

setup() arguments.

void backward(const Variables &inputs, const Variables &outputs, const vector<bool> &propagate_down, const vector<bool> &acccum)

Compute backprop multiplied with outputs’ grad and store it to inputs’ grad.

Checking shapes before calling backward_impl() which must be implemented in a derived function.

See also

setup() arguments.

void setup_recompute(const Variables &inputs, const Variables &outputs)

Setting up for recompute().

Setting up anything that needs to be done before forward execution for recomputation. If any of outpus of need_setup_recompute is true, this method is called just before forward().

See also

recompute()

void recompute(const Variables &inputs, const Variables &outputs)

Reproduce previous forward().

Reproduce previous forward() execution. This method is called during backward execution.

void set_active_input_mask(const vector<bool> &mask)

Activate or deactivate inputs.

Context context() const

Get Context used in this function.

virtual vector<dtypes> in_types() = 0

Get input dtypes.

Last in_type will be used repeatedly if size of in_types is smaller than size of inputs

virtual vector<dtypes> out_types() = 0

Get output dtypes.

Last out_type will be used repeatedly if size of out_types is smaller than size of outputs

virtual int min_inputs() = 0

Get minimum number of inputs.

This is meant to be used in setup function with in_types which is used to get maximum number of inputs.

virtual int min_outputs() = 0

Get minimum number of outputs.

This is meant to be used in setup function with out_types which is used to get max number of outputs.

virtual string name() = 0

Get function name in string.

virtual vector<string> allowed_array_classes() = 0

Get array classes that are allowed to be specified by Context.

inline virtual bool grad_depends_output_data(int i, int o) const

Dependency flag for checking if in-grad depends on out-data.

Checking if i-th input’ gradient computation requires o-th output’s data or not.

Note

If any of inputs requires an output variable data when computing its gradient, this function must be overridden to return appropriate boolean value. Otherwise, backward computation will be incorrect.

Parameters:
  • i[in] Input variable index.

  • o[in] Output variable index.

inline virtual bool grad_depends_input_data(int i, int j) const final

Dependency flag for checking if in-grad depends on in-data.

Checking if i-th input’ gradient computation requires j-th input’s data or not.

Parameters:
  • i[in] Input variable index.

  • j[in] Input variable index.

inline virtual bool auto_grad_depends_output_data(int i, int o) const

Dependency flag to check if in-grad depends on out-data for auto grad.

Checking if i-th input’s gradient computation requires o-th output’s data or not when auto grad (nnabla.grad). This method is refered in auto-forward mode to clear memory consumption dynamically.

Note

If the grad dependencies of the output are different between Function::backward and auto grad, this function must be overridden to return appropriate boolean value. Otherwise, auto grad computation will be incorrect.

Parameters:
  • i[in] Input variable index.

  • o[in] Output variable index.

inline virtual bool auto_grad_depends_input_data(int i, int j) const final

Dependency flag to check if in-grad depends on in-data for auto grad.

Checking if i-th input’s gradient computation requires j-th input’s data or not when auto grad (nnabla.grad). This method is refered in auto-forward mode to clear memory consumption dynamically.

Parameters:
  • i[in] Input variable index.

  • j[in] Input variable index.

inline virtual bool overwrite_input_data_in_forward(int i) const final

Dependency flag for checking if i-th input is overwritten in forward or not.

Parameters:

i[in] Input variable index.

inline virtual int inplace_data(int i) const

Get in-place-level of i-th input variable’s data (see below).

0 (NOT_INPLACE): Not in-placed 1 (INPLACE_NOT_MODIFY): In-placed but not modified. 2 (INPLACE): In-placed and modified.

Note

If a subclass uses in-place computation, the function must override this function.

Parameters:

i[in] Input variable index.

Return values:

Returns – 0 by default.

inline virtual int inplace_data_with(int i) const

Get the output variable index where i-th variables’ data in-placed to.

Note

This is only valid if the i-th variable is in-placed. The maintainer of a sub-class function must override this function.

Parameters:

i[in] Input variable index.

inline virtual bool prohibit_clear_input_buffers() const

A flag for preventing that the graph engine clears buffers of input variables even if clear_buffer is true and condition mets.

inline virtual bool prohibit_zero_input_grad() const

A flag for preventing that the graph engine sets input gradient buffer as 0 even when accum is true.

inline virtual bool need_setup_recompute(int o) const

A flag for checking if setup_recompute() is needed.

Checking if o-th output’ data requires setup_recompute().

Note

setup_recompute() will skipped during forward execution if none of outputs requires setup_recompute().

Parameters:

o[in] Output variable index.

virtual shared_ptr<Function> copy() const = 0

Copy another instance of Function with the same context.

inline bool ask_if_used_and_use()

Check whether this was already used, and turn it used.

template<typename ...Args>
class BaseFunction : public nbla::Function
#include <function.hpp>

Base function.

Keep arguments.

Public Functions

inline int num_args()

Get number of constructor arguments.

inline const tuple<Args...> &get_args()

Get constructor arguments as a tuple.

template<int Index>
inline auto get_arg() -> decltype(std::get<Index>(args_))

Get a constructor argument by index.

template<typename Item>
class FunctionDb
#include <function_registry.hpp>

Container of FunctionDbItem that can be queried by regex.

Public Functions

inline Item::function_t query(const vector<string> &backend)

Query function item (FunctionDbItem) by query key of device (Regex can be used).

inline void add(shared_ptr<Item> item)

Adding function item (FunctionDbItem).

template<typename Base, typename ...Args>
struct FunctionDbItem
#include <function_registry.hpp>

Item of FunctionDb that stores backend key and a creator function (with variadic template args).

template<typename Base, typename ...Args>
class FunctionRegistry
#include <function_registry.hpp>

This is used as a base class of function registry for each function that has numerically unique operation.

See NBLA_REGISTER_FUNCTION.

Public Functions

inline shared_ptr<Base> create(const Context &ctx, Args... args)

Create a new function instance.

class GarbageCollector
#include <garbage_collector.hpp>

Singleton for garbage collector registry.

This singleton class is intended to be used to freeing cpu/device memory held by an interpreter language which manages memory by GC (e.g. Python). GC function as c++ callback function can be registered via `register_collector` function. All registered GC function is called usually in case memory allocation fails.

Public Functions

void register_collector(collector_type f)

Register a GC function with a form `void ()`.

void collect()

Call all registered GC function.

class GlobalContext
#include <global_context.hpp>

Singleton class for storing global context.

Public Functions

Context get_current_context() const

Get current context.

void set_current_context(Context ctx)

Set current context.

class GlobalFunctionCallback
#include <global_function_callback.hpp>

Singleton class for storing global function callbacks.

Public Functions

void call_pre_hooks(const CgFunctionPtr&)

Call pre_hooks.

void call_post_hooks(const CgFunctionPtr&)

Call pre_hooks.

void set_pre_hook(const string &key, const function_hook_type &cb)

Set a pre_hook.

void set_post_hook(const string &key, const function_hook_type &cb)

Set a post_hook.

void unset_pre_hook(const string &key)

Unset a pre_hook.

void unset_post_hook(const string &key)

Unset a post_hook.

class GlobalSolverCallback
#include <global_solver_callback.hpp>

Singleton class for storing global function callbacks.

Public Functions

void call_pre_hooks()

Call pre_hooks.

void call_post_hooks()

Call pre_hooks.

void set_pre_hook(const string &key, const update_hook_type &cb)

Set a pre_hook.

void set_post_hook(const string &key, const update_hook_type &cb)

Set a post_hook.

void unset_pre_hook(const string &key)

Unset a pre_hook.

void unset_post_hook(const string &key)

Unset a post_hook.

template<typename T>
struct force_float
template<>
struct force_float<Half>
class Initializer

Subclassed by nbla::ConstantInitializer, nbla::NormalInitializer, nbla::UniformInitializer, nbla::UniformIntInitializer

class UniformInitializer : public nbla::Initializer
class ConstantInitializer : public nbla::Initializer
class NormalInitializer : public nbla::Initializer
class UniformIntInitializer : public nbla::Initializer
class SwapInOutScheduler
#include <swap_in_out_scheduler.hpp>

A class which manages GPU memory usage and schedules swap in/out throughout network computation.

If GPU memory is insufficient to train your model, SwapInOutScheduler enables you to compute it fast and effectively by using memory swapping strategy.

This class schedules the timing to swap out tensors to avoid out of memory and to swap in them before they are reused in computation.

The schedule is made to be based on the usage order of tensors in the first training iteration. It indicates the disorder of the usage order in the rest iteration will cause speed-down.

A scheduler takes the size of GPU memory which you want to manage. For example, when your GPU memory is up to 4 GB, the initialization is

SwapInOutScheduler scheduler(cpu_ctx, gpu_ctx, 4e9);
If out-of-memory error occurs in this configuration, the gradul reduction of 4e9 could solve the problem; for example, let the next size be 3.5e9.

This scheduler can be used easily as extension by enclosing a training block between SwapInOutScheduler::start_scheduling() and SwapInOutScheduler::end_scheduling(). And also you need set the callback functions into the arguments of forward, backward, and update. For example, in a training loop,

shceduler.start_scheduling();
// Input next data and label in this line.
loss->forward(false, true, nullptr,
              [&](const CgFunctionPtr &ptr) {
      scheduler.pre_function_callback(ptr); },
              [&](const CgFunctionPtr &ptr) {
      scheduler.post_function_callback(ptr); });
loss->variable()->grad()->fill(1.0);
loss->backward(nullptr, true, {},
              [&](const CgFunctionPtr &ptr) {
      scheduler.pre_function_callback(ptr); },
              [&](const CgFunctionPtr &ptr) {
      scheduler.post_function_callback(ptr); });
adam->update([&]() { swap_in_out_scheduler.pre_update_callback(); },
             [&]() { swap_in_out_scheduler.post_update_callback(); });
scheduler.end_scheduling();

Public Functions

NBLA_API SwapInOutScheduler(const Context &h_ctx, const Context &d_ctx, const size_t max, const size_t prefetch_max = 0, const bool save_host_mem = true, const bool save_host_mem_no_abort = false)

Constructor.

@params h_ctx Host context used as the destination of swap-out. @params d_ctx Device context. @params max Maximum GPU memory size managed by this class [bytes]. @params prefetch_max Maximum prefetch length. @params save_host_mem The flag to switch prefetch scheme to save host memory. @params save_host_mem_no_abort Irregular off-schedule does not abort program if cast prefetch irreversibly change the type of array.

NBLA_API ~SwapInOutScheduler()

Destructor.

NBLA_API void start_scheduling ()

This initializes the scheduler and starts the management of GPU memory in this iteration.

NBLA_API void end_scheduling ()

This finalizes the scheduler and stops the management of GPU memory in this iteration.

NBLA_API void pre_function_callback (const CgFunctionPtr &ptr)

To use the scheduler, this callback must be set in the pre-function-hook arguments of forward and backward function.

NBLA_API void post_function_callback (const CgFunctionPtr &ptr)

To use the scheduler, this callback must be set in the post-function-hook arguments of forward and backward function.

NBLA_API void pre_update_callback ()

To use the scheduler, this callback must be set in the pre-update-hook argument of the update method of a solver.

NBLA_API void post_update_callback ()

To use the scheduler, this callback must be set in the post-update-hook argument of the update method of a solver.

class AllocatorMemory
#include <allocator.hpp>

A RAII class for Memory allocated by a Allocator class.Shared pointer of AllocatorMemory.

At a destructor, the borrowed Memory will be returned to a Allocator instance registered at a constructor.

Public Functions

inline shared_ptr<Memory> memory()

Get a Memory intance owned by this.

inline size_t bytes() const

Get bytes.

See also

Memory::bytes

inline string device_id() const

Get device ID.

inline void *pointer()

Get a raw pointer retaining a memory block defined in a Memory implementation class.

inline const void *const_pointer() const

Get as const.

See also

pointer

NBLA_API AllocatorMemory(shared_ptr<Memory> memory, shared_ptr<Allocator> allocator)

Constructor.

This is called from Allocator::alloc to wrap a Memory instance.

Parameters:
  • memory[in] A Memory instance wrapped.

  • allocator[in] Used to return the wrapped memory to it when this instance is destroyed.

NBLA_API AllocatorMemory()

Constructor.

This is called to create an epmpty instance.

NBLA_API ~AllocatorMemory()

Destructor returns a Memory instance given at a constructor to an allocator.

class Allocator : public std::enable_shared_from_this<Allocator>
#include <allocator.hpp>

Allocator interface class.

A derived class implements logics that manage cached device memory blocks.

A derived class NaiveAllocator which takes a Memory class as a template argument implements an memory allocator without caching.

Todo:

Support streams or events.

Note

A Allocator instance must be instantiated as a shared_ptr.

Subclassed by nbla::CachingAllocatorWithBucketsBase, nbla::NaiveAllocator< MemoryType >, nbla::VirtualCachingAllocatorBase

Public Functions

Allocator()

Constructor does nothing.

AllocatorMemoryPtr alloc(size_t bytes, const string &device_id)

Request a memory block wrapped by AllocatorMemory.

A derived class must implement alloc_impl where a memory is newly created or obtained from memory pool.

Parameters:
  • bytes[in] Number of bytes of memory block requested.

  • device_id[in] Device specifier string. A format of allow specifier is determined depending on Memory class implementation.

Returns:

A memory block wrapped by AllocatorMemory. It’s assumed that compiler RVO is enabled to prevent copying a return value. The returned AllocatorMemory must be moved to Array instance using std::move.

void free(shared_ptr<Memory> memory)

User should call this.

This is called from destructor of AllocatorMemory.

A given memory block is returned to this allocator instance. A derived class must implement Allocator::free_impl where a memory is returned to pool or simply freed if it’s non-caching allocator.

size_t free_unused_caches()

Free all unused memory blocks from all devices.

Allocator::free_unused_device_caches is called for all device IDs previously passed to Allocator::alloc function.

size_t free_unused_device_caches(const string &device_id)

Free all unused memory blocks in a specified device.

A derived class must implement Allocator::free_unused_device_caches_impl where all unused memory blocks that are origianally created, i.e. not divided, are discarded from pool.

Parameters:

device_id[in] Specifies device by a string.

Returns:

Size of freed memory in bytes.

size_t device_memory_used_in_bytes(const string &device_id)

Get currently allocated memory size in a specifie device.

Parameters:

device_id[in] Specifies device by a string.

Returns:

Total size of memory currently allocated by this allocator in bytes.

virtual ~Allocator()

Destructor does nothing.

inline void print_memory_cache_map()

APIs for memory cache analysis.

class AllocatorCallback

Subclassed by nbla::PrintingAllocatorCallback

class PrintingAllocatorCallback : public nbla::AllocatorCallback
class CachingAllocatorWithBucketsBase : public nbla::Allocator
#include <caching_allocator_with_buckets.hpp>

A base class of CachingAllocatorWithBuckets.

This implements a caching logic but it leaves an instantiation of Memory class as a virtual function CachingAllocatorWithBucketsBase::make_memory_impl. It enables an easy realization of this allocator with any Memory class implementation such as CpuMemory and CudaMemory. The caching algorithm is described as following.

## Caching a previously requested memory into a memory pool

This allocator maintains a memory pool as a map from a requested memory configuration to a Memory instance previously created. A created memory block is re-used without allocation and free, which significantly reduces overhead due to memory allocation and deallocation, and implicit synchronization of device execution queues in CUDA for example.

## Size dependent memory pool

A memory pool is maintained as two separate pools for small size and large size memory respectively. By default, memory size less than 1MB is considered as a small block, otherwise large.

## Rounding rules of memory size

A requested memory size is rounded to a multiple of round_small_ (512B by default) or round_large_ (128KB by default) for small or large blocks respectively.

## Creation rules

If any of previously created memory block larger than a requested size is not found, a new Memory instance is created. If found, a minimum size memory block is used after applying the following split rule.

## Split rules

If the size of the found memory block is greater than or equal to round_small_ (512B by default) or small_alloc_ (1MB by default) + 1 for small or large respectively, the found memory block is split into two at an offset position by a requested size after rounding, then the second one is returned to the pool, and the first one is used.

Subclassed by nbla::CachingAllocatorWithBuckets< MemoryType >

template<class MemoryType>
class CachingAllocatorWithBuckets : public nbla::CachingAllocatorWithBucketsBase
#include <caching_allocator_with_buckets.hpp>

A realization of CachingAllocatorWithBuckets.

Template Parameters:

MemoryType – A Memory class implementation such as CpuMemory and CudaMemory.

class CpuMemory : public nbla::Memory
#include <cpu_memory.hpp>

Cpu memory implementation.

A memory block allocated by ::malloc function is managed by an instance.

class PhysicalMemory
#include <memory.hpp>

Physical memory interface class.

Public Functions

virtual size_t alloc() = 0

Allocate physical memory at least larger than bytes_.

This method must be implemented in derived class.

Returns:

Size of allocated bytes. (Requested bytes and actual allocated bytes would be different.)

class Memory
#include <memory.hpp>

Memory interface class.

The Memory class is designed to be managed by a Allocator instance. This is extended to implement a device memory class which is responsible for allocating/freeing a device memory block, and splitting into/merging blocks.

Subclassed by nbla::CpuMemory

Public Functions

NBLA_API Memory(size_t bytes, const string &device_id)

Constructor will be called from a inherited class constructor to set bytes and device_id.

virtual NBLA_API ~Memory()

A derived class must implements destructor.

This destructor does nothing.

The memory allocated in Memory::alloc_impl function of a derived class

must be freed in destuctor of the derived class. It is recommended to insert assertion ensuring that this->prev() returns false to check runtime fatal issue.

Note

If ptr_ is nullptr, a Memory has previously been merged to another, and is not retaining a memory block that should be freed.

inline size_t bytes() const

Get memory block size in bytes.

inline size_t requested_bytes() const

Get requested allocation memory size in bytes.

inline string device_id() const

Get device id as a string.

inline void *pointer()

Get a raw pointer retaining a memory block instance.

In CpuMemory realization, it is a raw memory block allocated by malloc.

inline const void *const_pointer() const

Get a raw pointer retaining a memory block instance.

In CpuMemory realization, it is a raw memory block allocated by malloc.

inline bool locked() const

This returns true if this Memory instance is in use.

The lock state is managed by AllocatorMemory instance in a RAII way. Allocator::alloc function returns a AllocatorMemory instance retaining a Memory instance with lock. At the end of life of the AllocatorMemory instance, the lock is released to inform whether the memory instance can be merged with a consecutive (next_ or prev_) memory block at Memory::try_merge function.

inline bool disabled()

This returns whether the Memory instance was disabled by merging to another memory block.

inline Memory *next() const

Returns the next memory block which has previously been split from an originally allocated memory block.

inline Memory *prev() const

Returns the previous memory block.

See also

Memory::next

inline void lock()

Get a lock of this Memory instance when it’s used to prevent merging with another.

The lock is obtained by AllocatorMemory in its initialization.

inline void release()

Release a lock when it’s not used.

The lock is released by AllocatorMemory in its destructor.

inline VecPhysicalMemoryPtr &get_physical_memory()

Get physical memory as reference.

inline void append_physical_memories(VecPhysicalMemoryPtr &p_mems)

Append physical memories.

inline void clear_physical_memory()

Clear physical memory.

void alloc()

Allocate memory by Memory::alloc_impl implemented in an implementation class.

This should be called before using this instance, and is designed to be called via Allocator::alloc_retry which should be called in Allocator::alloc_impl in an implementation class.

size_t bytes_active()

Returns number of bytes of memory block this owns.

This returns 0 if this is not originally allocated memory instance, i.e., divided by another memory.

shared_ptr<Memory> divide(size_t second_start)

This splits memory at an offset specified by second_start, and returns a second block.

A first block is retained in this instance.

void try_merge(Memory *from)

Merge another memory block specified by from if it’s possible.

Merging is performed if the following conditions are met;

  • `from` is a valid pointer.

  • `from` is not locked.

  • `from` is next or previous of this.

When merged, `from` will be disabled by Memory::disable.

void bind()

Bind physical memories on virtual address.

This method can be executed only if memory_type_ == MemType::Virtual.

void unbind()

Unbind physical memories from corresponding virtual address, and return physical memory list as vector.

This method can be executed only if memory_type_ == MemType::Virtual.

bool grow(VecPhysicalMemoryPtr &p_mems)

Glow virtual memory from already allocated virtual address.

inline virtual DeviceMemoryState get_device_memory_state()

Get device memory state.

In default this function does noting and return DeviceMemoryState::Unlocked.

inline virtual void lock_device_memory()

Request device memory state to `request`.

`request` must be DeviceMemoryState. In default this function does nothing.

Public Static Functions

static void associate_consecutive(Memory *left, Memory *right)

Set Memory::prev_/Memory::next_ pointers of `left` and `right` as connected.

template<typename MemoryType>
class NaiveAllocator : public nbla::Allocator
#include <naive_allocator.hpp>

Memory allocator wihout caching.

This always creates a new instance of Memory with allocation (Memory::alloc), and frees when it’s returned.

Template Parameters:

MemoryType – A Memory class implementation such as CpuMemory and CudaMemory.

class VirtualCachingAllocatorBase : public nbla::Allocator
#include <virtual_caching_allocator.hpp>

A base class of VirtualCachingAllocator.

This implements a virtual addressing logic and caching logic but it leaves an instantiation of Memory class as a virtual function `VirtualCachingAllocatorBase::create_physical_memory_impl` and `VirtualCachingAllocatorBase::create_virtual_memory_impl`. It enables an easy realization of this allocator with any Memory class implementation such as a pair of CudaVirtaulMemory and CudaPhysicalMemory.

Virtual addressing and caching logic is summarized as follows:

## Allocation When a memory is requested, this allocator combines multiple physical memories to map virtual memory whose size is enough for the request and returns mapped virtual address as Memeory instance. To reduce allocation overhead, cached virtual address and physical memory will be reused preferentially rather than newlly allocated. VirtualCachingAllocator manages virtual and physical cache separately and reuses memory at both level (reusing virtual adrress without freeing and reserving is preferable in terms of allocation overhead).

## Caching and reusing virtual address When a memory is freed and returned to VirtualCachingAllocator, returned virtual memory is cached for future memory request. Cached memories are reused in several ways depending on request as follows:

  • case 1: No cached memory exists in cache -> create new memory

  • case 2: A memory whose size is exactly the same as a request exists -> just return this cached memory

  • case 3: At least one smaller memory than a request exists -> grow this cached memory

  • case 4: Only larger memories exist -> unbind smallest memories and create new virtual memory See implementation for the detail of how to release, reuse, unbind, and bind virtual and physical memory.

## Free When a cached virtual memory is needed to be unbinded to move its physical memories to new virtual memory, the physical memories can be unbinded imediately. However, the virtual address have to be kept from being released until the device computation using this virtual address is finished, because device computation (e.g. CUDA) is fully asynchronous with host. This allocator keeps all virtual memories whose device_memory_state are DeviceMemoryState::Locked in waiting_list_. This waiting_list_ will be evaluated before every allocation request.

## Physical memory size and rounding Physical memory size is 20MB in default and mapped virtual memory will be rounded up by a multiple of 20MB (multiple of physical memory size). This memory size can be changed by VirtualCachingAllocatorBase::set_chunk_size. Larger size means faster allocation but larger fragmentation while smaller size means vice versa. Note that min and max size of a chunk depends on the device.

Subclassed by nbla::VirtualCachingAllocator< PhysicalMemoryType, VirtualMemoryType >

template<class PhysicalMemoryType, class VirtualMemoryType>
class VirtualCachingAllocator : public nbla::VirtualCachingAllocatorBase
class BaseNdArray

Subclassed by nbla::NdArray

Public Functions

virtual void update_python_user_reference_counts(const int diff) final

Update the refernce counts from a Python user.

virtual NBLA_API void set_array (SyncedArrayPtr array) final

Replace SyncedArray instance with previously created another one.

virtual NBLA_API SyncedArrayPtr array () final

Get SyncedArray instance held by this instance.

Note

This is not copying data. Modifying content affects this.

class NdArray : public nbla::BaseNdArray, public std::enable_shared_from_this<NdArray>
#include <nd_array.hpp>

Dtype and backend agnostic multi-dimensional array.Shared pointer of NdArray.

Public Functions

NBLA_API NdArray(const Shape_t &shape = {})

Ctor given shape.

Create an SyncedArray instance with corresponding size.

Parameters:

shape[in] Shape of creating array.

NBLA_API NdArray(SyncedArrayPtr array, const Shape_t &shape = {})

Ctor given previously created SyncedArray and shape.

Parameters:
NBLA_API void reshape (const Shape_t &shape, bool force=false)

Reshape.

Parameters:
  • shape[in] N-d array will be reshaped to this shape.

  • force[in] If total size doesn’t match and true is given, array will reset to total size, which means the content of array will become totally different one.

NBLA_API Ptr view (const Shape_t &shape)

Create an another instance with different shape but sharing array content.

Parameters:

shape[in] N-d array reshaped to.

NBLA_API Shape_t shape () const

Get shape.

NBLA_API Shape_t strides () const

Get strides.

NBLA_API Size_t size (Size_t axis=-1) const

Get total size (product of shape array).

Parameters:

axis – Size followed by given axis is computed.

NBLA_API Size_t ndim () const

Get number of dimensions.

NBLA_API void zero ()

Set all value to zero.

Note

This will be lazily evaluated when data is used.

NBLA_API void fill (double v)

Set all value to given value.

Note

This will be lazily evaluated when data is used.

Parameters:

value[in] Value array filled with.

NBLA_API const Array * get (dtypes dtype, const Context &ctx)

Get const array with specified dtype and backend description.

Parameters:
  • dtype[in] Enum of data type.

  • ctx[in] Descriptor of array backend.

NBLA_API shared_ptr< const Array > get_sp (dtypes dtype, const Context &ctx)

Get const array as a shared_ptr.

See also

get

NBLA_API unsigned long data_ptr (dtypes dtype, const Context &ctx, bool write_only=false)

Get array’s ptr.

Parameters:
  • dtype[in] Enum of data type.

  • ctx[in] Descriptor of array backend.

  • write_only[in] No synchronization happens.

NBLA_API Array * cast (dtypes dtype, const Context &ctx, bool write_only=false)

Get mutable array with specified dtype and backend description.

Parameters:
  • dtype[in] Enum of data type.

  • ctx[in] Descriptor of array backend.

  • write_only[in] No synchronization happens.

NBLA_API shared_ptr< Array > cast_sp (dtypes dtype, const Context &ctx, bool write_only=false)

Get a mutable array as a shared_ptr.

See also

cast

NBLA_API Ptr narrow (const Size_t dim, const Size_t start, const Size_t length)

Returns a new array that is a narrowed part of this array.

The narrowed part is specified by the slice of this array from `start` to `start` + `length` along the dimension `dim`. The returned array and this array share the same underlying allocated memory.

Parameters:
  • dim[in] Dimension along which to narrow. Currently, only 0 can be specified.

  • start[in] Starting index in specified dimension.

  • length[in] Distance to the ending index from `start`.

Public Static Functions

template<typename ...Args>
static inline Ptr create(Args... args)

Create a shared_ptr instance of NdArray.

class ParameterDirectory
class RandomManager
#include <random_manager.hpp>

Singleton class for storing global context.

Public Functions

std::mt19937 &get_rand_generator()

Get random generator.

unsigned int get_seed() const

Get seed value.

void set_seed(unsigned int seed)

Set seed and recreate random generator.

int get_count() const

Get count of seed sets used for extensions to track seed update.

class Recompute
#include <recompute.hpp>

Singleton class for storing global recompute flag.

Public Functions

bool get_global_recompute() const

Get current global recompute flag.

void set_global_recompute(bool recompute)

Set global recompute flag.

class SingletonManager
#include <singleton_manager.hpp>

API for getting or creating and deleting any singleton classes.

In NNabla, all Singleton should be instantiated via static function `get<SINGLETON>()`. This enables you to manage all singleton instance in one place, and also to delete them by your hand.

Public Static Functions

template<typename SINGLETON>
static SINGLETON *get()

Get or create an singleton instance of a class specified by the template argument.

This will create an instance, and schedule to delete the instance at the end of process, or let you delete by hand through erase function.

Returns:

A pointer to a singleton instance.

template<typename SINGLETON>
static int get_id()

Get an integer ID of a Singleton class.

This can be used as an argument of `erase_by_id` function.

static void clear()

Delete all singleton instance previously created via `get<SINGLETON>()`.

template<typename SINGLETON>
static void erase()

Delete a singleton instance specified by a template argument.

static void erase_by_id(int id)

Delete a singleton instance specified by an ID which can be gotten by `get_id<SINGLETON>()` function.

class NNabla
#include <singleton_manager.hpp>

Reusable resources for ones and zeros for any devices.

Public Functions

const void *ones(Size_t size, dtypes dtype, const Context &ctx)

Get ones array with specified size as a void pointer.

const void *zeros(Size_t size, dtypes dtype, const Context &ctx)

Get zeros array with specified size as a void pointer.

template<typename T>
class AdaBelief : public nbla::Solver
#include <adabelief.hpp>

AdaBelief solver defined as.

\[ \theta_{t+1} \leftarrow \theta_t - \alpha \frac{\sqrt{1 - \beta_2^t}}{1 - \beta_1^t} \frac{m_t}{\sqrt{s_t + \epsilon} + \epsilon} \]
where \(\theta_t\) is a gradient of a parameter, \(m_t\) and \(s_t\) are moving average and 0-mean variance of a sequence of gradients \(t=0,...,t\).

See also

See the paper linked below for more details. Juntang Zhuang, et al. (2020). AdaBelief Optimizer: Adapting Stepsizes by the Belief in Observed Gradients. https://arxiv.org/pdf/2010.07468.pdf

Param alpha:

\(\alpha\) Learning rate.

Param beta1:

\(\beta_1\) Decay rate of moving mean.

Param beta2:

\(\beta_2\) Decay rate of moving 0-mean variance.

Param eps:

\(\epsilon\) Tiny factor for avoiding 0-division.

Public Functions

inline virtual float learning_rate()

Set learning rate.

template<typename T>
class AdaBound : public nbla::Solver
#include <adabound.hpp>

AdaBound solver.

See also

See the paper linked below for more details. L. Luo, Y. Xiong, Y. Liu and X. Sun. Adaptive Gradient Methods with Dynamic Bound of Learning Rate. https://arxiv.org/abs/1902.09843

Public Functions

inline virtual float learning_rate()

Set learning rate.

template<typename T>
class Adadelta : public nbla::Solver
#include <adadelta.hpp>

Adadelta.

This is defined as

\[\begin{split} g_t \leftarrow \Delta w_t\\ v_t \leftarrow - \frac{RMS \left[ v_t \right]_{t-1}}{RMS \left[ g \right]_t}g_t\\ w_{t+1} \leftarrow w_t + \eta v_t \end{split}\]

See also

See the paper linked below for more details. Matthew D. Zeiler ADADELTA: An Adaptive Learning Rate Method https://arxiv.org/abs/1212.5701

Param lr:

\(\eta\) Learning rate.

Param decay:

\(\gamma\) Decay rate.

Param eps:

\(\epsilon\) Tiny factor for avoiding 0-division.

Public Functions

inline virtual float learning_rate()

Set learning rate.

template<typename T>
class Adagrad : public nbla::Solver
#include <adagrad.hpp>

Adagrad.

This is defined as

\[\begin{split} g_t \leftarrow \Delta w_t\\ G_t \leftarrow G_{t-1} + g_t^2\\ w_{t+1} \leftarrow w_t - \frac{\eta}{\sqrt{G_t} + \epsilon} g_t\\ \end{split}\]

See also

See the paper linked below for more details. John Duchi, Elad Hazan and Yoram Singer (2011) Adaptive Subgradient Methods for Online Learning and Stochastic Optimization http://www.jmlr.org/papers/volume12/duchi11a/duchi11a.pdf

Param lr:

\(\eta\) Learning rate.

Param eps:

\(\epsilon\) Tiny factor for avoiding 0-division.

Public Functions

inline virtual float learning_rate()

Set learning rate.

template<typename T>
class Adam : public nbla::Solver
#include <adam.hpp>

Adam solver defined as.

\[ \theta_{t+1} \leftarrow \theta_t - \alpha \frac{\sqrt{1 - \beta_2^t}}{1 - \beta_1^t} \frac{m_t}{\sqrt{v_t} + \epsilon} \]
where \(\theta_t\) is a gradient of a parameter, \(m_t\) and \(v_t\) are moving average and 0-mean variance of a sequence of gradients \(t=0,...,t\).

See also

See the paper linked below for more details. Kingma and Ba, Adam: A Method for Stochastic Optimization. https://arxiv.org/abs/1412.6980

Param alpha:

\(\alpha\) Learning rate.

Param beta1:

\(\beta_1\) Decay rate of moving mean.

Param beta2:

\(\beta_2\) Decay rate of moving 0-mean variance.

Param eps:

\(\epsilon\) Tiny factor for avoiding 0-division.

Public Functions

inline virtual float learning_rate()

Set learning rate.

template<typename T>
class Adamax : public nbla::Solver
#include <adamax.hpp>

Adamax solver defined as.

\[ \theta_{t+1} \leftarrow \theta_t - \alpha \frac{\sqrt{1 - \beta_2^t}}{1 - \beta_1^t} \frac{m_t}{u_t + \epsilon} \]
where \(\theta_t\) is a gradient of a parameter, \(m_t\) and \(u_t\) are moving average and exponentially weighted infinity norm of a sequence of gradients \(t=0,...,t\).

See also

See the paper linked below for more details. Kingma and Ba, Adam: A Method for Stochastic Optimization. https://arxiv.org/abs/1412.6980

Param alpha:

\(\alpha\) Learning rate.

Param beta1:

\(\beta_1\) Decay rate of moving mean.

Param beta2:

\(\beta_2\) Decay rate of exponentially weighted infinity norm.

Param eps:

\(\epsilon\) Tiny factor for avoiding 0-division.

Public Functions

inline virtual float learning_rate()

Set learning rate.

template<typename T>
class AdamW : public nbla::Solver
#include <adamw.hpp>

AdamW solver defined as.

\[ \theta_{t+1} \leftarrow \theta - \alpha \frac{\sqrt{1 - \beta_2^t}}{1 - \beta_1^t} \frac{m_t}{\sqrt{v_t} + \epsilon} - \eta_t\lambda w_{t-1} \]
where \(\theta_t\) is a gradient of a parameter, \(m_t\) and \(v_t\) are moving average and 0-mean variance of a sequence of gradients \(t=0,...,t\).

See also

See the paper linked below for more details. Kingma and Ba, AdamW: A Method for Stochastic Optimization. https://arxiv.org/abs/1412.6980

Param alpha:

\(\alpha\) Learning rate.

Param beta1:

\(\beta_1\) Decay rate of moving mean.

Param beta2:

\(\beta_2\) Decay rate of moving 0-mean variance.

Param eps:

\(\epsilon\) Tiny factor for avoiding 0-division.

Public Functions

inline virtual float learning_rate()

Set learning rate.

template<typename T>
class AMSBound : public nbla::Solver
#include <amsbound.hpp>

AMSBound solver.

See also

See the paper linked below for more details. L. Luo, Y. Xiong, Y. Liu and X. Sun. Adaptive Gradient Methods with Dynamic Bound of Learning Rate. https://arxiv.org/abs/1902.09843

Public Functions

inline virtual float learning_rate()

Set learning rate.

template<typename T>
class AMSGRAD : public nbla::Solver
#include <amsgrad.hpp>

AMSGRAD solver defined as.

\[\begin{split} m_t \leftarrow \beta_1 m_{t-1} + (1 - \beta_1) g_t\\ v_t \leftarrow \beta_2 v_{t-1} + (1 - \beta_2) g_t^2\\ \hat{v_t} = \max(\hat{v_{t-1}}, v_t)\\ \theta_{t+1} \leftarrow \theta_t - \alpha \frac{m_t}{\sqrt{\hat{v_t}} + \epsilon} \end{split}\]
where \(\theta_t\) is a gradient of a parameter, \(m_t\) and \(v_t\) are moving average and 0-mean variance of a sequence of gradients \(t=0,...,t\).

See also

See the paper linked below for more details. Reddi et al. On the convergence of ADAM and beyond. https://openreview.net/pdf?id=ryQu7f-RZ

Param alpha:

\(\alpha\) Learning rate.

Param beta1:

\(\beta_1\) Decay rate of moving mean.

Param beta2:

\(\beta_2\) Decay rate of moving 0-mean variance.

Param eps:

\(\epsilon\) Small value for avoiding zero division(:math:`\epsilon`). Note this does not appear in the paper.

Param bias_correction:

Apply bias correction to moving averages defined in ADAM. Note this does not appear in the paper.

Public Functions

inline virtual float learning_rate()

Set learning rate.

template<typename T>
class Lamb : public nbla::Solver
#include <lamb.hpp>

LAMB.

\[\begin{split} m_t &\leftarrow \beta_1 m_{t-1} + (1 - \beta_1) g_t\\ v_t &\leftarrow \beta_2 v_{t-1} + (1 - \beta_2) g_t^2\\ \hat{m} &= m_t / (1-\beta_1^t)\\ \hat{v} &= v_t / (1-\beta_2^t)\\ r &= \frac{\hat{m}}{\sqrt{\hat{v}}+\epsilon}\\ w_t &\leftarrow w_{t-1} - \eta_t \frac{\phi (\|w_{t-1}\|)}{\|r + \lambda w_{t-1} \|} \left(r + \lambda w_{t-1} \right) \end{split}\]

where \(g_t\) denotes a gradient, \(m_t\) and \(v_t\) are 1st and 2nd order momentum of the gradient initialized with 0 at \(t=0\), \(\lambda\) is the decoupled weight decay rate set by weight_decay method (lazy evaluation), \(\phi\) is a scaling function defined as \(\phi(z)=\min\{\max\{z, \gamma_l\}, \gamma_u\}\), and the rest is described in the arguments.

See also

See the paper linked below for more details. Yang You, Jing Li, Sashank Reddi. Large Batch Optimization for Deep Learning: Training BERT in 76 minutes. https://arxiv.org/abs/1904.00962

Param eta:

Learning rate ( \(\eta_t\)).

Param beta1:

Decay rate of first-order momentum ( \(\beta_1\)).

Param beta2:

Decay rate of second-order momentum ( \(\beta_2\)).

Param gamma_l:

Lower bound of the clamp scaling function \(\phi\) ( \(\gamma_l\)).

Param gamma_u:

Upper bound of the clamp scaling function \(\phi\) ( \(\gamma_u\)).

Param eps:

Small value for avoiding zero division ( \(\epsilon\)).

Param bias_correction:

Whether to apply bias correction in momentum computation \(\hat{m}\) and \(\hat{v}\).

Public Functions

inline virtual float learning_rate()

Set learning rate.

template<typename T>
class Lars : public nbla::Solver
#include <lars.hpp>

LARS solver defined as.

\[\begin{split} \lambda \leftarrow \eta \frac{\| w_t \|}{\| \Delta w_t + \beta w_t \|} \\ v_{t+1} \leftarrow m v_t + \gamma \lambda (\Delta w_t + \beta w_t) \\ w_{t+1} \leftarrow w_t - v_{t+1} \end{split}\]

See also

See the paper linked below for more details. Yang Youity, Igor Gitmann, and Boris Ginsburg: Large Batch Training of Convolutional Networks https://arxiv.org/pdf/1708.03888

Param lr:

\(\gamma\) Learning rate.

Param momentum:

\(m\) Momentum.

Param coefficient:

\(\eta\) Coefficient of the local learning rate.

Param eps:

\(\epsilon\) Tiny factor for avoiding 0-division.

Public Functions

inline virtual float learning_rate()

Set learning rate.

template<typename T>
class Lion : public nbla::Solver
#include <lion.hpp>

LION solver defined as.

\[\begin{split} u = \beta_1 m_{t} + (1 - \beta_1) g_t\\ u = {\rm sign}(u)\\ m_{t+1} &\leftarrow \beta_2 m_{t} + (1 - \beta_2) g_t\\ w_{t+1} &\leftarrow w_t - \alpha \left( u + \lambda w_t \right) \end{split}\]

See also

See the paper linked below for more details. Xiangning Chen et al., Symbolic Discovery of Optimization Algorithms. https://arxiv.org/abs/2302.06675

Param lr:

\(\alpha\) Learning rate.

Param beta1:

\(\beta_1\) Decay rate.

Param beta2:

\(\beta_2\) Decay rate.

Public Functions

inline virtual float learning_rate()

Set learning rate.

template<typename T>
class Momentum : public nbla::Solver
#include <momentum.hpp>

Momentum.

This is defined as

\[\begin{split} v_t \leftarrow \gamma v_{t-1} + \eta \Delta w_t\\ w_{t+1} \leftarrow w_t - v_t \end{split}\]

See also

See the paper linked below for more details. Ning Qian : On the Momentum Term in Gradient Descent Learning Algorithms http://www.columbia.edu/~nq6/publications/momentum.pdf

Param lr:

\(\eta\) Learning rate.

Param momentum:

\(\gamma\) Momentum

Public Functions

inline virtual float learning_rate()

Set learning rate.

template<typename T>
class Nesterov : public nbla::Solver
#include <nesterov.hpp>

Nesterov accelerated gradient.

This is defined as

\[\begin{split} v_t \leftarrow \gamma v_{t-1} - \eta \Delta w_t \\ w_{t+1} \leftarrow w_t - \gamma v_{t-1} + \left(1 + \gamma \right) v_t \end{split}\]

See also

See the paper linked below for more details. Yurii Nesterov A method for unconstrained convex minimization problem with the rate of convergence o(1/k2)

Param lr:

\(\eta\) Learning rate.

Param momentum:

\(\gamma\) Momentum

Public Functions

inline virtual float learning_rate()

Set learning rate.

template<typename T>
class RMSprop : public nbla::Solver
#include <rmsprop.hpp>

RMSprop.

This is defined as

\[\begin{split} g_t \leftarrow \Delta w_t\\ v_t \leftarrow \gamma v_{t-1} + \left(1 - \gamma \right) g_t^2\\ w_{t+1} \leftarrow w_t - \eta \frac{g_t}{\sqrt{v_t} + \epsilon} \end{split}\]

See also

See the paper linked below for more details. Geoff Hinton Lecture 6a : Overview of mini-batch gradient descent http://www.cs.toronto.edu/~tijmen/csc321/slides/lecture_slides_lec6.pdf

Param lr:

\(\eta\) Learning rate.

Param decay:

\(\gamma\) Decay rate.

Param eps:

\(\epsilon\) Tiny factor for avoiding 0-division.

Public Functions

inline virtual float learning_rate()

Set learning rate.

template<typename T>
class RMSpropGraves : public nbla::Solver
#include <rmsprop_graves.hpp>

RMSpropGraves.

This is defined as

\[\begin{split} n_t \leftarrow \rho n_{t-1} + \left(1 - \rho \right) e^2\\ g_t \leftarrow \rho g_{t-1} + \left(1 - \rho \right) e\\ d_t \leftarrow \beta d_{t-1} - \eta \frac{e}{\sqrt{n_t - {g_t}^2 + \epsilon}}\\ w_{t+1} \leftarrow w_t + d_t \end{split}\]

See also

See the paper linked below for more details. A. Graves Generating Sequences With Recurrent Neural Networks http://arxiv.org/pdf/1308.0850v5.pdf

Param lr:

\(\eta\) Learning rate.

Param decay:

\(\rho\) Decay rate.

Param momentum:

\(\beta\) Momentum.

Param eps:

\(\epsilon\) Tiny factor for avoiding 0-division.

Public Functions

inline virtual float learning_rate()

Set learning rate.

template<typename T>
class Sgd : public nbla::Solver
#include <sgd.hpp>

Vanilla SGD.

This is defined as

\[ w_{t+1} \leftarrow w_t - \eta \Delta w_t \]

Public Functions

inline virtual float learning_rate()

Set learning rate.

template<typename T>
class SgdW : public nbla::Solver
#include <sgdw.hpp>

SGDW.

\[\begin{split} m_{t} &\leftarrow \gamma m_{t-1} + \eta_t \alpha g_t\\ w_{t} &\leftarrow w_{t-1} - m_{t} - \eta_t \lambda w_{t-1} \end{split}\]
where \(g_t\) denotes a gradient, \(m_t\) is momentum of the gradient initialized with 0 at \(t=0\), \(\eta _t\) is the scheduled learning rate, \(\lambda\) is the decoupled weight decay rate set by weight_decay method (lazy evaluation), and the rest is described in the argument documentation.

See also

See the paper linked below for more details. Loshchilov and Hutter, Decoupled Weight Decay Regularization. https://arxiv.org/abs/1711.05101

Param lr:

Initial learning rate ( \(\alpha\)). Note that you have to manage the scheduled learning rate \(\eta_t\) yourelf. By denoting learning rate updated at the set_learning_rate by \(\alpha_t\), we define \(\eta_t = \frac{\alpha_t}{\alpha}\).

Param momentum:

Decay rate of momentum ( \(\gamma\)).

Param wd:

The default weight decay rate ( \(\lambda\)). The weight decay operation is fused into the update operation in SgdW. It uses this default decay rate unless you overwrite a decay rate via weight_decay for the next call of update.

Public Functions

inline virtual float learning_rate()

Set learning rate.

class UpdateHookWithObject
#include <solver.hpp>

Callback helper class for update callbacks.

This is used from Python frontend.

class Solver
#include <solver.hpp>

Solver interface which is extended to implement a new Solver class.

Solver takes care of update rule given gradients of parameters.

\[ w_{t+1} \leftarrow w_t - g_t(\Delta w_t) \]

The function \(g_t(\cdot)\) can have an internal state that is updated when it is called (e.g. Adam).

Subclassed by nbla::AMSBound< T >, nbla::AMSGRAD< T >, nbla::AdaBelief< T >, nbla::AdaBound< T >, nbla::Adadelta< T >, nbla::Adagrad< T >, nbla::Adam< T >, nbla::AdamW< T >, nbla::Adamax< T >, nbla::Lamb< T >, nbla::Lars< T >, nbla::Lion< T >, nbla::Momentum< T >, nbla::Nesterov< T >, nbla::RMSprop< T >, nbla::RMSpropGraves< T >, nbla::Sgd< T >, nbla::SgdW< T >

Public Functions

virtual ~Solver() = 0

Name of Solver class, usually class name.

virtual float learning_rate() = 0

Set learning rate.

bool weight_decay_is_fused() const

Whether the weight decay is lazily evaluated at update_impl.

void zero_grad()

Zeroing grads for all params_.

This is usually called before running a sequence of Function::backward() for propagating whole computation graph.

void set_parameters(const vector<pair<string, VariablePtr>> &params, bool reset = true, bool retain_state = false)

Adding parameters to be optimized via solver.

It calls set_impl().

Parameters:
  • params – Shared pointers of Variables

  • reset – Reset all parameters registered.

  • retain_state – Try to retain state (e.g. momentum) if a parameter is overwritten. Note that this will be ignored if reset=true.

void remove_parameters(const vector<string> &keys)

Remove previously registered parameters by keys.

void clear_parameters()

Clear all parameters.

vector<pair<string, VariablePtr>> get_parameters()

Get all parameters.

vector<pair<string, SolverState>> get_states()

Get all states.

void set_states(const vector<pair<string, SolverState>> &params)

Set states.

inline void clear_state(const string &key)

Clear states.

void update(update_hook_type pre_callback = nullptr, update_hook_type post_callback = nullptr)

Update all params using stored grads in params_ by backpropagation.

This internally calls update_impl() which must be implemented in a derived class.

void weight_decay(float decay_rate, update_hook_type pre_callback = nullptr, update_hook_type post_callback = nullptr)

Apply weight decay to raw gradient.

It must be called before running update() if necessary. This internally calls weight_decay_impl() which must be implemented in a derived class.

It is equivalent to add a squared sum of weight vectors to original loss function.

\[ L_{\rm R}({\mathbf w}) = L_{\rm orig}({\mathbf w}) + {\rm decay\_rate } \times ||{\mathbf w}||_2^2 \]

Parameters:

decay_rate – Coefficient of weight decay.

void clip_grad_by_norm(float norm, update_hook_type pre_callback = nullptr, update_hook_type post_callback = nullptr)

Clip gradients by norm.

The norm is calculated at each variable.

bool check_inf_grad(update_hook_type pre_callback = nullptr, update_hook_type post_callback = nullptr)

Check if there is any inf on the gradients which were setup.

bool check_nan_grad(update_hook_type pre_callback = nullptr, update_hook_type post_callback = nullptr)

Check if there is any nan on the gradients which were setup.

bool check_inf_or_nan_grad(update_hook_type pre_callback = nullptr, update_hook_type post_callback = nullptr)

Check if there is any inf or nan on the gradients which were setup.

void scale_grad(float scale, update_hook_type pre_callback = nullptr, update_hook_type post_callback = nullptr)

Scale gradients,then increase the loss scale.

virtual vector<string> allowed_array_classes()

Get array classes that are allowed to be specified by Context.

struct SolverState
#include <solver.hpp>

Struct for storing both parameter state Variable and iteration.

Public Members

unordered_map<string, VariablePtr> pstate

Parameter state maps.

uint32_t t

Iteration as state.

class SyncedArray : public std::enable_shared_from_this<SyncedArray>
#include <synced_array.hpp>

Synchronized array interface that implicitly transfers and cast arrays over devices and data types.Shared pointer of SyncedArray.

Public Functions

Array *cast(dtypes dtype, const Context &ctx, bool write_only = false, const int async_flags = AsyncFlag::NONE)

Cast and get array with dtype context.

This will return an array object with specified dtype and device with implicit synchronization over different dtypes/devices.

Parameters:
  • write_only[in] When true, just returns an Array instance requested without synchronization.

  • async_flags[in] AsyncFlag::NONE -> Synchronous synchronization AsyncFlag::ASYNC -> Asynchronous synchronization AsyncFlag::UNSAFE -> No synchronization to host AsyncFlag::ASYNC | AsyncFlag::UNSAFE -> The memory region of the source array of an asynchronous data transfer is not guranteed to be kept safe until the end of the transfer.

shared_ptr<Array> cast_sp(dtypes dtype, const Context &ctx, bool write_only = false, const int async_flags = AsyncFlag::NONE)

Cast and get array as a shared_ptr.

See also

cast

const Array *get(dtypes dtype, const Context &ctx, const int async_flags = AsyncFlag::NONE)

Get array with dtype context.

This will return an array object with specified dtype and device with implicit synchronization. Note that this function call does not move the head in array list.

TODO: Is “const” member function appropriate? This implicitly creates or modify array contents of specified dtype-context.

shared_ptr<const Array> get_sp(dtypes dtype, const Context &ctx, const int async_flags = AsyncFlag::NONE)

Get array as a shared pointer.

Array *head_array()

Get the head array.

shared_ptr<Array> head_array_sp()

Get the head array as a shared pointer.

const void *data_ptr(dtypes dtype, const Context &ctx, bool write_only = false, const int async_flags = AsyncFlag::NONE)

Get array’s ptr.

Parameters:
  • dtype[in] Enum of data type.

  • ctx[in] Descriptor of array backend.

  • write_only[in] No synchronization happens.

  • async_flags[in] AsyncFlag::NONE -> Synchronous synchronization happens. AsyncFlag::ASYNC -> Asynchronous synchronization happens. AsyncFlag::SAFE -> Same as AsyncFlag::NONE. AsyncFlag::ASYNC | AsyncFlag::SAFE -> Asynchronous synchronization happens and the synchronized source array keeps safe against the host operation.

inline dtypes dtype() const

Get dtype.

inline bool has_head_array()

Returns true if this SyncedArray has the head array.

inline string head_array_class()

Get the array class of the head.

inline Size_t get_num_arrays() const

Get the number of arrays.

inline Size_t size() const

Size.

void zero()

Fill all element with 0.

Note: This is lazily evaluated at calling of get() or cast().

void fill(float value)

Fill all element with given float value.

size_t modification_count() const

Get number of modification count.

Modification accounts for calling either cast, zero or fill.

bool clear_called() const

Get clear called flag.

void copy_from(const SyncedArray *src)

Copy values from another SynedArray.

Note

The copy is happening in a device and a dtype of source array.

bool zeroing() const

Get whether or not it fills array values obtained in cast/get call later.

This is provided to determine gradient accumulation flags in our computation graph engine, as well as to determine whether or not solver and communicator execute their operations by depending on whether gradients are updated.

shared_ptr<SyncedArray> narrow(const Size_t narrow_size, const Size_t offset)

Get a new narrowed SyncedArray.

inline bool is_narrowed() const

Returns true if this SyncedArray is narrowed.

inline bool is_child() const

Returns true if this SyncedArray has the Parent.

inline bool is_root() const

Returns true if this SyncedArray has not the Parent.

inline bool has_family() const

Returns true if this SyncedArray has a parent or child SyncedArrays.

int get_python_user_reference_counts() const

Get the refernce counts from a Python user.

void update_python_user_reference_counts(const int diff)

Update the refernce counts from a Python user.

class SyncedArrayCallback
#include <synced_array.hpp>

Singleton class to store a callback function for get, cast, and clear of SyncedArray.

Public Functions

bool empty()

Check if callback function is not set.

void set_callback_func(synced_array_callback_func_type f)

Set a callback function.

void call_callback(SyncedArrayPtr saptr, const SyncedArrayCallbackTag func_name, const dtypes dtype, const Context &ctx, const bool write_only, const bool first_creation, const bool off_recording)

Call callback.

class DlpackArrayRegistry
#include <dlpack_array_registry.hpp>

DlpackArrayCreator class this is never be instantiated.

This creator class is used only for DlpackArray.

Public Static Functions

static Context create_context(const DLTensor &dlp)

Interface to create a context for DlpackArray.

static DLDeviceType array_to_device_type(const string &array_class)

Interface to convert an array class to DLDeviceType.

static void add_map(const DLDeviceType device_type, const string &backend, const string &array_class)

Register the map from DLDeviceType to Array class and backend name.

static void add_map(const string &array_class, const DLDeviceType device_tyep)

Register the map from Array class to DLDeviceType.

class BaseVariable

Subclassed by nbla::Variable

Public Functions

virtual void update_python_user_reference_counts(const int diff) final

Update the refernce counts from a Python user.

inline virtual NdArrayPtr data() final

Get data NdArray.

inline virtual NdArrayPtr grad() final

Get grad NdArray.

virtual NBLA_API void set_data (NdArrayPtr data) final

Set data NdArray.

virtual NBLA_API void set_grad (NdArrayPtr grad) final

Set grad NdArray.

class Variable : public nbla::BaseVariable
#include <variable.hpp>

User interface for Array and passed to Function.Shared pointer of Variable.

Users will create arrays via Variable and pass them to Function. Variable has two array region internally, data and grad. Data region is used as an input and/or output of Function::forward(), while grad region is used for storing backprop error of Function::backward().

Public Functions

NBLA_API Variable(const Shape_t &shape = {})

Constructor.

Parameters:

shapeShape.

NBLA_API Variable(NdArrayPtr data)

Constructor given NdArray.

Parameters:

data – A reference of NdArray created by another can be passed.

NBLA_API void reshape (const vector< int64_t > &shape, bool force)

Reshape.

NBLA_API Ptr view ()

Create a new view object without copying data.

NBLA_API Ptr view (const Shape_t &shape)

Create a new view object given shape without copying data.

Parameters:

shapeShape. The total size of the shape must match the size of this instance.

inline Shape_t shape() const

Return shape of variable.

inline Shape_t strides() const

Return strides of variable.

NBLA_API Size_t size (Size_t axis=-1) const

Size of Array (Product of shape dimensions).

Parameters:

axis – Size followed by given axis is computed.

inline Size_t ndim() const

Number of dimensions of array.

template<typename T>
inline T *cast_data_and_get_pointer(const Context &ctx, bool write_only = false)

A shortcut function to cast data and get pointer.

template<typename T>
inline T *cast_grad_and_get_pointer(const Context &ctx, bool write_only = false)

A shortcut function to cast grad and get pointer.

template<typename T>
inline const T *get_data_pointer(const Context &ctx)

A shortcut function to get data pointer.

template<typename T>
inline const T *get_grad_pointer(const Context &ctx)

A shortcut function to get grad pointer.

Public Static Functions

template<typename ...Args>
static inline Ptr create(Args... args)

Create a shared_ptr instance of Variable.

namespace cg_utils

Functions

inline shared_ptr<CgVariable> get_item_nd(const Context &ctx, shared_ptr<CgVariable> in, vector<int> slice_shape)
template<typename T>
inline void copy_data_cgvariable_to_variable(const Context &ctx, shared_ptr<CgVariable> in, Variable *out)
namespace eigen

Typedefs

template<typename T>
using Matrix = Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>
template<typename T>
using RowVector = Eigen::Matrix<T, 1, Eigen::Dynamic>
template<typename T>
using ColVector = Eigen::Matrix<T, Eigen::Dynamic, 1>
template<typename T>
using MatrixMap = Eigen::Map<Matrix<T>>
template<typename T>
using ConstMatrixMap = Eigen::Map<const Matrix<T>>
template<typename T>
using RowVectorMap = Eigen::Map<RowVector<T>>
template<typename T>
using ConstRowVectorMap = Eigen::Map<const RowVector<T>>
template<typename T>
using ColVectorMap = Eigen::Map<ColVector<T>>
template<typename T>
using ConstColVectorMap = Eigen::Map<const ColVector<T>>
namespace ndi

Functions

template<typename T>
string str(const vector<T> &index)
template<typename T>
inline vector<T> strides(const vector<T> &shape)
template<typename T>
inline T nd2flat(const vector<T> &index, const vector<T> &stride)
template<typename T>
inline T nd2flat(const vector<T> &index, const vector<T> &stride, int axis)
template<typename T>
inline T nd2flat(const vector<T> &index, const vector<T> &stride, std::pair<int, int> axis)
template<typename T>
inline vector<T> flat2nd(T index, const vector<T> &stride)
template<typename T>
inline T inner_size(const vector<T> &shape, int axis)
template<typename T>
inline T outer_size(const vector<T> &shape, int axis)
template<typename T>
inline bool increment(vector<T> &index, const vector<T> &shape)
template<typename T>
inline bool increment(vector<T> &index, const vector<T> &shape, int axis)
template<typename Ta, typename Tb>
inline vector<Ta> multiply(const vector<Ta> &a, const vector<Tb> &b)
template<typename T>
inline vector<T> expand(const vector<T> &a, size_t n, const T &v)
template<typename T>
inline vector<T> make_index(size_t ndim, const T &init = T())
template<typename T>
inline vector<T> batch_reduced_shape(vector<T> &shape, int reduce_axis_up_to)
namespace parametric_functions

Functions

NBLA_API vector< CgVariablePtr > affine (Context &ctx, CgVariablePtr x, int base_axis, int n_out, ParameterDirectory parameters, bool with_bias, bool fix_parameters, Initializer *w_init=nullptr, Initializer *b_init=nullptr)
NBLA_API CgVariablePtr affine (CgVariablePtr x, int base_axis, int n_out, ParameterDirectory parameters, AffineOpts affine_opts=AffineOpts())
NBLA_API vector< CgVariablePtr > batch_normalization (Context &ctx, CgVariablePtr x, const vector< int > &axes, float decay_rate, float eps, bool batch_stats, ParameterDirectory parameters, const bool fix_parameters)
NBLA_API CgVariablePtr batch_normalization (CgVariablePtr x, bool batch_stat, ParameterDirectory parameters, BatchNormalizationOpts batch_opts=BatchNormalizationOpts())
NBLA_API vector< CgVariablePtr > convolution (Context &ctx, CgVariablePtr x, int base_axis, int n_map_out, const vector< int > &kernel, const vector< int > &pad, const vector< int > &stride, const vector< int > &dilation, int group, bool channel_last, ParameterDirectory parameters, bool with_bias, bool fix_parameters, Initializer *w_init, Initializer *b_init)
NBLA_API CgVariablePtr convolution (CgVariablePtr x, int base_axis, int n_map_out, const vector< int > &kernel, ParameterDirectory parameters, ConvolutionOpts conv_opts=ConvolutionOpts())
NBLA_API vector< CgVariablePtr > deconvolution (Context &ctx, CgVariablePtr x, int base_axis, int n_map_out, const vector< int > &kernel, const vector< int > &pad, const vector< int > &stride, const vector< int > &dilation, int group, ParameterDirectory parameters, bool with_bias, bool fix_parameters, Initializer *w_init, Initializer *b_init)
NBLA_API CgVariablePtr deconvolution (CgVariablePtr x, int base_axis, int n_map_out, const vector< int > &kernel, ParameterDirectory parameters, DeconvolutionOpts conv_opts=DeconvolutionOpts())
class AffineOpts
class ConvolutionOpts
class DeconvolutionOpts
class BatchNormalizationOpts
namespace utils

Utils.

NNabla utilities.

Functions

NBLA_API bool load_parameters (ParameterDirectory &pd, string filename)

Load parameters from a parameter file(*.h5, *.protobuf, *.prototxt, *.pb and so on)

NBLA_API bool save_parameters (ParameterDirectory &pd, string filename)

Save parameters to specified parameter file.

NBLA_API bool load_parameters_h5 (ParameterDirectory &pd, char *buf, int size)

Load parameters from a file buffer, specified by buffer address and length.

This buffer should contain the parameter file data load from a .h5 file.

NBLA_API bool load_parameters_pb (ParameterDirectory &pd, char *buf, int size)

Load parameters from a file buffer, specified by buffer address and length.

This buffer should contain the parameter file data load from a .protobuf file.

NBLA_API bool save_parameters_pb (ParameterDirectory &pd, char *buf, unsigned int &size)

Save parameters to a buffer, specified by buffer address and length.

NBLA_API bool save_parameters_h5 (ParameterDirectory &pd, char *buf, unsigned int &size)

Save parameters to a file buffer, specified by buffer address and length.

namespace nnp

NNabla format file utilities.

class Network
#include <nnp.hpp>

Network object associated with Nnp object.

The following code will get Network instance from Nnp object nnp, and set batch size.

shared_ptr<Network> network = nnp.get_network("net1");
network.set_batch_size(64);

The next block will get the references to the variable object in the computation graph by name. The computation graph is built when get_variable(name) is called first time, or first time since the batch size or network topology is changed.

nbla::CgVariablePtr x = network.get_variable("input");
nbla::CgVariablePtr y = network.get_variable("output");

You can set data to a variable by accessing array data by using NNabla C++ interface.

float *data = x->variable()->cast_data_and_get_pointer<float>(
    dl.Context().set_array_class("CpuCachedArray"));
for (int i = 0; i < x->variable()->size(); i++) {
    data[i] = ...;  // Set data
}

The forward propagation of the network can be executed at any variable by calling forward method. The function execution will be propagated from root (input) variables to to the variable.

y->forward(true);

Getting and displaying output are as follows.

const float *out = y->variable()->get_data_pointer<float>(
    dl.Context().set_array_class("CpuCachedArray"));
for (int i = 0; i < y->variable()->size(); i++) {
    std::cout << out[i] << ",";
}
std::cout << std::endl;

Note

The Network instance is created by a class member function Nnp::get_network(). The constructor is hidden, and not called directly by users.

Public Functions

NBLA_API string name () const

Network name.

NBLA_API void set_batch_size (int batch_size)

Set batch size.

Parameters:

batch_size[in] Overwrite the default batch size in nnp file.

NBLA_API int batch_size () const

Get batch size.

Return values:

Batch – size. The if set_batch_size is not previously called, batch size written in nnp file will be returned.

NBLA_API void replace_variable (const string &name, CgVariablePtr variable)

Replace an arbitrary variable in the network with a given variable.

The predecessors of the variable in the networks are discarded, and replaced with the predecessors of the given variable.

Parameters:
  • name[in] Name of variable in the network you are replacing.

  • variable[in] Replaced with this.

NBLA_API CgVariablePtr get_variable (const string &name)

Get a variable by name.

This is usually used to set or get data inside the variable. The construction of a computation graph is invoked by calling this if the graph is not latest or not created.

Parameters:

name[in] Name of variable in the network.

Return values:

Variable – in a computation graph.

class Executor
#include <nnp.hpp>

Executor associated with Nnp object.

The Executor object internally stores a Network object.

Public Functions

NBLA_API string name () const

Executor name.

NBLA_API string network_name () const

Network name.

NBLA_API void set_batch_size (int batch_size)

Set batch size.

Parameters:

batch_size[in] Overwrite the default batch size in Network.

NBLA_API int batch_size () const

Get batch size.

Return values:

Batch – size. The if set_batch_size is not previously called, batch size written in the Network of NNabla format file will be returned.

NBLA_API vector< DataVariable > get_data_variables ()

Get data variables.

Return values:

Data – variables where each item holds name info and CgVariable instance in the Network. The data inside the CgVariable should be gotten via Nnabla C++ interface.

NBLA_API vector< OutputVariable > get_output_variables ()

Get output variables.

Return values:

Output – variables where each item holds name info and CgVariable instance in the Network. The data inside the CgVariable should be gotten via Nnabla C++ interface.

NBLA_API shared_ptr< Network > get_network ()

Get the reference (shared_ptr) of Network object held in this.

NBLA_API void execute ()

Execute the network from inputs to outputs.

struct DataVariable
#include <nnp.hpp>

Data variable container.

The string fields corresponds to DataVariable in proto definition.

struct OutputVariable
#include <nnp.hpp>

Output variable container.

The string fields corresponds to OutputVariable in proto definition.

class Optimizer

Public Functions

NBLA_API string name () const

Optimizer name.

NBLA_API string network_name () const

Network name.

NBLA_API const int update_interval () const

Update interval.

NBLA_API shared_ptr< Network > get_network ()

Get the reference (shared_ptr) of Network object held in this.

NBLA_API const float update (const int iter)

Execute update operations including forward, backward and update.

Parameters:

iter – Number of iteration.

Return values:

loss – value.

struct DataVariable
#include <nnp.hpp>

Data variable container.

The string fields corresponds to DataVariable in proto definition.

struct GeneratorVariable
#include <nnp.hpp>

Generator variable container.

The string fields corresponds to GeneratorVariable in proto definition.

struct LossVariable
#include <nnp.hpp>

Loss variable container.

The string fields corresponds to LossVariable in proto definition.

struct ParameterVariable
#include <nnp.hpp>

Parameter variable container.

The string fields corresponds to ParameterVariable in proto definition.

class Monitor

Public Functions

NBLA_API string name () const

Monitor name.

NBLA_API string network_name () const

Network name.

shared_ptr<Network> get_network()

Get the reference (shared_ptr) of Network object held in this.

NBLA_API const float monitor_epoch ()

Execute monitor operations including forward process.

Return values:

monitored – value.

struct DataVariable
#include <nnp.hpp>

Data variable container.

The string fields corresponds to DataVariable in proto definition.

struct MonitorVariable
#include <nnp.hpp>

Monitor variable container.

The string fields corresponds to OutputVariable in proto definition.

class TrainingConfig

Public Functions

NBLA_API const long long int max_epoch () const

Max epoch.

NBLA_API const long long int iter_per_epoch () const

Iteration per epoch.

NBLA_API const bool save_best () const

Save best.

class Nnp
#include <nnp.hpp>

Handle of NNabla format files.

You can create an Nnp object by passing default context as below.

using namespace nbla::utils::nnp;
nbla::Context ctx{"cpu", "CpuCachedArray", "0", "default"};
Nnp nnp(ctx);

Suppose we have network.nnp which is previously created. You can add a previously dumped NNabla format file to Nnp object. Nnp will parse the file format and internally store the information such as network architectures, learned parameters and execution settings.

nnp.add("network.nnp");

Suppose a network “net1” is in network.npp. The following line will create a Network object from the nnp file. Network can create a computation graph defined in NNabla format files. The created computation graph can be executed in C++ code. See Network doc for the usage.

shared_ptr<Network> network = nnp.get_network("net1");

... // Use network here.

Suppose an executor “exe1” is in network.npp. The following line will create a Executor object from NNabla format files. The Executor can also create a computation graph of a network associated with the Executor field in NNabla format files. The Executor provides easier interface to set input, execute the graph, and get output.

shared_ptr<Executor> executor = nnp.get_executor("exe1");

... // Use executor here.

Public Functions

NBLA_API Nnp(const nbla::Context &ctx)

Constructor which sets default context.

Parameters:

ctx[in] Default context which overwrites the config in nnp file.

NBLA_API bool add (const string &filename)

Add nnp|nntxt|h5 file.

NBLA_API bool add (char *buffer, unsigned int size)

Add nnp on memory.

NBLA_API vector< string > get_network_names ()

Get Network name list from added files (nnp, nntxt etc.).

Return values:

A – vector of Network instance names.

NBLA_API shared_ptr< Network > get_network (const string &name)

Get Network object from added files (nnp, nntxt etc.).

Parameters:

name[in] Network name in loaded files (nnp, nntxt etc.)

Return values:

A – shared pointer of a Network instance.

NBLA_API vector< string > get_executor_names ()

Get Executor name list from added files (nnp, nntxt etc.).

Return values:

A – vector of Executor instance names.

NBLA_API shared_ptr< Executor > get_executor (const string &name)

Get Executor object from added file(s).

Parameters:

name[in] Executor name in loaded files (nnp, nntxt etc.)

Return values:

A – shared pointer of a Executor instance.

NBLA_API vector< string > get_optimizer_names ()

Get Optimizer name list from added files (nnp, nntxt etc.).

Return values:

A – vector of Optimizer instance names.

NBLA_API shared_ptr< Optimizer > get_optimizer (const string &name)

Get Optimizer object from added files (nnp, nntxt etc.).

Parameters:

name[in] Optimizer name in loaded files (nnp, nntxt etc.)

Return values:

A – shared pointer of a Optimizer instance.

NBLA_API vector< string > get_monitor_names ()

Get Monitor name list from added files (nnp, nntxt etc.).

Return values:

A – vector of Monitor instance names.

NBLA_API shared_ptr< Monitor > get_monitor (const string &name)

Get Monitor object from added files (nnp, nntxt etc.).

Parameters:

name[in] Monitor name in loaded files (nnp, nntxt etc.)

Return values:

A – shared pointer of a Monitor instance.

NBLA_API shared_ptr< TrainingConfig > get_training_config ()

Get TrainingConfig object from added files (nnp, nntxt etc.).

Return values:

A – shared pointer of a TrainingConfig instance.

NBLA_API vector< pair< string, VariablePtr > > get_parameters ()

Get parameters.

Return values:

A – vector of string and variable pointer pairs.

NBLA_API bool save_parameters (const string &filename)

Save parameters.

Parameters:

name[in] output binary filename (.protobuf or .h5)

namespace rnn

Functions

inline void compute_batch_sizes(const int *lengths, int lsize, int *batch_sizes)
template<typename U, bool accum = false>
inline void pack(const U *padded_sequence, const int *batch_sizes, U *packed_sequence, int T, int B, int D)
template<typename U, bool accum = false>
inline void pack_batch_first(const U *padded_sequence, const int *batch_sizes, U *packed_sequence, int T, int B, int D)
template<typename U, bool accum = false>
inline void pack_batch_first(const U *padded_sequence, const int *batch_sizes, U *packed_sequence, int T, int B, int D, int TL)
inline void compute_lengths(const int *batch_sizes, int bsize, int *lengths)
template<typename U, bool accum = false>
inline void unpack(const U *packed_sequence, const int *batch_sizes, U *padded_sequence, int T, int B, int D)
template<typename U, bool accum = false>
inline void unpack(const U *packed_sequence, const int *batch_sizes, U *padded_sequence, int T, int B, int D, int TL)
template<typename U, bool accum = false>
inline void unpack_batch_first(const U *packed_sequence, const int *batch_sizes, U *padded_sequence, int T, int B, int D)
template<typename U, bool accum = false>
inline void unpack_batch_first(const U *packed_sequence, const int *batch_sizes, U *padded_sequence, int T, int B, int D, int TL)
namespace warp_by_grid

Enums

enum PADDING_MODE

Values:

enumerator zero
enumerator repeat
enumerator reflect