# 全模态推理示例与通道指南

> 在线复用 vllm_projector.py 覆盖九种科学模态，以及 NEU06 从原始脑电到评测的完整示例。

---

LLMS 索引： [llms.txt](/Sage-SciPerception/zh/llms.txt)

---

仓库的 [`examples/`](https://github.com/InternLM/Sage-SciPerception/tree/main/examples) 目录提供三类开箱即用的推理与评测示例，覆盖单任务轻量客户端、九模态统一客户端，以及从原始信号提取特征的完整基准流程：

| 示例入口 | 适用范围 | 核心能力 |
| --- | --- | --- |
| [`examples/vllm_cif.py`](https://github.com/InternLM/Sage-SciPerception/blob/main/examples/vllm_cif.py) | 独立晶体（`cif/jarvis`）示例 | 仅依赖 `load_projector.py` 与 `modeling_projector.py`，无需安装 `evalkit`/`trainkit` |
| [`examples/vllm_projector.py`](https://github.com/InternLM/Sage-SciPerception/blob/main/examples/vllm_projector.py) | 全九模态统一客户端 | 封装 `GlmProjectorSession`，支持 `discrete`、`side`、`counts`、`atom_numbering`、`payload_labels`、多对象拼接与 `--sample-dir` |
| [`examples/neu06/`](https://github.com/InternLM/Sage-SciPerception/blob/main/examples/neu06/README.md) | SciTS NEU06 全量复现流程 | 原始脑电 REVE-base 提取（`reve_features.py`）、可断点续跑的批量生成（`generate_neu06.py`）与 CPU 评分 |

## 准备客户端环境 {#setup}

在公开仓库根目录安装客户端依赖、`evalkit` 与 `trainkit`，下载任意任务检查点，并按[部署章节](/book/03-vllm-serving/)启动启用 `--enable-prompt-embeds` 的 vLLM 服务：

```bash
pip install "scipkit[infer]"   # 或在源码根目录：pip install -e ".[infer]"

scip download glm-5.3-flash/spectra/qm9s --local-dir ./checkpoints
```

## 直接消费公开训练样例（`--sample-dir`） {#sample-dir}

[`internlm/Sage-SciPerception-Training`](https://huggingface.co/datasets/internlm/Sage-SciPerception-Training) 为全部九个已验证模态提供两行可运行的 `sage-training-data-v1` 切片。下载任意切片后，`vllm_projector.py` 会自动读取 `nodes.npy`、`offsets.npy`、可选的 `modality.npy`（`discrete`）或 `side.npy`（`side`）、`payload_labels` 与 `question`。

先加 `--dry-run` 在 CPU 上完成投影前向与提示词拼接自检，无需启动 vLLM 服务：

```bash
hf download internlm/Sage-SciPerception-Training --repo-type dataset \
  --include "samples/spectra/qm9s/*" --local-dir ./training-samples

python examples/vllm_projector.py \
  --component ./checkpoints/projectors/glm-5.3-flash/spectra/qm9s \
  --base-model /path/to/GLM-5.3-Flash-BF16 \
  --sample-dir ./training-samples/samples/spectra/qm9s \
  --row 0 \
  --dry-run
```

去掉 `--dry-run` 并指定服务地址即可发送请求：

```bash
python examples/vllm_projector.py \
  --component ./checkpoints/projectors/glm-5.3-flash/spectra/qm9s \
  --base-model /path/to/GLM-5.3-Flash-BF16 \
  --sample-dir ./training-samples/samples/spectra/qm9s \
  --row 0 \
  --api-base http://127.0.0.1:8000/v1 \
  --model sage-sciperception
```

## 九种模态的输入张量与附加通道 {#channels}

使用 `--features` 直接传入自有数组时，请与检查点 `config.json` 声明的通道和形状保持一致：

| 任务 | 投影模块 ID | `--features` 形状 | 附加参数 |
| --- | --- | --- | --- |
| 晶体形成能 | `glm-5.3-flash/cif/jarvis` | `[num_atoms, 64]` | — |
| 心电分类 | `glm-5.3-flash/ecg/phu01` | `[360, 768]` | — |
| 脑电睡眠分期 | `glm-5.3-flash/eeg/neu06` | `[34, 512]` | 原始脑电提取见下文 [NEU06 完整示例](#neu06) |
| DNA 剪接位点 | `glm-5.3-flash/dna/splice` | `[1, 3]` | — |
| 蛋白质结构排序 | `glm-5.3-flash/protein-struct/psr` | `[<=256, 384]` | — |
| XRD 晶系分类 | `glm-5.3-flash/xrd/simxrd` | `[110, 512]` | — |
| 分子三谱识别 | `glm-5.3-flash/spectra/qm9s` | `[347, 50]` | `--discrete modality.npy`（`[347, 1]`，取值 `0`/`1`/`2`） |
| RNA 家族分类 | `glm-5.3-flash/rna/ncrna` | `[len_seq, 640]` | `--side side.npy`（`[len_seq, 13]`，按核苷酸广播） |
| 蛋白质界面位点 | `glm-5.3-flash/protein-seq/mpbind` | `[len_win, 1280]` | `--side side.npy`（`[len_win, 2]`）与可选 `--payload-labels` |

以下示例覆盖 `discrete`、`side`、`payload_labels` 与多对象拼接场景：

**示例 6-1.** 使用 vllm\_projector\.py 运行光谱离散通道、RNA\/蛋白侧输入与多对象比较。

```bash
# 分子三谱（QM9S）：连续谱片段 + 离散谱型索引 + 自动 atom_numbering
python examples/vllm_projector.py \
  --component ./checkpoints/projectors/glm-5.3-flash/spectra/qm9s \
  --base-model /path/to/GLM-5.3-Flash-BF16 \
  --features /path/to/nodes.npy \
  --discrete /path/to/modality.npy \
  --question "Identify the molecule from its IR, Raman, and UV-Vis spectra."

# 非编码 RNA（BEACON ncRNA）：逐核苷酸状态 + 广播后的 13 维概率侧输入
python examples/vllm_projector.py \
  --component ./checkpoints/projectors/glm-5.3-flash/rna/ncrna \
  --base-model /path/to/GLM-5.3-Flash-BF16 \
  --features /path/to/nodes.npy \
  --side /path/to/side.npy \
  --question "Classify this non-coding RNA sequence into its Rfam family."

# 蛋白质界面位点（MPBind）：逐残基状态 + 逐残基侧输入 + 1-based 残基编号
python examples/vllm_projector.py \
  --component ./checkpoints/projectors/glm-5.3-flash/protein-seq/mpbind \
  --base-model /path/to/GLM-5.3-Flash-BF16 \
  --features /path/to/nodes.npy \
  --side /path/to/side.npy \
  --payload-labels '[[1, 2, 3, 4, 5]]' \
  --question "Predict protein-protein interaction interface residues in positions 1 through 5."

# 多对象比较：按对象顺序拼接特征并用 --payload-tokens 传入各自长度
python examples/vllm_projector.py \
  --component ./checkpoints/projectors/glm-5.3-flash/cif/jarvis \
  --base-model /path/to/GLM-5.3-Flash-BF16 \
  --features /path/to/two_crystals.pt \
  --payload-tokens 12,18 \
  --question "Compare the formation energies of the two crystals."
```

## SciTS NEU06 完整示例（原始 EEG → 评测） {#neu06}

[`examples/neu06/`](https://github.com/InternLM/Sage-SciPerception/blob/main/examples/neu06/README.md) 展示在 [SciTS](https://huggingface.co/datasets/OpenTSLab/SciTS) NEU06（1,024 段 30 秒 Fpz-Cz 单通道脑电，分为 Wake、N1、N2、N3、REM 五类）上运行已发布 `glm-5.3-flash/eeg/neu06` 投影模块的完整链路。v0.1.0 发布成绩为宏平均 F1 **72.01%** / 准确率 **76.46%**；公开示例脚本独立重跑取得宏平均 F1 **71.82%** / 准确率 **76.46%**（两次全量均覆盖 1,024 题，为不同次运行，原运行逐题预测未保留）。

### 固定版本输入来源

| 输入 | 来源 | 固定版本 | 许可证 |
| --- | --- | --- | --- |
| SciTS | [`OpenTSLab/SciTS`](https://huggingface.co/datasets/OpenTSLab/SciTS) | `6ce690e904898cc8fd9014ea2e5bebdfa749c1d2` | CC BY-NC-SA 4.0 |
| REVE-base | [`brain-bzh/reve-base`](https://huggingface.co/brain-bzh/reve-base) | `33841986f6cc1438d9dcdbca5a2137d60f6007d9` | REVE Responsible Use License v1.0 |
| `positions.json` | [`brain-bzh/reve-positions`](https://huggingface.co/brain-bzh/reve-positions) | `befa5b57a455b77cf302daf610c2e9ed8140bace` | 上游未声明 |
| GLM-5.3-Flash-BF16 | [`zai-org/GLM-5.3-Flash-BF16`](https://huggingface.co/zai-org/GLM-5.3-Flash-BF16) | `f9fed3bbc82291073373d67452e9f25317ef7577` | 见上游说明 |
| NEU06 projector | [`internlm/Sage-SciPerception`](https://huggingface.co/internlm/Sage-SciPerception/tree/main/projectors/glm-5.3-flash/eeg/neu06) | `7c228fc03394027fed19bda1ff26388394b0031a` | Apache-2.0 |

### 仅对已有预测文件评分（CPU）

如果已有符合格式（每行含 `eval_id` 与 `content`）的 `responses.jsonl`，只需 Python 3.12+ 与 `uv` 即可在 CPU 上评分：

```bash
SCITS=/path/to/SciTS
RUN=/path/to/neu06-run

uvx --from huggingface-hub hf download OpenTSLab/SciTS meta_data.jsonl --repo-type dataset \
  --revision 6ce690e904898cc8fd9014ea2e5bebdfa749c1d2 --local-dir "$SCITS"
uv run --project evalkit scip-eval score --task scits-neu06 \
  --references "$SCITS/meta_data.jsonl" --responses "$RUN/responses.jsonl" --out-dir "$RUN/score"
```

### 从原始 EEG 波形重现特征、生成并评分

先在 CPU 环境准备依赖、拉取固定版本资产，并用 [`reve_features.py`](https://github.com/InternLM/Sage-SciPerception/blob/main/examples/neu06/reve_features.py) 提取 REVE-base 特征：

```bash
WORK=/path/to/neu06-work
SCITS=$WORK/SciTS
REVE=$WORK/reve-base
GLM=$WORK/GLM-5.3-Flash-BF16
PROJECTOR=./checkpoints/projectors/glm-5.3-flash/eeg/neu06
RUN=$WORK/run

python3.12 -m venv "$WORK/venv"
. "$WORK/venv/bin/activate"
python -m pip install torch==2.15.0 transformers==5.24.1 scipy==1.18.1 safetensors numpy einops huggingface_hub
python -m pip install ./evalkit

hf download OpenTSLab/SciTS --repo-type dataset --revision 6ce690e904898cc8fd9014ea2e5bebdfa749c1d2 \
  --include meta_data.jsonl --include "Neuroscience-Sleep-Sleep_staging-Classification/*" --local-dir "$SCITS"
hf download brain-bzh/reve-base --revision 33841986f6cc1438d9dcdbca5a2137d60f6007d9 --local-dir "$REVE"
hf download brain-bzh/reve-positions positions.json --revision befa5b57a455b77cf302daf610c2e9ed8140bace \
  --local-dir "$WORK/reve-positions"
hf download zai-org/GLM-5.3-Flash-BF16 --revision f9fed3bbc82291073373d67452e9f25317ef7577 --local-dir "$GLM"
python load_projector.py glm-5.3-flash/eeg/neu06 --revision 7c228fc03394027fed19bda1ff26388394b0031a \
  --local-dir ./checkpoints

python examples/neu06/reve_features.py --scits "$SCITS" --reve "$REVE" \
  --positions "$WORK/reve-positions/positions.json" --out "$RUN/features"
```

在八张 GPU 上使用 vLLM 0.32.0 启动底座服务（参数与重跑实测一致）：

```bash
vllm serve "$GLM" \
  --served-model-name sage-sciperception \
  --tensor-parallel-size 8 \
  --dtype bfloat16 \
  --max-model-len 98304 \
  --max-num-seqs 256 \
  --max-num-batched-tokens 16384 \
  --gpu-memory-utilization 0.88 \
  --enable-chunked-prefill \
  --no-enable-prefix-caching \
  --enable-prompt-embeds \
  --generation-config vllm \
  --seed 20261018 \
  --trust-remote-code \
  --logprobs-mode raw_logprobs \
  --no-enable-flashinfer-autotune \
  --reasoning-parser glm47 \
  --tool-call-parser glm47 \
  --enable-auto-tool-choice \
  --host 127.0.0.1 --port 8000
```

最后运行 [`generate_neu06.py`](https://github.com/InternLM/Sage-SciPerception/blob/main/examples/neu06/generate_neu06.py) 并交由 EvalKit 评分（贪心解码，最大生成 8,192 tokens；首次试跑可传 `--limit 16` 并在评分时加 `--only-responded`）：

```bash
python examples/neu06/generate_neu06.py --features "$RUN/features" --projector "$PROJECTOR" \
  --projector-code modeling_projector.py --base-model "$GLM" \
  --api-base http://127.0.0.1:8000/v1 --model sage-sciperception --out "$RUN/responses.jsonl"
uv run --project evalkit scip-eval score --task scits-neu06 \
  --references "$SCITS/meta_data.jsonl" --responses "$RUN/responses.jsonl" --out-dir "$RUN/score"
```
