Introduction
In this tutorial, you'll learn how to work with Liquid AI's LFM3, a set of open-weight 2B-class multimodal models. These models can process text, vision, audio, and more - all in a compact 1GB package at 4-bit precision. We'll start by setting up your environment and then walk through loading and using one of these models for basic tasks.
Prerequisites
Before starting, ensure you have:
- A computer with Python 3.8 or higher installed
- At least 4GB of RAM (8GB recommended)
- Internet connection to download the model
- Basic understanding of Python programming
Step-by-Step Instructions
1. Create a New Python Project
First, create a new directory for this project and set up a virtual environment to keep our dependencies isolated.
mkdir lfm3_project
cd lfm3_project
python -m venv lfm3_env
source lfm3_env/bin/activate # On Windows: lfm3_env\Scripts\activate
Why: Using a virtual environment ensures that our project's dependencies don't interfere with other Python projects on your system.
2. Install Required Libraries
Install the necessary Python packages for working with LFM3 models.
pip install torch transformers accelerate
Why: PyTorch provides the deep learning framework, transformers gives us access to pre-trained models, and accelerate helps with model loading optimizations.
3. Download the LFM3 Model
Since LFM3 is open-weight, we can download it directly. For this tutorial, we'll use the text processing variant.
import torch
from transformers import AutoTokenizer, AutoModel
# Define model name
model_name = "liquid-ai/LFM3-1.7B"
# Load tokenizer and model
print("Loading tokenizer...")
tokenizer = AutoTokenizer.from_pretrained(model_name)
print("Loading model...")
model = AutoModel.from_pretrained(model_name, torch_dtype=torch.float16)
print("Model loaded successfully!")
Why: We're using the Hugging Face transformers library to load the pre-trained model and tokenizer. The float16 precision helps reduce memory usage while maintaining good performance.
4. Test the Model with Sample Text
Now let's test our model with a simple text input to see how it processes information.
# Sample input text
input_text = "What is the capital of France?"
# Tokenize input
inputs = tokenizer(input_text, return_tensors="pt")
# Run model inference
with torch.no_grad():
outputs = model(**inputs)
last_hidden_states = outputs.last_hidden_state
print("Input text:", input_text)
print("Model output shape:", last_hidden_states.shape)
print("First few values of output:", last_hidden_states[0, 0, :5])
Why: This test shows that our model can process text input and generate embeddings. The output shape tells us how the model represents the text internally.
5. Create a Simple Text Generation Function
Let's build a more practical function that can generate text based on our input.
def generate_text(prompt, max_length=50):
# Tokenize input
inputs = tokenizer(prompt, return_tensors="pt")
# Generate output
with torch.no_grad():
outputs = model.generate(
**inputs,
max_length=max_length,
num_return_sequences=1,
do_sample=True,
temperature=0.7
)
# Decode output
generated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
return generated_text
# Test the function
prompt = "The future of AI will be"
generated = generate_text(prompt)
print("Prompt:", prompt)
print("Generated text:", generated)
Why: This function demonstrates how to use the model for text generation tasks, which is one of the key capabilities of multimodal models like LFM3.
6. Process Different Input Types
One of LFM3's strengths is handling multiple input types. Let's see how to process text inputs.
# Test with different prompts
prompts = [
"Explain quantum computing in simple terms",
"What are the benefits of renewable energy?",
"How does photosynthesis work?"
]
for i, prompt in enumerate(prompts):
print(f"\n--- Test {i+1} ---")
print(f"Prompt: {prompt}")
result = generate_text(prompt, max_length=100)
print(f"Response: {result[:100]}...")
Why: Testing with various prompts helps us understand how the model generalizes across different topics and question types.
7. Optimize Memory Usage
Since LFM3 is designed to be lightweight, let's optimize our memory usage.
# Move model to GPU if available
if torch.cuda.is_available():
model = model.to("cuda")
print("Model moved to GPU")
else:
print("Using CPU")
# Clear cache after loading
torch.cuda.empty_cache() if torch.cuda.is_available() else None
# Print model information
print(f"Model parameters: {model.num_parameters()}")
print(f"Model device: {next(model.parameters()).device}")
Why: Moving the model to GPU when available can significantly speed up processing. The memory optimization ensures we're using the system efficiently.
Summary
In this tutorial, you've learned how to:
- Create a Python project with proper dependencies
- Load the LFM3 multimodal model using Hugging Face transformers
- Process text inputs and generate responses
- Optimize memory usage for efficient processing
You've now set up a working environment for using Liquid AI's LFM3 models. This foundation allows you to expand into more complex multimodal tasks like combining text with vision or audio processing, which are part of the full LFM3 suite.
Remember that LFM3's 2B parameters and 4-bit precision make it ideal for running on consumer hardware, opening up AI capabilities to a wider audience without requiring expensive compute resources.