Skip to main content

Experiment framework for ML/LLM workflows with reproducible run artifacts.

Project description

ztxexp

PyPI version License: MIT Python Versions

ztxexp 是一个面向深度学习和大模型实验的抽象框架,目标是让实验迭代更快、更可复现。

NEW

  • 2026-03-02 01:25:15 (Asia/Shanghai): 新增 examples/template_library 可复制模板库(27 个场景模板),覆盖基础构建、并行调度、分析清理、ML、LLM、工程运维;并接入 MkDocs 自动生成页面(示例模板库 导航)。
  • 2026-03-02 01:02:25 (Asia/Shanghai): 新增 codex-trace.md 用于记录关键工作节点,并将其维护要求持久化到 AGENTS.md;同时将该文件加入 .gitignore 以避免提交本地 trace。
  • 2026-03-02 00:58:21 (Asia/Shanghai): 在 ztxexp.utils 新增 12 个高频实验工具函数,覆盖嵌套配置处理、配置差异比较、可读 run 命名、原子写入、JSONL 读写、重试调用与批处理切分。
    • 新增函数:flatten_dictunflatten_dictdeep_merge_dictsdict_diffsanitize_filenamebuild_run_namesplit_batcheswrite_text_atomicsave_json_atomicappend_jsonlload_jsonlretry_call
  • 规则(持久化):后续每次对项目进行功能或行为更新时,都必须在本板块追加一条记录,包含“更新时间”和“更新内容”。

问题

在真实项目里,实验常见痛点是:

  • 参数空间大,配置组合容易失控。
  • 并行执行后目录结构混乱,成功/失败难追溯。
  • 结果聚合脚本碎片化,清理成本高。

方案

ztxexp 提供四个核心抽象:

  1. ExpManager: 负责配置构建(gridvariantsmodifywhere)。
  2. ExpRunner: 负责执行调度(sequential / process_pool / joblib / dynamic)。
  3. ResultAnalyzer: 负责聚合与清理。
  4. ExperimentPipeline: 一体化入口,适合绝大多数场景。

v0.30 统一了 run 产物协议(schema v2):

  • config.json
  • run.json
  • metrics.json(可选)
  • artifacts/

成功判定规则:run.json.status == "succeeded"

5 分钟跑通

安装

pip install ztxexp

可选:启用 PyTorch 辅助工具。

pip install "ztxexp[torch]"

如果你要导出 Excel 透视表:

pip install "ztxexp[excel]"

最小示例

from ztxexp import ExperimentPipeline, RunContext


def exp_fn(ctx: RunContext):
    lr = ctx.config["lr"]
    model = ctx.config["model"]

    # 业务产物统一放 artifacts 目录
    (ctx.run_dir / "artifacts" / "info.txt").write_text(
        f"run={ctx.run_id}, model={model}, lr={lr}\n",
        encoding="utf-8",
    )

    # 返回 dict 会自动写入 metrics.json
    return {"score": round((1.0 - lr) + (0.05 if model == "tiny" else 0.02), 4)}


summary = (
    ExperimentPipeline("./results_demo", base_config={"seed": 42})
    .grid({"lr": [0.001, 0.01]})
    .variants([{"model": "tiny"}, {"model": "base"}])
    .exclude_completed()
    .run(exp_fn, mode="sequential")
)

print(summary)

聚合结果

from ztxexp import ResultAnalyzer

analyzer = ResultAnalyzer("./results_demo")
df = analyzer.to_dataframe(statuses=("succeeded",))
print(df[["run_id", "model", "lr", "score"]])
analyzer.to_csv("./results_demo/summary.csv", sort_by=["model", "lr"])

示例模板库(可复制)

模板库目标是“复制后只改业务逻辑”,尽量覆盖常见 Python 实验场景:

  1. 基础构建:最小实验、网格+变体、多种子复现、manager/runner 解耦。
  2. 并行调度:process_pooljoblibdynamic、非法配置 SkipRun
  3. 结果分析:DataFrame 导出、CSV、透视表、清理策略、排行榜。
  4. ML 场景:分类、回归、时序、异常检测、推荐排序。
  5. LLM 场景:Prompt、RAG、Tool Use、安全评测、服务压测。
  6. 工程运维:消融、预算受限搜索、断点恢复、数据版本对比、复现性审计。

文档中可直接复制代码:

常见坑

  1. 返回值不是 dict | None 会被判定为失败,并写入 error.log

  2. 仍按旧版 _SUCCESS 判断成功 v0.30 不再使用 _SUCCESS,以 run.json 为准。

  3. 直接把大文件写在 run 根目录 建议统一放到 artifacts/,便于后续清理和归档。

  4. dynamic 模式用于生产实时场景 dynamic 是实验特性(experimental),更适合离线批任务。

API 导航

文档采用“源码注释驱动”模式,不再手工维护 API Markdown:

  1. mkdocs build 时自动扫描 ztxexp/*.py
  2. 自动生成首页 index.mdreference/ API 页面;
  3. mkdocstrings 从类/函数 docstring 渲染参数、返回值与示例。

本地入口:

  • 生成脚本:scripts/gen_ref_pages.py
  • 模板文档:examples-lib/(由 examples/template_library 自动生成)
  • 构建产物:docs/index.htmldocs/reference/(构建后生成)

常用命令:

pip install -e ".[docs]"
NO_MKDOCS_2_WARNING=1 mkdocs build --strict
NO_MKDOCS_2_WARNING=1 mkdocs serve
# 或使用脚本:sh mk.sh build / sh mk.sh serve

贡献

欢迎提交 Issue 或 PR:

许可证

MIT License

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ztxexp-0.3.0.tar.gz (33.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ztxexp-0.3.0-py3-none-any.whl (30.3 kB view details)

Uploaded Python 3

File details

Details for the file ztxexp-0.3.0.tar.gz.

File metadata

  • Download URL: ztxexp-0.3.0.tar.gz
  • Upload date:
  • Size: 33.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.7

File hashes

Hashes for ztxexp-0.3.0.tar.gz
Algorithm Hash digest
SHA256 b411f773638d6305555f0d190147952e23d6eedb9a071dd75ac9f92a2a850e3f
MD5 3b0a14ff950f36805a96c3d9435ede88
BLAKE2b-256 cd9fbeddfc4cf7fba75ef8d9cf58f596ec3382a45d69349ac1d90ad2d5e9ff25

See more details on using hashes here.

File details

Details for the file ztxexp-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: ztxexp-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 30.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.7

File hashes

Hashes for ztxexp-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 eed51699c05b52095695a9769a756707405d9b3e5659d9f3fe1a65357c8d1300
MD5 c8f70f5e4e96d24feedfa61cacbd176d
BLAKE2b-256 54e37867e92b5181199f7f3a843bad351e278a97a899079f1735c3329dea7087

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page