Skip to content

docs(rfc): move model-topology logic from C++ to Python (design-only) - #133

Merged
drunkcoding merged 13 commits into
mainfrom
refactor/topology-logic-python-extraction
Aug 9, 2026
Merged

docs(rfc): move model-topology logic from C++ to Python (design-only)#133
drunkcoding merged 13 commits into
mainfrom
refactor/topology-logic-python-extraction

Conversation

@drunkcoding

Copy link
Copy Markdown
Contributor

RFC (design-only, pre-implementation)

This is a draft, design-only PR opened before implementation, per request. It contains only the plan doc at docs/rfcs/topology-logic-python-extraction.md. No source code is changed yet.

Summary

Reduce the amount of model-structure semantics computed inside the C++ hot-path module core/model/model_topology.cpp, moving it into Python (where the topology is already built in get_topology()), without moving any performance-critical runtime lookup or any memory/IO work.

Scoped extraction: Python computes is_sparse + packed corr_id per node and passes them through a new set_topology_v2 binding; C++ stops re-deriving those from loop indices but keeps everything else — byte_size from kTensorIndex, GPU device placement, kHostMemoryPool allocation, partition reads, SetDevice, and the uint32_t TensorID -> Node* runtime lookups (GetNodeFromTensorID, GetNodeBodyFromCorrID).

Why this scope (honest caveat)

Analysis shows the movable surface is small and coupled to the memory-allocation loop it lives in. InitializeTopology interleaves trivial index math (movable) with memory/IO work (must stay C++). The value is separation of concerns / single source of truth for model semantics, not performance. Section 8 of the plan includes an explicit decision gate — including a "do not refactor" option.

Status

  • Momus plan review: APPROVED (all file/line references verified, every task has executable QA).
  • BLOCKER: the build/verify tasks (rebuild extension + smoke generation) are blocked because /mnt/raid0nvme0 is at 100% (≈2 GB free of 14 TB). A from-source CUDA/C++ rebuild will fail with ENOSPC until space is freed. This PR is therefore design-only until disk is available.

Non-goals

No changes to on-disk format, name_id_map.json, checkpoint layout, or the numeric values of corr_id/is_sparse (only where they are computed). All changes are additive (v2 binding alongside v1) for trivial rollback.

See the committed plan for the full task breakdown, verification matrix, risks, and rollback.

drunkcoding and others added 9 commits August 5, 2026 12:14
Scoped v2 extraction: Python computes is_sparse/corr_id, passed via a new set_topology_v2 binding; C++ keeps all memory/IO and the uint32_t TensorID runtime lookups. Momus-approved, design-only pre-implementation RFC.

Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent)

Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
Extract InitializeTopology's pipeline construction into BuildTopologyFromSpecs and add InitializeTopologyV2 that reads is_sparse and corr_id from the caller instead of deriving them from loop indices. v1 path preserved.

Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent)

Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
Forward the enriched (name, is_sparse, groups, corr_ids) topology from Python to InitializeTopologyV2; set_topology (v1) kept intact.

Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent)

Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
get_topology results are lowered to the v2 tuple and sent via set_topology_v2, mirroring the C++ bit-packing exactly.

Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent)

Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
Remove the (name, groups) InitializeTopology/SetTopology path and its binding; the (name, is_sparse, groups, corr_ids) form is now the only set_topology. The signature change spans header+impl+binding so it lands atomically.

Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent)

Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
Extract is_sparse/corr_id lowering into moe_infinity/utils/topology.build_topology_specs and call the single set_topology; drops the inline _build_topology_v2 method.

Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent)

Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
Covers dense/sparse flag, structural corr_id packing, last-stage 0xFFFFFFFF marker, and equality with an independent reference.

Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent)

Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent)

Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
@drunkcoding
drunkcoding marked this pull request as ready for review August 5, 2026 14:12
drunkcoding added 2 commits August 9, 2026 11:15
Signed-off-by: drunkcoding <leyang.xue@ed.ac.uk>
Signed-off-by: drunkcoding <leyang.xue@ed.ac.uk>
@drunkcoding

Copy link
Copy Markdown
Contributor Author

Scope decision required for d0ffd65:

A. Accept v1 removal: smaller surface, but breaking API and no rollback/parity oracle.
B. Restore v1 unused: shared builder, executable v1/v2 parity, and one-line rollback.

Recommendation and implemented default: B, matching RFC T9. Production uses set_topology_v2; set_topology remains an unused tested fallback.

drunkcoding added 2 commits August 9, 2026 11:16
Signed-off-by: drunkcoding <leyang.xue@ed.ac.uk>
Signed-off-by: drunkcoding <leyang.xue@ed.ac.uk>
@drunkcoding
drunkcoding merged commit af11723 into main Aug 9, 2026
8 checks passed
@drunkcoding
drunkcoding deleted the refactor/topology-logic-python-extraction branch August 9, 2026 18:42
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