Reduce
#include <popops/Reduce.hpp>
Define types of operations used in a reduce.
-
namespace popops
Common functions, such as elementwise and reductions.
Unnamed Group
-
poplar::Tensor reduce(poplar::Graph &graph, const poplar::Tensor &in, const poplar::Type &outType, const std::vector<std::size_t> &dims, ReduceParams params, std::vector<poplar::ComputeSet> &css, const poplar::DebugContext &debugContext = {}, const poplar::OptionFlags &options = {})
Apply a reduction operation to a tensor.
- Deprecated:
The reduce overloads that expect a vector of compute sets are deprecated. Please use the reduceMany() function instead.
These are alternate forms that add their vertices to a vector of compute sets instead of a poplar::program::Sequence. The caller is expected to add each compute set to a poplar::program::Sequence (in a poplar::program::Execute) themselves, like this:
Sequence seq; std::vector<ComputeSet> css; auto A = reduce(..., css); auto B = reduce(..., css); for (const auto &cs : css) { seq.add(Execute(cs));
This allows you to do multiple reductions in parallel. Note that the reductions are not aware of each other, so it may be more efficient to concatenate tensors and do a single reduction instead if they have the same shape, operation, and input and output types.
scaleandupdateare only valid with theADD,SQUARE_ADDorLOG_ADDoperations.LOG_ADDperforms all arithmetic consistent with the input and output being log probabilities. In other words, theupdateis another log add operation and thescaleis a log multiply operation.Internally, this creates a new variable for the output then calls reduceWithOutput(). The type of the output will be
outType.The options parameter accepts the following:
accumType.interTile (float, half)
The type to use for intermediate values between tiles. If the type specified is smaller than the output type then the option is ignored and the output type is used instead.
accumType.inVertex (float, half)
The type to use for intermediate values within a vertex. If the type specified is smaller than the output type then the option is ignored and the output type is used instead.
If either of the above options are not set then the intermediate type will default to either the input tensor element type or float if the input is of type half and the reduction operation benefits from higher precision (for example, add).
The input and output types that are supported depend on the operation:
ADD,SQUARE_ADD,MUL: float->float, half->half, int->int, float->half, half->floatLOG_ADD: float->float, half->half, float->half, half->floatMAX,MIN: float->float, half->half, int->intLOGICAL_AND,LOGICAL_OR: bool->bool
- Parameters
graph – The graph to add the operation to.
in – The tensor to be reduced.
outType – The output type of the reduce operation.
dims – The dimensions to reduce in.
prog – The program sequence to add the operation to.
debugContext – Optional debug information.
-
poplar::Tensor reduce(poplar::Graph &graph, const poplar::Tensor &in, const std::vector<std::size_t> &dims, ReduceParams params, std::vector<poplar::ComputeSet> &css, const poplar::DebugContext &debugContext = {}, const poplar::OptionFlags &options = {})
-
void reduceWithOutput(poplar::Graph &graph, const poplar::Tensor &in, const poplar::Tensor &out, const std::vector<std::size_t> &dims, ReduceParams params, std::vector<poplar::ComputeSet> &css, const poplar::DebugContext &debugContext = {}, const poplar::OptionFlags &options = {})
Functions
-
poplar::Tensor reduce(poplar::Graph &graph, const poplar::Tensor &in, const poplar::Type &outType, const std::vector<std::size_t> &dims, ReduceParams params, poplar::program::Sequence &prog, const poplar::DebugContext &debugContext = {}, const poplar::OptionFlags &options = {})
Apply a reduction operation to a tensor.
scaleandupdateare only valid with theADD,SQUARE_ADDorLOG_ADDoperations.LOG_ADDperforms all arithmetic consistent with the input and output being log probabilities. In other words, theupdateis another log add operation and thescaleis a log multiply operation.Internally, this creates a new variable for the output then calls reduceWithOutput(). The type of the output will be
outType.The options parameter accepts the following:
accumType.interTile (float, half)
The type to use for intermediate values between tiles. If the type specified is smaller than the output type then the option is ignored and the output type is used instead.
accumType.inVertex (float, half)
The type to use for intermediate values within a vertex. If the type specified is smaller than the output type then the option is ignored and the output type is used instead.
If either of the above options are not set then the intermediate type will default to either the input tensor element type or float if the input is of type half and the reduction operation benefits from higher precision (for example, add).
The input and output types that are supported depend on the operation:
ADD,SQUARE_ADD,MUL: float->float, half->half, int->int, float->half, half->floatLOG_ADD: float->float, half->half, float->half, half->floatMAX,MIN: float->float, half->half, int->intLOGICAL_AND,LOGICAL_OR: bool->bool
- Parameters
graph – The graph to add the operation to.
in – The tensor to be reduced.
outType – The output type of the reduce operation.
dims – The dimensions to reduce in.
prog – The program sequence to add the operation to.
debugContext – Optional debug information.
-
poplar::Tensor reduce(poplar::Graph &graph, const poplar::Tensor &in, const std::vector<std::size_t> &dims, ReduceParams params, poplar::program::Sequence &prog, const poplar::DebugContext &debugContext = {}, const poplar::OptionFlags &options = {})
Apply a reduction operation to a tensor.
An alias for reduce(graph, in, in.elementType(), …)
scaleandupdateare only valid with theADD,SQUARE_ADDorLOG_ADDoperations.LOG_ADDperforms all arithmetic consistent with the input and output being log probabilities. In other words, theupdateis another log add operation and thescaleis a log multiply operation.Internally, this creates a new variable for the output then calls reduceWithOutput(). The type of the output will be
outType.The options parameter accepts the following:
accumType.interTile (float, half)
The type to use for intermediate values between tiles. If the type specified is smaller than the output type then the option is ignored and the output type is used instead.
accumType.inVertex (float, half)
The type to use for intermediate values within a vertex. If the type specified is smaller than the output type then the option is ignored and the output type is used instead.
If either of the above options are not set then the intermediate type will default to either the input tensor element type or float if the input is of type half and the reduction operation benefits from higher precision (for example, add).
The input and output types that are supported depend on the operation:
ADD,SQUARE_ADD,MUL: float->float, half->half, int->int, float->half, half->floatLOG_ADD: float->float, half->half, float->half, half->floatMAX,MIN: float->float, half->half, int->intLOGICAL_AND,LOGICAL_OR: bool->bool
- Parameters
graph – The graph to add the operation to.
in – The tensor to be reduced.
outType – The output type of the reduce operation.
dims – The dimensions to reduce in.
prog – The program sequence to add the operation to.
debugContext – Optional debug information.
-
void reduceWithOutput(poplar::Graph &graph, const poplar::Tensor &in, const poplar::Tensor &out, const std::vector<std::size_t> &dims, ReduceParams params, poplar::program::Sequence &prog, const poplar::DebugContext &debugContext = {}, const poplar::OptionFlags &options = {})
Apply a reduction operation to a tensor.
This is similar to reduce() but allows you to specify the output. If the tile mapping of
outis not complete it will be set. Otherwise it won’t be changed.scaleandupdateare only valid with theADD,SQUARE_ADDorLOG_ADDoperations.LOG_ADDperforms all arithmetic consistent with the input and output being log probabilities. In other words, theupdateis another log add operation and thescaleis a log multiply operation.Internally, this creates a new variable for the output then calls reduceWithOutput(). The type of the output will be
outType.The options parameter accepts the following:
accumType.interTile (float, half)
The type to use for intermediate values between tiles. If the type specified is smaller than the output type then the option is ignored and the output type is used instead.
accumType.inVertex (float, half)
The type to use for intermediate values within a vertex. If the type specified is smaller than the output type then the option is ignored and the output type is used instead.
If either of the above options are not set then the intermediate type will default to either the input tensor element type or float if the input is of type half and the reduction operation benefits from higher precision (for example, add).
The input and output types that are supported depend on the operation:
ADD,SQUARE_ADD,MUL: float->float, half->half, int->int, float->half, half->floatLOG_ADD: float->float, half->half, float->half, half->floatMAX,MIN: float->float, half->half, int->intLOGICAL_AND,LOGICAL_OR: bool->bool
- Parameters
graph – The graph to add the operation to.
in – The tensor to be reduced.
outType – The output type of the reduce operation.
dims – The dimensions to reduce in.
prog – The program sequence to add the operation to.
debugContext – Optional debug information.
-
void reduceMany(poplar::Graph &graph, const std::vector<SingleReduceOp> &reductions, std::vector<poplar::Tensor> &outputs, poplar::program::Sequence &prog, const poplar::DebugContext &debugContext = {}, const poplar::OptionFlags &options = {})
Perform many reductions (in parallel if possible).
Please see the documentation for reduce() for details of the common inputs.
- Parameters
reductions – The inputs to each reduction to perform. The
outTypeattribute controls the element type of the output tensor ifoutputsis empty, otherwise it is ignored. Ifoutputsis empty anduseOutTypeisfalsethen the output element type will be set to the same element type as the correspondingintensor.outputs – The tensors to store the output of the reductions. This may be empty in which case
reduceManywill create the tensors. If the tile mapping is not set or not complete it will be set completely by this function.
- Throws
poputils::poplibs_error – If
outputsis not empty then its size must exactly match the size of reductions else an exception will be thrown.poputils::poplibs_error – If
outputsis empty and any reduction hasparams.updateset to true then an exception will be thrown.outputsis required to perform an update reduction.
-
struct ReduceParams
- #include <Reduce.hpp>
Stores parameters for the reduce operation, as well as the basic operation being performed (for example,
addormul).Public Functions
-
ReduceParams() = default
-
inline ReduceParams(popops::Operation op, bool update, poplar::Tensor scale)
Define the details of the reduce operation that will be performed by the reduce() and reduceWithOutput() functions.
- Parameters
op – The reduce operation to use.
scale – Can (optionally) scale the output.
update – Specify that the output should be updated, where
out += reduce(in)rather thanout = reduce(in).
-
ReduceParams() = default
-
struct SingleReduceOp
- #include <Reduce.hpp>
The parameterisation of the inputs to a single reduction for the reduceMany() function.
Please see the documentation for reduce() for a description of the struct members.
Public Functions
Public Members
-
ReduceParams params
-
bool useOutType
Note that if
useOutTypeisfalsethen the element type ofinis used.Also note that
OutTypeis ignored if theoutputsvector is not empty when calling reduceMany().
-
ReduceParams params
-
poplar::Tensor reduce(poplar::Graph &graph, const poplar::Tensor &in, const poplar::Type &outType, const std::vector<std::size_t> &dims, ReduceParams params, std::vector<poplar::ComputeSet> &css, const poplar::DebugContext &debugContext = {}, const poplar::OptionFlags &options = {})