DiffusionModelBase<T>

Base Classes · AiDotNet.Diffusion

Base class for diffusion-based generative models providing common functionality.

Implements: IDiffusionModel<T>, IModelShape, IParameterChunkSource<T>, IParameterManifestProvider, IParameterSurfaceLifecycle, ISelfSupervisedModel

Known derived types: DDPMModel<T>, LatentDiffusionModelBase<T>

For Beginners

This is the foundation that all diffusion models build upon. It handles the common tasks that every diffusion model needs:

  • The generation loop (iteratively denoising from noise)
  • Adding noise during training
  • Computing the training loss
  • Saving and loading the model

Specific diffusion models (like DDPM, Latent Diffusion) extend this base to implement their unique noise prediction architectures.

How It Works

This abstract base class implements the common behavior for all diffusion models, including the generation loop, noise addition, loss computation, and state management.

Properties

Property Summary
ILossFunction<T> DefaultLossFunction { get; }
INoiseScheduler<T> Scheduler { get; }
NeuralNetworkArchitecture<T> Architecture { get; } Gets the optional neural network architecture used for custom layer configuration.
ParameterLayoutSnapshot ParameterLayout { get; }
bool IsQuantizationAwareTrainingEnabled { get; } Whether quantization-aware training is engaged for Train (G5, #1624).
bool SupportsParameterInitialization { get; }
long ParameterCount { get; }

Methods

Method Summary
Dictionary<string, T> GetFeatureImportance()
DynamicShapeInfo GetDynamicShapeInfo()
IDiffusionModel<T> Clone() Creates a deep copy of the model.
IEnumerable<ParameterChunk<T>> GetParameterStateChunks() Exposes canonical registered state for device-side checkpoints.
IEnumerable<Tensor<T>> GetParameterChunks() Streams the diffusion stack’s trainable weight tensors per-tensor.
IEnumerable<int> GetActiveFeatureIndices()
IFullModel<T, Tensor<T>, Tensor<T>> DeepCopy()
IFullModel<T, Tensor<T>, Tensor<T>> WithParameters(Vector<T> parameters)
ModelMetadata<T> GetModelMetadata()
ModelOptions GetOptions()
T ComputeLoss(Tensor<T> cleanSamples, Tensor<T> noise, int[] timesteps)
T GetLastLoss() The noise-prediction loss of the most recent Train step, on the fused plan or the eager tape.
Tensor<T> Generate(int[] shape, int numInferenceSteps, int? seed)
Tensor<T> Predict(Tensor<T> input)
Tensor<T> PredictNoise(Tensor<T> noisySample, int timestep)
Tensor<T> PredictNoiseBatched(Tensor<T> noisyBatch, int[] timesteps) Batched per-element noise prediction (industry-standard DDPM training pattern).
ValueTask<Tensor<T>> GenerateAsync(int[] shape, int numInferenceSteps, int? seed, CancellationToken cancellationToken) Async overload of Generate.
Vector<T> ComputeGradients(Tensor<T> input, Tensor<T> target, ILossFunction<T> lossFunction)
Vector<T> GetParameters()
Vector<T> SanitizeParameters(Vector<T> parameters)
bool IsFeatureUsed(int featureIndex)
byte[] Serialize()
int[] GetInputShape()
int[] GetOutputShape()
void ApplyGradients(Vector<T> gradients, T learningRate)
void Deserialize(byte[] data)
void DisableQuantizationAwareTraining() Forces quantization-aware training OFF (the default); use after an explicit enable.
void Dispose()
void EnableQuantizationAwareTraining(QuantizationConfiguration config) Forces quantization-aware training ON (G5, #1624): from the next Train step the forward pass uses fake-quantized weights (quantize→dequantize, simulating int8 inference precision) while keeping full-precision shadow weights that the optim…
void LoadModel(string filePath)
void LoadState(Stream stream)
void SaveModel(string filePath)
void SaveState(Stream stream)
void SetActiveFeatureIndices(IEnumerable<int> featureIndices)
void SetParameterChunks(IEnumerable<Tensor<T>> chunks) Streaming counterpart to SetParameters: assigns weights from per-tensor chunks in GetParameterChunks order without materializing a flat aggregate.
void SetParameters(Vector<T> parameters)
void Train(Tensor<T> input, Tensor<T> expectedOutput)