Table of Contents

Constructor LM

Namespace
LMKit.Model
Assembly
LM-Kit.NET.dll

LM(ModelCard, string, DeviceConfiguration, LoadingOptions, ModelDownloadingProgressCallback, ModelLoadingProgressCallback)

Creates an instance of the Model class from a ModelCard object.

public LM(ModelCard modelCard, string storagePath = "", LM.DeviceConfiguration deviceConfiguration = null, LM.LoadingOptions loadingOptions = null, LM.ModelDownloadingProgressCallback downloadingProgress = null, LM.ModelLoadingProgressCallback loadingProgress = null)

Parameters

modelCard ModelCard

A ModelCard object.

storagePath string

An optional parameter that specifies the location on the local machine where the model should be downloaded, if applicable.

deviceConfiguration LM.DeviceConfiguration

An optional LM.DeviceConfiguration object specifying device configuration options. Defaults to null.

loadingOptions LM.LoadingOptions

An optional LM.LoadingOptions object specifying various loading options. Defaults to null.

downloadingProgress LM.ModelDownloadingProgressCallback

An optional reference to a ModelDownloadingProgressCallback to monitor the model's downloading progress.

loadingProgress LM.ModelLoadingProgressCallback

An optional ModelLoadingProgressCallback reference to monitor the progress of model loading.

Exceptions

ArgumentNullException
ModelNotDownloadedException
ModelNotLoadedException

LM(Uri, string, DeviceConfiguration, LoadingOptions, ModelDownloadingProgressCallback, ModelLoadingProgressCallback)

Creates an instance of the Model class from a System.Uri object.

public LM(Uri uri, string storagePath = "", LM.DeviceConfiguration deviceConfiguration = null, LM.LoadingOptions loadingOptions = null, LM.ModelDownloadingProgressCallback downloadingProgress = null, LM.ModelLoadingProgressCallback loadingProgress = null)

Parameters

uri Uri

Defines the Uniform Resource Identifier (URI) indicating the location of the model. This can either be a web address using the HTTP protocol or a path to a local file.

storagePath string

An optional parameter that specifies the location on the local machine where the model should be downloaded, if applicable.

deviceConfiguration LM.DeviceConfiguration

An optional LM.DeviceConfiguration object specifying device configuration options. Defaults to null.

loadingOptions LM.LoadingOptions

An optional LM.LoadingOptions object specifying various loading options. Defaults to null.

downloadingProgress LM.ModelDownloadingProgressCallback

An optional reference to a ModelDownloadingProgressCallback to monitor the model's downloading progress.

loadingProgress LM.ModelLoadingProgressCallback

An optional ModelLoadingProgressCallback reference to monitor the progress of model loading.

Exceptions

ArgumentNullException
ModelNotDownloadedException
ModelNotLoadedException

LM(string, DeviceConfiguration, LoadingOptions, ModelLoadingProgressCallback)

Creates an instance of the Model class from a file.

public LM(string modelPath, LM.DeviceConfiguration deviceConfiguration = null, LM.LoadingOptions loadingOptions = null, LM.ModelLoadingProgressCallback loadingProgress = null)

Parameters

modelPath string

A path to a model file, in GGUF format.

deviceConfiguration LM.DeviceConfiguration

An optional LM.DeviceConfiguration object specifying device configuration options. Defaults to null.

loadingOptions LM.LoadingOptions

An optional LM.LoadingOptions object specifying various loading options. Defaults to null.

loadingProgress LM.ModelLoadingProgressCallback

An optional ModelLoadingProgressCallback reference to monitor the progress of model loading.

Examples

using LMKit.Model;
using System;

// Load from local GGUF file
LM model = new LM("models/llama-3.2-1b.Q4_K_M.gguf");

Console.WriteLine($"Model: {model.Name}");
Console.WriteLine($"Architecture: {model.Architecture}");
Console.WriteLine($"Parameters: {model.ParameterCount:N0}");
Console.WriteLine($"Context length: {model.ContextLength}");

// Load with GPU offloading
var config = new LM.DeviceConfiguration { GpuLayerCount = 20 };
LM gpuModel = new LM("models/llama-3.2-1b.Q4_K_M.gguf", deviceConfiguration: config);

Exceptions

ArgumentNullException
FileNotFoundException
ModelNotLoadedException

LM(Stream, DeviceConfiguration, LoadingOptions, ModelLoadingProgressCallback, bool)

Creates an instance of the Model class from a caller-supplied modelStream carrying a plaintext GGUF model. The stream must be readable and seekable; its current position is reset to the start of the stream when reading. By default the constructor takes ownership of the stream and disposes it once loading completes (or fails); pass leaveOpen = true to retain ownership in the caller.

public LM(Stream modelStream, LM.DeviceConfiguration deviceConfiguration = null, LM.LoadingOptions loadingOptions = null, LM.ModelLoadingProgressCallback loadingProgress = null, bool leaveOpen = false)

Parameters

modelStream Stream

The readable, seekable stream containing GGUF bytes.

deviceConfiguration LM.DeviceConfiguration

Optional device configuration.

loadingOptions LM.LoadingOptions

Optional loading options.

loadingProgress LM.ModelLoadingProgressCallback

Optional progress callback fired during native loading.

leaveOpen bool

When true, the caller retains ownership of modelStream. Defaults to false.

Remarks

Stream-based loads use the same callback-based native pipeline as encrypted and streamed-archive loads: the GGUF metadata block is read once up front and tensor bytes are pulled on demand directly from the stream. No on-disk extraction occurs and no full plaintext copy is materialized in memory.

Each stream load gets a fresh synthetic cache identifier, so the native weights cache will not deduplicate stream loads across calls even when the underlying bytes are identical. Load the same GGUF from a file path if you want the cache to recycle the native handle.

Both plaintext GGUF and LM-Kit LMK archive containers are accepted on this constructor; the magic bytes at the start of the stream decide which path runs. If the stream starts with an LM-Kit encrypted-container magic ("LMKE"), the constructor throws a clear InvalidDataException directing callers to LoadEncryptedFromStream(Stream, GgufEncryptionScheme, string, DeviceConfiguration, LoadingOptions, ModelLoadingProgressCallback, bool). Whisper and standalone ONNX models are not currently supported from a raw stream and must be loaded by file path.

Exceptions

ArgumentNullException

If modelStream is null.

ArgumentException

If the stream is not readable or not seekable.

InvalidDataException

If the stream does not contain a plaintext GGUF.

ModelNotLoadedException

If the native loader fails.

Share