ScaledAdd

#include <popops/ScaledAdd.hpp>

Functions for scaling and adding tensors.

namespace popops

Common functions, such as elementwise and reductions.

Enums

enum class ScaledAddSpecialisation

Values:

enumerator DEFAULT
enumerator X_MINUS_AX_PLUS_BY

Functions

void scaledAddTo(poplar::Graph &graph, poplar::Tensor A, poplar::Tensor B, float scaleB, poplar::program::Sequence &prog, const poplar::DebugContext &debugContext = {}, const poplar::OptionFlags &options = {})

Add the elements of one tensor multiplied by a scalar to another tensor.

Performs the calculations A += scaleB * B

The operation is performed after casting B to the type of A.

Scaled add options

  • optimizeForSpeed (true, false) [=false]

    The scaledAdd vertices default to being optimized to aid memory allocation. To optimise them for speed instead, set this option to true.

  • scaleFloatToHalfTolerance (double) [=1e-6]

    Where the tensors A, B are of type half and a scaleB is provided as a float or a tensor of type float, it is possible to to implement the scaledAddTo in half precision if scaleB

    can be cast to half precision with acceptable accuracy. Otherwise full precision arithmetic can be used internally, but at the cost of speed. Floating point arithmetic will be selected if the relative error in casting is greater than the relative tolerance.

    Only applies to

    scaledAddTo() with scaleB.

Parameters
  • graph – The Poplar graph.

  • A – The destination tensor.

  • B – The second tensor to add elements from (must be of the same shape as A).

  • scaleB – The scalar to multiply elements of B with before addition.

  • prog – A sequence program to which the code performing the add will be appended.

  • debugContext – Optional debug information.

  • options – A list of flags to control optimizations.

void scaledAddTo(poplar::Graph &graph, poplar::Tensor A, poplar::Tensor B, poplar::Tensor scaleB, poplar::program::Sequence &prog, const poplar::DebugContext &debugContext = {}, const poplar::OptionFlags &options = {})

Add the elements of one tensor each multiplied by a (scalar) tensor to another tensor.

Performs the calculations A += scaleB * B

The operation is performed after casting scaleB and B to the type of A.

Parameters
  • graph – The Poplar graph.

  • A – The destination tensor.

  • B – The second tensor to add elements from (must be of the same shape as A).

  • scaleB – The scalar tensor to multiply elements of B with before addition.

  • prog – A sequence program to which the code performing the add will be appended.

  • debugContext – Optional debug information.

  • options – A list of flags to control optimizations. See scaledAddTo().

void scaledSubtractFrom(poplar::Graph &graph, poplar::Tensor A, poplar::Tensor B, float scaleB, poplar::program::Sequence &prog, const poplar::DebugContext &debugContext = {}, const poplar::OptionFlags &options = {})

Subtract the elements of one tensor multiplied by a scalar from another tensor.

Performs the calculations A -= scaleB * B

The operation is performed after casting B to type A.

Parameters
  • graph – The Poplar graph.

  • A – The destination tensor.

  • B – The second tensor providing the elements to subtract (must be of the same shape as A).

  • scaleB – The scalar to multiply elements of B with before subtraction.

  • prog – A sequence program to which the code performing the add will be appended.

  • debugContext – Optional debug information.

  • options – A list of flags to control optimizations. See scaledAddTo().

void scaledSubtractFrom(poplar::Graph &graph, poplar::Tensor A, poplar::Tensor B, poplar::Tensor scaleB, poplar::program::Sequence &prog, const poplar::DebugContext &debugContext = {}, const poplar::OptionFlags &options = {})

Subtract the elements of one tensor each multiplied by a (scalar) tensor from another tensor.

Performs the calculations A -= scaleB * B

The operation is performed after casting scaleB, and B to the type of A.

Parameters
  • graph – The Poplar graph.

  • A – The destination tensor.

  • B – The second tensor providing the elements to subtract (must be of the same shape as A).

  • scaleB – The scalar tensor to multiply elements of B with before subtraction.

  • prog – A sequence program to which the code performing the add will be appended.

  • debugContext – Optional debug information.

  • options – A list of flags to control optimizations. See scaledAddTo().

void scaledAddTo(poplar::Graph &graph, poplar::Tensor A, poplar::Tensor scaleA, poplar::Tensor B, poplar::Tensor scaleB, poplar::program::Sequence &prog, const poplar::DebugContext &debugContext = {}, const poplar::OptionFlags &options = {})

Scale the elements of one tensor and add the scaled elements of another tensor to it.

The two scaling factors are (scalar) tensors.

Performs the calculations A = scaleA * A + scaleB * B

The operation is performed after casting scaleA, scaleB and B to the type of A.

Parameters
  • graph – The Poplar graph.

  • A – The destination tensor.

  • scaleA – The scalar tensor to multiply elements of A with before addition.

  • B – The second tensor to add elements from (must be of the same shape as A).

  • scaleB – The scalar tensor to multiply elements of B with before addition.

  • prog – A sequence program to which the code performing the add will be appended.

  • debugContext – Optional debug information.

  • options – A list of flags to control optimizations. See scaledAddTo().

