Implementation:416rehman DeepZero Processor Base Classes
| Knowledge Sources | |
|---|---|
| Domains | Pipeline_Orchestration, Extensibility |
| Last Updated | 2026-10-27 00:00 GMT |
Overview
Four typed processor base classes (Ingest, Map, Reduce, BulkMap) that define the contract for pipeline stages, with lifecycle hooks, configuration dataclasses, and result types.
Description
DeepZero defines four processor lanes, each a subclass of the abstract Processor base class. Every pipeline stage is an instance of one of these:
- IngestProcessor: discovers samples from a target path. Runs once, always the first stage. Returns list[Sample].
- MapProcessor: processes one sample at a time. The engine fans out via ThreadPoolExecutor. Must be thread-safe. Returns ProcessorResult.
- ReduceProcessor: sees ALL active samples at once. Acts as a global sync barrier. Returns list[str] of sample IDs to keep (others are filtered out).
- BulkMapProcessor: processes all samples in one invocation. More efficient than Map when the external process has high startup cost. Returns one ProcessorResult per entry, matched by index.
Each processor declares an optional Config dataclass (a ClassVar) whose fields name accepted YAML keys. The engine instantiates it from the YAML config dict at pipeline load time. If Config is None, the processor receives the raw config dict.
The provides ClassVar lists the data keys and artifact template names a processor records, which downstream Jinja2 prompts can reference. deepzero validate checks each prompt against the names its upstream stages declare.
Usage
- Building a custom processor: subclass one of the four base classes and implement the abstract process() method.
- Declaring configuration: define a @dataclass Config classvar with fields matching YAML keys.
- Declaring outputs: set the provides ClassVar to list data keys and artifact names downstream stages can reference.
- Implementing skip logic: override should_skip() on a MapProcessor to skip already-processed samples.
Code Reference
Source Location
- Repository: DeepZero
- File: src/deepzero/engine/stage.py
Signature
class Processor(ABC):
processor_type: ProcessorType
Config: ClassVar[type | None] = None
provides: ClassVar[tuple[str, ...]] = ()
description: ClassVar[str] = ""
version: ClassVar[str] = "1.0"
def __init__(self, spec: StageSpec): ...
def validate(self, ctx: ProcessorContext) -> list[str]: ...
def setup(self, global_config: dict[str, Any]) -> None: ...
def teardown(self) -> None: ...
def max_parallelism(self) -> int | None: ...
class IngestProcessor(Processor):
processor_type = ProcessorType.INGEST
@abstractmethod
def process(self, ctx: ProcessorContext, target: Path) -> list[Sample]: ...
class MapProcessor(Processor):
processor_type = ProcessorType.MAP
@abstractmethod
def process(self, ctx: ProcessorContext, entry: ProcessorEntry) -> ProcessorResult: ...
def should_skip(self, ctx: ProcessorContext, entry: ProcessorEntry) -> str | None: ...
class ReduceProcessor(Processor):
processor_type = ProcessorType.REDUCE
@abstractmethod
def process(self, ctx: ProcessorContext, entries: list[ProcessorEntry]) -> list[str]: ...
class BulkMapProcessor(Processor):
processor_type = ProcessorType.BULK_MAP
@abstractmethod
def process(self, ctx: ProcessorContext, entries: list[ProcessorEntry]) -> list[ProcessorResult]: ...
Import
from deepzero.engine.stage import (
IngestProcessor, MapProcessor, ReduceProcessor, BulkMapProcessor,
Processor, ProcessorContext, ProcessorEntry, ProcessorResult, StageSpec,
)
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| spec | StageSpec | Yes | Stage name, processor ref, config, parallel, timeout, on_failure |
| ctx | ProcessorContext | Yes (process) | Pipeline dir, global config, LLM, logger, progress, shutdown event |
| target | Path | Yes (Ingest) | File or directory to discover samples from |
| entry | ProcessorEntry | Yes (Map) | Single sample with lazy-loaded upstream history |
| entries | list[ProcessorEntry] | Yes (Reduce/BulkMap) | All active samples |
Outputs
| Name | Type | Description |
|---|---|---|
| Ingest returns | list[Sample] | Discovered samples |
| Map returns | ProcessorResult | ok/filter/fail/timed_out for one sample |
| Reduce returns | list[str] | Sample IDs to keep (others filtered out) |
| BulkMap returns | list[ProcessorResult] | One result per entry, matched by index |
Usage Examples
Custom MapProcessor
from dataclasses import dataclass
from pathlib import Path
from deepzero.engine.stage import (
MapProcessor, ProcessorContext, ProcessorEntry, ProcessorResult,
)
class HashTagCounter(MapProcessor):
description = "counts hash symbols in text files"
@dataclass
class Config:
min_hashes: int = 0
provides = ("hash_count",)
def process(self, ctx: ProcessorContext, entry: ProcessorEntry) -> ProcessorResult:
text = entry.source_path.read_text(encoding="utf-8", errors="replace")
count = text.count("#")
if count < self.config.min_hashes:
return ProcessorResult.filter(f"{count} hashes < min {self.config.min_hashes}")
return ProcessorResult.ok(data={"hash_count": count})
Custom ReduceProcessor
from deepzero.engine.stage import ReduceProcessor, ProcessorContext, ProcessorEntry
class KeepAllSorted(ReduceProcessor):
description = "keeps all samples, sorted by filename"
def process(self, ctx: ProcessorContext, entries: list[ProcessorEntry]) -> list[str]:
sorted_entries = sorted(entries, key=lambda e: e.filename)
return [e.sample_id for e in sorted_entries]
Related Pages
Implements Principle
- Principle:416rehman_DeepZero_Processor_Contract
- Principle:416rehman_DeepZero_Pipeline_Execution
- Principle:416rehman_DeepZero_Extensibility_by_Subclassing