Skip to content

feat(pixel-grid): add independent native grid detector - #493

Closed
huyanxius wants to merge 9 commits into
1024XEngineer:mainfrom
huyanxius:feat/490-pixel-grid-detector
Closed

feat(pixel-grid): add independent native grid detector#493
huyanxius wants to merge 9 commits into
1024XEngineer:mainfrom
huyanxius:feat/490-pixel-grid-detector

Conversation

@huyanxius

@huyanxius huyanxius commented Aug 20, 2026

Copy link
Copy Markdown
Collaborator

新增独立 Rust 像素网格识别库,为后续 PyO3 工具 API 提供稳定的六字段检测契约;算法源码位于仓库根目录,不属于 Python 后端,也不进入任何生成或工作流调用链。

Why

模型生成的像素风图片通常仍存在隐含网格、抗锯齿和非整数像素单元。重建模块需要先获得可替换、可验证的网格估计,同时 Rust 算法不应被归入 Python 后端目录或通过生产 CLI 暴露。

Changes

  • 新增独立 Rust library,读取 PNG/JPEG 字节并返回 colsrowsstep_xstep_yconsensusconfidence
  • 复用 Pixel Art Fixer 固定提交的多检测器共识算法,覆盖快速一致、三方共识和仲裁路径。
  • 在解码前限制 32MiB 输入、4MP 像素数与解码分配;任一轴 <3px 时统一标记为低置信度。
  • 将模块放置在根目录 native/pixel-perfect/crates/detector/,删除生产 stdin/stdout CLI 和 JSON 依赖。
  • 更新独立 path-filter CI、许可证、固定上游版本和测试 fixture 溯源。

Implementation

  • detect_bytes 是唯一算法入口,DetectorMode::FullDetectorMode::Fast 保持显式区分。
  • 检测器使用 crate-local manifest 与 lockfile,可在其他原生模块尚未合入时独立构建;本 crate 不依赖 Python、Windup 应用或其他像素模块。
  • Python 原生扩展与工具 API 由 feat(pixel-perfect): expose an independent tool API #492 单独交付。

Verification

  • cargo fmt --check:通过。
  • cargo test --release --locked:5 项测试通过,0 项失败。
  • git diff --check upstream/main...HEAD:通过。
  • GitHub Actions:等待当前 head 完成。

Scope

Related Issues

Closes #490

Refs #489

Pixel-style images need a reusable grid estimate before reconstruction can run.

Port the fixed upstream consensus detector behind bounded PNG/JPEG bytes and a six-field JSON CLI.

The detector now runs independently without importing the application or generation pipeline.
The native detector needs a reproducible package boundary and dependency graph.

Add the Rust manifest, locked dependency set, and local target exclusion.

The module can now be built in isolation with deterministic dependency resolution.
The detector contract needs evidence for both simple grids and disputed inputs.

Cover arbitration, sub-three-pixel confidence, JSON fields, and bounded encoded input.

Regressions in consensus execution or resource handling now fail at the module boundary.
The reused detector must remain reviewable and license compliant.

Document the standalone CLI, capability boundary, fixed upstream revision, and MIT terms.

Maintainers can trace both the algorithm and its arbitration fixture to their source.
The standalone native module needs a gate that does not widen backend CI.

Run format and locked release tests only when detector paths or its workflow change.

Detector changes now receive isolated validation without touching generation jobs.
@vercel

vercel Bot commented Aug 20, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

1 Skipped Deployment
Project Deployment Actions Updated (UTC)
windup Ignored Ignored Preview Aug 21, 2026 3:56am

@codecov

codecov Bot commented Aug 20, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@fennoai fennoai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

已完成对固定 0b1e5acd6fa62988dff453161cb09773ecdc0731...baa1eb679134341f3a5d8397d24f99e8aec76f21 完整变更范围的审查,覆盖 CI path filter、Rust crate 边界、PNG/JPEG 解码与资源限制、full/fast 检测路径、六字段 JSON 契约以及测试 fixture。未发现满足审查标准的可操作 P0–P3 正确性、可靠性、安全性或性能问题。

验证:远端 PR checks 中 testlint-and-testcodecov/patch 均通过;固定 diff git diff --check 通过。当前审查环境未安装 Cargo,因此未在本地重复运行 Rust 测试。

@xiaocheny214 xiaocheny214 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@huyanxius 这是rust语言吗?虽然rust语言性能很高......但这怎么接入后端是python语言的项目中?这个对外提供的api接口我也没有看到👀

@huyanxius

Copy link
Copy Markdown
Collaborator Author

