Coverage for  / home / jenkins / .local / lib / python3.10 / site-packages / hyper_parallel / data / omni / build_dataset.py: 87%

45 statements  

« 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"""Compose an Online source with the configured Omni transform lifecycle.""" 

16 

17from __future__ import annotations 

18 

19from collections.abc import Mapping 

20from typing import Any 

21 

22from hyper_parallel.data.constants import ONLINE_SPLIT_COUNT 

23from hyper_parallel.data.omni.omni_transform import ( 

24 OmniDataTransform, 

25 _OmniTransformStrategy, 

26) 

27from hyper_parallel.data.online import ( 

28 MappingTransformDataset, 

29 OnlineDataPath, 

30 build_online_mapping_source, 

31) 

32 

33 

34def build_online_omni_mapping_dataset( 

35 *, 

36 data_config: Mapping[str, Any], 

37 data_path: OnlineDataPath | None = None, 

38 transform: OmniDataTransform | None = None, 

39 training_config: Any = None, 

40 sampler_type: str = "single", 

41) -> Any: 

42 """Build an Online Mapping source and apply its Omni transform lazily. 

43 

44 Args: 

45 data_config: Online Mapping source options. 

46 data_path: Optional local source path, ordered paths, or pre-split 

47 train/valid/test path mapping. 

48 transform: Omni sample transform selected by the Trainer. 

49 training_config: Training plan providing the random seed. 

50 sampler_type: Mapping sampler policy controlling source-level shuffle. 

51 

52 Returns: 

53 A transformed Online Mapping Dataset or train-valid-test tuple. 

54 

55 Raises: 

56 TypeError: If transform is not an OmniDataTransform. 

57 ValueError: If no Omni transform is configured. 

58 """ 

59 

60 if transform is None: 

61 raise ValueError("Online Omni Dataset requires a data_transform") 

62 

63 if not isinstance(transform, OmniDataTransform): 

64 raise TypeError("Online Omni Dataset transform must be an OmniDataTransform") 

65 

66 dataset_config = dict(data_config) 

67 training_seed = training_config.seed if training_config is not None else None 

68 dataset_config["random_seed"] = 42 if training_seed is None else int(training_seed) 

69 

70 sample_filter = transform.is_valid_sample 

71 source_dataset = build_online_mapping_source( 

72 data_path=data_path, 

73 data_config=dataset_config, 

74 sample_filter=sample_filter, 

75 sampler_type=sampler_type, 

76 ) 

77 transformed_dataset = _OmniMappingDataset.apply(source_dataset, transform) 

78 return transformed_dataset 

79 

80 

81class _OmniMappingDataset(MappingTransformDataset): 

82 """Retain the Omni encoding lifecycle around one Mapping source.""" 

83 

84 def __init__(self, source_dataset: Any, transform: OmniDataTransform) -> None: 

85 """Build the pre-selection strategy and retain post-selection hooks.""" 

86 self.data_transform = transform 

87 self._transform_strategy = _OmniTransformStrategy.from_transform(transform) 

88 super().__init__(source_dataset, self._transform_strategy.prepare_samples) 

89 

90 @classmethod 

91 def apply(cls, source_dataset: Any, transform: OmniDataTransform) -> Any: 

92 """Wrap one Mapping source or each available train-valid-test split.""" 

93 if isinstance(source_dataset, tuple) and len(source_dataset) == ONLINE_SPLIT_COUNT: 

94 transformed_splits = [] 

95 for split_dataset in source_dataset: 

96 transformed_dataset = None 

97 if split_dataset is not None: 

98 transformed_dataset = cls(split_dataset, transform) 

99 transformed_splits.append(transformed_dataset) 

100 

101 mapping_splits = ( 

102 transformed_splits[0], 

103 transformed_splits[1], 

104 transformed_splits[2], 

105 ) 

106 return mapping_splits 

107 

108 if source_dataset is None: 

109 return None 

110 

111 mapping_dataset = cls(source_dataset, transform) 

112 return mapping_dataset 

113 

114 def encode_selected_sample(self, sample: Any) -> Any: 

115 """Finish encoding one sample after packing selection.""" 

116 encoded_sample = self._transform_strategy.encode_selected_sample(sample) 

117 return encoded_sample 

118 

119 def encode_batch(self, batch: Any) -> Any: 

120 """Apply the transform's final batch encoding hook.""" 

121 encoded_batch = self.data_transform.encode_batch(batch) 

122 return encoded_batch 

123 

124 

125__all__ = ["build_online_omni_mapping_dataset"]