Cast

#include <popops/Cast.hpp>

Casts between tensor types.

namespace popops

Common functions, such as elementwise and reductions.

Functions

poplar::Tensor cast(poplar::Graph &graph, const poplar::Tensor &src, const poplar::Type &dstType, poplar::program::Sequence &prog, const poplar::DebugContext &debugContext = {})

Cast elements of the specified src tensor to dstType, returning the result as a new tensor.

Note: If dstType == src.elementType(), then the operation is a copy.

Parameters
  • graph – The graph that the operation will be added to.

  • src – Source tensor to cast.

  • dstType – The type of the destination tensor.

  • prog – Program to add the cast operation to.

  • debugContext – Optional debug information.

Returns

The resultant cast tensor.

poplar::Tensor cast(poplar::Graph &graph, const poplar::Tensor &src, const poplar::Type &dstType, const poplar::Tensor &metadata, poplar::program::Sequence &prog, const poplar::DebugContext &debugContext = {})

Cast elements of the specified src tensor to dstType, returning the result as a new tensor.

Note: If dstType == src.elementType(), then the operation is a copy.

Parameters
  • graph – The graph that the operation will be added to.

  • src – Source tensor to cast.

  • dstType – The type of the destination tensor.

  • metadata – A tensor of type QUARTER_METADATA initialised with the format and scale to use when casting to the destination tensor. The returned tensor will contain this metadata.

  • prog – Program to add the cast operation to.

  • debugContext – Optional debug information.

Returns

The resultant cast tensor.

poplar::program::Program cast(poplar::Graph &graph, poplar::Tensor src, poplar::Tensor dst, const poplar::DebugContext &debugContext = {})

Create a program to copy tensor casting between types (for example, half->float).

Precondition: src.shape() == dst.shape()

Note: If dst.elementType() == src.elementType(), then the operation is just a copy.

Parameters
  • graph – The graph that the operation will be added to.

  • src – Source tensor.

  • dst – Destination tensor.

  • debugContext – Optional debug information.

Returns

The program to perform this operation.

void cast(poplar::Graph &graph, poplar::Tensor src, poplar::Tensor dst, poplar::ComputeSet cs)

Create vertices to copy element wise from the src tensor to the dst tensor casting between types (for example, half->float).

The vertices are added to the specified compute set.

Precondition: src.shape() == dst.shape()

Parameters
  • graph – The graph that the operation will be added to.

  • src – Source tensor.

  • dst – Destination tensor.

  • cs – Compute set to add the vertices to.

poplar::Tensor cast(poplar::Graph &graph, poplar::Tensor src, const poplar::Type &dstType, poplar::ComputeSet cs, const poplar::DebugContext &debugContext = {})

Create vertices to cast elements of the specified src tensor to dstType, returning the result as a new tensor.

The vertices are added to the specified compute set.

Parameters
  • graph – The graph that the operation will be added to.

  • src – Source tensor.

  • dstType – Destination type.

  • cs – Compute set to add the vertices to.

  • debugContext – Optional debug information.

Returns

Resultant destination tensor.

poplar::Tensor cast(poplar::Graph &graph, poplar::Tensor src, const poplar::Type &dstType, const poplar::Tensor &metadata, poplar::ComputeSet cs, const poplar::DebugContext &debugContext = {})

Create vertices to cast elements of the specified src tensor to dstType, returning the result as a new tensor.

The vertices are added to the specified compute set.

Parameters
  • graph – The graph that the operation will be added to.

  • src – Source tensor.

  • dstType – Destination type.

  • metadata – A tensor of type QUARTER_METADATA initialised with the format and scale to use when casting to the destination tensor. The returned tensor will contain this metadata.

  • cs – Compute set to add the vertices to.

  • debugContext – Optional debug information.

Returns

Resultant destination tensor.

poplar::Tensor checkAccuracyWhenCast(poplar::Graph &graph, const poplar::Tensor &input, poplar::Type outputType, double tolerance, poplar::program::Sequence &prog, const poplar::DebugContext &debugContext = {})

Helper function which checks the relative error in the tensor input when casting it to type outputType.

The result is a single element bool tensor which is set to true if the error is less than tolerance.

Preconditions:

  • input.elementType() == FLOAT

  • outputType == HALF

  • input.numElements() == 1

Parameters
  • graph – The graph that the operation will be added to.

  • input – Input tensor.

  • outputType – Output type after the cast operation.

  • tolerance – Allowed tolerance in error from cast operation.

  • prog – Program to add the check onto.

  • debugContext – Optional debug information.

Throws

poputil::poplibs_error – If either input or outputType are not either half or float.

Returns

Boolean tensor indicating that the error is less than tolerance.