Skip to content

Tasks

Task preprocessing preserves finite numerical references and metadata through its JSONL transport. Unsupported values must be converted in the preprocessing function before Inspect maps records to samples.

See Numerical task data for supported values, a worked example and migration from older task caches.

Modules:

Name Description
task_preprocessor

Classes:

Name Description
DefaultTaskPreprocessor

Default task preprocessor that returns the input dataset unchanged.

TaskPreprocessingFunction

A protocol for a function that preprocesses datasets.

TaskPreprocessor

Preprocess a dataset and preserve its supported JSON values in task data.

DefaultTaskPreprocessor

Bases: TaskPreprocessor

Default task preprocessor that returns the input dataset unchanged.

Methods:

Name Description
__call__

Preprocesses the input dataset for a specific task.

__init__

Initializes the default task preprocessor.

Attributes:

Name Type Description
cache_key str

Version custom preprocessing rather than guessing its implementation.

Source code in evalsense/tasks/task_preprocessor.py
class DefaultTaskPreprocessor(TaskPreprocessor):
    """Default task preprocessor that returns the input dataset unchanged."""

    def __init__(self, name="Identity", *, version: str | None = None) -> None:
        """Initializes the default task preprocessor."""
        super().__init__(
            name=name,
            preprocessing_function=default_task_preprocessing_function,
            version=version,
        )

cache_key property

cache_key: str

Version custom preprocessing rather than guessing its implementation.

__call__

__call__(
    hf_dataset: Dataset,
    dataset_manager: DatasetManager,
    field_spec: FieldSpec | RecordToSample | None = None,
    force_reprocess: bool = False,
) -> Dataset

Preprocesses the input dataset for a specific task.

Parameters:

Name Type Description Default
hf_dataset Dataset

The input dataset to preprocess, in HuggingFace format.

required
dataset_manager DatasetManager

The dataset manager used to retrieve the dataset.

required
field_spec FieldSpec

Specification mapping dataset fields to sample fields. See Inspect AI documentation for more details.

None
force_reprocess bool

Whether to force reprocess the dataset even if it already exists. Defaults to False.

False

Returns:

Type Description
Dataset

The preprocessed dataset.

Raises:

Type Description
ValueError

A record contains a non-finite floating-point value.

TypeError

A record contains a value unsupported by the JSON transport.

Source code in evalsense/tasks/task_preprocessor.py
def __call__(
    self,
    hf_dataset: datasets.Dataset,
    dataset_manager: DatasetManager,
    field_spec: FieldSpec | RecordToSample | None = None,
    force_reprocess: bool = False,
) -> Dataset:
    """Preprocesses the input dataset for a specific task.

    Args:
        hf_dataset (datasets.Dataset): The input dataset to preprocess,
            in HuggingFace format.
        dataset_manager (DatasetManager): The dataset manager used to
            retrieve the dataset.
        field_spec (FieldSpec): Specification mapping dataset fields to
            sample fields. See Inspect AI documentation for more details.
        force_reprocess (bool): Whether to force reprocess the dataset
            even if it already exists. Defaults to False.

    Returns:
        (Dataset): The preprocessed dataset.

    Raises:
        ValueError: A record contains a non-finite floating-point value.
        TypeError: A record contains a value unsupported by the JSON transport.
    """
    tasks_path = Path(dataset_manager.version_path / "tasks")
    tasks_path.mkdir(parents=True, exist_ok=True)
    data_key = fingerprint(
        {
            "dataset": dataset_manager.record,
            "content": _dataset_fingerprint(hf_dataset),
            "preprocessor": self.cache_key,
        }
    )
    dataset_filename = f"{to_safe_filename(self.name)[:40]}-{data_key}.jsonl"
    task_data_path = tasks_path / dataset_filename

    if not task_data_path.exists() or force_reprocess:
        preprocessed_hf_dataset = self.preprocessing_function(
            hf_dataset, dataset_manager
        )
        write_task_data(preprocessed_hf_dataset, task_data_path)

    dataset = json_dataset(str(task_data_path), sample_fields=field_spec)
    return dataset

__init__

__init__(
    name="Identity", *, version: str | None = None
) -> None

Initializes the default task preprocessor.

Source code in evalsense/tasks/task_preprocessor.py
def __init__(self, name="Identity", *, version: str | None = None) -> None:
    """Initializes the default task preprocessor."""
    super().__init__(
        name=name,
        preprocessing_function=default_task_preprocessing_function,
        version=version,
    )

TaskPreprocessingFunction

Bases: Protocol

A protocol for a function that preprocesses datasets.

