Skip to content

perf(engram): add optional sorted host-table reads to engram_gather (from sgl-project/sglang#45879) - #2

Merged
ll2088 merged 1 commit into
mainfrom
hop/adopt/sgl-project-sglang-45879
Nov 2, 2026
Merged

ll2088 merged 1 commit into
mainfrom
hop/adopt/sgl-project-sglang-45879

Conversation

@ll2088

@ll2088 ll2088 commented Oct 23, 2026 •

Copy link
Copy Markdown
Owner

借鉴上游改动

来源: sgl-project/sglang#45879 · [Perf] engram: Read host-table rows in address order on GPUs without host page tables
生成: Hop AI(deepseek-v4.1-flash),基线 main @ d0b9d54

改动说明

在 engram_gather 中新增可选 sort_ids 参数:先对 ids 排序,按地址顺序读取表行,再通过 dst 索引写回原位置,输出逐位一致;未启用时行为完全不变。当前仓库未提供 engram.py 全文,因此仅移植内核侧能力,层侧自动启用逻辑需在具备该文件时另行接入。

文件

  • python/sglang/kernels/ops/embeddings/engram_gather.py · edit

四维验证

运行日志 · 结论:全部通过

维度 结果
效果 success
性能 success
稳定性 success
可用性 success

评审重点

  • 对比 sort_ids=False 与 sort_ids=True 的 gather 输出,确认逐位一致(含重复 id 与乱序 id)。
  • 覆盖越界 id(小于 row_lo 或大于等于 row_hi)场景,确认两种路径均输出零行。
  • 验证 ids 为 int32 与 int64 两种 dtype 时排序路径均正确写回原位置。
  • 确认 sort_ids=False 时内核参数与原有行为完全一致,无额外排序开销。
  • 在 host 表远大于 GPU 地址转换范围的 PCIe 主机上做基准,确认排序路径收益为正。

已知风险

  • 当前仓库未提供 engram.py 全文,层侧基于 CUDA 属性的自动启用逻辑未接入,需后续在具备该文件时补充。
  • torch.sort 对重复 id 的稳定顺序不影响输出,但会引入额外排序开销,小表或设备表上可能得不偿失。
  • dst 索引张量由 torch.sort 返回,其 dtype 与 ids 一致,需确保内核 int64 转换在 int32 下也正确。
  • HAS_DST 为 constexpr,两种路径会各自编译一份内核,增加编译时间。

由 Hop 自动生成。代码由 AI 依据上游 diff 移植,四维验证通过不代表没有逻辑问题,合并前请人工 code review。


CI States

Latest PR Test (Base): ❌ Missing run-ci label -- add it to run CI tests.
Latest PR Test (Extra): ❌ Run #39951621336
Latest PR Test (AMD ROCm 10): ➖ No AMD PR run found for this commit.

Ported from sgl-project#45879
Generated by Hop (AI); review before merging.
@github-actions

Copy link
Copy Markdown

Required checks failed on 07e957b; this PR cannot be merged until they pass.

PR title (run)

PR title must use "[Type] scope: title" or "[Type] [Hardware] scope: title". Type must be one of: Feat, Fix, Refactor, Perf, Doc, Test, CI, Chore. Scope is required and may be any module or model name, with unrestricted letter case (letters, numbers, dots, underscores, slashes, or hyphens). Hardware is optional; use a single tag without spaces. Use uppercase hardware tags (AMD, NVIDIA, NPU, MI300X), except for exactly these four spellings: Intel, Apple, Huawei, Ascend. Reverts must use GitHub's default format: Revert "original title". Use one space between fields and after the colon, with a non-empty title and no leading or trailing whitespace. Examples: "[Fix] attention: Fix KV cache indexing", "[Feat] qwen3: Add model support", "[Perf] [AMD] flash-attn: Optimize decode performance".
Edit the title; the check reruns on its own.

Lint (run)
Run pre-commit run --all-files locally and push the result. The run log shows any other failing step.

@ll2088
ll2088 merged commit a15b8d3 into main Nov 2, 2026
54 of 66 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant