[connector/manager] refactor connector write protocol & add spec group lookup in instanceInfo - #127
[connector/manager] refactor connector write protocol & add spec group lookup in instanceInfo#127Tyndalllll wants to merge 2 commits into
Conversation
- Added a new method `FindSpecGroup` in `InstanceInfo` for efficient O(log n) lookup of `LocationSpecGroup` by name using `std::string_view`.
There was a problem hiding this comment.
👋 Review Summary
Nice refactor -- centralizing the binary-search lookup into FindSpecGroup, splitting the init path into focused helpers, and unifying the v1/v2 cross-rank write protocol makes the codebase meaningfully easier to follow. The fail-fast validation in StartWriteCache and the explicit EC_INSTANCE_NOT_EXIST error for missing instances are both welcome improvements over the previous silent-fallthrough behavior.
🛡️ Key Risks & Issues
_start_write divergence hash is incomplete (inline comment on connector.py:834)
The shared _start_write helper hashes block_keys and extra_fields to detect cross-rank input divergence, but omits spec_group_names. This is safe for all current call sites (groups are deterministic from uniform class state), but the helper is documented as reusable -- a future caller with per-rank differentiated groups would silently pass divergence detection. Low urgency, but worth closing now while the code is fresh.
🧪 Verification Advice
- The
std::includeschange inselect_location_policy.ccrelies onrequested_spec_namesbeing sorted. This invariant holds because all production callers passgroup->spec_names(), whichLocationSpecGroup::SortSpecNames()guarantees. Consider adding a debug-modeassert(std::is_sorted(...))at the top ofExistsForWriteto catch any future caller that breaks this precondition. - The v2 write path (
batch_set_v2) now mirrors v1's per-block MIN all_reduce andvalid_save_masksemantics. A targeted integration test that simulates a multi-rank v2 write where one rank has fewer local blocks than rank 0 would exercise thevalid_save_mask/prefix_write_countinteraction infinish_mask.
💡 Thoughts & Suggestions
- The
FilterWriteCacherefactor to branch once onlocation_spec_group_names.empty()and then assign two different lambdas is a clean pattern -- it avoids the per-block branch the old code had. Good call capturinginstance_infoby value in the lambda (keeps theshared_ptralive) while letting the rest capture by reference. FindSpecGroupcorrectly mirrors theSortLocationSpecGroupscomparator (a.name() < b.name()vsg.name() < n). Theconst LocationSpecGroup*return type is a natural fit for the "nullable lookup" pattern.- Minor note:
IsSpecNameInSpecGroupstill passesgroup_name.data()toPREFIX_LOGwith%s(line 100). Since the parameter isstd::string_view,data()is not guaranteed null-terminated by the standard (even though it will be in practice fromstd::string-sourced views). TheStartWriteCachelog on line 485 already uses the safer%.*spattern -- it might be worth aligningIsSpecNameInSpecGroupto match for consistency.
🤖 Generated by Qoder • View workflow run
| f"on MLA model; skipping write.") | ||
| return None, {name: val for name, val in extra_fields}, True | ||
|
|
||
| local_hash = hash(tuple(block_keys) + tuple(v for _, v in extra_fields)) |
There was a problem hiding this comment.
The divergence-detection hash here covers block_keys and extra_fields, but the spec_group_names argument is excluded from the hash. Since _start_write is designed as the shared cross-rank primitive for both v1 and v2, a future caller that passes per-rank differentiated spec groups would silently pass divergence detection -- ranks would proceed with mismatched manager state, potentially leading to storage corruption or cross-rank gloo hangs.
For the current v1/v2 call sites this is harmless because spec_group_names is derived deterministically from class state that is uniform across all ranks. But since _start_write is explicitly documented as a reusable helper, including tuple(spec_group_names) in the hash would close this latent gap:
local_hash = hash(tuple(block_keys) + tuple(spec_group_names) + tuple(v for _, v in extra_fields))Not blocking -- just a defensive improvement for when this helper gets reused.
🤖 Generated by Qoder • Fix in Qoder
Summary
Unify v1/v2 write protocol in the SGLang connector and speed up spec group lookup on the manager side.
Commits
1.
[manager] enhance instance info and cache manager with spec group lookupInstanceInfo::FindSpecGroup— lookup viastd::lower_bound.IsSpecNameInSpecGroupto takeInstanceInfo&and reuse the new lookup.StartWriteCache/FilterWriteCachewith fail-fast branches keyed on whetherlocation_spec_group_namesis provided.all_of/any_ofwithstd::includes(pre-sorted) inselect_location_policy.cc.2.
[connector] refactor sglang connector for modularity and v2 cross-rank safetyShared write-protocol helpers (used by both v1 and v2):
_start_write: rank 0 issues the request and broadcasts(result, hash, extras); non-rank-0 validates the hash →skip_transfer; MLA non-rank-0 short-circuits._finish_write: rank 0 commits; returnsFalseon failure so callers can fall back._sync_per_block_flags: per-blockMINall_reduce; skipped for MLA / single rank.Align v2 cross-rank semantics with v1:
valid_save_maskso each rank writes only blocks it actually has local data for.prefix_write_countand prependFalses tofinish_mask, matching v1.Other:
_init_kvcm_clientinto_register_pool_specs/_register_instance/_init_transfer_client.location_spec_groupsnot sent to older managers).Risk
location_spec_groupsonly sent when extra pools exist, so older managers are unaffected.