A native Rust implementation of BitNet b1.58 2B4T model for efficient 1-bit LLM inference using the Candle framework.
git clone <repository>
cd bitnet-rust
cargo build --release
# Ask a single question
cargo run "What is the capital of France?"
# Or using the binary name
cargo run --bin bitnet "Explain quantum computing"
# Start interactive chat session
cargo run -- --interactive
# or
cargo run -- -i
# Run without arguments to be prompted for input
cargo run
use bitnet_rust::{BitNetGenerator, BitNetConfig, BitNetForCausalLM};
use anyhow::Result;
fn main() -> Result<()> {
// Load model (see examples/simple_chat.rs for full setup)
let generator = BitNetGenerator::new(model, tokenizer, device);
// Generate response
let response = generator.generate("What is machine learning?")?;
println!("Response: {}", response);
// Generate with custom system prompt
let response = generator.generate_with_system(
"Write a poem",
Some("You are a creative poet")
)?;
println!("Poem: {}", response);
Ok(())
}
Run the included example:
cargo run --example simple_chat
The main interface for text generation:
impl BitNetGenerator {
// Create new generator
pub fn new(model: BitNetForCausalLM, tokenizer: Tokenizer, device: Device) -> Self;
// Generate response with default system prompt
pub fn generate(&self, message: &str) -> Result<String>;
// Generate response with custom system prompt
pub fn generate_with_system(&self, message: &str, system: Option<&str>) -> Result<String>;
// Get the device being used
pub fn device(&self) -> &Device;
}
// Format Llama 3 chat template
pub fn format_llama3_chat(system: &str, user_message: &str) -> String;
// Interactive chat session
pub fn run_interactive_chat<F>(generate_fn: F) -> Result<()>
where F: FnMut(&str) -> Result<String>;
For multi-turn conversations:
let mut conv = ConversationHistory::new("You are helpful".to_string());
conv.add_user_message("Hello".to_string());
conv.add_assistant_message("Hi there!".to_string());
let prompt = conv.get_prompt();
The model automatically downloads from Hugging Face Hub:
microsoft/bitnet-b1.58-2B-4Tπ BitNet b1.58 2B4T - Native 1-bit LLM
============================================================
β Using Apple GPU (Metal) - optimal for M1/M2/M3 Macs
π¬ Query: What is the capital of France?
β‘ Generating response...
============================================================
π€ Response: The capital of France is Paris. It is located in the north-central part of the country and is the largest city in France, serving as the political, economic, and cultural center of the nation.
============================================================
β±οΈ Time: 2.34s | Device: Metal(0)
candle-core & candle-nn: ML frameworkhf-hub: Hugging Face model loadingtokenizers: Text tokenizationanyhow: Error handlingserde: JSON parsingrand: Random sampling[Add your license here]
[Add contribution guidelines here]
Rust
89.6%
Python
10.4%
A native Rust implementation of BitNet b1.58 2B4T model for efficient 1-bit LLM inference using the Candle framework.
git clone <repository>
cd bitnet-rust
cargo build --release
# Ask a single question
cargo run "What is the capital of France?"
# Or using the binary name
cargo run --bin bitnet "Explain quantum computing"
# Start interactive chat session
cargo run -- --interactive
# or
cargo run -- -i
# Run without arguments to be prompted for input
cargo run
use bitnet_rust::{BitNetGenerator, BitNetConfig, BitNetForCausalLM};
use anyhow::Result;
fn main() -> Result<()> {
// Load model (see examples/simple_chat.rs for full setup)
let generator = BitNetGenerator::new(model, tokenizer, device);
// Generate response
let response = generator.generate("What is machine learning?")?;
println!("Response: {}", response);
// Generate with custom system prompt
let response = generator.generate_with_system(
"Write a poem",
Some("You are a creative poet")
)?;
println!("Poem: {}", response);
Ok(())
}
Run the included example:
cargo run --example simple_chat
The main interface for text generation:
impl BitNetGenerator {
// Create new generator
pub fn new(model: BitNetForCausalLM, tokenizer: Tokenizer, device: Device) -> Self;
// Generate response with default system prompt
pub fn generate(&self, message: &str) -> Result<String>;
// Generate response with custom system prompt
pub fn generate_with_system(&self, message: &str, system: Option<&str>) -> Result<String>;
// Get the device being used
pub fn device(&self) -> &Device;
}
// Format Llama 3 chat template
pub fn format_llama3_chat(system: &str, user_message: &str) -> String;
// Interactive chat session
pub fn run_interactive_chat<F>(generate_fn: F) -> Result<()>
where F: FnMut(&str) -> Result<String>;
For multi-turn conversations:
let mut conv = ConversationHistory::new("You are helpful".to_string());
conv.add_user_message("Hello".to_string());
conv.add_assistant_message("Hi there!".to_string());
let prompt = conv.get_prompt();
The model automatically downloads from Hugging Face Hub:
microsoft/bitnet-b1.58-2B-4Tπ BitNet b1.58 2B4T - Native 1-bit LLM
============================================================
β Using Apple GPU (Metal) - optimal for M1/M2/M3 Macs
π¬ Query: What is the capital of France?
β‘ Generating response...
============================================================
π€ Response: The capital of France is Paris. It is located in the north-central part of the country and is the largest city in France, serving as the political, economic, and cultural center of the nation.
============================================================
β±οΈ Time: 2.34s | Device: Metal(0)
candle-core & candle-nn: ML frameworkhf-hub: Hugging Face model loadingtokenizers: Text tokenizationanyhow: Error handlingserde: JSON parsingrand: Random sampling[Add your license here]
[Add contribution guidelines here]
Rust
89.6%
Python
10.4%