class nbla::Function

class Function

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.