Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions nemo_curator/stages/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,15 @@ def __init_subclass__(cls, **kwargs):
msg = f"{cls.__name__} must not override '_batch_size'"
raise TypeError(msg)

for attr in ("name", "resources", "batch_size"):
Comment thread
sarahyurick marked this conversation as resolved.
if isinstance(cls.__dict__.get(attr), property):
msg = (
f"{cls.__name__} must not define '{attr}' as a @property. "
f"Use a plain class attribute or dataclass field instead, "
f"so that ProcessingStage.with_() can override it."
)
raise TypeError(msg)

def num_workers(self) -> int | None:
"""Number of workers required. If None, then executor will determine the number of workers."""
return None
Expand Down
5 changes: 1 addition & 4 deletions nemo_curator/stages/synthetic/nemotron_cc/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,7 @@ class BaseSyntheticStage(ProcessingStage[DocumentBatch, DocumentBatch]):
client: AsyncLLMClient | LLMClient = None
model_name: str = None
generation_config: GenerationConfig | None = None

@property
def name(self) -> str:
return "NemotronCCBaseStage"
name: str = "NemotronCCBaseStage"

def __post_init__(self) -> None:
self.is_async_client = isinstance(self.client, AsyncLLMClient)
Expand Down
10 changes: 2 additions & 8 deletions nemo_curator/stages/synthetic/nemotron_cc/nemotron_cc.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,7 @@ class DiverseQAPostProcessingStage(ProcessingStage[DocumentBatch, DocumentBatch]
tokenizer: AutoTokenizer | None = None
prefix: str = "Here are the questions and answers based on the provided text:"
max_num_pairs: int = 10

@property
def name(self) -> str:
return "DiverseQAPostProcessing"
name: str = "DiverseQAPostProcessing"

def process(self, batch: DocumentBatch) -> DocumentBatch:
df = batch.to_pandas()
Expand Down Expand Up @@ -149,10 +146,7 @@ class KnowledgeListPostProcessingStage(ProcessingStage[DocumentBatch, DocumentBa
"""

input_field: str = "knowledge_list"

@property
def name(self) -> str:
return "KnowledgeListPostProcessing"
name: str = "KnowledgeListPostProcessing"

def process(self, batch: DocumentBatch) -> DocumentBatch:
df = batch.to_pandas()
Expand Down
62 changes: 62 additions & 0 deletions tests/stages/common/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,68 @@ def inputs(self) -> tuple[list[str], list[str]]:
def outputs(self) -> tuple[list[str], list[str]]:
return [], []

def test_name_property_decorator(self):
"""Test that ProcessingStage raises an error if a derived class defines 'name' as a @property."""
with pytest.raises(TypeError, match="must not define 'name' as a @property"):

class MockStagePropertyName(ProcessingStage[MockTask, MockTask]):
@property
def name(self) -> str:
return "PropertyName"

def process(self, task: MockTask) -> MockTask:
return task

def test_resources_property_decorator(self):
"""Test that ProcessingStage raises an error if a derived class defines 'resources' as a @property."""
with pytest.raises(TypeError, match="must not define 'resources' as a @property"):

class MockStagePropertyResources(ProcessingStage[MockTask, MockTask]):
name = "MockStagePropertyResources"

@property
def resources(self) -> Resources:
return Resources(cpus=1.0)

def process(self, task: MockTask) -> MockTask:
return task

def test_batch_size_property_decorator(self):
"""Test that ProcessingStage raises an error if a derived class defines 'batch_size' as a @property."""
with pytest.raises(TypeError, match="must not define 'batch_size' as a @property"):

class MockStagePropertyBatchSize(ProcessingStage[MockTask, MockTask]):
name = "MockStagePropertyBatchSize"

@property
def batch_size(self) -> int:
return 1

def process(self, task: MockTask) -> MockTask:
return task

def test_plain_attribute_override_allowed(self):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this may not be needed and should already be covered by TestProcessingStageWith which uses ConcreteProcessingStage. What do you think @omkar-334 ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right... TestProcessingStageWith already tests for attribute overrides. Thanks for this!. Ive removed it now

"""Test that plain attribute overrides for name, resources, and batch_size are still allowed."""

class PlainAttrStage(ProcessingStage[MockTask, MockTask]):
name = "PlainAttrStage"
resources = Resources(cpus=4.0)
batch_size = 8

def process(self, task: MockTask) -> MockTask:
return task

stage = PlainAttrStage()
assert stage.name == "PlainAttrStage"
assert stage.resources == Resources(cpus=4.0)
assert stage.batch_size == 8

# with_() should work fine on plain attributes
modified = stage.with_(name="Modified", resources=Resources(cpus=2.0), batch_size=4)
assert modified.name == "Modified"
assert modified.resources == Resources(cpus=2.0)
assert modified.batch_size == 4

def test_nested_class_inheritance(self):
"""Test that nested class inheritance raises an error if a derived class overrides the _name, _resources, or _batch_size property."""
with pytest.raises(TypeError, match="MockStageNestedOverriddenName must not override '_name'"):
Expand Down