Skip to main content

Profile, filter and re-pack a dataset

Source on GitHub: the workflows/dataset-statistics folder, with its README and its workflow file.

Looks at a Parquet dataset before it is used, then cleans and re-packs it, all with DuckDB on a CPU. The first task profiles every column (type, distinct values, minimum, maximum, average, quartiles, nulls) and the length of the text column; the second drops exact duplicates of the text and rows outside a length range, and writes the rest as evenly sized Parquet files.

StepTask definitionCompute
Profile the columns and the text lengthsswaleio/duckdbcpu-4
Drop duplicates and outliers, re-pack into Parquet filesswaleio/duckdbcpu-4
Upload the files, when hub_repo is setswaleio/hf-uploadcpu-4

The Compute column gives each step's compute type, the compute its tasks are allocated.

Every step is its own task.

Inputs​

InputDefaultDescription
datasetthe train_sft split of HuggingFaceH4/ultrafeedback_binarizedParquet file or glob, as a DuckDB path.
text_columnpromptColumn deduplicated and filtered on.
min_chars20Shortest text kept.
max_chars4000Longest text kept.
rows_per_file20000Rows per output file, roughly.
hub_repoemptyHugging Face dataset repo to upload the files to, under data/.

The upload reads the secret hf_token. For datasets of tens of gigabytes, give the tasks cpu-16.

Output​

packed/train-0.parquet, packed/train-1.parquet and so on, on the workspace. The profile is in the first task's log.

Run it​

You need the CLI, logged in to your account, and a project to add the workflow to; Getting started sets them up.

  1. Save the file under The workflow file as workflow.yaml, and add it to a project:

    swale workflow create --project-name my-project --file workflow.yaml

    The command prints Workflow created: followed by the workflow's ID.

  2. The workflow reads the secret hf_token. Create it as a project secret, or as an account secret that allows the project, as Use secrets in workflows shows.

  3. Start a run with the workflow's ID:

    swale workflow run start --project-name my-project --workflow-id <workflow-id>

    Each input you leave out takes its default from Inputs, and --input <name>=<value> gives it another value.

Create and manage workflows covers the other ways to add a workflow, Start and manage workflow runs how to follow a run, and View and stream task logs how to read its tasks' logs.

The workflow file​

Workflow definition syntax describes each key in it.

workflow.yaml
name: Profile filter and re-pack a dataset
description: Profiles a Parquet dataset's columns, drops exact duplicates and rows outside a length range, and re-packs the rest into evenly sized Parquet files.
compute_type: cpu-4

inputs:
dataset:
description: Parquet file or glob, as a DuckDB path such as hf://datasets/<owner>/<dataset>/<file>.
default: hf://datasets/HuggingFaceH4/ultrafeedback_binarized/data/train_sft-*.parquet
text_column:
description: Column whose duplicates are dropped and whose length is filtered on.
default: prompt
min_chars:
description: Shortest text to keep, in characters.
default: "20"
max_chars:
description: Longest text to keep, in characters.
default: "4000"
rows_per_file:
description: Rows in each output Parquet file, roughly.
default: "20000"
hub_repo:
description: Hugging Face dataset repo to upload the files to, as namespace/name. Leave empty to keep them on the workspace only.
default: ""

entry_point: main

blocks:
main:
tasks:
profile:
name: Profile
uses: swaleio/duckdb
args:
sql: |
CREATE TABLE source AS SELECT * FROM '${{inputs.dataset}}';
SUMMARIZE source;
SELECT count(*) AS rows,
count(DISTINCT "${{inputs.text_column}}") AS distinct_texts,
min(length("${{inputs.text_column}}")) AS shortest,
round(median(length("${{inputs.text_column}}"))) AS median_length,
max(length("${{inputs.text_column}}")) AS longest
FROM source;
SELECT (length("${{inputs.text_column}}") // 500) * 500 AS length_from, count(*) AS rows
FROM source GROUP BY ALL ORDER BY length_from LIMIT 20;

filter_and_pack:
name: Filter and re-pack
uses: swaleio/duckdb
start_on:
- profile
args:
sql: |
CREATE TABLE kept AS
SELECT * EXCLUDE (copy) FROM (
SELECT *, row_number() OVER (PARTITION BY "${{inputs.text_column}}" ORDER BY "${{inputs.text_column}}") AS copy
FROM '${{inputs.dataset}}'
WHERE length("${{inputs.text_column}}") BETWEEN ${{inputs.min_chars}} AND ${{inputs.max_chars}})
WHERE copy = 1;
SELECT count(*) AS kept FROM kept;
-- One row group of about rows_per_file rows per file, written single-threaded
-- so the files fill in order.
SET threads = 1;
COPY kept TO '${{env.WORKFLOW_STORAGE}}/packed'
(FORMAT parquet, ROW_GROUP_SIZE ${{inputs.rows_per_file}}, ROW_GROUPS_PER_FILE 1,
FILENAME_PATTERN 'train-{i}', OVERWRITE_OR_IGNORE);
SELECT parse_filename(filename) AS file, count(*) AS rows
FROM read_parquet('${{env.WORKFLOW_STORAGE}}/packed/*.parquet', filename = true)
GROUP BY ALL ORDER BY file;

publish:
name: Upload when asked
start_on:
- filter_and_pack
if:
condition: ${{inputs.hub_repo}}
true: upload

upload:
tasks:
upload_files:
name: Upload to the Hugging Face Hub
uses: swaleio/hf-upload
args:
repo: ${{inputs.hub_repo}}
repo_type: dataset
source: ${{env.WORKFLOW_STORAGE}}/packed
path_in_repo: data
token: ${{secrets.hf_token}}

Licenses of the defaults​