Of course. Running an LLM on a Windows machine with 8GB of RAM is challenging, but definitely possible with the right settings. The key is to use a smaller, more heavily quantized model and to configure Llamafile to use system RAM efficiently.
Here is a breakdown of the strategy and the updated script.
Key Strategy for Low-RAM Systems
- Use a Smaller Model: The
Q6_K model you chose is high quality but might be too large for an 8GB system once you account for Windows OS overhead. We will switch to a Q4_K_M quantization. This model is smaller and uses less RAM at the cost of a slight reduction in quality, making it a much better fit.
- Force CPU-Only Mode: We need to explicitly tell Llamafile not to look for or use a GPU. The flag for this is
--n-gpu-layers 0.
- Limit RAM for Context: We'll reduce the context size (
-c). This determines how much of the conversation the model can "remember." A smaller context window uses significantly less RAM. We'll start with 2048, which is a reasonable balance.
- Optimize CPU Threads: We'll tell Llamafile how many physical CPU cores to use (
-t) for better performance.
Revised PowerShell Script for Low-RAM CPU Execution
This script downloads the smaller model and runs it with optimized settings for your hardware.
<#
.SYNOPSIS
Downloads and runs a RAM-optimized Gemma Llamafile model on CPU.
.DESCRIPTION
This script is designed for Windows machines with limited RAM (e.g., 8GB).
1. It downloads a smaller, 4-bit quantized version of the Gemma model.
2. It unblocks the file to allow execution.
3. It runs the model forcing CPU-only mode with parameters optimized for low memory usage.
.NOTES
You should customize the '$cpuThreads' variable to match the number of
physical cores on your CPU for best performance.
#>
# --- START OF CONFIGURATION ---
# 1. FIND YOUR CPU CORE COUNT:
# - Open Task Manager (Ctrl+Shift+Esc)
# - Go to the "Performance" tab and click on "CPU".
# - Look for "Cores" on the right side. Use that number here.
# - Example: If you have 6 cores, set this to 6.
[int]$cpuThreads = 4
# 2. CONTEXT SIZE (RAM USAGE):
# - This controls how much conversation history the model remembers.
# - Smaller numbers use less RAM. 2048 is a good start for 8GB systems.
# - If it crashes, try a smaller value like 1024.
[int]$contextSize = 2048
# --- END OF CONFIGURATION ---
# Define file and download URLs
$modelName = "google_gemma-3-4b-it-Q4_K_M.llamafile"
$downloadUrl = "https://huggingface.co/Mozilla/gemma-3-4b-it-llamafile/resolve/main/$modelName"
$logFile = "gemma_run_log.txt"
# Function for logging to console and file
function Write-Log {
param(
[string]$Message,
[string]$Level = "INFO"
)
$logEntry = "[$(Get-Date -Format 'yyyy-MM-dd HH:mm:ss')] [$Level] $Message"
$colorMap = @{
"INFO" = "Green";
"WARN" = "Yellow";
"ERROR" = "Red";
"DEBUG" = "Cyan"
}
Write-Host $logEntry -ForegroundColor $colorMap[$Level]
$logEntry | Out-File -FilePath $logFile -Append
}
try {
Write-Log -Message "Script started. Preparing to run Llamafile with CPU optimization." -Level "DEBUG"
Write-Log -Message "CPU Threads: $cpuThreads, Context Size: $contextSize" -Level "DEBUG"
# --- 1. Download the Model ---
if (Test-Path -Path $modelName) {
Write-Log -Message "Model file '$modelName' already exists. Skipping download." -Level "WARN"
} else {
Write-Log -Message "Downloading smaller Q4_K_M model: '$modelName'..." -Level "INFO"
try {
Invoke-WebRequest -Uri $downloadUrl -OutFile $modelName -UseBasicParsing
Write-Log -Message "Model download completed successfully." -Level "INFO"
} catch {
throw "Failed to download model. Please check your internet connection and the URL: $downloadUrl"
}
}
# --- 2. Prepare for Execution ---
Write-Log -Message "Unblocking file to allow execution (required on Windows for downloaded files)." -Level "INFO"
Unblock-File -Path $modelName
# --- 3. Run the Model ---
Write-Log -Message "Starting Llamafile server. A browser window should open shortly." -Level "INFO"
Write-Log -Message "To STOP the server, press CTRL+C in this PowerShell window." -Level "WARN"
# Explanation of arguments:
# .\$modelName : The executable file.
# --n-gpu-layers 0 : CRITICAL! Forces 100% CPU mode, using zero GPU layers.
# -t $cpuThreads : Sets the number of CPU threads to use for processing.
# -c $contextSize : Sets the context size. Lower uses less RAM.
# --host 0.0.0.0 : (Optional) Allows other devices on your local network to access it.
# --port 8080 : (Optional) The port for the web server.
$arguments = "--n-gpu-layers 0 -t $cpuThreads -c $contextSize --host 0.0.0.0 --port 8080"
Write-Log -Message "Executing command: .\$modelName $arguments" -Level "DEBUG"
# Start the process and wait for it to finish.
# You will interact with the model in the browser, not in this terminal.
Start-Process -FilePath ".\$modelName" -ArgumentList $arguments -Wait -NoNewWindow
} catch {
Write-Log -Message "An unrecoverable error occurred: $_" -Level "ERROR"
exit 1
}
Write-Log -Message "Script finished. The Llamafile process has been terminated." -Level "INFO"
How to Run It (Step-by-Step)
-
Find Your CPU Core Count:
- Press
Ctrl + Shift + Esc to open Task Manager.
- Go to the Performance tab and click on CPU.
- On the right side, find the value for Cores. This is the number you should use.
-
Save the Script:
- Copy the entire script block above.
- Paste it into a text editor (like Notepad or VS Code).
- Edit the
$cpuThreads variable at the top of the script to match your core count.
- Save the file as
run_gemma_cpu.ps1 in a folder of your choice (e.g., C:\Llama\).
-
Set PowerShell Execution Policy (One-time setup):
- Open PowerShell as an Administrator. (Search for PowerShell, right-click, and select "Run as administrator").
- Run the following command to allow scripts to run. This is a standard, safe setting for running your own local scripts.
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
- Press
Y and Enter to confirm. You can now close the Administrator PowerShell window.
-
Run the Script:
- Open a regular PowerShell window (not as an administrator).
- Navigate to the folder where you saved your script:
cd C:\Llama\
- Execute the script by typing its name:
.\run_gemma_cpu.ps1
What to Expect
- The script will first download the
google_gemma-3-4b-it-Q4_K_M.llamafile file (about 2.4 GB).
- After the download, it will start the Llamafile server. You will see log output in your PowerShell window.
- A web browser window should automatically open to
http://127.0.0.1:8080. If it doesn't, you can open it manually.
- Performance will be slow. The initial prompt processing will take the most time as it loads the model and processes your text on the CPU. Subsequent responses in the same chat session should be faster.
- To stop the model, go back to the PowerShell window where the script is running and press
Ctrl + C.