Coverage for / home / jenkins / .local / lib / python3.10 / site-packages / hyper_parallel / distributed / encoder_parallel / contracts.py: 100%
38 statements
« prev ^ index » next coverage.py v7.13.1, created at 2026-12-05 05:12 +0800
« prev ^ index » next coverage.py v7.13.1, created at 2026-12-05 05:12 +0800
1# Copyright 2026 Huawei Technologies Co., Ltd
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14# ============================================================================
15"""Model-neutral contracts for visual encoder parallel execution."""
17from __future__ import annotations
19from dataclasses import dataclass
20from typing import Any, Callable, Mapping
22import torch
23from torch import nn
26@dataclass(frozen=True)
27class EncoderWorkItem:
28 """One image and the decoder token span that consumes its features."""
30 item_id: int
31 batch_index: int
32 sequence_length: int
33 output_rows: int
34 decoder_token_start: int
35 decoder_token_length: int
36 payload_start: int
37 payload_stop: int
38 metadata: Mapping[str, Any]
39 feature_row_start: int = 0
42@dataclass(frozen=True)
43class EncoderSegment:
44 """One padded image shard in a packed ViT call."""
46 item: EncoderWorkItem | None
47 local_start: int
48 local_valid_length: int
49 local_padded_length: int
50 packed_offset: int
52 @property
53 def is_dummy(self) -> bool:
54 """A missing image identifies the synchronization-only segment."""
55 return self.item is None
58EncoderFeature = tuple[EncoderWorkItem, torch.Tensor]
59"""One image job paired with its aligned feature rows."""
62@dataclass(frozen=True)
63class EncoderLocalInputs:
64 """Device patches packed in the order of one plan's local segments."""
66 patches: torch.Tensor
69@dataclass(frozen=True)
70class EncoderParallelAdapterSpec:
71 """Model-owned visual boundaries and a runtime bridge factory.
73 ``bind(model)`` returns an object with ``input_fields``, ``head_counts``,
74 ``validate_training``, ``parse_images``, ``pack_payload``, ``payload_field``,
75 ``empty_payload_shape``, ``encode_local`` and ``project``.
76 Sharded spatial projection additionally requires ``projector_patch_count``,
77 ``projector_input_indices``, ``project_blocks`` and ``feature_interval``.
78 ``balance_cost=encoder_flops`` requires ``estimate_work_cost(items, encoder_cp_size)``
79 returning a positive finite cost per image ID.
80 Default execution does not require these extensions.
81 The runtime uses these operations without inspecting model-specific state.
82 Module replacement is selected separately by recipe ``replace_module``.
83 """
85 isolated_subtrees: tuple[str, ...]
86 attention_pattern: str
87 replacement_attention_type: type[nn.Module]
88 bind: Callable[[nn.Module], Any]