Table of Contents

Class InferenceScope<T>

Namespace
AiDotNet.Memory
Assembly
AiDotNet.dll

Provides ambient (thread-local) context support for InferenceContext<T>. Allows code to access the current inference context without parameter threading.

public static class InferenceScope<T>

Type Parameters

T

The numeric type for tensor elements.

Inheritance
InferenceScope<T>
Inherited Members

Remarks

InferenceScope enables the ambient context pattern, where a context is set once and then accessible throughout the call stack without passing it as a parameter.

Usage example:

using var context = new InferenceContext<float>();
using var scope = InferenceScope<float>.Begin(context);

// Deep in the call stack, any code can now do: if (InferenceScope<float>.IsActive) { var tensor = InferenceScope<float>.RentOrCreate(shape); }

Scopes can be nested. When disposed, the previous scope is automatically restored:

using var outer = InferenceScope<float>.Begin(outerContext);
using var inner = InferenceScope<float>.Begin(innerContext);
// Current is now innerContext
// When inner is disposed, Current becomes outerContext again

Properties

Current

Gets or sets the current inference context for this thread. Returns null if no scope is active.

public static InferenceContext<T>? Current { get; set; }

Property Value

InferenceContext<T>

IsActive

Gets whether an inference scope is currently active on this thread.

public static bool IsActive { get; }

Property Value

bool

Methods

Begin(InferenceContext<T>)

Begins a new inference scope with the specified context. The previous context is saved and restored when the returned handle is disposed.

public static InferenceScopeHandle<T> Begin(InferenceContext<T> context)

Parameters

context InferenceContext<T>

The inference context to make current.

Returns

InferenceScopeHandle<T>

A handle that restores the previous context when disposed.

RentOrCreate(int[])

Rents a tensor from the current context if active, otherwise creates a new tensor.

public static Tensor<T> RentOrCreate(int[] shape)

Parameters

shape int[]

The shape of the tensor to rent or create.

Returns

Tensor<T>

A pooled tensor if a scope is active and pooling is enabled; otherwise a newly allocated tensor.

RentOrCreateLike(Tensor<T>)

Rents or creates a tensor with the same shape as the template.

public static Tensor<T> RentOrCreateLike(Tensor<T> template)

Parameters

template Tensor<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.