Class InferenceContext<T>
Provides a scoped context for inference operations with automatic tensor pooling and lifecycle management. All tensors rented through this context are tracked and automatically returned to the pool when disposed.
public class InferenceContext<T> : IDisposable
Type Parameters
TThe numeric type for tensor elements (e.g., float, double).
- Inheritance
-
InferenceContext<T>
- Implements
- Inherited Members
Remarks
InferenceContext simplifies memory management during neural network inference by: - Tracking all rented tensors automatically - Returning all tensors to the pool on disposal (even if Release wasn't called) - Providing convenient rent methods for common tensor shapes
Basic usage example:
using var context = new InferenceContext<float>();
var input = context.Rent2D(32, 784); // Rent a batch of inputs
var hidden = context.Rent2D(32, 256); // Rent hidden layer buffer
var output = context.Rent2D(32, 10); // Rent output buffer
// Perform inference operations...
// All tensors automatically returned when context is disposed
For ambient context support (avoiding parameter threading), use InferenceScope<T>:
using var context = new InferenceContext<float>();
using var scope = InferenceScope<float>.Begin(context);
// Now any code can access the context via InferenceScope<float>.Current
var tensor = InferenceScope<float>.RentOrCreate(new[] { 32, 784 });
Constructors
InferenceContext(TensorPool<T>?, int)
Initializes a new instance of the InferenceContext<T> class.
public InferenceContext(TensorPool<T>? pool = null, int maxPoolSizeMB = 256)
Parameters
poolTensorPool<T>An existing tensor pool to use. If null, a new pool is created and owned by this context.
maxPoolSizeMBintThe maximum pool size in MB if creating a new pool. Ignored if pool is provided.
Remarks
When a pool is provided, the context does not own it and will not dispose it. When no pool is provided, the context creates and owns its own pool.
Properties
IsPoolingEnabled
Gets or sets whether pooling is enabled. When disabled, tensors are allocated directly without pool reuse, which can be useful for debugging.
public bool IsPoolingEnabled { get; set; }
Property Value
Pool
Gets the underlying tensor pool used by this context.
public TensorPool<T> Pool { get; }
Property Value
- TensorPool<T>
RentedTensorCount
Gets the number of tensors currently rented from this context.
public int RentedTensorCount { get; }
Property Value
Methods
Dispose()
Disposes the context and returns all rented tensors to the pool.
public void Dispose()
Dispose(bool)
Disposes the context resources.
protected virtual void Dispose(bool disposing)
Parameters
disposingboolTrue if called from Dispose(), false if called from finalizer.
Release(Tensor<T>)
Releases a tensor back to the pool before the context is disposed. Use this for early release of tensors that are no longer needed.
public void Release(Tensor<T> tensor)
Parameters
tensorTensor<T>The tensor to release.
Remarks
Releasing tensors early can reduce peak memory usage during long inference operations. Tensors not explicitly released will be automatically returned when the context is disposed.
Exceptions
- ObjectDisposedException
Thrown if the context has been disposed.
Rent(int[])
Rents a tensor with the specified shape from the pool. The tensor is tracked and will be automatically returned when this context is disposed.
public Tensor<T> Rent(int[] shape)
Parameters
shapeint[]The shape of the tensor to rent.
Returns
- Tensor<T>
A tensor with the requested shape.
Exceptions
- ObjectDisposedException
Thrown if the context has been disposed.
Rent1D(int)
Rents a 1D tensor (vector) with the specified length.
public Tensor<T> Rent1D(int length)
Parameters
lengthintThe number of elements in the tensor.
Returns
- Tensor<T>
A 1D tensor with shape [length].
Rent2D(int, int)
Rents a 2D tensor (matrix) with the specified dimensions.
public Tensor<T> Rent2D(int rows, int cols)
Parameters
Returns
- Tensor<T>
A 2D tensor with shape [rows, cols].
Rent3D(int, int, int)
Rents a 3D tensor with the specified dimensions.
public Tensor<T> Rent3D(int dim0, int dim1, int dim2)
Parameters
dim0intThe first dimension (e.g., batch size).
dim1intThe second dimension (e.g., sequence length).
dim2intThe third dimension (e.g., feature size).
Returns
- Tensor<T>
A 3D tensor with shape [dim0, dim1, dim2].
Rent4D(int, int, int, int)
Rents a 4D tensor with the specified dimensions (typically for image data).
public Tensor<T> Rent4D(int batch, int channels, int height, int width)
Parameters
batchintThe batch size.
channelsintThe number of channels (e.g., RGB = 3).
heightintThe image height.
widthintThe image width.
Returns
- Tensor<T>
A 4D tensor with shape [batch, channels, height, width].
RentLike(Tensor<T>)
Rents a tensor with the same shape as the template tensor.
public Tensor<T> RentLike(Tensor<T> template)
Parameters
templateTensor<T>The tensor whose shape should be matched.
Returns
- Tensor<T>
A tensor with the same shape as the template.
Exceptions
- ArgumentNullException
Thrown if template is null.