Prhub

#47755 [Bug] Fix tmp directory for `lm_eval`

原始 PR 作者 yewentao256 合并时间 2026-07-08 00:40 文件变更 1 提交数 2 评论 0 代码增减 +2 / -1

执行摘要

修复 GSM8K 评测硬编码 /tmp 权限问题

运行 GSM8K 评测时,由于 download_and_cache_file 函数硬编码缓存路径为 /tmp,在某些没有写入权限的环境中会抛出 PermissionError: [Errno 13] Permission denied: '/tmp/train.jsonl'。PR 作者在 body 中给出了完整的报错堆栈。

该 PR 变更简单清晰,风险低,建议快速合并。可关注类似硬编码路径问题是否存在于其他评测配置中。

讨论亮点

无实质性 Review 讨论。LucasWilkinsonAndreasKaratzas 直接批准,Claude Bot 是自动回复。

实现拆解

  1. 添加导入:在 tests/evals/gsm8k/gsm8k_eval.py 中新增 import tempfile
  2. 替换硬编码路径:在 download_and_cache_file 函数中,当 filenameNone 时,将原来的 os.path.join("/tmp", url.split("/")[-1]) 改为 os.path.join(tempfile.gettempdir(), url.split("/")[-1]),从而使用平台推荐的临时目录。
  3. 测试验证:PR 作者在 body 中展示测试已通过。
文件 模块 状态 重要度
tests/evals/gsm8k/gsm8k_eval.py 评测工具 modified 3.68

关键符号

download_and_cache_file

关键源码片段

tests/evals/gsm8k/gsm8k_eval.py test-coverage

唯一变更文件,修复硬编码 `/tmp` 为 `tempfile.gettempdir()`,提升便携性。

import os
import tempfile # 新增:使用平台临时目录而非硬编码 /tmpdef download_and_cache_file(url: str, filename: str | None = None) -> str:
    """Download and cache a file from a URL."""
    if filename is None:
        # 旧代码:os.path.join("/tmp", url.split("/")[-1])
        # 新代码:使用 tempfile.gettempdir() 避免权限问题
        filename = os.path.join(tempfile.gettempdir(), url.split("/")[-1])
​
    if os.path.exists(filename):
        return filename
​
    print(f"Downloading from {url} to {filename}")
    response = requests.get(url, stream=True)
    response.raise_for_status()
​
    with open(filename, "wb") as f:
        for chunk in response.iter_content(chunk_size=1024):
            f.write(chunk)
​
    return filename

评论区精华

没有提炼出高价值讨论线程

当前评论区没有形成足够清晰的争议点或结论,后续有更多讨论时会体现在这里。

风险与影响

风险极低。tempfile.gettempdir() 在大多数环境中返回 /tmp,因此不会有行为变化,且可移植性更强。唯一需要注意的风险是并发测试可能写入相同文件,但原始 download_and_cache_file 已有 os.path.exists 检查,因此风险可控。

影响范围极小,仅涉及 GSM8K 评测工具的缓存路径,不影响其他模块。用户无需修改任何配置即可在无 /tmp 写入权限的环境中正常运行评测。

关联 Issue

未识别关联 Issue

当前没有检测到明确关联的 Issue 链接,后续同步到相关引用后会出现在这里。

完整报告

参与讨论