inference
Operate Baichuan2 chat/base inference, terminal chat, Streamlit chat, and OpenAI-compatible chat-completions workflows.
- License 许可
- Apache-2.0
- 最近更新
- 2026-09-03
- Stars
- ★ 351
- Forks
- 27
01 前言 Front Matter
- name
- inference
- license
- Apache 2.0
- metadata
- { "repo": "Baichuan2", "disco-role": "operating", "sub-skill-id": "inference", "required-backend": "cuda-for-primary-chat-demos", "supported-workflows": [ "python-chat-inference", "python-base-generation", "interactive-cli-chat", "streamlit-web-chat", "openai-compatible-chat-completions" ], "verified-inspection-context": { "torch": "2.5.1+cu121", "cuda-smoke": "passed-on-nvidia-a100", "transformers": "5.15.0", "deepspeed-cli-help": "passed", "streamlit-cli-help": "passed" } }
- description
- Operate Baichuan2 chat/base inference, terminal chat, Streamlit chat, and OpenAI-compatible chat-completions workflows.
- disable-model-invocation
- true
02 资源文件 6
- 参考文档references/api-reference.md4.1 KB
- 参考文档references/troubleshooting.md6.2 KB
- 参考文档references/workflows.md6.7 KB
- 脚本scripts/chat_cli.py9.9 KB
- 脚本scripts/chat_web_demo.py8.1 KB
- 脚本scripts/run_openai_api.py8.2 KB
03 SKILL.md 正文
Baichuan2 inference
Use this sub-skill when the user wants to run or understand Baichuan2 inference: Python chat snippets, Base-model text generation, the interactive terminal demo, the Streamlit chat UI, or the OpenAI-compatible /v1/chat/completions server.
Do not route quantization, CPU deployment conversion, model checkpoint normalization, fine-tuning, DeepSpeed training, or LoRA work here. Send those requests to the deployment or fine-tuning sub-skill.
Route by user intent
| User asks for | Route | Key files |
|---|---|---|
| "chat with Baichuan2 in Python" or "use model.chat" | Chat Python recipe | references/workflows.md |
| "run the Base model" or "plain text completion" | Base-model generation recipe | references/workflows.md |
| "interactive CLI" or "terminal demo" | Chat CLI helper | scripts/chat_cli.py |
| "web demo" or "browser UI" | Streamlit helper | scripts/chat_web_demo.py |
| "OpenAI API compatible server" or "chat completions endpoint" | Flask API helper | scripts/run_openai_api.py, references/api-reference.md |
| Load/model/runtime failure | Troubleshooting | references/troubleshooting.md |
Core operating facts
- Baichuan2 publishes Chat and Base model families for 7B and 13B. Use
*-Chatfor assistant-style conversations and*-Basefor continuation/generation tasks. - Chat-model code uses
AutoTokenizer.from_pretrained(..., use_fast=False, trust_remote_code=True),AutoModelForCausalLM.from_pretrained(..., device_map="auto", torch_dtype=...), andGenerationConfig.from_pretrained(model_id)before callingmodel.chat(tokenizer, messages). - Base-model code uses tokenization plus
model.generate(...); do not use the CLI/web/API wrappers with Base checkpoints because those wrappers expect Chat model behavior. device_map="auto"lets Transformers place weights across available GPUs. To constrain visible GPUs, setCUDA_VISIBLE_DEVICESbefore launch.torch.bfloat16is the README chat-inference dtype and is appropriate on Ampere-class GPUs such as A100. The bundled chat/API demos default tofloat16to match the native demo scripts; use--dtype bfloat16on hardware that supports it.- The prepared inspection environment verified a CUDA-capable stack with torch
2.5.1+cu121, transformers5.15.0, Streamlit CLI help, DeepSpeed CLI help, and a CUDA tensor smoke test on NVIDIA A100. DeepSpeed is not required for this inference sub-skill.
Minimal chat model recipe
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer
from transformers.generation.utils import GenerationConfig
model_id = "baichuan-inc/Baichuan2-13B-Chat"
tokenizer = AutoTokenizer.from_pretrained(
model_id,
use_fast=False,
trust_remote_code=True,
)
model = AutoModelForCausalLM.from_pretrained(
model_id,
device_map="auto",
torch_dtype=torch.bfloat16,
trust_remote_code=True,
)
model.generation_config = GenerationConfig.from_pretrained(model_id)
messages = [{"role": "user", "content": "解释一下“温故而知新”"}]
response = model.chat(tokenizer, messages)
print(response)
For streaming chat, call model.chat(tokenizer, messages, stream=True). The iterator yields progressively longer response strings, so terminal UIs should print only the delta after the previous position.
Bundled helpers
Run --help before loading weights:
python scripts/chat_cli.py --help
python scripts/run_openai_api.py --help
python scripts/chat_web_demo.py --help
Check launch configuration without downloading/loading weights:
python scripts/chat_cli.py --dry-run --model baichuan-inc/Baichuan2-13B-Chat
python scripts/run_openai_api.py --dry-run --host 127.0.0.1 --port 8000
python scripts/chat_web_demo.py --dry-run --model baichuan-inc/Baichuan2-13B-Chat
Launch examples:
# Terminal chat, Chat model only.
python scripts/chat_cli.py --model baichuan-inc/Baichuan2-13B-Chat --dtype float16 --stream
# OpenAI-compatible non-streaming API.
python scripts/run_openai_api.py --model baichuan-inc/Baichuan2-13B-Chat --host 0.0.0.0 --port 8000 --dtype float16
# Streamlit web UI. Streamlit server flags configure the web host/port;
# arguments after `--` are passed to the Baichuan2 helper.
streamlit run scripts/chat_web_demo.py --server.address 0.0.0.0 --server.port 8501 -- --model baichuan-inc/Baichuan2-13B-Chat --dtype float16
API behavior to remember
- Endpoint:
POST /v1/chat/completions. - Request body should contain
messages, each withroleandcontent. - The bundled API helper is intentionally non-streaming. If a client sends
"stream": true, it returns HTTP 400 with a streaming-not-supported error. - The API helper loads one configured Chat model at process startup. The request
modelfield is accepted for OpenAI-style clients but does not switch the already-loaded model.
When to escalate
- Missing weights, private model access, incompatible remote code, or network timeouts: use
references/troubleshooting.md. - Out-of-memory or dtype failures: use
references/troubleshooting.md. - Need 4-bit/8-bit loading, CPU-only operation, or checkpoint conversion: route to deployment.
- Need SFT, LoRA, DeepSpeed, training data validation, or model output checkpoints: route to fine-tuning.
04 同仓库更多 Skills
passnet-triton-opt
Make a single PassNet Triton kernel fast AND numerically faithful: performance model (when a replacement can win at all), block/grid/warp tuning, autotune policy, launch overhead, and per-op numeric recipes to pass the dtype baseline tolerances. Use when a pass matches and is correct but speedup below expected, or when correctness fails by small numeric margins.
passnet-solve
END-TO-END playbook for solving one PassNet sample: analyze the computation graphs, decide the optimization strategy (what to fuse, what to replace, what to leave alone), drive the iteration loop, and maximize the sample score. This is the ENTRY skill — invoke it first for any PassNet optimization task; it tells you when to use passnet-pattern-fusion, passnet-triton-opt and passnet-feedback.
passnet-skill
PassNet GPU kernel optimization via compiler passes. Design and implement Triton-based optimization passes, create pass files under ./pass_dir/, self-evaluate with pass_evaluator, and iterate to maximize GPU speedup.
passnet-pattern-fusion
Author PassNet pass files: write patterns that actually MATCH the FX graph, pick fusion regions (which ops to absorb into one Triton kernel), and structure multi-pass submissions with the mandatory shared-dispatch architecture. Use when creating or fixing pattern()/replacement_args()/replacement_func() files, when a pass "failed to match", or when deciding how to fuse multiple kernels/ops.
passnet-orchestrate
Round/batch planning and decision support for PassNet graph optimization. Use for multi-sample triage, worker allocation, eval-budget policy, and post-eval keep/revert decisions. For solving one concrete sample end to end, use passnet-solve as the entry skill; consult this skill only when you need broader planning or a specific decision gate.
passnet-feedback
Fast, deterministic feedback for PassNet work: pre-flight pattern/match verification WITHOUT burning a GPU evaluation, per-node bottleneck analysis of a sample's graphs, and eval-log parsing into per-variant status + estimated score + failure classification. Use BEFORE every GPU evaluation (check_pattern), at the START of a sample (analyze_graph), and AFTER every evaluation (parse_eval_log).