Class TensorShapeExtensions
- Namespace
- AiDotNet.JitCompiler.IR
- Assembly
- AiDotNet.dll
Provides extension methods and utilities for working with tensor shapes in the IR.
public static class TensorShapeExtensions
- Inheritance
-
TensorShapeExtensions
- Inherited Members
Remarks
This class provides helper methods for working with tensor shapes (represented as int[] arrays). It integrates with the existing Tensor<T> infrastructure which already uses int[] for shapes.
For Beginners: In AiDotNet, tensor shapes are represented as integer arrays.
For example:
- [5] is a vector with 5 elements
- [3, 4] is a 3×4 matrix
- [2, 3, 4] is a 3D tensor
This class provides utilities to work with these shapes:
- Check if two shapes are compatible for operations
- Compute the result shape when broadcasting
- Validate shapes
- Compare shapes
These utilities are used by the JIT compiler to understand tensor dimensions and generate optimized code.
Methods
BroadcastWith(int[], int[])
Computes the broadcast shape resulting from combining two shapes.
public static int[] BroadcastWith(this int[] shape1, int[] shape2)
Parameters
Returns
- int[]
The broadcast result shape.
Remarks
The broadcast shape is computed by taking the maximum dimension at each position when comparing from right to left.
For Beginners: This calculates what shape results when broadcasting two tensors.
Examples:
- [3, 4] + [3, 4] → [3, 4] (same shape)
- [3, 4] + [1, 4] → [3, 4] (first dimension expands from 1 to 3)
- [3, 4] + [4] → [3, 4] (vector broadcasts to match all rows)
- [5, 3, 4] + [4] → [5, 3, 4] (vector broadcasts across all 5×3 positions)
The result tells us what shape the output will have after the operation.
Exceptions
- InvalidOperationException
Thrown if shapes are not compatible.
GetElementCount(int[])
Computes the total number of elements in a tensor with the given shape.
public static int GetElementCount(this int[] shape)
Parameters
shapeint[]The tensor shape.
Returns
- int
The total number of elements, or -1 if any dimension is dynamic.
Remarks
For Beginners: This calculates how many total values a tensor holds.
For example:
- [] has 1 element (scalar - a single number)
- [5] has 5 elements
- [3, 4] has 3 × 4 = 12 elements
- [2, 3, 4] has 2 × 3 × 4 = 24 elements
If any dimension is -1 (meaning "dynamic" or "unknown"), returns -1.
GetRank(int[])
Gets the rank (number of dimensions) of a tensor shape.
public static int GetRank(this int[] shape)
Parameters
shapeint[]The tensor shape.
Returns
- int
The number of dimensions.
Remarks
For Beginners: The rank is how many dimensions the tensor has.
- [5] has rank 1 (a vector)
- [3, 4] has rank 2 (a matrix)
- [2, 3, 4] has rank 3 (a 3D tensor)
- [] has rank 0 (a scalar - single number)
GetShapeHashCode(int[])
Computes a hash code for a tensor shape.
public static int GetShapeHashCode(this int[] shape)
Parameters
shapeint[]The shape to hash.
Returns
- int
A hash code.
Remarks
This hash code can be used to cache compiled graphs based on shape. Shapes with the same dimensions will have the same hash.
For Beginners: This creates a unique number that represents the shape.
It's like a fingerprint for the shape - two identical shapes will have the same hash code. This is used to quickly check if we've already compiled code for a tensor of this shape, so we can reuse it instead of recompiling.
GetShape<T>(Tensor<T>)
Extracts the shape from a Tensor.
public static int[] GetShape<T>(this Tensor<T> tensor)
Parameters
tensorTensor<T>The tensor.
Returns
- int[]
The shape as an int array.
Type Parameters
TThe numeric type of the tensor.
Remarks
For Beginners: This gets the shape from an existing Tensor object.
Since Tensor already has a Shape property, this just returns it. It's provided for consistency with the IR infrastructure.
IsCompatibleWith(int[], int[])
Checks if this shape is compatible with another shape for broadcasting.
public static bool IsCompatibleWith(this int[] shape1, int[] shape2)
Parameters
Returns
- bool
True if the shapes are compatible for broadcasting.
Remarks
Broadcasting allows operations between tensors of different shapes by automatically expanding dimensions. Two shapes are compatible if: - They have the same rank and all dimensions match, OR - One dimension is 1 (can be broadcast), OR - One tensor has fewer dimensions (will be expanded)
For Beginners: Broadcasting lets you do operations on tensors of different sizes.
For example:
- [3, 4] and [3, 4] are compatible (same shape)
- [3, 4] and [1, 4] are compatible (first dimension broadcasts)
- [3, 4] and [4] are compatible (vector broadcasts across all rows)
- [3, 4] and [3, 5] are NOT compatible (incompatible dimensions)
This is very useful in neural networks where you often add a bias vector to every row of a matrix - broadcasting handles this automatically.
IsValidShape(int[])
Validates that a shape is well-formed.
public static bool IsValidShape(this int[] shape)
Parameters
shapeint[]The shape to validate.
Returns
- bool
True if valid.
Remarks
A shape is valid if all dimensions are either positive or -1 (dynamic). Zero dimensions are not allowed.
For Beginners: This checks that a shape makes sense.
Valid shapes:
- [] (scalar)
- [5] (vector with 5 elements)
- [3, 4] (3×4 matrix)
- [-1, 4] (dynamic first dimension, 4 columns)
Invalid shapes:
- [0, 4] (can't have zero dimension)
- [3, -2] (only -1 is allowed for dynamic)
ShapeToString(int[])
Creates a string representation of a shape.
public static string ShapeToString(this int[] shape)
Parameters
shapeint[]The shape to represent.
Returns
- string
A string representation.
Remarks
For Beginners: This converts a shape to a readable string for debugging.
Examples:
- [] → "scalar"
- [5] → "[5]"
- [3, 4] → "[3, 4]"
- [2, -1, 4] → "[2, ?, 4]" (? means dynamic)
ShapesEqual(int[]?, int[]?)
Checks if two shapes are exactly equal.
public static bool ShapesEqual(int[]? shape1, int[]? shape2)
Parameters
Returns
- bool
True if shapes are equal.
Remarks
For Beginners: This checks if two shapes are identical.
Examples:
- [3, 4] equals [3, 4] → true
- [3, 4] equals [4, 3] → false (different order!)
- [3, 4] equals [1, 4] → false (different dimensions)