Documentation
¶
Overview ¶
Package shapeinference calculates the shape resulting from operations.
This can be useful for new backends to test and help plan for buffer space for temporary or output buffers.
It defines BinaryOp function for shape inference for the majority of binary functions, using the standard broadcasting rules.
The majority of the unary functions don't change the shape, except those that explicitly say that in their name, like Reshape, etc.
For the remainder ops, it defines one function per OpType.
It also defines some classes of operations that can be used.
Index ¶
- Variables
- func BinaryOp(opType backends.OpType, lhsShape, rhsShape shapes.Shape) shapes.Shape
- func BroadcastInDimOp(operand, outputShape shapes.Shape, broadcastAxes []int)
- func BroadcastOp(operand shapes.Shape, prefixDims []int) shapes.Shape
- func ComparisonOp(opType backends.OpType, lhsShape, rhsShape shapes.Shape) shapes.Shape
- func ConcatenateOp(inputs []shapes.Shape, axis int) shapes.Shape
- func GatherOp(operand, startIndices shapes.Shape, indexVectorAxis int, ...) shapes.Shape
- func ReduceOp(operand shapes.Shape, axes []int) shapes.Shape
- func ReshapeOp(operand shapes.Shape, dims []int) shapes.Shape
- func ScatterOp(operand, indices, updates shapes.Shape, indexVectorAxis int, ...) shapes.Shape
- func SliceOp(operand shapes.Shape, starts, limits, strides []int) shapes.Shape
- func TransposeOp(operand shapes.Shape, permutations []int) shapes.Shape
- func UnaryOp(opType backends.OpType, operand shapes.Shape) shapes.Shape
- func WhereOp(condition, onTrue, onFalse shapes.Shape) shapes.Shape
Constants ¶
This section is empty.
Variables ¶
var ( // BooleanOperations take booleans as input, aka. logical operations. BooleanOperations = types.SetWith( backends.OpTypeLogicalAnd, backends.OpTypeLogicalOr, backends.OpTypeLogicalXor, backends.OpTypeLogicalNot, ) // BitwiseOperations operates only on integer (binary) numbers and won't work on floats or complex numbers. BitwiseOperations = types.SetWith( backends.OpTypeBitwiseAnd, backends.OpTypeBitwiseOr, backends.OpTypeBitwiseXor, backends.OpTypeBitwiseNot, backends.OpTypeBitCount, backends.OpTypeShiftLeft, backends.OpTypeShiftRightArithmetic, backends.OpTypeShiftRightLogical, backends.OpTypeBitCount, backends.OpTypeClz, ) // NumberOperations can take any type of number as input: integer, float or complex. NumberOperations = types.SetWith( backends.OpTypeAdd, backends.OpTypeSub, backends.OpTypeMul, backends.OpTypeDiv, backends.OpTypePow, backends.OpTypeRem, backends.OpTypeAbs, backends.OpTypeSign, backends.OpTypeEqual, backends.OpTypeGreaterOrEqual, backends.OpTypeGreaterThan, backends.OpTypeLessOrEqual, backends.OpTypeLessThan, backends.OpTypeEqualTotalOrder, backends.OpTypeGreaterOrEqualTotalOrder, backends.OpTypeGreaterThanTotalOrder, backends.OpTypeLessOrEqualTotalOrder, backends.OpTypeLessThanTotalOrder, ) SignedNumberOperations = types.SetWith( backends.OpTypeNeg, ) // FloatOperations operates only on float (and not on complex numbers). FloatOperations = types.SetWith( backends.OpTypeErf, backends.OpTypeLogistic, backends.OpTypeCos, backends.OpTypeSin, backends.OpTypeTanh, ) // FloatOrComplexOperations operates only on float or complex numbers and won't work on integer or boolean values. FloatOrComplexOperations = types.SetWith( backends.OpTypeExp, backends.OpTypeExpm1, backends.OpTypeLog, backends.OpTypeLog1p, backends.OpTypeCeil, backends.OpTypeFloor, backends.OpTypeRound, backends.OpTypeRsqrt, backends.OpTypeSqrt, backends.OpTypeIsFinite, ) // ComplexOperations operates only on complex numbers. ComplexOperations = types.SetWith( backends.OpTypeImag, backends.OpTypeReal, backends.OpTypeConj, ) // StandardBinaryOperations include all operations that have two operands usually named lhs (left-hand-side) and // rhs (right-hand-side) and are usually commutative (invariant to order). StandardBinaryOperations = types.SetWith( backends.OpTypeAdd, backends.OpTypeSub, backends.OpTypeMul, backends.OpTypeDiv, backends.OpTypePow, backends.OpTypeRem, backends.OpTypeBitwiseAnd, backends.OpTypeBitwiseOr, backends.OpTypeBitwiseXor, backends.OpTypeLogicalAnd, backends.OpTypeLogicalOr, backends.OpTypeLogicalXor, backends.OpTypeMax, backends.OpTypeMin, ) // ComparisonOperations include all operations that take two inputs and returns booleans with the results of // a comparison. ComparisonOperations = types.SetWith( backends.OpTypeEqual, backends.OpTypeEqualTotalOrder, backends.OpTypeGreaterOrEqual, backends.OpTypeGreaterOrEqualTotalOrder, backends.OpTypeGreaterThan, backends.OpTypeGreaterThanTotalOrder, backends.OpTypeLessOrEqual, backends.OpTypeLessOrEqualTotalOrder, backends.OpTypeLessThan, backends.OpTypeLessThanTotalOrder, ) // StandardUnaryOperations include all operations that have a single operand as input, and the return shape is the // same as the input (so no reductions). StandardUnaryOperations = types.SetWith( backends.OpTypeLogicalNot, backends.OpTypeBitwiseNot, backends.OpTypeBitCount, backends.OpTypeClz, backends.OpTypeErf, backends.OpTypeExp, backends.OpTypeExpm1, backends.OpTypeLog, backends.OpTypeLog1p, backends.OpTypeLogistic, backends.OpTypeCeil, backends.OpTypeFloor, backends.OpTypeRound, backends.OpTypeRsqrt, backends.OpTypeSqrt, backends.OpTypeImag, backends.OpTypeReal, backends.OpTypeConj, backends.OpTypeCos, backends.OpTypeSin, backends.OpTypeTanh, backends.OpTypeAbs, backends.OpTypeNeg, backends.OpTypeSign, ) )
Functions ¶
func BinaryOp ¶
BinaryOp returns the expected output shape for ops in the StandardBinaryOperations set -- those include all operations that have two operands usually named lhs (left-hand-side) and rhs (right-hand-side), and they are usually commutative (invariant to order).
It may throw (panic) an exception if the data type (shape.DType) is invalid for the operation -- e.g.: non-matching dtypes, or LogicalAnd not having booleans (dtype.Bool) as input.
func BroadcastInDimOp ¶
BroadcastInDimOp verifies the arguments are valid. The output shape is already known, so nothing is returned.
func BroadcastOp ¶
BroadcastOp adds the prefixDims to the start of the shape.
func ComparisonOp ¶
ComparisonOp returns the broadcast shape with dtype set to Bool, for comparison operations (Equal, LessThan, GreaterOrEqual, etc.)
func ConcatenateOp ¶
ConcatenateOp calculates the output shape of a Concatenate operation. It takes a slice of input shapes and the dimension along which to concatenate.
func GatherOp ¶
func GatherOp(operand, startIndices shapes.Shape, indexVectorAxis int, offsetOutputAxes, collapsedSliceAxes, startIndexMap, sliceSizes []int, indicesAreSorted bool) shapes.Shape
GatherOp returns the output shape of a Gather operation.
func ReshapeOp ¶
ReshapeOp to the given dimensions: trivial output shape, but this function also checks that the sizes are the same.
Notice the backends.Reshape doesn't support auto-scaling dimensions (set to -1), as graph.Reshape does.
func ScatterOp ¶
func ScatterOp(operand, indices, updates shapes.Shape, indexVectorAxis int, updateWindowAxes, insertedWindowAxes, scatterAxesToOperandAxes []int) shapes.Shape
ScatterOp checks that the parameters are consistent. The output shape returned is the unchanged operand -- the scattered updates are applied to the operand, but its shape is unchanged.
The Scatter operations indicesAreSorted and uniqueIndices don't play a role in this.
func SliceOp ¶ added in v0.19.1
SliceOp calculates the output shape for a Slice operation. It checks that starts, limits, and strides have the correct length (matching operand rank), and that the slice parameters are valid for the operand's dimensions. Strides must be positive.
func TransposeOp ¶
TransposeOp all axes of the operand. There must be one value in permutations for each axis in the operand. The output will have: output.Shape.Dimension[ii] = operand.Shape.Dimension[permutations[i]].
func UnaryOp ¶
UnaryOp checks the validity of the data type for StandardUnaryOperations set and throws an exception (panic) in case of mismatch.
It returns the same shape as the operand -- there is no broadcast on standard unary operations.
Types ¶
This section is empty.