Table of Contents

Class WeightMapping

Namespace
AiDotNet.ModelLoading
Assembly
AiDotNet.dll

Maps weight names between different model formats.

public class WeightMapping
Inheritance
WeightMapping
Inherited Members

Remarks

Different ML frameworks and model releases use different naming conventions. This class provides mappings to translate between them.

For Beginners: Model weights have names like paths in a file system.

For example, in Stable Diffusion:

  • HuggingFace: "model.diffusion_model.input_blocks.0.0.weight"
  • Our model: "unet.inputConv.weight"

This class translates between these naming conventions so we can load weights from any source into our models.

Constructors

WeightMapping(Dictionary<string, string>?)

Initializes a new instance with custom mappings.

public WeightMapping(Dictionary<string, string>? mappings = null)

Parameters

mappings Dictionary<string, string>

Direct name mappings.

Properties

DirectMappingCount

Gets the count of direct mappings.

public int DirectMappingCount { get; }

Property Value

int

DirectMappings

Gets all direct mappings.

public IReadOnlyDictionary<string, string> DirectMappings { get; }

Property Value

IReadOnlyDictionary<string, string>

PatternMappingCount

Gets the count of pattern mappings.

public int PatternMappingCount { get; }

Property Value

int

Methods

AddMapping(string, string)

Adds a direct name mapping.

public void AddMapping(string sourceName, string targetName)

Parameters

sourceName string

Source weight name.

targetName string

Target weight name.

AddPatternMapping(string, string)

Adds a pattern-based mapping.

public void AddPatternMapping(string pattern, string replacement)

Parameters

pattern string

Regex pattern to match.

replacement string

Replacement pattern.

CreateCLIPTextEncoder()

Creates weight mapping for CLIP text encoder.

public static WeightMapping CreateCLIPTextEncoder()

Returns

WeightMapping

Weight mapping configured for CLIP text encoder.

CreateSDXLVAE()

Creates weight mapping for SDXL VAE.

public static WeightMapping CreateSDXLVAE()

Returns

WeightMapping

Weight mapping configured for SDXL VAE.

CreateStableDiffusionV1UNet()

Creates weight mapping for Stable Diffusion v1.x UNet.

public static WeightMapping CreateStableDiffusionV1UNet()

Returns

WeightMapping

Weight mapping configured for SD v1.x UNet.

CreateStableDiffusionV1VAE()

Creates weight mapping for Stable Diffusion v1.x VAE.

public static WeightMapping CreateStableDiffusionV1VAE()

Returns

WeightMapping

Weight mapping configured for SD v1.x VAE.

Map(string)

Maps a source weight name to the target name.

public string? Map(string sourceName)

Parameters

sourceName string

The source weight name.

Returns

string

The mapped target name, or null if no mapping exists.