data_juicer.core¶
- class data_juicer.core.Adapter(cfg: dict)[source]¶
Bases:
object
- MAX_BATCH_SIZE = 10000¶
- static execute_and_probe(dataset, operators, sample_interval=0.5)[source]¶
Process the input dataset and probe related information for each OP in the specified operator list.
For now, we support the following targets to probe: “resource”: resource utilization for each OP. “speed”: average processing speed for each OP.
The probe result is a list and each item in the list is the probe result for each OP.
- static take_batch(dataset, config)[source]¶
Split the dataset into batches based on configuration and load factor.
- Parameters:
dataset – The dataset to be split
config – Configuration settings, including batch size
- Returns:
An iterator of batches
- adapt_workloads(dataset, operators)[source]¶
Manage the scheduling and load balancing for the dataset processing.
- Parameters:
dataset – The dataset that needs to be processed
operators – Operators in the data recipe
- probe_small_batch(dataset, operators)[source]¶
Perform small batch pre-execution to probe available resources, current load and estimated OP speed, returning load factors and speed ranks for each OP.
- Parameters:
dataset – The dataset to pre-execute small batch on
operators – The OP list to be pre-execution and probe
- Returns:
A list of probe results for each OP and the length of data batch to probe.
- batch_size_strategy(load_analysis_res, base_bs=1, util_th=0.9)[source]¶
Decide the batch size for each op according to their workload analysis result and expected utilization threshold. We need to guarantee that the resource utilization won’t exceed the threshold. Now we only consider the buckets effect, which means the max batch size is decided by the max utilization of all types of resources except GPU util (decided by num_proc).
- class data_juicer.core.Analyzer(cfg=None)[source]¶
Bases:
object
This Analyzer class is used to analyze a specific dataset.
It will compute stats for all filter ops in the config file, apply multiple analysis (e.g. OverallAnalysis, ColumnWiseAnalysis, etc.) on these stats, and generate the analysis results (stats tables, distribution figures, etc.) to help users understand the input dataset better.
- class data_juicer.core.NestedDataset(*args, **kargs)[source]¶
Bases:
Dataset
,DJDataset
Enhanced HuggingFace-Dataset for better usability and efficiency.
- process(operators, *, work_dir=None, exporter=None, checkpointer=None, tracer=None)[source]¶
process a list of operators on the dataset.
- map(*args, **kargs)[source]¶
Override the map func, which is called by most common operations, such that the processed samples can be accessed by nested manner.
- filter(*args, **kargs)[source]¶
Override the filter func, which is called by most common operations, such that the processed samples can be accessed by nested manner.
- select(*args, **kargs)[source]¶
Override the select func, such that selected samples can be accessed by nested manner.
- classmethod from_dict(*args, **kargs)[source]¶
Override the from_dict func, which is called by most from_xx constructors, such that the constructed dataset object is NestedDataset.
- add_column(*args, **kargs)[source]¶
Override the add column func, such that the processed samples can be accessed by nested manner.
- select_columns(*args, **kargs)[source]¶
Override the select columns func, such that the processed samples can be accessed by nested manner.
- remove_columns(*args, **kargs)[source]¶
Override the remove columns func, such that the processed samples can be accessed by nested manner.
- cleanup_cache_files()[source]¶
Override the cleanup_cache_files func, clear raw and compressed cache files.
- static load_from_disk(*args, **kargs)[source]¶
Loads a dataset that was previously saved using [save_to_disk] from a dataset directory, or from a filesystem using any implementation of fsspec.spec.AbstractFileSystem.
- Parameters:
dataset_path (str) – Path (e.g. “dataset/train”) or remote URI (e.g. “s3//my-bucket/dataset/train”) of the dataset directory where the dataset will be loaded from.
fs (fsspec.spec.AbstractFileSystem, optional) –
Instance of the remote filesystem where the dataset will be saved to.
<Deprecated version=”2.8.0”>
fs was deprecated in version 2.8.0 and will be removed in 3.0.0. Please use storage_options instead, e.g. storage_options=fs.storage_options
</Deprecated>
keep_in_memory (bool, defaults to None) – Whether to copy the dataset in-memory. If None, the dataset will not be copied in-memory unless explicitly enabled by setting datasets.config.IN_MEMORY_MAX_SIZE to nonzero. See more details in the [improve performance](../cache#improve-performance) section.
storage_options (dict, optional) –
Key/value pairs to be passed on to the file-system backend, if any.
<Added version=”2.8.0”/>
- Returns:
If dataset_path is a path of a dataset directory, the dataset requested.
If dataset_path is a path of a dataset dict directory, a datasets.DatasetDict with each split.
- Return type:
[Dataset] or [DatasetDict]
Example:
`py >>> ds = load_from_disk("path/to/dataset/directory") `
- class data_juicer.core.Executor(cfg=None)[source]¶
Bases:
object
This Executor class is used to process a specific dataset.
It will load the dataset and unify the format, then apply all the ops in the config file in order and generate a processed dataset.
- sample_data(dataset_to_sample: Dataset | None = None, load_data_np=None, sample_ratio: float = 1.0, sample_algo: str = 'uniform', **kwargs)[source]¶
Sample a subset from the given dataset.
- Parameters:
dataset_to_sample – Dataset to sample from. If None, will use the formatter linked by the executor. Default is None.
load_data_np – number of workers when loading the dataset.
sample_ratio – The ratio of the sample size to the original dataset size. Default is 1.0 (no sampling).
sample_algo – Sampling algorithm to use. Options are “uniform”, “frequency_specified_field_selector”, or “topk_specified_field_selector”. Default is “uniform”.
- Returns:
A sampled Dataset.
- class data_juicer.core.Exporter(export_path, export_shard_size=0, export_in_parallel=True, num_proc=1, export_ds=True, keep_stats_in_res_ds=False, keep_hashes_in_res_ds=False, export_stats=True)[source]¶
Bases:
object
The Exporter class is used to export a dataset to files of specific format.
- KiB = 1024¶
- MiB = 1048576¶
- GiB = 1073741824¶
- TiB = 1099511627776¶
- __init__(export_path, export_shard_size=0, export_in_parallel=True, num_proc=1, export_ds=True, keep_stats_in_res_ds=False, keep_hashes_in_res_ds=False, export_stats=True)[source]¶
Initialization method.
- Parameters:
export_path – the path to export datasets.
export_shard_size – the size of each shard of exported dataset. In default, it’s 0, which means export the dataset to a single file.
num_proc – number of process to export the dataset.
export_ds – whether to export the dataset contents.
keep_stats_in_res_ds – whether to keep stats in the result dataset.
keep_hashes_in_res_ds – whether to keep hashes in the result dataset.
export_stats – whether to export the stats of dataset.
- export(dataset)[source]¶
Export method for a dataset.
- Parameters:
dataset – the dataset to export.
- Returns:
- export_compute_stats(dataset, export_path)[source]¶
Export method for saving compute status in filters
- static to_jsonl(dataset, export_path, num_proc=1, **kwargs)[source]¶
Export method for jsonl target files.
- Parameters:
dataset – the dataset to export.
export_path – the path to store the exported dataset.
num_proc – the number of processes used to export the dataset.
kwargs – extra arguments.
- Returns:
- static to_json(dataset, export_path, num_proc=1, **kwargs)[source]¶
Export method for json target files.
- Parameters:
dataset – the dataset to export.
export_path – the path to store the exported dataset.
num_proc – the number of processes used to export the dataset.
kwargs – extra arguments.
- Returns:
- class data_juicer.core.Monitor[source]¶
Bases:
object
Monitor resource utilization and other information during the data processing.
Resource utilization dict: (for each func) ‘’’python {
‘time’: 10, ‘resource’: [
- {
‘timestamp’: xxx, ‘CPU count’: xxx, ‘GPU free mem.’: xxx. …
}, {
‘timestamp’: xxx, ‘CPU count’: xxx, ‘GPU free mem.’: xxx, …
},
]
}¶
Based on the structure above, the resource utilization analysis result will add several extra fields on the first level: ‘’’python {
‘time’: 10, ‘resource’: […], ‘resource_analysis’: {
- ‘GPU free mem.’: {
‘max’: xxx, ‘min’: xxx, ‘avg’: xxx,
}
}¶
Only those fields in DYNAMIC_FIELDS will be analyzed.
- DYNAMIC_FIELDS = {'Available mem.', 'CPU util.', 'Free mem.', 'GPU free mem.', 'GPU used mem.', 'GPU util.', 'Mem. util.', 'Used mem.'}¶
- static monitor_current_resources()[source]¶
Detect the resource utilization of the current environment/machine. All data of “util.” is ratios in the range of [0.0, 1.0]. All data of “mem.” is in MB.
- static analyze_resource_util_list(resource_util_list)[source]¶
Analyze the resource utilization for a given resource util list. Compute {‘max’, ‘min’, ‘avg’} of resource metrics for each dict item.
- static analyze_single_resource_util(resource_util_dict)[source]¶
Analyze the resource utilization for a single resource util dict. Compute {‘max’, ‘min’, ‘avg’} of each resource metrics.
- static monitor_func(func, args=None, sample_interval=0.5)[source]¶
Process the input dataset and probe related information for each OP in the specified operator list.
For now, we support the following targets to probe: “resource”: resource utilization for each OP. “speed”: average processing speed for each OP.
The probe result is a list and each item in the list is the probe result for each OP.
- class data_juicer.core.Tracer(work_dir, show_num=10)[source]¶
Bases:
object
The tracer to trace the sample changes before and after an operator process.
The comparison results will be stored in the work directory.
- __init__(work_dir, show_num=10)[source]¶
Initialization method.
- Parameters:
work_dir – the work directory to store the comparison results
show_num – the maximum number of samples to show in the comparison result files.
- trace_mapper(op_name: str, previous_ds: Dataset, processed_ds: Dataset, text_key: str)[source]¶
Compare datasets before and after a Mapper.
This will mainly show the different sample pairs due to the modification by the Mapper
- Parameters:
op_name – the op name of mapper
previous_ds – dataset before the mapper process
processed_ds – dataset processed by the mapper
text_key – which text_key to trace
- Returns:
- trace_batch_mapper(op_name: str, previous_ds: Dataset, processed_ds: Dataset, text_key: str)[source]¶
Compare datasets before and after a BatchMapper.
This will mainly show the new samples augmented by the BatchMapper
- Parameters:
op_name – the op name of mapper
previous_ds – dataset before the mapper process
processed_ds – dataset processed by the mapper
text_key – which text_key to trace
- Returns:
- trace_filter(op_name: str, previous_ds: Dataset, processed_ds: Dataset)[source]¶
Compare datasets before and after a Filter.
This will mainly show the filtered samples by the Filter
- Parameters:
op_name – the op name of filter
previous_ds – dataset before the filter process
processed_ds – dataset processed by the filter
- Returns:
- trace_deduplicator(op_name: str, dup_pairs: list)[source]¶
Compare datasets before and after a Deduplicator.
This will mainly show the near-duplicate sample pairs extracted by the Deduplicator. Different from the other two trace methods, the trace process for deduplicator is embedded into the process method of deduplicator, but the other two trace methods are independent of the process method of mapper and filter operators
- Parameters:
op_name – the op name of deduplicator
dup_pairs – duplicate sample pairs obtained from deduplicator
- Returns: