Install & Run Stable Code 3B on Ubuntu

Install & Run Stable Code 3B on Ubuntu
Stable Code 3B

Stable Code 3B is a state-of-the-art large language model (LLM) designed for code completion, boasting a compact size that allows it to run efficiently even on standard laptops without dedicated GPUs.

Overview of Stable Code 3B

Stable Code 3B is a large language model developed by Stability AI, specifically tailored for code generation tasks. With 3 billion parameters, it delivers performance comparable to larger models while maintaining a significantly smaller footprint.

Key Features:

  • Compact Size: At around 6 GB, it can be easily downloaded and run on common hardware.
  • Multi-language Support: Trained on 18 programming languages for broad compatibility.
  • Fill in the Middle Capabilities: Enables better context understanding and more relevant code generation.
  • Extended Context Size: Supports sequences of up to 16,384 tokens for handling longer code contexts effectively.

System Requirements

Before installing Stable Code 3B, ensure that your system meets the following requirements:

  • Operating System: Ubuntu (preferably version 20.04 or later)
  • RAM: At least 8 GB (16 GB recommended for better performance)
  • Disk Space: Minimum of 10 GB free space for installation and operation
  • Python: Version 3.7 or later
  • CUDA Toolkit: Required if using a GPU (for NVIDIA GPUs)

Installation Steps

Step 1: Update Your System

Ensure your system is up-to-date before proceeding with the installation.

sudo apt update && sudo apt upgrade -y

Step 2: Install Required Dependencies

Install necessary libraries and tools:

sudo apt install python3-pip git -y

Step 3: Install Hugging Face Hub

Stable Code 3B can be accessed via Hugging Face’s model hub. Install the required library:

pip install huggingface-hub

Step 4: Download Stable Code 3B Model

Create a directory for the model files:

mkdir stable-code-3b-GPTQ

Then, download the model using Hugging Face CLI:

huggingface-cli download TheBloke/stable-code-3b-GPTQ --local-dir stable-code-3b-GPTQ --local-dir-use-symlinks False

Step 5: Set Up Environment

Use a virtual environment for dependency management:

python3 -m venv stablecode-env
source stablecode-env/bin/activate

Step 6: Install Additional Libraries

For running with PyTorch:

pip install torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/cu113

Step 7: Verify Installation

Check if the model is accessible through Python:

from huggingface_hub import hf_hub_download

model_path = hf_hub_download("TheBloke/stable-code-3b-GPTQ")
print("Model downloaded at:", model_path)

Running Stable Code 3B

Step 1: Import Necessary Libraries

Create a Python script (e.g., run_stable_code.py) and add the following code:

import torch
from transformers import AutoModelForCausalLM, AutoTokenizer

# Load model and tokenizer from Hugging Face Hub
model_name = "TheBloke/stable-code-3b-GPTQ"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(model_name)

Step 2: Generate Code Snippets

Use the model to generate code snippets:

def generate_code(prompt):
    inputs = tokenizer(prompt, return_tensors="pt")
    outputs = model.generate(**inputs)
    generated_code = tokenizer.decode(outputs[0], skip_special_tokens=True)
    return generated_code

# Example prompt
prompt = "Write a Python function to calculate Fibonacci numbers."
code_snippet = generate_code(prompt)
print(code_snippet)

Step 3: Run Your Script

Execute the script:

python run_stable_code.py

Troubleshooting Common Issues

Issue 1: Memory Errors

  • Reduce batch size or use a system with higher RAM.

Issue 2: Model Download Failures

  • Ensure a stable internet connection. Try using a different network if issues persist.

Issue 3: CUDA Errors (if using GPU)

  • Ensure CUDA toolkit version matches your PyTorch installation. Check compatibility on the PyTorch website.

Conclusion

Installing and running Stable Code 3B on Ubuntu is a straightforward process that enables developers to leverage advanced code generation tools. By following this guide, you should be able to set up your environment successfully and begin utilizing this powerful AI model for various coding tasks.

With its compact size and impressive capabilities, Stable Code 3B is an essential tool for modern software development workflows, enhancing productivity and creativity in programming.

References

  1. Run DeepSeek Janus-Pro 7B on Mac: A Comprehensive Guide Using ComfyUI
  2. Run DeepSeek Janus-Pro 7B on Mac: Step-by-Step Guide
  3. Run DeepSeek Janus-Pro 7B on Windows: A Complete Installation Guide
  4. Install & Run Stable Code 3B on macOS
  5. Install & Run Stable Code 3B on Windows
  6. Install & Run Stable Code 3B on Linux