Constructor LM
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
modelCardModelCardA ModelCard object.
storagePathstringAn optional parameter that specifies the location on the local machine where the model should be downloaded, if applicable.
deviceConfigurationLM.DeviceConfigurationAn optional LM.DeviceConfiguration object specifying device configuration options. Defaults to
null.loadingOptionsLM.LoadingOptionsAn optional LM.LoadingOptions object specifying various loading options. Defaults to
null.downloadingProgressLM.ModelDownloadingProgressCallbackAn optional reference to a ModelDownloadingProgressCallback to monitor the model's downloading progress.
loadingProgressLM.ModelLoadingProgressCallbackAn optional ModelLoadingProgressCallback reference to monitor the progress of model loading.
Exceptions
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
uriUriDefines 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.
storagePathstringAn optional parameter that specifies the location on the local machine where the model should be downloaded, if applicable.
deviceConfigurationLM.DeviceConfigurationAn optional LM.DeviceConfiguration object specifying device configuration options. Defaults to
null.loadingOptionsLM.LoadingOptionsAn optional LM.LoadingOptions object specifying various loading options. Defaults to
null.downloadingProgressLM.ModelDownloadingProgressCallbackAn optional reference to a ModelDownloadingProgressCallback to monitor the model's downloading progress.
loadingProgressLM.ModelLoadingProgressCallbackAn optional ModelLoadingProgressCallback reference to monitor the progress of model loading.
Exceptions
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
modelPathstringA path to a model file, in GGUF format.
deviceConfigurationLM.DeviceConfigurationAn optional LM.DeviceConfiguration object specifying device configuration options. Defaults to
null.loadingOptionsLM.LoadingOptionsAn optional LM.LoadingOptions object specifying various loading options. Defaults to
null.loadingProgressLM.ModelLoadingProgressCallbackAn 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
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
modelStreamStreamThe readable, seekable stream containing GGUF bytes.
deviceConfigurationLM.DeviceConfigurationOptional device configuration.
loadingOptionsLM.LoadingOptionsOptional loading options.
loadingProgressLM.ModelLoadingProgressCallbackOptional progress callback fired during native loading.
leaveOpenboolWhen 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
modelStreamis 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.