Skip to main content
ONNX Runtime GenAI supports Multi-LoRA, allowing you to dynamically load, manage, and switch between multiple LoRA (Low-Rank Adaptation) adapters at runtime without reloading the base model.

Overview

Multi-LoRA support enables:
  • Dynamic adapter loading: Load adapters on-demand without restarting
  • Efficient memory usage: Share the base model across multiple adapters
  • Adapter switching: Change adapters between generations
  • Reference counting: Automatically manage adapter lifecycle

Use Cases

Multi-Tenant Serving

Serve different fine-tuned models to different users while sharing the base model

Task-Specific Adaptation

Switch between adapters optimized for different tasks (summarization, translation, etc.)

A/B Testing

Test different adapter versions without infrastructure changes

Personalization

Provide personalized model behavior per user or session

Preparing LoRA Adapters

First, create your LoRA adapters using the Model Builder:
  • Base model weights should be in path_to_base_model
  • LoRA adapter weights should be in path_to_lora_weights
  • The adapter must be compatible with the base model architecture
See the Model Builder guide for more details.

Using Multi-LoRA at Runtime

Python Example

Here’s a complete example showing how to use multiple LoRA adapters:

C++ Example

C# Example

API Reference

Adapters Class

method
Creates an Adapters manager instance for the given model.Parameters:
  • model: The base model to manage adapters for
Returns: Adapters instance
method
Loads a LoRA adapter from disk.Parameters:
  • adapter_file_path: Path to the adapter weights file
  • adapter_name: Unique identifier for this adapter
Throws: Error if adapter name already exists
method
Unloads a previously loaded adapter.Parameters:
  • adapter_name: Name of the adapter to unload
Throws:
  • Error if adapter not found
  • Error if adapter is still in use (ref count > 0)

Generator Methods

method
Sets the active LoRA adapter for this generator.Parameters:
  • adapters: The Adapters manager instance
  • adapter_name: Name of the adapter to activate
Throws: Error if adapter not found

Best Practices

  • Load adapters at application startup for better performance
  • Unload adapters only when they’re no longer needed across all sessions
  • The library uses reference counting to prevent unloading adapters that are in use
Use descriptive, consistent names for your adapters:
  • task-based: “summarization”, “translation”, “code-generation”
  • user-based: “user_123”, “tenant_abc”
  • version-based: “summarization_v1”, “summarization_v2”
  • Each adapter adds memory overhead (typically small compared to base model)
  • Monitor memory usage when loading many adapters
  • Consider lazy-loading adapters on-demand for large deployments
  • Ensure adapters are created from the same base model
  • Use consistent precision (fp16, fp32) across base model and adapters
  • Verify adapter architecture matches the base model

Performance Tips

1

Pre-load Common Adapters

Load frequently-used adapters at startup to avoid latency during inference.
2

Reuse Generator Instances

When possible, reuse generator instances and just switch adapters rather than creating new generators.
3

Batch Similar Requests

Group requests that use the same adapter together to minimize adapter switching overhead.
4

Monitor Reference Counts

Keep track of which adapters are in use to optimize when to load/unload them.

Troubleshooting

“Adapter still in use” error when unloading:This occurs when trying to unload an adapter that has active references. Ensure all generators using this adapter have completed or been destroyed.
“Adapter not found” error:
  • Verify the adapter name is spelled correctly (case-sensitive)
  • Ensure the adapter was successfully loaded before attempting to use it
  • Check that the adapter hasn’t been unloaded
Memory issues with many adapters:
  • Limit the number of simultaneously loaded adapters
  • Implement an LRU cache to automatically unload least-used adapters
  • Monitor system memory and adapter usage patterns

Next Steps

Model Builder

Learn how to create LoRA adapters

Runtime Options

Configure additional runtime settings

Python API

Explore the Adapters API reference

Examples

View complete examples on GitHub