You can pass this function to a TaskPreprocessor to perform some task-specific preprocessing on a dataset. This is especially useful in cases in which a single dataset can be used for multiple different tasks, with each requiring different preprocessing steps.

Methods:

Name Description
__call__

Preprocesses the input dataset for a specific task.

Source code in evalsense/tasks/task_preprocessor.py
class TaskPreprocessingFunction(Protocol):
    """A protocol for a function that preprocesses datasets.

    You can pass this function to a TaskPreprocessor to perform some
    task-specific preprocessing on a dataset. This is especially useful
    in cases in which a single dataset can be used for multiple different
    tasks, with each requiring different preprocessing steps.
    """

    def __call__(
        self, hf_dataset: datasets.Dataset, dataset_manager: DatasetManager
    ) -> datasets.Dataset:
        """Preprocesses the input dataset for a specific task.

        Args:
            hf_dataset (datasets.Dataset): The input dataset to preprocess,
                in HuggingFace format.
            dataset_manager (DatasetManager): The dataset manager used to
                retrieve the dataset.

        Returns:
            (datasets.Dataset): The preprocessed dataset.
        """
        ...

__call__

__call__(
    hf_dataset: Dataset, dataset_manager: DatasetManager
) -> datasets.Dataset

Preprocesses the input dataset for a specific task.

Parameters:

Name Type Description Default
hf_dataset Dataset

The input dataset to preprocess, in HuggingFace format.

required
dataset_manager DatasetManager

The dataset manager used to retrieve the dataset.

required

Returns:

Type Description
Dataset

The preprocessed dataset.

Source code in evalsense/tasks/task_preprocessor.py
def __call__(
    self, hf_dataset: datasets.Dataset, dataset_manager: DatasetManager
) -> datasets.Dataset:
    """Preprocesses the input dataset for a specific task.

    Args:
        hf_dataset (datasets.Dataset): The input dataset to preprocess,
            in HuggingFace format.
        dataset_manager (DatasetManager): The dataset manager used to
            retrieve the dataset.

    Returns:
        (datasets.Dataset): The preprocessed dataset.
    """
    ...

TaskPreprocessor

Preprocess a dataset and preserve its supported JSON values in task data.

Stored Arrow values are written to JSONL without decimal-precision rounding. Materialise transformations in the preprocessing function; display formats and lazy transforms are not applied during serialisation. Unsupported values must be converted before the record-to-sample mapper runs.

Methods:

Name Description
__call__

Preprocesses the input dataset for a specific task.

__init__

Initializes the task preprocessor.

Attributes:

Name Type Description
cache_key str

Version custom preprocessing rather than guessing its implementation.

Source code in evalsense/tasks/task_preprocessor.py
class TaskPreprocessor:
    """Preprocess a dataset and preserve its supported JSON values in task data.

    Stored Arrow values are written to JSONL without decimal-precision rounding.
    Materialise transformations in the preprocessing function; display formats
    and lazy transforms are not applied during serialisation. Unsupported values
    must be converted before the record-to-sample mapper runs.
    """

    def __init__(
        self,
        name: str,
        preprocessing_function: TaskPreprocessingFunction,
        *,
        version: str | None = None,
    ) -> None:
        """Initializes the task preprocessor.

        Args:
            name (str): The name of the task preprocessor.
            preprocessing_function (TaskPreprocessingFunction): The function used to
                preprocess the dataset.
            version (str | None): Required for custom code. Bump when its behavior
                or external inputs change.
        """
        self.name = name
        self.preprocessing_function = preprocessing_function
        self.version = version

    @property
    def cache_key(self) -> str:
        """Version custom preprocessing rather than guessing its implementation."""
        return fingerprint(
            {
                "implementation": type(self),
                "function": self.preprocessing_function,
                "transport": TASK_DATA_FORMAT,
            },
            version=self.version,
        )

    def __call__(
        self,
        hf_dataset: datasets.Dataset,
        dataset_manager: DatasetManager,
        field_spec: FieldSpec | RecordToSample | None = None,
        force_reprocess: bool = False,
    ) -> Dataset:
        """Preprocesses the input dataset for a specific task.

        Args:
            hf_dataset (datasets.Dataset): The input dataset to preprocess,
                in HuggingFace format.
            dataset_manager (DatasetManager): The dataset manager used to
                retrieve the dataset.
            field_spec (FieldSpec): Specification mapping dataset fields to
                sample fields. See Inspect AI documentation for more details.
            force_reprocess (bool): Whether to force reprocess the dataset
                even if it already exists. Defaults to False.

        Returns:
            (Dataset): The preprocessed dataset.

        Raises:
            ValueError: A record contains a non-finite floating-point value.
            TypeError: A record contains a value unsupported by the JSON transport.
        """
        tasks_path = Path(dataset_manager.version_path / "tasks")
        tasks_path.mkdir(parents=True, exist_ok=True)
        data_key = fingerprint(
            {
                "dataset": dataset_manager.record,
                "content": _dataset_fingerprint(hf_dataset),
                "preprocessor": self.cache_key,
            }
        )
        dataset_filename = f"{to_safe_filename(self.name)[:40]}-{data_key}.jsonl"
        task_data_path = tasks_path / dataset_filename

        if not task_data_path.exists() or force_reprocess:
            preprocessed_hf_dataset = self.preprocessing_function(
                hf_dataset, dataset_manager
            )
            write_task_data(preprocessed_hf_dataset, task_data_path)

        dataset = json_dataset(str(task_data_path), sample_fields=field_spec)
        return dataset

