Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Content Type Classifier #361

Merged
merged 15 commits into from
Dec 16, 2024
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ All of our text pipelines have great multilingual support.
- [Heuristic Filtering](https://docs.nvidia.com/nemo-framework/user-guide/latest/datacuration/qualityfiltering.html)
- Classifier Filtering
- [fastText](https://docs.nvidia.com/nemo-framework/user-guide/latest/datacuration/qualityfiltering.html)
- GPU-Accelerated models: [Domain, Quality, and Safety Classification](https://docs.nvidia.com/nemo-framework/user-guide/latest/datacuration/distributeddataclassification.html)
- GPU-Accelerated models: [Domain, Quality, Safety, Educational Content, and Type of Speech Classification](https://docs.nvidia.com/nemo-framework/user-guide/latest/datacuration/distributeddataclassification.html)
- **GPU-Accelerated Deduplication**
- [Exact Deduplication](https://docs.nvidia.com/nemo-framework/user-guide/latest/datacuration/gpudeduplication.html)
- [Fuzzy Deduplication](https://docs.nvidia.com/nemo-framework/user-guide/latest/datacuration/gpudeduplication.html) via MinHash Locality Sensitive Hashing
Expand Down Expand Up @@ -160,7 +160,7 @@ To get started with NeMo Curator, you can follow the tutorials [available here](

- [`tinystories`](https://github.com/NVIDIA/NeMo-Curator/tree/main/tutorials/tinystories) which focuses on data curation for training LLMs from scratch.
- [`peft-curation`](https://github.com/NVIDIA/NeMo-Curator/tree/main/tutorials/peft-curation) which focuses on data curation for LLM parameter-efficient fine-tuning (PEFT) use-cases.
- [`distributed_data_classification`](https://github.com/NVIDIA/NeMo-Curator/tree/main/tutorials/distributed_data_classification) which focuses on using the quality and domain classifiers to help with data annotation.
- [`distributed_data_classification`](https://github.com/NVIDIA/NeMo-Curator/tree/main/tutorials/distributed_data_classification) which focuses on using the domain and quality classifiers to help with data annotation.
- [`single_node_tutorial`](https://github.com/NVIDIA/NeMo-Curator/tree/main/tutorials/single_node_tutorial) which demonstrates an end-to-end data curation pipeline for curating Wikipedia data in Thai.
- [`image-curation`](https://github.com/NVIDIA/NeMo-Curator/blob/main/tutorials/image-curation/image-curation.ipynb) which explores the scalable image curation modules.

Expand Down
5 changes: 4 additions & 1 deletion docs/user-guide/api/classifiers.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,7 @@ Classifiers
:members:

.. autoclass:: nemo_curator.classifiers.AegisClassifier
:members:
:members:

.. autoclass:: nemo_curator.classifiers.TypeOfSpeechClassifier
:members:
2 changes: 1 addition & 1 deletion docs/user-guide/bestpractices.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Here are the methods in increasing order of compute required for them.

#. `fastText <https://docs.nvidia.com/nemo-framework/user-guide/latest/datacuration/qualityfiltering.html#classifier-filtering>`_ is an n-gram based bag-of-words classifier. It is typically trained on a high quality reference corpus and a low quality corpus (typically unfiltered Common Crawl dumps). While NeMo Curator does not provide pretrained versions of the classifier, training it is incredibly fast and easy. It only requires 100,000 - 1,000,000 text samples to train on, and can complete training in mere seconds. Its small size also allows it to train and run inference on the CPU. Due to these factors, we recommend using fastText classifiers on large scale pretraining datasets where you don't have the compute budget for more sophisticated methods.

#. `BERT-style classifiers <https://docs.nvidia.com/nemo-framework/user-guide/latest/datacuration/distributeddataclassification.html>`_ - NeMo Curator's distributed data classification modules work with many BERT-style classifiers for `domain classification <https://huggingface.co/nvidia/domain-classifier>`_, quality classification, and more. For this comparison, we'll focus on just the text quality classifier. NeMo Curator provides a pretrained version of the classifier on HuggingFace and NGC that can be immediately used. We recommend using these classifiers towards the end of your data filtering pipeline for pretraining.
#. `BERT-style classifiers <https://docs.nvidia.com/nemo-framework/user-guide/latest/datacuration/distributeddataclassification.html>`_ - NeMo Curator's distributed data classification modules work with many BERT-style classifiers for `domain classification <https://huggingface.co/nvidia/domain-classifier>`_, `quality classification <https://huggingface.co/nvidia/quality-classifier-deberta>`_, and more. For this comparison, we'll focus on just the text quality classifier. NeMo Curator provides a pretrained version of the classifier on HuggingFace and NGC that can be immediately used. We recommend using these classifiers towards the end of your data filtering pipeline for pretraining.

#. `Language model labelling <https://docs.nvidia.com/nemo-framework/user-guide/latest/datacuration/syntheticdata.html>`_ - Language models can be used to label text as high quality or low quality. NeMo Curator allows you to connect to arbitrary LLM inference endpoints which you can use to label your data. One example of such an endpoint would be Nemotron-4 340B Instruct on `build.nvidia.com <https://build.nvidia.com/explore/discover#nemotron-4-340b-instruct>`_. Due to their size, these models can require a lot of compute and are usually infeasible to run across an entire pretraining dataset. We recommend using these large models on very little amounts of data. Fine-tuning datasets can make good use of them.

Expand Down
3 changes: 3 additions & 0 deletions docs/user-guide/cpuvsgpu.rst
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ The following NeMo Curator modules are GPU based.

* Domain Classification
* Quality Classification
* AEGIS Safety Models
* FineWeb Educational Content Classification
* Type of Speech Classification

GPU modules store the ``DocumentDataset`` using a ``cudf`` backend instead of a ``pandas`` one.
To read a dataset into GPU memory, one could use the following function call.
Expand Down
15 changes: 11 additions & 4 deletions docs/user-guide/distributeddataclassification.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ NeMo Curator provides a module to help users run inference with pre-trained mode
This is achieved by chunking the datasets across multiple computing nodes, each equipped with multiple GPUs, to accelerate the classification task in a distributed manner.
Since the classification of a single text document is independent of other documents within the dataset, we can distribute the workload across multiple nodes and GPUs to perform parallel processing.

Domain, quality, content safety, and educational content models are tasks we include as examples within our module.
Domain, quality, content safety, educational content, and type of speech models are tasks we include as examples within our module.

Here, we summarize why each is useful for training an LLM:

Expand All @@ -27,6 +27,8 @@ Here, we summarize why each is useful for training an LLM:

- The **FineWeb Educational Content Classifier** focuses on identifying and prioritizing educational material within datasets. This classifier is especially useful for training LLMs on specialized educational content, which can improve their performance on knowledge-intensive tasks. Models trained on high-quality educational content demonstrate enhanced capabilities on academic benchmarks such as MMLU and ARC, showcasing the classifier's impact on improving the knowledge-intensive task performance of LLMs.

- The **Type of Speech Classifier** TODO

-----------------------------------------
Usage
-----------------------------------------
Expand All @@ -43,7 +45,7 @@ It is easy to extend ``DistributedDataClassifier`` to your own model.
Check out ``nemo_curator.classifiers.base.py`` for reference.

Domain Classifier
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
^^^^^^^^^^^^^^^^^

The Domain Classifier is used to categorize text documents into specific domains or subject areas. This is particularly useful for organizing large datasets and tailoring the training data for domain-specific LLMs.

Expand All @@ -65,7 +67,7 @@ In this example, the domain classifier is obtained directly from `Hugging Face <
It filters the input dataset to include only documents classified as "Games" or "Sports".

Quality Classifier
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
^^^^^^^^^^^^^^^^^^

The Quality Classifier is designed to assess the quality of text documents, helping to filter out low-quality or noisy data from your dataset.

Expand All @@ -87,7 +89,7 @@ The quality classifier is obtained from `Hugging Face <https://huggingface.co/nv
In this example, it filters the input dataset to include only documents classified as "High" or "Medium" quality.

AEGIS Safety Model
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
^^^^^^^^^^^^^^^^^^

Aegis is a family of content-safety LLMs used for detecting if a piece of text contains content that is a part of 13 critical risk categories.
There are two variants, `defensive <https://huggingface.co/nvidia/Aegis-AI-Content-Safety-LlamaGuard-Defensive-1.0>`_ and `permissive <https://huggingface.co/nvidia/Aegis-AI-Content-Safety-LlamaGuard-Permissive-1.0>`_, that are useful for filtering harmful data out of your training set.
Expand Down Expand Up @@ -178,6 +180,11 @@ For example, to create a dataset with only highly educational content (scores 4
high_edu_dataset = result_dataset[result_dataset["fineweb-edu-score-int"] >= 4]
high_edu_dataset.to_json("high_educational_content/")

Type of Speech Classifier
^^^^^^^^^^^^^^^^^^^^^^^^^

TODO

-----------------------------------------
CrossFit Integration
-----------------------------------------
Expand Down
65 changes: 65 additions & 0 deletions examples/classifiers/type_of_speech_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# Copyright (c) 2024, NVIDIA CORPORATION. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import argparse
import time

from nemo_curator.classifiers import TypeOfSpeechClassifier
from nemo_curator.datasets import DocumentDataset
from nemo_curator.utils.distributed_utils import get_client
from nemo_curator.utils.script_utils import ArgumentHelper


def main(args):
global_st = time.time()

# Input can be a string or list
input_file_path = "/path/to/data"
output_file_path = "./"

client_args = ArgumentHelper.parse_client_args(args)
client_args["cluster_type"] = "gpu"
client = get_client(**client_args)

input_dataset = DocumentDataset.read_json(
input_file_path, backend="cudf", add_filename=True
)

type_of_speech_classifier = TypeOfSpeechClassifier(filter_by=["Blogs", "News"])
result_dataset = type_of_speech_classifier(dataset=input_dataset)

result_dataset.to_json(output_file_dir=output_file_path, write_to_filename=True)

global_et = time.time()
print(
f"Total time taken for type of speech classifier inference: {global_et-global_st} s",
flush=True,
)

client.close()


def attach_args(
parser=argparse.ArgumentParser(
formatter_class=argparse.ArgumentDefaultsHelpFormatter
),
):
argumentHelper = ArgumentHelper(parser)
argumentHelper.add_distributed_classifier_cluster_args()

return argumentHelper.parser


if __name__ == "__main__":
main(attach_args().parse_args())
2 changes: 2 additions & 0 deletions nemo_curator/classifiers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@
from .domain import DomainClassifier
from .fineweb_edu import FineWebEduClassifier
from .quality import QualityClassifier
from .type_of_speech import TypeOfSpeechClassifier

__all__ = [
"DomainClassifier",
"QualityClassifier",
"AegisClassifier",
"FineWebEduClassifier",
"TypeOfSpeechClassifier",
]
137 changes: 137 additions & 0 deletions nemo_curator/classifiers/type_of_speech.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
# Copyright (c) 2024, NVIDIA CORPORATION. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import os
from dataclasses import dataclass
from typing import List, Optional

os.environ["RAPIDS_NO_INITIALIZE"] = "1"
from crossfit.backend.torch.hf.model import HFModel
from transformers import AutoConfig, AutoTokenizer

from nemo_curator.classifiers.base import (
DistributedDataClassifier,
HFDeberta,
_get_suggest_memory_for_classifier,
_run_classifier_helper,
)
from nemo_curator.datasets import DocumentDataset

TYPE_OF_SPEECH_IDENTIFIER = "TODO"


@dataclass
class TypeOfSpeechModelConfig:
model: str = "microsoft/deberta-v3-base"
fc_dropout: float = 0.2
max_len: int = 512


class TypeOfSpeechModel(HFModel):
def __init__(
self,
config: TypeOfSpeechModelConfig,
autocast: bool = False,
max_mem_gb: Optional[int] = None,
):
self.config = config
self.autocast = autocast
if max_mem_gb is None:
max_mem_gb = _get_suggest_memory_for_classifier()

super().__init__(self.config.model, max_mem_gb=max_mem_gb)

def load_model(self, device: str = "cuda"):
model = HFDeberta.from_pretrained(TYPE_OF_SPEECH_IDENTIFIER)
model.set_autocast(self.autocast)
model = model.to(device)
return model.eval()

def load_tokenizer(self):
return AutoTokenizer.from_pretrained(TYPE_OF_SPEECH_IDENTIFIER)

def load_config(self):
return AutoConfig.from_pretrained(TYPE_OF_SPEECH_IDENTIFIER)


class TypeOfSpeechClassifier(DistributedDataClassifier):
sarahyurick marked this conversation as resolved.
Show resolved Hide resolved
"""
TypeOfSpeechClassifier is a text classification model designed to categorize documents into one of 11 distinct speech types based on their content.
It analyzes and understands the nuances of textual information, enabling accurate classification across a diverse range of content types.
This class is optimized for running on multi-node, multi-GPU setups to enable fast and efficient inference on large datasets.

Attributes:
filter_by (list[str], optional): The classes to filter the dataset by.
If None, all classes will be included. Defaults to None.
batch_size (int): The number of samples per batch for inference. Defaults to 256.
text_field (str): The field in the dataset that should be classified.
pred_column (str): The column name where predictions will be stored. Defaults to "type_of_speech_pred".
prob_column (str, optional): The column name where prediction probabilities will be stored. Defaults to None.
max_chars (int): The maximum number of characters in each document to consider for classification. Defaults to 2000.
device_type (str): The type of device to use for inference, either "cuda" or "cpu". Defaults to "cuda".
autocast (bool): Whether to use mixed precision for faster inference. Defaults to True.
max_mem_gb (int, optional): The maximum amount of memory in GB to allocate for the model. If None,
it defaults to the available GPU memory minus 4 GB.

"""

def __init__(
self,
filter_by: Optional[List[str]] = None,
batch_size: int = 256,
text_field: str = "text",
pred_column: str = "type_of_speech_pred",
prob_column: Optional[str] = None,
max_chars: int = 2000,
device_type: str = "cuda",
autocast: bool = True,
max_mem_gb: Optional[int] = None,
):
config = AutoConfig.from_pretrained(DOMAIN_IDENTIFIER)

self.text_field = text_field
self.prob_column = prob_column
self.labels = list(config.label2id.keys())
self.labels.sort(key=lambda x: config.label2id[x])
self.out_dim = len(self.labels)

model = TypeOfSpeechModel(
config=TypeOfSpeechModelConfig, autocast=autocast, max_mem_gb=max_mem_gb
)

super().__init__(
model=model,
labels=self.labels,
filter_by=filter_by,
batch_size=batch_size,
out_dim=self.out_dim,
pred_column=pred_column,
max_chars=max_chars,
device_type=device_type,
autocast=autocast,
)

def _run_classifier(self, dataset: DocumentDataset) -> DocumentDataset:
print("Starting type of speech classifier inference", flush=True)
df = dataset.df
df = _run_classifier_helper(
df=df,
model=self.model,
labels=self.labels,
max_chars=self.max_chars,
batch_size=self.batch_size,
label_col=self.pred_column,
text_field=self.text_field,
prob_col=self.prob_column,
)
return DocumentDataset(df)
Loading