File size: 9,066 Bytes
4c30587 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 | """
Base classes for data processing.
This module provides abstract interfaces for data processing that can be
extended to support different datasets. The core abstraction is chunk-based
processing where long context is split into chunks for sequential reading.
Key concepts:
- Chunk: A unit of text to be processed (could be a turn, session, paragraph, etc.)
- ChunkMode: How to split data into chunks (turn, turn-pair, full-session, etc.)
"""
from abc import ABC, abstractmethod
from typing import List, Dict, Any, Optional, Tuple
from dataclasses import dataclass
from enum import Enum
from tqdm import tqdm
class ChunkMode(Enum):
"""Defines how to split data into chunks."""
TURN = "turn" # Each utterance/turn is a chunk
TURN_PAIR = "turn-pair" # Pairs of turns (e.g., user-assistant)
FULL_SESSION = "full-session" # Entire session as one chunk
PARAGRAPH = "paragraph" # Split by paragraphs
FIXED_LENGTH = "fixed-length" # Fixed token/char length chunks
@dataclass
class DataSample:
"""
Standardized data sample for training/evaluation.
This provides a unified interface regardless of the original dataset format.
Different datasets will map their data to this structure.
"""
sample_id: str
chunks: List[str] # List of text chunks for sequential processing
qa_list: List[Dict[str, Any]] # List of QA items for evaluation
metadata: Dict[str, Any] # Additional dataset-specific metadata
def __post_init__(self):
if self.metadata is None:
self.metadata = {}
class DataProcessor(ABC):
"""
Abstract base class for data processing.
Subclasses should implement methods for extracting chunks and QA data
from dataset-specific formats. This enables:
1. Different datasets to be processed uniformly
2. Multi-dataset training by combining processors
3. Easy extension for new datasets
"""
# Supported chunk modes for this processor
supported_chunk_modes: List[ChunkMode] = [
ChunkMode.TURN,
ChunkMode.TURN_PAIR,
ChunkMode.FULL_SESSION
]
def __init__(self, chunk_mode: str = "turn-pair"):
"""
Initialize the data processor.
Args:
chunk_mode: How to split data into chunks
"""
self.chunk_mode = self._parse_chunk_mode(chunk_mode)
def is_interactive(self) -> bool:
"""
Whether this dataset is interactive (observations revealed step by step).
Interactive datasets should override this and provide iter_chunks() that
yields observations without exposing future steps.
"""
return False
def supports_parallel_env(self) -> bool:
"""
Whether interactive episodes can run in parallel safely.
"""
return False
def _parse_chunk_mode(self, mode: str) -> ChunkMode:
"""Parse chunk mode string to enum."""
mode_map = {
"turn": ChunkMode.TURN,
"turn-pair": ChunkMode.TURN_PAIR,
"full-session": ChunkMode.FULL_SESSION,
"paragraph": ChunkMode.PARAGRAPH,
"fixed-length": ChunkMode.FIXED_LENGTH,
}
if mode not in mode_map:
raise ValueError(f"Unknown chunk mode: {mode}. Supported: {list(mode_map.keys())}")
chunk_mode = mode_map[mode]
if chunk_mode not in self.supported_chunk_modes:
raise ValueError(f"Chunk mode {mode} not supported by {self.__class__.__name__}")
return chunk_mode
@abstractmethod
def extract_chunks(self, data: Dict) -> List[str]:
"""
Extract text chunks from raw data.
This is the core method that converts dataset-specific format
into a list of text chunks for sequential processing.
Args:
data: Raw data dict (dataset-specific format)
Returns:
List of text chunks
"""
pass
def iter_chunks(self, data: Dict):
"""
Yield chunks sequentially.
Default behavior yields from extract_chunks(). Interactive datasets
should override to stream observations step-by-step.
"""
return iter(self.extract_chunks(data))
def get_episode_length(self, data: Dict) -> Optional[int]:
"""
Return episode length if known. Interactive datasets may return None.
"""
if self.is_interactive():
for key in ("episode_length", "num_steps", "max_steps", "trajectory_length"):
value = data.get(key) if isinstance(data, dict) else None
if isinstance(value, int) and value > 0:
return value
return None
return len(self.extract_chunks(data))
@abstractmethod
def get_sample_id(self, data: Dict) -> str:
"""
Get unique identifier for a data sample.
Args:
data: Raw data dict
Returns:
Sample ID string
"""
pass
@abstractmethod
def get_qa_list(self, data: Dict) -> List[Dict[str, Any]]:
"""
Extract QA list from raw data.
Args:
data: Raw data dict
Returns:
List of QA dicts, each containing at least 'question' and 'answer'
"""
pass
def get_metadata(self, data: Dict) -> Dict[str, Any]:
"""
Extract additional metadata from raw data.
Override in subclasses to provide dataset-specific metadata.
Args:
data: Raw data dict
Returns:
Metadata dict
"""
return {}
def process(self, data: Dict) -> DataSample:
"""
Process raw data into standardized DataSample.
Args:
data: Raw data dict
Returns:
Standardized DataSample
"""
return DataSample(
sample_id=self.get_sample_id(data),
chunks=self.extract_chunks(data),
qa_list=self.get_qa_list(data),
metadata=self.get_metadata(data)
)
def process_batch(self, data_list: List[Dict],
show_progress: bool = True) -> List[DataSample]:
"""
Process a batch of raw data.
Args:
data_list: List of raw data dicts
show_progress: Whether to show progress bar
Returns:
List of DataSamples
"""
iterator = tqdm(data_list, desc="Processing data") if show_progress else data_list
return [self.process(data) for data in iterator]
class MultiDatasetProcessor:
"""
Processor for multi-dataset training.
Combines multiple DataProcessors to handle data from different datasets.
Supports weighted sampling for balanced training.
"""
def __init__(self):
self.processors: Dict[str, DataProcessor] = {}
self.weights: Dict[str, float] = {}
def register(self, name: str, processor: DataProcessor, weight: float = 1.0):
"""
Register a dataset processor.
Args:
name: Dataset name
processor: DataProcessor instance
weight: Sampling weight for this dataset
"""
self.processors[name] = processor
self.weights[name] = weight
def get_processor(self, name: str) -> DataProcessor:
"""Get processor by name."""
if name not in self.processors:
raise ValueError(f"Unknown dataset: {name}. Registered: {list(self.processors.keys())}")
return self.processors[name]
def process(self, data: Dict, dataset_name: str) -> DataSample:
"""
Process data from a specific dataset.
Args:
data: Raw data dict
dataset_name: Name of the dataset
Returns:
DataSample with dataset name in metadata
"""
processor = self.get_processor(dataset_name)
sample = processor.process(data)
sample.metadata['dataset'] = dataset_name
return sample
# Global registry for data processors
_PROCESSOR_REGISTRY: Dict[str, type] = {}
def register_processor(name: str):
"""
Decorator to register a data processor class.
Usage:
@register_processor("my_dataset")
class MyDataProcessor(DataProcessor):
...
"""
def decorator(cls):
_PROCESSOR_REGISTRY[name] = cls
return cls
return decorator
def get_processor(name: str, **kwargs) -> DataProcessor:
"""
Get a data processor instance by name.
Args:
name: Registered processor name
**kwargs: Arguments to pass to processor constructor
Returns:
DataProcessor instance
"""
if name not in _PROCESSOR_REGISTRY:
raise ValueError(f"Unknown processor: {name}. Available: {list(_PROCESSOR_REGISTRY.keys())}")
return _PROCESSOR_REGISTRY[name](**kwargs)
def list_processors() -> List[str]:
"""List all registered processor names."""
return list(_PROCESSOR_REGISTRY.keys())
|