@huyanxius 这是rust语言吗?虽然rust语言性能很高......但这怎么接入后端是python语言的项目中?这个对外提供的api接口我也没有看到👀

api 接口我特地没定,交给你们后端来定吧

@huyanxius 这是rust语言吗?虽然rust语言性能很高......但这怎么接入后端是python语言的项目中?这个对外提供的api接口我也没有看到👀

目前决定采用PyO3 原生扩展,Rust 源码仍放仓库根目录 native/pixel-perfect,不放进 Python backend;通过 maturin 编译成 Python 可导入的原生模块,后端只保留调用适配层

The detector is a native algorithm library rather than Python backend code.

Move the crate into the root native workspace and remove the retired CLI boundary.

Keep the detector independently buildable for the later PyO3 binding.
The detector CI still watched the retired backend-native path.

Point path filters and the working directory at the root native workspace.

Detector checks now run for the relocated crate.
@huyanxius huyanxius changed the title feat(pixel-grid): add standalone grid detector feat(pixel-grid): add independent native grid detector Aug 21, 2026
@huyanxius

Copy link
Copy Markdown
Collaborator Author

这个问题成立,已按评审调整边界:Rust 检测器已从 backend/native 移到仓库根目录 native/pixel-perfect/crates/detector,本 PR 只交付纯 Rust library。Python 接入不会转写算法,也不再走子进程;#492 将通过 PyO3/maturin 编译原生扩展,由现有 Python 后端直接导入,并在 #496 交付独立 /tools/pixel-perfect API。三个 PR 仍按检测器、重建器、绑定与 API 分开评审,均不进入生成管线。设计变更记录:#490 (comment)

A shared workspace would couple later pull requests before their merge order is established.

Keep the detector lockfile with its crate and remove the premature workspace manifest.

Each native module can now build and land independently.
The detector no longer builds through a shared native workspace.

Run its workflow from the crate directory with the crate-local lockfile.

CI now matches the independently reviewable module boundary.
xyh202131
xyh202131 previously approved these changes Aug 21, 2026

@xyh202131 xyh202131 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

已复核当前提交:原生像素网格检测器保持独立库边界,输入大小、像素量与解码资源限制明确,六字段检测契约和 full/fast 路径均有测试覆盖;当前 CI 全部通过且无未解决审查线程。

xyh202131
xyh202131 previously approved these changes Aug 21, 2026

@xyh202131 xyh202131 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

按请求批量审批当前提交。

@xyh202131
xyh202131 dismissed stale reviews from themself August 21, 2026 09:16

按请求撤销此前审批。

@minorcell

Copy link
Copy Markdown
Member

@fennoai

这是在做什么?看起来对架构有极大的调整?

@fennoai

fennoai Bot commented Aug 24, 2026

Copy link
Copy Markdown
Contributor

结论

