Skip to content

增加算子库基准日志执行器#34

Open
ghangz wants to merge 2 commits into
MetaX-MACA:mainfrom
ghangz:mengz/mcoplib-benchmark-log-runner
Open

增加算子库基准日志执行器#34
ghangz wants to merge 2 commits into
MetaX-MACA:mainfrom
ghangz:mengz/mcoplib-benchmark-log-runner

Conversation

@ghangz

@ghangz ghangz commented Jun 10, 2026

Copy link
Copy Markdown

这次改动补上了算子库基准日志执行器,主要是为了解决算子库构建和诊断流程里相关信息不够集中、人工整理成本较高的问题,让日常排查、验证和结果归档更直接。

实现上补充了对应工具或脚本逻辑,补上了对应测试,同时尽量保持现有用法不变,避免影响已有流程。

这一分支已经在沐曦算力环境完成实际验证,相关检查均已通过,现提交合入。

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a utility script tools/run_benchmark_with_logs.py and its corresponding unit tests to run mcoplib benchmarks and save structured logs (including metadata, standard output, standard error, and exit codes). The code review feedback highlights a potential path traversal vulnerability where the user-supplied --op parameter is used directly in directory paths, and suggests sanitizing it. Additionally, suggestions were made to record extra environment variables (such as CUDA_VISIBLE_DEVICES and PYTHONPATH) for better reproducibility, and to set ensure_ascii=False when writing the JSON metadata to preserve non-ASCII characters.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment on lines +25 to +28
def run_with_logs(root: Path, op: str, log_root: Path, extra_args: list[str]) -> int:
run_id = datetime.now(timezone.utc).strftime("%Y%m%dT%H%M%SZ") + f"_{op}"
run_dir = log_root / run_id
run_dir.mkdir(parents=True, exist_ok=True)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

security-high high

安全风险:路径穿越漏洞 (Path Traversal)

由于 --op 参数是由用户输入的,并且直接用于拼接生成 run_idrun_dir 路径:

run_id = datetime.now(timezone.utc).strftime("%Y%m%dT%H%M%SZ") + f"_{op}"
run_dir = log_root / run_id

如果用户传入类似于 ../../etc 的恶意算子名称,可能会导致程序在预期的日志目录之外创建目录并写入 metadata.json 等文件。

改进建议:

  1. 限制 op 仅包含字母、数字和下划线,防止路径穿越。
  2. 在创建日志目录之前,先校验基准测试脚本 mcoplib_mxbenchmark_ops.py 是否存在,避免在路径不正确时创建空的/脏的日志目录。
def run_with_logs(root: Path, op: str, log_root: Path, extra_args: list[str]) -> int:
    if not op or not all(c.isalnum() or c == "_" for c in op):
        print(f"Error: Invalid operator name '{op}'. Only alphanumeric characters and underscores are allowed.", file=sys.stderr)
        return 1

    benchmark_dir = root / "benchmark"
    script_path = benchmark_dir / "mcoplib_mxbenchmark_ops.py"
    if not script_path.is_file():
        print(f"Error: Benchmark script not found at '{script_path}'", file=sys.stderr)
        return 1

    run_id = datetime.now(timezone.utc).strftime("%Y%m%dT%H%M%SZ") + f"_{op}"
    run_dir = log_root / run_id
    run_dir.mkdir(parents=True, exist_ok=True)

Comment on lines +36 to +40
"environment": {
"MACA_PATH": os.environ.get("MACA_PATH"),
"CUDA_HOME": os.environ.get("CUDA_HOME"),
"LD_LIBRARY_PATH": os.environ.get("LD_LIBRARY_PATH"),
},

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

可维护性与可复现性改进:记录关键 GPU 环境变量

在进行算子基准测试时,CUDA_VISIBLE_DEVICESMACA_VISIBLE_DEVICES 决定了具体使用哪张 GPU 卡,而 PYTHONPATH 决定了 Python 模块的搜索路径。这些环境变量对于复现测试结果和排查问题至关重要。

建议在 metadata.jsonenvironment 中补充记录这些关键环境变量。

        "environment": {
            "MACA_PATH": os.environ.get("MACA_PATH"),
            "CUDA_HOME": os.environ.get("CUDA_HOME"),
            "LD_LIBRARY_PATH": os.environ.get("LD_LIBRARY_PATH"),
            "PYTHONPATH": os.environ.get("PYTHONPATH"),
            "CUDA_VISIBLE_DEVICES": os.environ.get("CUDA_VISIBLE_DEVICES"),
            "MACA_VISIBLE_DEVICES": os.environ.get("MACA_VISIBLE_DEVICES"),
        },

Comment thread tools/run_benchmark_with_logs.py Outdated
"LD_LIBRARY_PATH": os.environ.get("LD_LIBRARY_PATH"),
},
}
(run_dir / "metadata.json").write_text(json.dumps(metadata, indent=2), encoding="utf-8")

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

可读性改进:支持非 ASCII 字符直观显示

在写入 metadata.json 时,默认的 json.dumps 会将非 ASCII 字符(例如中文路径或环境变量中的中文字符)转义为 \uXXXX 格式。

由于写入文件时已经指定了 encoding="utf-8",建议设置 ensure_ascii=False,这样可以直接保存人类可读的非 ASCII 字符,方便在中文环境下排查问题。

Suggested change
(run_dir / "metadata.json").write_text(json.dumps(metadata, indent=2), encoding="utf-8")
(run_dir / "metadata.json").write_text(json.dumps(metadata, indent=2, ensure_ascii=False), encoding="utf-8")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant