Views
No views yet

pip install -U vllm \
--pre \
--extra-index-url https://wheels.vllm.ai/nightlyspeculative_config to set the draft model.1from transformers import AutoTokenizer
2from vllm import LLM, SamplingParams
3
4model_name = "openbmb/MiniCPM4-8B"
5prompt = [{"role": "user", "content": "Please recommend 5 tourist attractions in Beijing. "}]
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=32768,
14 dtype="bfloat16",
15 gpu_memory_utilization=0.8,
16 speculative_config={
17 "method": "eagle",
18 "model": "openbmb/MiniCPM4-8B-Eagle-vLLM",
19 "num_speculative_tokens": 2,
20 "max_model_len": 32768,
21 },
22)
23sampling_params = SamplingParams(top_p=0.7, temperature=0.7, max_tokens=1024, repetition_penalty=1.02)
24
25outputs = llm.generate(prompts=input_text, sampling_params=sampling_params)
26
27print(outputs[0].outputs[0].text)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.9977997200264581, 1.014658295992452, 1.0349680404997148, 1.059429246056193, 1.0888815016813513, 1.1243301355211495, 1.166977103606075, 1.2182568066927284, 1.2798772354275727, 1.3538666751582975, 1.4426259039919596, 1.5489853358570191, 1.6762658237220625, 1.8283407612492941, 2.0096956085876183, 2.225478927469756, 2.481536379650452, 2.784415934557119, 3.1413289096347365, 3.560047844772632, 4.048719380066383, 4.752651957515948, 5.590913044973868, 6.584005926629993, 7.7532214876576155, 9.119754865903639, 10.704443927019176, 12.524994176518703, 14.59739595363613, 16.93214476166354, 19.53823297353041, 22.417131025031697, 25.568260840911098, 28.991144156566317, 32.68408069090375, 36.65174474170465, 40.90396065611201, 45.4664008671033, 50.37147343433591, 55.6804490772103, 61.470816952306556, 67.8622707390618, 75.00516023410414, 83.11898235973767, 92.50044360202462, 103.57086856690864, 116.9492274587385, 118.16074567836519, 119.18497548708795, 120.04810876261652, 120.77352815196981, 121.38182790207875, 121.89094985353891, 122.31638758099915, 122.6714244963338, 122.9673822552567, 123.21386397019609, 123.41898278254268, 123.58957065488238, 123.73136519024158, 123.84917421274221, 123.94701903496814, 124.02825801299717, 124.09569231686116],
6 "short_factor": [0.9977997200264581, 1.014658295992452, 1.0349680404997148, 1.059429246056193, 1.0888815016813513, 1.1243301355211495, 1.166977103606075, 1.2182568066927284, 1.2798772354275727, 1.3538666751582975, 1.4426259039919596, 1.5489853358570191, 1.6762658237220625, 1.8283407612492941, 2.0096956085876183, 2.225478927469756, 2.481536379650452, 2.784415934557119, 3.1413289096347365, 3.560047844772632, 4.048719380066383, 4.752651957515948, 5.590913044973868, 6.584005926629993, 7.7532214876576155, 9.119754865903639, 10.704443927019176, 12.524994176518703, 14.59739595363613, 16.93214476166354, 19.53823297353041, 22.417131025031697, 25.568260840911098, 28.991144156566317, 32.68408069090375, 36.65174474170465, 40.90396065611201, 45.4664008671033, 50.37147343433591, 55.6804490772103, 61.470816952306556, 67.8622707390618, 75.00516023410414, 83.11898235973767, 92.50044360202462, 103.57086856690864, 116.9492274587385, 118.16074567836519, 119.18497548708795, 120.04810876261652, 120.77352815196981, 121.38182790207875, 121.89094985353891, 122.31638758099915, 122.6714244963338, 122.9673822552567, 123.21386397019609, 123.41898278254268, 123.58957065488238, 123.73136519024158, 123.84917421274221, 123.94701903496814, 124.02825801299717, 124.09569231686116],
7 "original_max_position_embeddings": 32768
8 }
9}python3 tests/test_generate.py1from transformers import AutoModelForCausalLM, AutoTokenizer
2import torch
3torch.manual_seed(0)
4
5path = 'openbmb/MiniCPM4-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]
18model_inputs = tokenizer.apply_chat_template(messages, return_tensors="pt", add_generation_prompt=True).to(device)
19
20model_outputs = model.generate(
21 model_inputs,
22 max_new_tokens=1024,
23 top_p=0.7,
24 temperature=0.7
25)
26output_token_ids = [
27 model_outputs[i][len(model_inputs[i]):] for i in range(len(model_inputs))
28]
29
30responses = tokenizer.batch_decode(output_token_ids, skip_special_tokens=True)[0]
31print(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.9977997200264581, 1.014658295992452, 1.0349680404997148, 1.059429246056193, 1.0888815016813513, 1.1243301355211495, 1.166977103606075, 1.2182568066927284, 1.2798772354275727, 1.3538666751582975, 1.4426259039919596, 1.5489853358570191, 1.6762658237220625, 1.8283407612492941, 2.0096956085876183, 2.225478927469756, 2.481536379650452, 2.784415934557119, 3.1413289096347365, 3.560047844772632, 4.048719380066383, 4.752651957515948, 5.590913044973868, 6.584005926629993, 7.7532214876576155, 9.119754865903639, 10.704443927019176, 12.524994176518703, 14.59739595363613, 16.93214476166354, 19.53823297353041, 22.417131025031697, 25.568260840911098, 28.991144156566317, 32.68408069090375, 36.65174474170465, 40.90396065611201, 45.4664008671033, 50.37147343433591, 55.6804490772103, 61.470816952306556, 67.8622707390618, 75.00516023410414, 83.11898235973767, 92.50044360202462, 103.57086856690864, 116.9492274587385, 118.16074567836519, 119.18497548708795, 120.04810876261652, 120.77352815196981, 121.38182790207875, 121.89094985353891, 122.31638758099915, 122.6714244963338, 122.9673822552567, 123.21386397019609, 123.41898278254268, 123.58957065488238, 123.73136519024158, 123.84917421274221, 123.94701903496814, 124.02825801299717, 124.09569231686116],
6 "short_factor": [0.9977997200264581, 1.014658295992452, 1.0349680404997148, 1.059429246056193, 1.0888815016813513, 1.1243301355211495, 1.166977103606075, 1.2182568066927284, 1.2798772354275727, 1.3538666751582975, 1.4426259039919596, 1.5489853358570191, 1.6762658237220625, 1.8283407612492941, 2.0096956085876183, 2.225478927469756, 2.481536379650452, 2.784415934557119, 3.1413289096347365, 3.560047844772632, 4.048719380066383, 4.752651957515948, 5.590913044973868, 6.584005926629993, 7.7532214876576155, 9.119754865903639, 10.704443927019176, 12.524994176518703, 14.59739595363613, 16.93214476166354, 19.53823297353041, 22.417131025031697, 25.568260840911098, 28.991144156566317, 32.68408069090375, 36.65174474170465, 40.90396065611201, 45.4664008671033, 50.37147343433591, 55.6804490772103, 61.470816952306556, 67.8622707390618, 75.00516023410414, 83.11898235973767, 92.50044360202462, 103.57086856690864, 116.9492274587385, 118.16074567836519, 119.18497548708795, 120.04810876261652, 120.77352815196981, 121.38182790207875, 121.89094985353891, 122.31638758099915, 122.6714244963338, 122.9673822552567, 123.21386397019609, 123.41898278254268, 123.58957065488238, 123.73136519024158, 123.84917421274221, 123.94701903496814, 124.02825801299717, 124.09569231686116],
7 "original_max_position_embeddings": 32768
8 }
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-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-8B",
7 messages=[
8 {"role": "user", "content": "Write an article about Artificial Intelligence."},
9 ],
10 temperature=0.7,
11 max_tokens=1024,
12)
13
14print(response.choices[0].message.content)


1@article{minicpm4,
2 title={{MiniCPM4}: Ultra-Efficient LLMs on End Devices},
3 author={MiniCPM Team},
4 year={2025}
5}