Instructions to use stabilityai/stable-code-3b with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use stabilityai/stable-code-3b with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-generation", model="stabilityai/stable-code-3b")# Load model directly from transformers import AutoTokenizer, AutoModelForCausalLM tokenizer = AutoTokenizer.from_pretrained("stabilityai/stable-code-3b") model = AutoModelForCausalLM.from_pretrained("stabilityai/stable-code-3b") - llama-cpp-python
How to use stabilityai/stable-code-3b with llama-cpp-python:
# !pip install llama-cpp-python from llama_cpp import Llama llm = Llama.from_pretrained( repo_id="stabilityai/stable-code-3b", filename="stable-code-3b-Q5_K_M.gguf", )
output = llm( "Once upon a time,", max_tokens=512, echo=True ) print(output)
- Notebooks
- Google Colab
- Kaggle
- Local Apps
- llama.cpp
How to use stabilityai/stable-code-3b with llama.cpp:
Install from brew
brew install llama.cpp # Start a local OpenAI-compatible server with a web UI: llama-server -hf stabilityai/stable-code-3b:Q5_K_M # Run inference directly in the terminal: llama-cli -hf stabilityai/stable-code-3b:Q5_K_M
Install from WinGet (Windows)
winget install llama.cpp # Start a local OpenAI-compatible server with a web UI: llama-server -hf stabilityai/stable-code-3b:Q5_K_M # Run inference directly in the terminal: llama-cli -hf stabilityai/stable-code-3b:Q5_K_M
Use pre-built binary
# Download pre-built binary from: # https://github.com/ggerganov/llama.cpp/releases # Start a local OpenAI-compatible server with a web UI: ./llama-server -hf stabilityai/stable-code-3b:Q5_K_M # Run inference directly in the terminal: ./llama-cli -hf stabilityai/stable-code-3b:Q5_K_M
Build from source code
git clone https://github.com/ggerganov/llama.cpp.git cd llama.cpp cmake -B build cmake --build build -j --target llama-server llama-cli # Start a local OpenAI-compatible server with a web UI: ./build/bin/llama-server -hf stabilityai/stable-code-3b:Q5_K_M # Run inference directly in the terminal: ./build/bin/llama-cli -hf stabilityai/stable-code-3b:Q5_K_M
Use Docker
docker model run hf.co/stabilityai/stable-code-3b:Q5_K_M
- LM Studio
- Jan
- vLLM
How to use stabilityai/stable-code-3b with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "stabilityai/stable-code-3b" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "stabilityai/stable-code-3b", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }'Use Docker
docker model run hf.co/stabilityai/stable-code-3b:Q5_K_M
- SGLang
How to use stabilityai/stable-code-3b with SGLang:
Install from pip and serve model
# Install SGLang from pip: pip install sglang # Start the SGLang server: python3 -m sglang.launch_server \ --model-path "stabilityai/stable-code-3b" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "stabilityai/stable-code-3b", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }'Use Docker images
docker run --gpus all \ --shm-size 32g \ -p 30000:30000 \ -v ~/.cache/huggingface:/root/.cache/huggingface \ --env "HF_TOKEN=<secret>" \ --ipc=host \ lmsysorg/sglang:latest \ python3 -m sglang.launch_server \ --model-path "stabilityai/stable-code-3b" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "stabilityai/stable-code-3b", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }' - Ollama
How to use stabilityai/stable-code-3b with Ollama:
ollama run hf.co/stabilityai/stable-code-3b:Q5_K_M
- Unsloth Studio new
How to use stabilityai/stable-code-3b with Unsloth Studio:
Install Unsloth Studio (macOS, Linux, WSL)
curl -fsSL https://unsloth.ai/install.sh | sh # Run unsloth studio unsloth studio -H 0.0.0.0 -p 8888 # Then open http://localhost:8888 in your browser # Search for stabilityai/stable-code-3b to start chatting
Install Unsloth Studio (Windows)
irm https://unsloth.ai/install.ps1 | iex # Run unsloth studio unsloth studio -H 0.0.0.0 -p 8888 # Then open http://localhost:8888 in your browser # Search for stabilityai/stable-code-3b to start chatting
Using HuggingFace Spaces for Unsloth
# No setup required # Open https://huggingface.co/spaces/unsloth/studio in your browser # Search for stabilityai/stable-code-3b to start chatting
- Docker Model Runner
How to use stabilityai/stable-code-3b with Docker Model Runner:
docker model run hf.co/stabilityai/stable-code-3b:Q5_K_M
- Lemonade
How to use stabilityai/stable-code-3b with Lemonade:
Pull the model
# Download Lemonade from https://lemonade-server.ai/ lemonade pull stabilityai/stable-code-3b:Q5_K_M
Run and chat with the model
lemonade run user.stable-code-3b-Q5_K_M
List all available models
lemonade list
How could I run inference with the gguf models?
How could I inference with the gguf models? When I run the gguf models with llama.cpp executable, it shows:
gguf_init_from_file: invalid magic characters 'vers'
llama_model_load: error loading model: llama_model_loader: failed to load model from stable-code-3b-Q6_K.gguf
llama_load_model_from_file: failed to load model
llama_init_from_gpt_params: error: failed to load model 'stable-code-3b-Q6_K.gguf'
main: error: unable to load model
Can use LangChain for the same , Here is a sample code in python :-
from langchain.callbacks.manager import CallbackManager
from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler
from langchain.chains import LLMChain
from langchain.llms import LlamaCpp
from langchain.prompts import PromptTemplate
import time
from googletrans import Translator
n_gpu_layers = 14 # Change this value based on your model and your GPU VRAM pool.
n_batch = 50 # Should be between 1 and n_ctx, consider the amount of VRAM in your GPU.
# Callbacks support token-wise streaming
callback_manager = CallbackManager([StreamingStdOutCallbackHandler()])
# Make sure the model path is correct for your system!
llm = LlamaCpp(
model_path="models/stable-code-3b.gguf",
n_gpu_layers=n_gpu_layers,
n_batch=n_batch,
max_new_tokens=512,
callback_manager=callback_manager,
verbose=True, # Verbose is required to pass to the callback manager
)
# Prompt Template
template = """
Write a python code for:
{_prompt_} <|endoftext|>
"""
prompt = PromptTemplate(input_variables=["tweet"], template=template)
res = llm(prompt.format(tweet="How to connect to a sql database"))
print(res)
A Chinese boy and his father are playing football in the sunshine
How could I inference with the gguf models? When I run the gguf models with llama.cpp executable, it shows:
gguf_init_from_file: invalid magic characters 'vers' llama_model_load: error loading model: llama_model_loader: failed to load model from stable-code-3b-Q6_K.gguf llama_load_model_from_file: failed to load model llama_init_from_gpt_params: error: failed to load model 'stable-code-3b-Q6_K.gguf' main: error: unable to load model
Huggingface has made an own inference c++ engine, it abuses the gguf format instead of changing the name. No one I know ever used that engine but it's there, causing incompatible files to be uploaded.
I'ver seen those type of gguf files a few times, it's extremely confusing to people.
If you want a GGUF file, then download it either from "TheBloke", he's converting and hosting them all here on Huggingface and those will all come with explanation and they should work.
Or you download the repository and use llama.cpp convert.py and quantize to create your own gguf.
If you intend to work a lot with a particular model that's recommended, as you can quickly regenerate any quantization or improvement you need.
How could I inference with the gguf models? When I run the gguf models with llama.cpp executable, it shows:
gguf_init_from_file: invalid magic characters 'vers' llama_model_load: error loading model: llama_model_loader: failed to load model from stable-code-3b-Q6_K.gguf llama_load_model_from_file: failed to load model llama_init_from_gpt_params: error: failed to load model 'stable-code-3b-Q6_K.gguf' main: error: unable to load modelHuggingface has made an own inference c++ engine, it abuses the gguf format instead of changing the name. No one I know ever used that engine but it's there, causing incompatible files to be uploaded.
I'ver seen those type of gguf files a few times, it's extremely confusing to people.If you want a GGUF file, then download it either from "TheBloke", he's converting and hosting them all here on Huggingface and those will all come with explanation and they should work.
Or you download the repository and use llama.cpp convert.py and quantize to create your own gguf.
If you intend to work a lot with a particular model that's recommended, as you can quickly regenerate any quantization or improvement you need.
Thanks for the detail explanation. I downloaded the gguf from TheBloke, it works.
@davideuler If you got,
gguf_init_from_file: invalid magic characters 'vers'
Then, you probably have a situation like this:
(.venv) root@C.10039740:~/LanguageLearning-Models/models$ curl https://huggingface.co/TheBloke/Mixtral-8x7B-Instruct-v0.1-GGUF/raw/main/mixtral-8x7b-instruct-v0.1.Q4_K_M.gguf
version https://git-lfs.github.com/spec/v1
oid sha256:9193684683657e90707087bd1ed19fd0b277ab66358d19edeadc26d6fdec4f53
size 26441533376
"vers" is "version", meaning you downloaded a github LFS file rather than the underlying file itself. You should still be able to download the actual model with the correct link.
@davideuler If you got,
gguf_init_from_file: invalid magic characters 'vers'
Then, you probably have a situation like this:
(.venv) root@C.10039740:~/LanguageLearning-Models/models$ curl https://huggingface.co/TheBloke/Mixtral-8x7B-Instruct-v0.1-GGUF/raw/main/mixtral-8x7b-instruct-v0.1.Q4_K_M.gguf
version https://git-lfs.github.com/spec/v1
oid sha256:9193684683657e90707087bd1ed19fd0b277ab66358d19edeadc26d6fdec4f53
size 26441533376"vers" is "version", meaning you downloaded a github LFS file rather than the underlying file itself. You should still be able to download the actual model with the correct link.
Hi, thanks for the detail explanation. Got it to work.