refactor: 重构流水线架构,添加Pipeline抽象并拆分IOHandler
This commit is contained in:
@@ -1,21 +1,32 @@
|
||||
"""DPO preference learning data processor."""
|
||||
|
||||
from typing import Dict, List, Any, Optional
|
||||
from typing import Any, Dict, List, Optional
|
||||
|
||||
import torch
|
||||
from torch import Tensor
|
||||
|
||||
from pipeline.tokenize import AutoTokenizer
|
||||
from pipeline.strategies import PromptStrategy, ChatMLStrategy
|
||||
from pipeline.processors.base import BaseProcessor, _encode_with_mask
|
||||
from pipeline.processors.base import BaseProcessor, ProcessorSchema, encode_with_mask
|
||||
from pipeline.processors.factory import ProcessorFactory
|
||||
|
||||
|
||||
@ProcessorFactory.register("dpo")
|
||||
class DPOProcessor(BaseProcessor):
|
||||
"""DPO preference learning data processor.
|
||||
"""DPO (Direct Preference Optimization) data processor.
|
||||
|
||||
Supports custom prompt strategy via constructor parameter.
|
||||
Processes query, chosen, and rejected responses for preference learning.
|
||||
|
||||
Input schema:
|
||||
- query: str - User query/prompt
|
||||
- chosen: str - Preferred assistant response
|
||||
- rejected: str - Dispreferred assistant response
|
||||
|
||||
Output schema:
|
||||
- chosen: int32 tensor - Token IDs for preferred response
|
||||
- chosen_mask: bool tensor - True for response tokens
|
||||
- rejected: int32 tensor - Token IDs for dispreferred response
|
||||
- rejected_mask: bool tensor - True for response tokens
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
@@ -26,6 +37,22 @@ class DPOProcessor(BaseProcessor):
|
||||
self.tokenizer = tokenizer
|
||||
self.strategy = strategy or ChatMLStrategy(tokenizer)
|
||||
|
||||
@property
|
||||
def schema(self) -> ProcessorSchema:
|
||||
return ProcessorSchema(
|
||||
input_fields={
|
||||
"query": str,
|
||||
"chosen": str,
|
||||
"rejected": str,
|
||||
},
|
||||
output_fields={
|
||||
"chosen": torch.int32,
|
||||
"chosen_mask": torch.bool,
|
||||
"rejected": torch.int32,
|
||||
"rejected_mask": torch.bool,
|
||||
},
|
||||
)
|
||||
|
||||
def process(self, input_dict: Dict[str, Any]) -> Dict[str, Tensor]:
|
||||
query_tokens = self.tokenizer.encode(input_dict["query"])
|
||||
chosen_tokens = self.tokenizer.encode(input_dict["chosen"])
|
||||
@@ -33,10 +60,10 @@ class DPOProcessor(BaseProcessor):
|
||||
|
||||
prompt = self.strategy.assemble_prompt(query_tokens)
|
||||
|
||||
chosen_t, chosen_m = _encode_with_mask(
|
||||
chosen_t, chosen_m = encode_with_mask(
|
||||
prompt, self.strategy.assemble_response(chosen_tokens)
|
||||
)
|
||||
rejected_t, rejected_m = _encode_with_mask(
|
||||
rejected_t, rejected_m = encode_with_mask(
|
||||
prompt, self.strategy.assemble_response(rejected_tokens)
|
||||
)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user