void scaledAddTo(poplar::Graph &graph, poplar::Tensor A, poplar::Tensor scaleA, poplar::Tensor B, poplar::Tensor scaleB, poplar::program::Sequence &prog, const ScaledAddSpecialisation speciality, const poplar::DebugContext &debugContext = {}, const poplar::OptionFlags &options = {})

Scale the elements of one tensor and add the scaled elements of another tensor to it.

The two scaling factors are (scalar) tensors.

Performs the calculations A = scaleA' * A + scaleB * B where scaleA’ is a function of scaleA specified by the “speciality” option.

The operation is performed after casting scaleA, scaleB and B to the type of A.

Parameters
  • graph – The Poplar graph.

  • A – The destination tensor.

  • scaleA – The scalar tensor to multiply elements of A with before addition.

  • B – The second tensor to add elements from (must be of the same shape as A).

  • scaleB – The scalar tensor to multiply elements of B with before addition.

  • prog – A sequence program to which the code performing the add will be appended.

  • speciality – Choice of ScaledAdd expression formulation

  • debugContext – Optional debug information.

  • options – A list of flags to control optimizations. See scaledAddTo().

void scaledAddTo(poplar::Graph &graph, poplar::Tensor A, float scaleA, poplar::Tensor B, float scaleB, poplar::program::Sequence &prog, const poplar::DebugContext &debugContext = {}, const poplar::OptionFlags &options = {})

Scale the elements of one tensor and add the scaled elements of another tensor to it.

The two scaling factors are constants.

Performs the calculations A = scaleA * A + scaleB * B

If A and B are of different types, B is first cast to the type of A and the operation performed.

Parameters
  • graph – The Poplar graph.

  • A – The destination tensor.

  • scaleA – The constant to multiply elements of A with before addition.

  • B – The second tensor to add elements from (must be of the same shape as A).

  • scaleB – The constant to multiply elements of B with before addition.

  • prog – A sequence program to which the code performing the add will be appended.

  • debugContext – Optional debug information.

  • options – A list of flags to control optimizations. See scaledAddTo().

void scaledAddTo(poplar::Graph &graph, poplar::Tensor A, float scaleA, poplar::Tensor B, float scaleB, poplar::program::Sequence &prog, const ScaledAddSpecialisation speciality, const poplar::DebugContext &debugContext = {}, const poplar::OptionFlags &options = {})

Scale the elements of one tensor and add the scaled elements of another tensor to it.

The two scaling factors are constants.

Performs the calculations A = scaleA' * A + scaleB * B where scaleA’ is a function of scaleA specified by the “speciality” option.

If A and B are of different types, B is first cast to the type of A and the operation performed.

Parameters
  • graph – The Poplar graph.

  • A – The destination tensor.

  • scaleA – The constant to multiply elements of A with before addition.

  • B – The second tensor to add elements from (must be of the same shape as A).

  • scaleB – The constant to multiply elements of B with before addition.

  • prog – A sequence program to which the code performing the add will be appended.

  • speciality – Choice of ScaledAdd expression formulation

  • debugContext – Optional debug information.

  • options – A list of flags to control optimizations. See scaledAddTo().

void scaledSubtractFrom(poplar::Graph &graph, poplar::Tensor A, poplar::Tensor scaleA, poplar::Tensor B, poplar::Tensor scaleB, poplar::program::Sequence &prog, const poplar::DebugContext &debugContext = {}, const poplar::OptionFlags &options = {})

Scale the elements of one tensor and subtract the scaled elements of another tensor to it.

The two scaling factors are (scalar) tensors.

Performs the calculations A = scaleA * A - scaleB * B

The operation is performed after casting scaleA, scaleB and B to the type of A.

Parameters
  • graph – The Poplar graph.

  • A – The destination tensor.

  • scaleA – The scalar tensor to multiply elements of A with before subtraction.

  • B – The second tensor to subtract elements from (must be of the same shape as A).

  • scaleB – The scalar tensor to multiply elements of B with before subtraction.

  • prog – A sequence program to which the code performing the subtract will be appended.

  • debugContext – Optional debug information.

  • options – A list of flags to control optimizations. See scaledAddTo().

void scaledSubtractFrom(poplar::Graph &graph, poplar::Tensor A, float scaleA, poplar::Tensor B, float scaleB, poplar::program::Sequence &prog, const poplar::DebugContext &debugContext = {}, const poplar::OptionFlags &options = {})

Scale the elements of one tensor and subtract the scaled elements of another tensor to it.

The two scaling factors are constants.

Performs the calculations A = scaleA * A - scaleB * B

If A and B are of different types, B is first cast to the type of A and the operation performed.

Parameters
  • graph – The Poplar graph.

  • A – The destination tensor.

  • scaleA – The constant to multiply elements of A with before subtraction.

  • B – The second tensor to subtract elements from (must be of the same shape as A).

  • scaleB – The constant to multiply elements of B with before subtraction.

  • prog – A sequence program to which the code performing the subtract will be appended.

  • debugContext – Optional debug information.

  • options – A list of flags to control optimizations. See scaledAddTo().