执行摘要
修复 macOS 上 Python 3.12+ 中 Triton stub 子模块导入失败的 bug。
该变更旨在修复issue #21548中报告的问题:在macOS上,SGLang使用Triton stub模拟triton模块,以便在没有真实Triton的环境下运行。但随Python 3.12发布,CPython移除了sys.meta_path中find_module()的回退机制(当finder未实现find_spec()时),导致旧stub无法处理点分triton.*子模块导入,引发ModuleNotFoundError: No module named 'triton.compiler'。PR body中引用具体表述:"On Python 3.12, CPython removed the sys.meta_path fallback to find_module() when a finder does not implement find_spec(), so the old stub no longer materialized those sub-modules."
该PR值得精读,尤其对于处理Python导入系统兼容性、macOS/MPS环境集成或代码重构的工程师。关注点包括:
find_spec方法的实现细节,展示了如何动态模拟模块和子模块。- 从
find_module/load_module到find_spec的迁移决策,体现了对Python版本演进的适应。 - 讨论中如何通过测试验证修复,确保跨Python版本的兼容性。
review讨论中,主要交锋围绕移除过时导入协议和验证修复效果:
- yeahdongcn 在Issue评论中提问根因并建议测试Python 3.11和3.12兼容性,随后在review中建议"Could you please remove
find_module/load_module",认为这些方法自Python 3.4起已过时。 - karanb192 回应确认问题是Python 3.12特有,并通过测试验证修复有效;在后续提交中移除了
find_module/load_module方法,并说明"The latest revision removesfind_module/load_moduleentirely and uses onlyfind_spec"。 - 讨论还涉及移除冗余mock,karanb192 在评论中解释"
find_spec()on_TritonFinderalready handles dottedtriton.*sub-module imports dynamically, so the explicittriton.compilermocks and their old inline comment are no longer needed."
参与讨论