这条 PR 不是在迁移或重构现有 Windup 架构,而是在仓库根目录新增一个目前完全隔离的 Rust library,作为后续像素重建能力的底层检测组件。

  • 新增内容native/pixel-perfect/crates/detector/,包含 PNG/JPEG 解码、像素网格检测算法、测试 fixture 和独立 Cargo.lock
  • 唯一公共入口detect_bytes;输出 colsrowsstep_xstep_yconsensusconfidence 六个字段。
  • 当前没有接入生产链路:固定 SHA 差异中没有修改 backend/frontend/、现有 CLI、Docker、Python API 或默认生成流程;仓库内也没有其他模块引用这个 crate。
  • 没有建立 Rust workspace:它使用 crate-local Cargo.toml 和 lockfile,可以在其他 native 模块尚未合入时单独构建。
  • CI 也是隔离的:新增 workflow 只在 native/pixel-perfect/** 变化时运行 cargo fmt --checkcargo test --release --locked,不会改变现有工作流的执行路径。

看起来“架构调整很大”的主要原因是算法本身被完整拆成了多个 Rust 模块,共新增约 4,879 行、21 个文件;这些代码主要是 Pixel Art Fixer 的多检测器共识算法及 Windup 的输入边界封装,不代表应用层被重写。

后续真正的集成点

后续 #492 才会提供 PyO3 binding 和工具 API,届时才会把这个 Rust crate 接到 Python/应用侧;#491 则负责网格重建。换句话说,这个 PR 更像是先把“检测内核”单独落地,刻意避免现在就扩大生产架构的耦合面。

我也按 PR 指定的固定提交范围 0b1e5acd6fa62988dff453161cb09773ecdc0731...0e07222d7eb432d5de92f18dd961383f26bf2574 核对过,git diff --check 无输出,未发现超出上述边界的文件变更。

View job run

@huyanxius

Copy link
Copy Markdown
Collaborator Author

@fennoai

这是在做什么?看起来对架构有极大的调整?

@minorcell 这个是新增了一个完美像素化的检测模块,刻意没有动架构,但是按照和导师讨论需要把完美像素化接入生成像素风格的生成管线,所以这里架构会有一个变动

@fennoai

fennoai Bot commented Aug 24, 2026

Copy link
Copy Markdown
Contributor

结论

明白了:当前 PR 是先落地“完美像素化”的检测内核,后续再把它接入生成像素风格的管线。所以你说的架构变动是预期中的下一阶段变化,但不发生在这条 PR 的当前差异里。

我按固定提交范围 0b1e5acd6fa62988dff453161cb09773ecdc0731...0e07222d7eb432d5de92f18dd961383f26bf2574 重新确认过:当前只新增 native/pixel-perfect/crates/detector/ 和独立 CI,没有修改 backend/frontend/、现有生成入口、Docker 或默认 workflow 调度。

推荐的接入分层

后续接入时建议保留下面的边界,而不是让 Rust 检测算法直接依赖生成管线:

  1. Rust detector 层:继续只负责 PNG/JPEG bytes -> 六字段检测结果,保持无 Python、无数据库、无任务队列依赖。
  2. 原生绑定层:由 feat(pixel-perfect): expose an independent tool API #492 的 PyO3/tool API 把 Rust 结果转换成 Python 可消费的稳定 DTO,并统一处理错误、超时和资源限制。
  3. 生成编排层:在现有生成流程中增加一个可配置的 pixel-perfect 阶段,例如“生成结果 -> 检测网格 -> 按策略重建/修正”,而不是把检测逻辑复制到 Python。
  4. 产品策略层:决定何时启用、检测失败是否降级、低置信度是否要求用户确认,以及是否允许用户覆盖 step_x/step_y

这样架构确实会新增一条处理链,但属于新增适配器和生成阶段,不是把现有后端整体迁移到 Rust。建议后续 PR 把“是否启用完美像素化”和“检测/重建失败时的 fallback”作为显式配置,避免默认改变所有图片生成行为。

对这条 PR 的判断

因此我会把 #493 理解为一个合理的基础设施 PR:先建立可独立测试、可替换的检测契约;真正需要重点评审架构影响的是后续 #491/#492 以及生成管线接入 PR,包括调用位置、任务耗时、失败降级和结果缓存。

View job run

@huyanxius
huyanxius requested review from minorcell and nighca August 24, 2026 05:53
@nighca

nighca commented Aug 24, 2026

Copy link
Copy Markdown
Contributor

@minorcell 这个是新增了一个完美像素化的检测模块,刻意没有动架构,但是按照和导师讨论需要把完美像素化接入生成像素风格的生成管线,所以这里架构会有一个变动

@huyanxius 如果按 #489 这里的结论,我们应该不需要一个“grid detector”模块?因为目标分辨率是已知的

@huyanxius

Copy link
Copy Markdown
Collaborator Author

@minorcell 这个是新增了一个完美像素化的检测模块,刻意没有动架构,但是按照和导师讨论需要把完美像素化接入生成像素风格的生成管线,所以这里架构会有一个变动

@huyanxius 如果按 #489 这里的结论,我们应该不需要一个“grid detector”模块?因为目标分辨率是已知的

我觉得这个可能得做一下测试?因为图片生成模型很可能并不会按照我们目标像素生成,所以这里的grid detector我觉得可以作为一个检测工具校验模型输出是否符合已知目标网格,否则如果:
项目目标:32×32
模型隐含网格:256×256
完美像素化沿用项目目标:32×32
就会出现明显问题

@nighca

nighca commented Aug 24, 2026

Copy link
Copy Markdown
Contributor

因为图片生成模型很可能并不会按照我们目标像素生成

@huyanxius 这个是大概率会发生的。只是即使我们检测了,我们也没法基于检测结果做什么?

@huyanxius

Copy link
Copy Markdown
Collaborator Author

因为图片生成模型很可能并不会按照我们目标像素生成

@huyanxius 这个是大概率会发生的。只是即使我们检测了,我们也没法基于检测结果做什么?

对,但是可以根据生图模型实际的像素来进行完美像素化……不过好像无济于事,因为他不是项目约束了
只是如果不这样的话,大概率会得到一张没法用的资产。这个grid detector能保证“完美像素化后的资产是切实可用的,即使他不是项目所约束的像素密度”,基于我们不会再给用户提供非完美像素化的资产,我觉得这样倒是有必要的?

@nighca

nighca commented Aug 24, 2026

Copy link
Copy Markdown
Contributor

只是如果不这样的话,大概率会得到一张没法用的资产。

@huyanxius 以上面具体的 256x256、32x32 为例,如果用户指定了 32x32,即使图片生成模型遵循得不好,我们给他 32x32 也要比给他 256x256 更好。

32x32 的问题是生成的图片遵循不好导致在降分辨率后人物形象的识别率低,但不是完全不可用

而 256x256 的问题是,在游戏中实际渲染时

  • 要么按 256x256 渲染,那这几乎是不可用的(比视觉上的识别率低更差),因为分辨率不同而跟游戏整体视觉上不匹配
  • 要么按 32x32 渲染,那么上面说的人物形象识别率低的问题同样也存在

可以看到,不管哪个情况,256x256 都不会比 32x32 更好。

这个grid detector能保证“完美像素化后的资产是切实可用的,即使他不是项目所约束的像素密度”,基于我们不会再给用户提供非完美像素化的资产,我觉得这样倒是有必要的?

如果不是项目要的像素密度,那它就不是“完美像素”的。如果用户对这个的预期是 32x32,那么我们自动识别并处理后的 256x256 并不会比原图(如 1024x1024)更符合用户需求

@huyanxius

Copy link
Copy Markdown
Collaborator Author

只是如果不这样的话,大概率会得到一张没法用的资产。

@huyanxius 以上面具体的 256x256、32x32 为例,如果用户指定了 32x32,即使图片生成模型遵循得不好,我们给他 32x32 也要比给他 256x256 更好。

32x32 的问题是生成的图片遵循不好导致在降分辨率后人物形象的识别率低,但不是完全不可用

而 256x256 的问题是,在游戏中实际渲染时

  • 要么按 256x256 渲染,那这几乎是不可用的(比视觉上的识别率低更差),因为分辨率不同而跟游戏整体视觉上不匹配
  • 要么按 32x32 渲染,那么上面说的人物形象识别率低的问题同样也存在

可以看到,不管哪个情况,256x256 都不会比 32x32 更好。

这个grid detector能保证“完美像素化后的资产是切实可用的,即使他不是项目所约束的像素密度”,基于我们不会再给用户提供非完美像素化的资产,我觉得这样倒是有必要的?

如果不是项目要的像素密度,那它就不是“完美像素”的。如果用户对这个的预期是 32x32,那么我们自动识别并处理后的 256x256 并不会比原图(如 1024x1024)更符合用户需求

ok,那这个 PR 关了吗?这样看来就没有grid detector的必要了

@johnnyzhang-eng

Copy link
Copy Markdown
Contributor

在 21 张人工标注为像素画的真实生产母版上量了隐含网格:逻辑高 4223、中位 92,跨度 55.8 倍;若项目目标 32px,降采样倍率落在 0.17.0 倍、只有 2/21 是整数倍——不是「隐含 256、目标 32」这种固定偏差,每张都不一样,所以目标分辨率已知并不能定下这一步怎么采。另外现有的 detect_pixel_size 在这批上不可靠:11/21 返回 1(检测不出网格),还有 1 张把 107×199、6783 色的主体判成 4 像素高。

@nighca

nighca commented Aug 24, 2026

Copy link
Copy Markdown
Contributor

在 21 张人工标注为像素画的真实生产母版上量了隐含网格:逻辑高 4223、中位 92,跨度 55.8 倍;若项目目标 32px,降采样倍率落在 0.17.0 倍、只有 2/21 是整数倍——不是「隐含 256、目标 32」这种固定偏差,每张都不一样,所以目标分辨率已知并不能定下这一步怎么采

@johnnyzhang-eng 没理解,每张的“隐含网格”不同为什么会导致不知道怎么采?图片原分辨率我们是知道的,目标分辨率我们也是知道的,那降分辨率的采样行为不就是确定的吗?

@nighca

nighca commented Aug 24, 2026

Copy link
Copy Markdown
Contributor

ok,那这个 PR 关了吗?这样看来就没有grid detector的必要了

嗯,我没问题

@huyanxius

Copy link
Copy Markdown
Collaborator Author

根据讨论,不再引入grid detector,PR 关闭

@huyanxius huyanxius closed this Aug 24, 2026
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.

feat(pixel-grid): add an independent grid detector

6 participants