cache_key property

cache_key: str

Version custom preprocessing rather than guessing its implementation.

__call__

__call__(
    hf_dataset: Dataset,
    dataset_manager: DatasetManager,
    field_spec: FieldSpec | RecordToSample | None = None,
    force_reprocess: bool = False,
) -> Dataset

Preprocesses the input dataset for a specific task.

Parameters:

Name Type Description Default
hf_dataset Dataset

The input dataset to preprocess, in HuggingFace format.

required
dataset_manager DatasetManager

The dataset manager used to retrieve the dataset.

required
field_spec FieldSpec

Specification mapping dataset fields to sample fields. See Inspect AI documentation for more details.

None
force_reprocess bool

Whether to force reprocess the dataset even if it already exists. Defaults to False.

False

Returns:

Type Description
Dataset

The preprocessed dataset.

Raises:

Type Description
ValueError

A record contains a non-finite floating-point value.

TypeError

A record contains a value unsupported by the JSON transport.

Source code in evalsense/tasks/task_preprocessor.py
def __call__(
    self,
    hf_dataset: datasets.Dataset,
    dataset_manager: DatasetManager,
    field_spec: FieldSpec | RecordToSample | None = None,
    force_reprocess: bool = False,
) -> Dataset:
    """Preprocesses the input dataset for a specific task.

    Args:
        hf_dataset (datasets.Dataset): The input dataset to preprocess,
            in HuggingFace format.
        dataset_manager (DatasetManager): The dataset manager used to
            retrieve the dataset.
        field_spec (FieldSpec): Specification mapping dataset fields to
            sample fields. See Inspect AI documentation for more details.
        force_reprocess (bool): Whether to force reprocess the dataset
            even if it already exists. Defaults to False.

    Returns:
        (Dataset): The preprocessed dataset.

    Raises:
        ValueError: A record contains a non-finite floating-point value.
        TypeError: A record contains a value unsupported by the JSON transport.
    """
    tasks_path = Path(dataset_manager.version_path / "tasks")
    tasks_path.mkdir(parents=True, exist_ok=True)
    data_key = fingerprint(
        {
            "dataset": dataset_manager.record,
            "content": _dataset_fingerprint(hf_dataset),
            "preprocessor": self.cache_key,
        }
    )
    dataset_filename = f"{to_safe_filename(self.name)[:40]}-{data_key}.jsonl"
    task_data_path = tasks_path / dataset_filename

    if not task_data_path.exists() or force_reprocess:
        preprocessed_hf_dataset = self.preprocessing_function(
            hf_dataset, dataset_manager
        )
        write_task_data(preprocessed_hf_dataset, task_data_path)

    dataset = json_dataset(str(task_data_path), sample_fields=field_spec)
    return dataset

__init__

__init__(
    name: str,
    preprocessing_function: TaskPreprocessingFunction,
    *,
    version: str | None = None,
) -> None

Initializes the task preprocessor.

Parameters:

Name Type Description Default
name str

The name of the task preprocessor.

required
preprocessing_function TaskPreprocessingFunction

The function used to preprocess the dataset.

required
version str | None

Required for custom code. Bump when its behavior or external inputs change.

None
Source code in evalsense/tasks/task_preprocessor.py
def __init__(
    self,
    name: str,
    preprocessing_function: TaskPreprocessingFunction,
    *,
    version: str | None = None,
) -> None:
    """Initializes the task preprocessor.

    Args:
        name (str): The name of the task preprocessor.
        preprocessing_function (TaskPreprocessingFunction): The function used to
            preprocess the dataset.
        version (str | None): Required for custom code. Bump when its behavior
            or external inputs change.
    """
    self.name = name
    self.preprocessing_function = preprocessing_function
    self.version = version