InferenceScope<T>

Helpers & Utilities · AiDotNet.Memory

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

How It Works

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:

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

Properties

Property Summary
InferenceContext<T> Current { get; set; } Gets or sets the current inference context for this thread.
bool IsActive { get; } Gets whether an inference scope is currently active on this thread.

Methods

Method Summary
InferenceScopeHandle<T> Begin(InferenceContext<T> context) Begins a new inference scope with the specified context.
Tensor<T> RentOrCreate(int[] shape) Rents a tensor from the current context if active, otherwise creates a new tensor.
Tensor<T> RentOrCreateLike(Tensor<T> template) Rents or creates a tensor with the same shape as the template.