Introduction
In this tutorial, you'll learn how to use AMD's Instella-MoE-16B-A3B, a groundbreaking open-source Mixture-of-Experts (MoE) language model. This model is unique because it has 16 billion total parameters but only activates 2.8 billion parameters per token, making it highly efficient. We'll walk through setting up the environment, loading the model, and running inference with this cutting-edge technology.
Prerequisites
Before starting this tutorial, you'll need:
- Basic understanding of Python programming
- Access to a machine with AMD GPU(s) (MI300X or MI325X recommended)
- Python 3.8 or higher installed
- Basic knowledge of machine learning concepts
Step-by-Step Instructions
1. Setting Up Your Environment
1.1 Install Required Dependencies
First, create a virtual environment and install the necessary packages. This ensures your system doesn't conflict with existing installations.
python -m venv instella_env
source instella_env/bin/activate # On Windows: instella_env\Scripts\activate
pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/rocm5.7
pip install transformers accelerate
Why: We're installing PyTorch with ROCm support for AMD GPUs, and Hugging Face's Transformers library which provides easy access to pre-trained models.
1.2 Verify GPU Availability
Check that your AMD GPU is properly recognized by PyTorch.
import torch
print(f"GPU Available: {torch.cuda.is_available()}")
print(f"GPU Count: {torch.cuda.device_count()}")
if torch.cuda.is_available():
print(f"Current GPU: {torch.cuda.get_device_name(0)}")
Why: This confirms that your system can utilize the AMD GPU for model inference, which is crucial for efficient performance.
2. Loading the Instella-MoE Model
2.1 Download Model Files
Download the model weights and configuration files from AMD's official repository. You can use git to clone the repository:
git clone https://github.com/AMD/instella-moe-16b-a3b.git
cd instella-moe-16b-a3b
Why: This gives you access to all the model components including weights, configuration files, and inference code needed to run the model.
2.2 Load the Model Using Transformers
Now, load the model using Hugging Face's Transformers library:
from transformers import AutoTokenizer, AutoModelForCausalLM
# Load tokenizer and model
model_name = "AMD/instella-moe-16b-a3b"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype=torch.float16, device_map="auto")
Why: The AutoModelForCausalLM class automatically detects the model architecture and loads it properly, while device_map="auto" ensures efficient GPU utilization.
3. Running Inference
3.1 Prepare Input Text
Prepare a prompt for the model to generate text from:
input_text = "The future of artificial intelligence is"
inputs = tokenizer(input_text, return_tensors="pt").to("cuda")
print(f"Input tokens: {inputs['input_ids'].shape}")
Why: Tokenizing the input text converts it into the numerical format the model expects, and we're moving it to GPU for processing.
3.2 Generate Text
Generate text using the model with specified parameters:
# Generate text
with torch.no_grad():
outputs = model.generate(
inputs["input_ids"],
max_length=100,
num_return_sequences=1,
temperature=0.7,
do_sample=True,
pad_token_id=tokenizer.eos_token_id
)
# Decode and print the generated text
generated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
print(generated_text)
Why: The generate function uses the model's MoE architecture to efficiently produce text, with parameters like temperature controlling randomness in generation.
3.3 Analyze Model Efficiency
Check how many parameters are actually being used:
# Calculate parameter usage
total_params = sum(p.numel() for p in model.parameters())
print(f"Total parameters: {total_params:,}")
# Note: In MoE models, only a subset of parameters are active per token
print("Model uses 2.8B active parameters per token, 16B total parameters")
Why: Understanding parameter usage helps appreciate the efficiency of the MoE architecture, which allows for larger models with better performance.
4. Advanced Usage
4.1 Batch Inference
Process multiple inputs simultaneously:
prompts = [
"Machine learning is",
"Deep learning has",
"Neural networks are"
]
# Tokenize all prompts
inputs = tokenizer(prompts, return_tensors="pt", padding=True, truncation=True).to("cuda")
# Generate for all prompts
with torch.no_grad():
outputs = model.generate(
inputs["input_ids"],
max_length=50,
num_return_sequences=1,
temperature=0.8,
do_sample=True,
pad_token_id=tokenizer.eos_token_id
)
# Decode all outputs
for i, output in enumerate(outputs):
generated_text = tokenizer.decode(output, skip_special_tokens=True)
print(f"Prompt {i+1}: {prompts[i]}")
print(f"Generated: {generated_text}\n")
Why: Batch processing allows you to efficiently handle multiple inputs at once, making it practical for real-world applications.
4.2 Experiment with Different Parameters
Try different generation settings to see how they affect output:
# Try different temperatures
for temp in [0.5, 0.8, 1.2]:
print(f"\nTemperature: {temp}")
with torch.no_grad():
output = model.generate(
inputs["input_ids"],
max_length=30,
temperature=temp,
do_sample=True,
pad_token_id=tokenizer.eos_token_id
)
generated = tokenizer.decode(output[0], skip_special_tokens=True)
print(generated)
Why: Adjusting parameters like temperature lets you control the creativity vs. consistency of generated text, which is essential for different use cases.
Summary
In this tutorial, you've learned how to set up and use AMD's Instella-MoE-16B-A3B model. You've installed the required dependencies, loaded the model, and run both single and batch inference tasks. You've also explored how the model's MoE architecture works by understanding that while it has 16 billion total parameters, only 2.8 billion are active per token, making it highly efficient. This knowledge gives you a foundation to experiment with other MoE models and explore the future of efficient large language models.



