Consolidate exact trainer-serving parity - #57
Conversation
Broly Security ScanNote Summary 3 actionable finding(s) in this PR
All actionable items are in the table below. No finding is at or above
Dismiss false positivesTick a box to dismiss the finding; untick it to bring the finding back. That is the same as replying
Note Re-scan this PR anytime with
|
b0ad7c3 to
6a62d38
Compare
6a62d38 to
2bf6fe0
Compare
| def _validate_r3_source_path(raw: Any, *, final: bool) -> Path: | ||
| path = Path(str(raw or "")) | ||
| if not path.is_absolute() or (final and path.name.startswith(".")): | ||
| raise ValueError(f"R3 source path must be an absolute payload path: {path}") | ||
| configured = os.getenv("XORL_R3_SHARED_ROOTS", "") | ||
| roots = [ | ||
| Path(entry).expanduser().resolve(strict=True) for entry in configured.split(os.pathsep) if entry.strip() | ||
| ] | ||
| if not roots: | ||
| raise ValueError("XORL_R3_SHARED_ROOTS must name the trusted SGLang side-channel root") | ||
| parent = path.parent.resolve(strict=True) | ||
| if not any(parent == root or root in parent.parents for root in roots): | ||
| raise ValueError(f"R3 source path is outside XORL_R3_SHARED_ROOTS: {path}") | ||
| return path |
7ea50ca to
3b0ccb2
Compare
3b0ccb2 to
19c4328
Compare
| def _load_sglang_file_routing_slice(self, value: Mapping[str, Any], start: int, count: int) -> List[torch.Tensor]: | ||
| if value.get("format") != "spans": | ||
| raise ValueError("SGLang R3 source reference must use spans format") | ||
| kind = str(value.get("kind", "")) | ||
| expected_dtype = torch.int32 if kind == "routed_experts" else torch.float32 | ||
| expected_dtype_name = "int32" if kind == "routed_experts" else "float32" | ||
| items = value.get("items") | ||
| total = int(value.get("count", -1)) | ||
| if kind not in {"routed_experts", "routed_expert_logits"} or not isinstance(items, list): | ||
| raise ValueError(f"Invalid SGLang R3 source reference for {kind!r}") | ||
| if total != len(items) or start < 0 or count < 0 or start + count > total: | ||
| raise ValueError(f"SGLang R3 source slice out of range: start={start}, count={count}, total={total}") | ||
|
|
||
| loaded: List[torch.Tensor] = [] | ||
| for datum_idx, item in enumerate(items[start : start + count], start=start): | ||
| if not isinstance(item, Mapping) or item.get("schema") != "xorl.r3.spans.v1": | ||
| raise ValueError(f"Invalid R3 span datum {datum_idx}") | ||
| shape = item.get("shape") | ||
| spans = item.get("spans") | ||
| if ( | ||
| item.get("dtype") != expected_dtype_name | ||
| or not isinstance(shape, list) | ||
| or len(shape) != 3 | ||
| or not isinstance(spans, list) | ||
| ): | ||
| raise ValueError(f"Invalid R3 span metadata for datum {datum_idx}") | ||
| pieces: List[torch.Tensor] = [] | ||
| for span_idx, span in enumerate(spans): | ||
| if not isinstance(span, Mapping): | ||
| raise ValueError(f"Invalid R3 span {datum_idx}/{span_idx}") | ||
| rows = int(span.get("rows", -1)) | ||
| source_row = int(span.get("source_row", -1)) | ||
| row_nbytes = int(span.get("row_nbytes", -1)) | ||
| offset = int(span.get("offset", -1)) + source_row * row_nbytes | ||
| source_shape = span.get("source_shape") | ||
| expected_row_nbytes = math.prod(shape[1:]) * 4 | ||
| if ( | ||
| span.get("dtype") != expected_dtype_name | ||
| or rows < 0 | ||
| or source_row < 0 | ||
| or row_nbytes != expected_row_nbytes | ||
| or offset < 0 | ||
| or not isinstance(source_shape, list) | ||
| or len(source_shape) != 3 | ||
| or source_shape[1:] != shape[1:] | ||
| or source_row + rows > int(source_shape[0]) | ||
| ): | ||
| raise ValueError(f"Invalid R3 span geometry for datum {datum_idx}/{span_idx}") | ||
| path = self._wait_for_r3_source(span) | ||
| required = offset + rows * row_nbytes | ||
| if path.stat().st_size < required: | ||
| raise ValueError(f"R3 source {path} is shorter than span {datum_idx}/{span_idx}") | ||
| if rows == 0: | ||
| pieces.append(torch.empty((0, *shape[1:]), dtype=expected_dtype)) | ||
| continue | ||
| storage = torch.from_file(str(path), shared=False, size=path.stat().st_size // 4, dtype=expected_dtype) | ||
| pieces.append(storage[offset // 4 : required // 4].reshape(rows, *shape[1:])) | ||
| if sum(piece.shape[0] for piece in pieces) != int(shape[0]): | ||
| raise ValueError(f"R3 span coverage mismatch for datum {datum_idx}") | ||
| loaded.append(pieces[0] if len(pieces) == 1 else torch.cat(pieces, dim=0)) | ||
| return loaded |
Summary
This consolidates the exact trainer-side work previously split across the earlier XoRL PRs into one branch based directly on
main. It is paired with togethercomputer/xorl-sglang#21.Highlights
Validation
Companion serving change
Out of scope
Sparse-delta receiver integration is intentionally not included.