Views
No views yet
Note: Mirror of openbmb/MiniCPM4.1-8B with a single change tomodeling_minicpm.py: theCacheLayerMixin/DynamicLayerimport is wrapped in atry/except ImportErrorfallback so the module imports undertransformers < 4.54. This is required for OpenVINO export via optimum-intel, which capsminicpmat transformers 4.53.3. The InfLLMv2 sparse-attention path that uses those names is gated ontorch.cuda.is_available()and does not run on the CPU export box. Weights are byte-identical to the upstream model.



transformers>=4.56.1from transformers import AutoModelForCausalLM, AutoTokenizer
2import torch
3torch.manual_seed(0)
4
5path = 'openbmb/MiniCPM4.1-8B'
6device = "cuda"
7tokenizer = AutoTokenizer.from_pretrained(path)
8model = AutoModelForCausalLM.from_pretrained(path, torch_dtype=torch.bfloat16, device_map=device, trust_remote_code=True)
9
10# User can directly use the chat interface
11# responds, history = model.chat(tokenizer, "Write an article about Artificial Intelligence.", temperature=0.7, top_p=0.7)
12# print(responds)
13
14# User can also use the generate interface
15messages = [
16 {"role": "user", "content": "Write an article about Artificial Intelligence."},
17]
18prompt_text = tokenizer.apply_chat_template(
19 messages,
20 tokenize=False,
21 add_generation_prompt=True,
22)
23model_inputs = tokenizer([prompt_text], return_tensors="pt").to(device)
24
25model_outputs = model.generate(
26 **model_inputs,
27 max_new_tokens=32768,
28 top_p=0.95,
29 temperature=0.6
30)
31output_token_ids = [
32 model_outputs[i][len(model_inputs[i]):] for i in range(len(model_inputs['input_ids']))
33]
34
35responses = tokenizer.batch_decode(output_token_ids, skip_special_tokens=True)[0]
36print(responses)InfLLM v2, a sparse attention mechanism designed for efficient long-sequence inference. It requires the infllmv2_cuda_impl library.1git clone -b feature_infer https://github.com/OpenBMB/infllmv2_cuda_impl.git
2cd infllmv2_cuda_impl
3git submodule update --init --recursive
4pip install -e . # or python setup.py install sparse_config field in config.json:1{
2 ...,
3 "sparse_config": {
4 "kernel_size": 32,
5 "kernel_stride": 16,
6 "init_blocks": 1,
7 "block_size": 64,
8 "window_size": 2048,
9 "topk": 64,
10 "use_nope": false,
11 "dense_len": 8192
12 }
13}kernel_size (default: 32): The size of semantic kernels.kernel_stride (default: 16): The stride between adjacent kernels.init_blocks (default: 1): The number of initial blocks that every query token attends to. This ensures attention to the beginning of the sequence.block_size (default: 64): The block size for key-value blocks.window_size (default: 2048): The size of the local sliding window.topk (default: 64): The specifies that each token computes attention with only the top-k most relevant key-value blocks.use_nope (default: false): Whether to use the NOPE technique in block selection for improved performance.dense_len (default: 8192): Since Sparse Attention offers limited benefits for short sequences, the model can use standard (dense) attention for shorter texts. The model will use dense attention for sequences with a token length below dense_len and switch to sparse attention for sequences exceeding this length. Set this to -1 to always use sparse attention regardless of sequence length.config.json file, adjust the rope_scaling fields.1{
2 ...,
3 "rope_scaling": {
4 "rope_type": "longrope",
5 "long_factor": [0.9982316082870437, 1.033048153422584, 1.0749920956484724, 1.1255096879436193, 1.1863348602111476, 1.259543828902579, 1.3476188888731149, 1.4535223827776373, 1.5807816745852985, 1.7335856049489526, 1.9168922912975785, 2.1365471404135326, 2.3994084200118646, 2.713475511863602, 3.0880118452194134, 3.533650295140154, 4.062463396503134, 4.687974098908333, 5.425075306704039, 6.289818967956352, 7.29902962722721, 8.6357018163639, 10.210822723989212, 12.053807765671676, 14.193944598909404, 16.65780676784363, 19.463620727694074, 22.628311203524586, 26.150106147261315, 30.02526691405111, 34.23183327975347, 38.73811934094828, 43.502489489729555, 48.47627117965394, 53.61139491762471, 58.857366522037935, 64.16798299215064, 69.51359464319125, 74.86555458220285, 80.21497790341579, 85.55322183307433, 90.89611806932027, 96.26245306514224, 101.68269304046481, 107.18619510219668, 112.82253283014026, 118.63764063163615, 119.88866203644656, 120.9462882391725, 121.837565139014, 122.58663780572562, 123.2147719894291, 123.74049454862576, 124.17980424685767, 124.54641761955492, 124.85202548028222, 125.10654406389756, 125.31835105170659, 125.49450117164764, 125.64091910903052, 125.76256945356558, 125.86360463815589, 125.94749252260765, 126.01712561287873],
6 "short_factor": [0.9982316082870437, 1.033048153422584, 1.0749920956484724, 1.1255096879436193, 1.1863348602111476, 1.259543828902579, 1.3476188888731149, 1.4535223827776373, 1.5807816745852985, 1.7335856049489526, 1.9168922912975785, 2.1365471404135326, 2.3994084200118646, 2.713475511863602, 3.0880118452194134, 3.533650295140154, 4.062463396503134, 4.687974098908333, 5.425075306704039, 6.289818967956352, 7.29902962722721, 8.6357018163639, 10.210822723989212, 12.053807765671676, 14.193944598909404, 16.65780676784363, 19.463620727694074, 22.628311203524586, 26.150106147261315, 30.02526691405111, 34.23183327975347, 38.73811934094828, 43.502489489729555, 48.47627117965394, 53.61139491762471, 58.857366522037935, 64.16798299215064, 69.51359464319125, 74.86555458220285, 80.21497790341579, 85.55322183307433, 90.89611806932027, 96.26245306514224, 101.68269304046481, 107.18619510219668, 112.82253283014026, 118.63764063163615, 119.88866203644656, 120.9462882391725, 121.837565139014, 122.58663780572562, 123.2147719894291, 123.74049454862576, 124.17980424685767, 124.54641761955492, 124.85202548028222, 125.10654406389756, 125.31835105170659, 125.49450117164764, 125.64091910903052, 125.76256945356558, 125.86360463815589, 125.94749252260765, 126.01712561287873],
7 "original_max_position_embeddings": 65536
8 }
9}1cd /your_path
2git clone https://huggingface.co/openbmb/MiniCPM4.1-8B-Eagle31git clone https://github.com/LDLINGLINGLING/sglang.git
2cd sglang
3pip install -e "python[all]"1python -m sglang.launch_server \
2 --model-path "openbmb/MiniCPM4.1-8B" \
3 --host "127.0.0.1" \
4 --port 30002 \
5 --mem-fraction-static 0.9 \
6 --speculative-algorithm EAGLE3 \
7 --speculative-draft-model-path "your/path/MiniCPM4_1-8B-Eagle3-bf16" \
8 --speculative-num-steps 3 \
9 --speculative-eagle-topk 1 \
10 --speculative-num-draft-tokens 32 \
11 --temperature 0.71import openai
2
3client = openai.Client(base_url=f"http://localhost:30002/v1", api_key="None")
4
5response = client.chat.completions.create(
6 model="openbmb/MiniCPM4.1-8B",
7 messages=[
8 {"role": "user", "content": "Write an article about Artificial Intelligence."},
9 ],
10 temperature=0.6,
11 max_tokens=32768,
12)
13
14print(response.choices[0].message.content)--speculative-algorithm EAGLE3: Enables EAGLE3 speculative decoding--speculative-draft-model-path: Path to the draft model for speculation--speculative-num-steps: Number of speculative steps (default: 3)--speculative-eagle-topk: Top-k parameter for EAGLE (default: 1)--speculative-num-draft-tokens: Number of draft tokens (default: 32)--mem-fraction-static: Memory fraction for static allocation (default: 0.9)1git clone -b openbmb https://github.com/OpenBMB/sglang.git
2cd sglang
3
4pip install --upgrade pip
5pip install -e "python[all]"python -m sglang.launch_server --model openbmb/MiniCPM4.1-8B --trust-remote-code --port 30000 --chat-template chatml1import openai
2
3client = openai.Client(base_url=f"http://localhost:30000/v1", api_key="None")
4
5response = client.chat.completions.create(
6 model="openbmb/MiniCPM4.1-8B",
7 messages=[
8 {"role": "user", "content": "Write an article about Artificial Intelligence."},
9 ],
10 temperature=0.6,
11 max_tokens=32768,
12)
13
14print(response.choices[0].message.content)architectures in config.json as LlamaForCausalLM.1cd /your_path
2git clone https://huggingface.co/openbmb/MiniCPM4.1-8B-Eagle31git clone https://github.com/LDLINGLINGLING/vllm.git
2cd vllm
3pip install -e .1VLLM_USE_V1=1 \
2vllm serve openbmb/MiniCPM4.1-8B \
3--seed 42 \
4--trust-remote-code \
5--speculative-config '{
6 "model": "your/path/MiniCPM4_1-8B-Eagle3-bf16",
7 "num_speculative_tokens": 3,
8 "method": "eagle3",
9 "draft_tensor_parallel_size": 1
10}'1import openai
2
3client = openai.Client(base_url="http://localhost:8000/v1", api_key="EMPTY")
4
5response = client.chat.completions.create(
6 model="openbmb/MiniCPM4.1-8B",
7 messages=[
8 {"role": "user", "content": "Write an article about Artificial Intelligence."},
9 ],
10 temperature=0.6,
11 max_tokens=32768,
12 extra_body=dict(add_special_tokens=True), # Ensures special tokens are added for chat template
13)
14
15print(response.choices[0].message.content)VLLM_USE_V1=1: Enables vLLM v1 API--speculative-config: JSON configuration for speculative decoding
model: Path to the draft model for speculationnum_speculative_tokens: Number of speculative tokens (default: 3)method: Speculative decoding method (eagle3)draft_tensor_parallel_size: Tensor parallel size for draft model (default: 1)--seed: Random seed for reproducibility--trust-remote-code: Allow execution of remote code for custom models1pip install -U vllm \
2 --pre \
3 --extra-index-url https://wheels.vllm.ai/nightly1from transformers import AutoTokenizer
2from vllm import LLM, SamplingParams
3
4model_name = "openbmb/MiniCPM4.1-8B"
5prompt = [{"role": "user", "content": "Write an article about Artificial Intelligence."}]
6
7tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
8input_text = tokenizer.apply_chat_template(prompt, tokenize=False, add_generation_prompt=True)
9
10llm = LLM(
11 model=model_name,
12 trust_remote_code=True,
13 max_num_batched_tokens=65536,
14 dtype="bfloat16",
15 gpu_memory_utilization=0.8,
16)
17sampling_params = SamplingParams(top_p=0.95, temperature=0.6, max_tokens=32768)
18
19outputs = llm.generate(prompts=input_text, sampling_params=sampling_params)
20
21print(outputs[0].outputs[0].text)Note: In vLLM's chat API,add_special_tokensisFalseby default. This means important special tokens—such as the beginning-of-sequence (BOS) token—will not be added automatically. To ensure the input prompt is correctly formatted for the model, you should explicitly setextra_body={"add_special_tokens": True}.
vllm serve openbmb/MiniCPM4.1-8B --trust-remote-code 1import openai
2
3client = openai.Client(base_url="http://localhost:8000/v1", api_key="EMPTY")
4
5response = client.chat.completions.create(
6 model="openbmb/MiniCPM4.1-8B",
7 messages=[
8 {"role": "user", "content": "Write an article about Artificial Intelligence."},
9 ],
10 temperature=0.6,
11 max_tokens=32768,
12 extra_body=dict(add_special_tokens=True), # Ensures special tokens are added for chat template
13
14)
15
16print(response.choices[0].message.content)1git clone https://github.com/OpenBMB/cpm.cu.git --recursive
2cd cpm.cu
3python3 setup.py installrope_scaling field in the config.json file as the following to enable LongRoPE.1{
2 ...,
3 "rope_scaling": {
4 "rope_type": "longrope",
5 "long_factor": [0.9982316082870437, 1.033048153422584, 1.0749920956484724, 1.1255096879436193, 1.1863348602111476, 1.259543828902579, 1.3476188888731149, 1.4535223827776373, 1.5807816745852985, 1.7335856049489526, 1.9168922912975785, 2.1365471404135326, 2.3994084200118646, 2.713475511863602, 3.0880118452194134, 3.533650295140154, 4.062463396503134, 4.687974098908333, 5.425075306704039, 6.289818967956352, 7.29902962722721, 8.6357018163639, 10.210822723989212, 12.053807765671676, 14.193944598909404, 16.65780676784363, 19.463620727694074, 22.628311203524586, 26.150106147261315, 30.02526691405111, 34.23183327975347, 38.73811934094828, 43.502489489729555, 48.47627117965394, 53.61139491762471, 58.857366522037935, 64.16798299215064, 69.51359464319125, 74.86555458220285, 80.21497790341579, 85.55322183307433, 90.89611806932027, 96.26245306514224, 101.68269304046481, 107.18619510219668, 112.82253283014026, 118.63764063163615, 119.88866203644656, 120.9462882391725, 121.837565139014, 122.58663780572562, 123.2147719894291, 123.74049454862576, 124.17980424685767, 124.54641761955492, 124.85202548028222, 125.10654406389756, 125.31835105170659, 125.49450117164764, 125.64091910903052, 125.76256945356558, 125.86360463815589, 125.94749252260765, 126.01712561287873],
6 "short_factor": [0.9982316082870437, 1.033048153422584, 1.0749920956484724, 1.1255096879436193, 1.1863348602111476, 1.259543828902579, 1.3476188888731149, 1.4535223827776373, 1.5807816745852985, 1.7335856049489526, 1.9168922912975785, 2.1365471404135326, 2.3994084200118646, 2.713475511863602, 3.0880118452194134, 3.533650295140154, 4.062463396503134, 4.687974098908333, 5.425075306704039, 6.289818967956352, 7.29902962722721, 8.6357018163639, 10.210822723989212, 12.053807765671676, 14.193944598909404, 16.65780676784363, 19.463620727694074, 22.628311203524586, 26.150106147261315, 30.02526691405111, 34.23183327975347, 38.73811934094828, 43.502489489729555, 48.47627117965394, 53.61139491762471, 58.857366522037935, 64.16798299215064, 69.51359464319125, 74.86555458220285, 80.21497790341579, 85.55322183307433, 90.89611806932027, 96.26245306514224, 101.68269304046481, 107.18619510219668, 112.82253283014026, 118.63764063163615, 119.88866203644656, 120.9462882391725, 121.837565139014, 122.58663780572562, 123.2147719894291, 123.74049454862576, 124.17980424685767, 124.54641761955492, 124.85202548028222, 125.10654406389756, 125.31835105170659, 125.49450117164764, 125.64091910903052, 125.76256945356558, 125.86360463815589, 125.94749252260765, 126.01712561287873],
7 "original_max_position_embeddings": 65536
8 }
9}python3 tests/test_generate.py1python3 -m cpmcu.cli \
2 --model-path $BASE_MODEL_PATH \
3 --draft-model-path $EAGLE3_DRAFT_MODEL_PATH \
4 --prompt-text "Write an article about Artificial Intelligence." \
5 --use-eagle3 true# case 1: main-cli
./build/bin/llama-cli -m MiniCPM4.1-8B-Q4_K_M.gguf -p "Write an article about Artificial Intelligence." -n 1500
# case 2: server
## launch server
./build/bin/llama-server -m MiniCPM4.1-8B-Q4_K_M.gguf --host 127.0.0.1 --port 8080 -c 4096 -fa on &
## send request
curl -X POST http://127.0.0.1:8080/v1/chat/completions \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-3.5-turbo",
"messages": [{"role": "user", "content": "Write an article about Artificial Intelligence."}],
"max_tokens": 1500
}'ollama run openbmb/minicpm4.1enable_thinking=True in tokenizer.apply_chat_template to enable hybrid reasoning mode, and set enable_thinking=False to enable non-reasoning mode. Similarly, user can directly add /no_think at the end of the query to enable non-reasoning mode. If not add any special token or add /think at the end of the query, the model will enable reasoning mode.1# Enable reasoning mode
2prompt_text = tokenizer.apply_chat_template(
3 messages,
4 tokenize=False,
5 add_generation_prompt=True,
6 enable_thinking=True
7)
8# Enable non-reasoning mode
9prompt_text = tokenizer.apply_chat_template(
10 messages,
11 tokenize=False,
12 add_generation_prompt=True,
13 enable_thinking=False
14)1@article{minicpm4,
2 title={Minicpm4: Ultra-efficient llms on end devices},
3 author={MiniCPM, Team},
4 journal={arXiv preprint arXiv:2506.07900},
5 year={2025}
6}