{"id":27166784,"url":"https://github.com/Lightning-AI/litData","last_synced_at":"2025-04-09T04:02:36.915Z","repository":{"id":223321058,"uuid":"758163683","full_name":"Lightning-AI/litData","owner":"Lightning-AI","description":"Transform datasets at scale. Optimize datasets for fast AI model training.","archived":false,"fork":false,"pushed_at":"2025-03-29T20:41:56.000Z","size":2698,"stargazers_count":440,"open_issues_count":60,"forks_count":59,"subscribers_count":14,"default_branch":"main","last_synced_at":"2025-04-05T16:02:35.411Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Lightning-AI.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":".github/CODEOWNERS","security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-02-15T18:44:16.000Z","updated_at":"2025-04-04T23:10:40.000Z","dependencies_parsed_at":"2024-05-19T09:22:54.154Z","dependency_job_id":"ad08263d-9c4a-4623-884d-a3ffe34b5af5","html_url":"https://github.com/Lightning-AI/litData","commit_stats":null,"previous_names":["lightning-ai/lit-data","lightning-ai/litdata"],"tags_count":37,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Lightning-AI%2FlitData","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Lightning-AI%2FlitData/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Lightning-AI%2FlitData/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Lightning-AI%2FlitData/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Lightning-AI","download_url":"https://codeload.github.com/Lightning-AI/litData/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247974712,"owners_count":21026742,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-04-09T04:02:35.787Z","updated_at":"2025-04-09T04:02:36.888Z","avatar_url":"https://github.com/Lightning-AI.png","language":"Python","funding_links":[],"categories":["Python"],"sub_categories":[],"readme":"\u003cdiv align=\"center\"\u003e\n\u003cimg src=\"https://pl-flash-data.s3.amazonaws.com/lit_data_logo.webp\" alt=\"LitData\" width=\"800px\"/\u003e\n\n\u0026nbsp;\n\u0026nbsp;\n\n**Transform datasets at scale.    \nOptimize data for fast AI model training.**\n\n\n\u003cpre\u003e\nTransform                              Optimize\n  \n✅ Parallelize data processing       ✅ Stream large cloud datasets          \n✅ Create vector embeddings          ✅ Accelerate training by 20x           \n✅ Run distributed inference         ✅ Pause and resume data streaming      \n✅ Scrape websites at scale          ✅ Use remote data without local loading\n\u003c/pre\u003e\n\n---\n\n![PyPI](https://img.shields.io/pypi/v/litdata)\n![Downloads](https://img.shields.io/pypi/dm/litdata)\n![License](https://img.shields.io/github/license/Lightning-AI/litdata)\n[![Discord](https://img.shields.io/discord/1077906959069626439?label=Get%20Help%20on%20Discord)](https://discord.gg/VptPCZkGNa)\n\n\u003cp align=\"center\"\u003e\n  \u003ca href=\"https://lightning.ai/\"\u003eLightning AI\u003c/a\u003e •\n  \u003ca href=\"#quick-start\"\u003eQuick start\u003c/a\u003e •\n  \u003ca href=\"#speed-up-model-training\"\u003eOptimize data\u003c/a\u003e •\n  \u003ca href=\"#transform-datasets\"\u003eTransform data\u003c/a\u003e •\n  \u003ca href=\"#key-features\"\u003eFeatures\u003c/a\u003e •\n  \u003ca href=\"#benchmarks\"\u003eBenchmarks\u003c/a\u003e •\n  \u003ca href=\"#start-from-a-template\"\u003eTemplates\u003c/a\u003e •\n  \u003ca href=\"#community\"\u003eCommunity\u003c/a\u003e\n\u003c/p\u003e\n\n\u0026nbsp;\n\n\u003ca target=\"_blank\" href=\"https://lightning.ai/docs/overview/prep-data/optimize-datasets-for-model-training-speed\"\u003e\n  \u003cimg src=\"https://pl-bolts-doc-images.s3.us-east-2.amazonaws.com/app-2/get-started-badge.svg\" height=\"36px\" alt=\"Get started\"/\u003e\n\u003c/a\u003e\n\n\u003c/div\u003e\n\n\u0026nbsp;\n\n# Transform data at scale. Optimize for fast model training.\nLitData scales [data processing tasks](#transform-datasets) (data scraping, image resizing, distributed inference, embedding creation) on local or cloud machines. It also enables [optimizing datasets](#speed-up-model-training) to accelerate AI model training and work with large remote datasets without local loading.\n\n\u0026nbsp;\n\n# Quick start\nFirst, install LitData:\n\n```bash\npip install litdata\n```\n\nChoose your workflow:\n\n🚀 [Speed up model training](#speed-up-model-training)    \n🚀 [Transform datasets](#transform-datasets)\n\n\u0026nbsp;\n\n\u003cdetails\u003e\n  \u003csummary\u003eAdvanced install\u003c/summary\u003e\n\nInstall all the extras\n```bash\npip install 'litdata[extras]'\n```\n\n\u003c/details\u003e\n\n\u0026nbsp;\n\n----\n\n# Speed up model training\nAccelerate model training (20x faster) by optimizing datasets for streaming directly from cloud storage. Work with remote data without local downloads with features like loading data subsets, accessing individual samples, and resumable streaming.\n\n**Step 1: Optimize the data**\nThis step will format the dataset for fast loading. The data will be written in a chunked binary format.\n\n```python\nimport numpy as np\nfrom PIL import Image\nimport litdata as ld\n\ndef random_images(index):\n    fake_images = Image.fromarray(np.random.randint(0, 256, (32, 32, 3), dtype=np.uint8))\n    fake_labels = np.random.randint(10)\n\n    # You can use any key:value pairs. Note that their types must not change between samples, and Python lists must\n    # always contain the same number of elements with the same types.\n    data = {\"index\": index, \"image\": fake_images, \"class\": fake_labels}\n\n    return data\n\nif __name__ == \"__main__\":\n    # The optimize function writes data in an optimized format.\n    ld.optimize(\n        fn=random_images,                   # the function applied to each input\n        inputs=list(range(1000)),           # the inputs to the function (here it's a list of numbers)\n        output_dir=\"fast_data\",             # optimized data is stored here\n        num_workers=4,                      # The number of workers on the same machine\n        chunk_bytes=\"64MB\"                  # size of each chunk\n    )\n```\n\n**Step 2: Put the data on the cloud**\n\nUpload the data to a [Lightning Studio](https://lightning.ai) (backed by S3) or your own S3 bucket:\n```bash\naws s3 cp --recursive fast_data s3://my-bucket/fast_data\n```\n\n**Step 3: Stream the data during training**\n\nLoad the data by replacing the PyTorch DataSet and DataLoader with the StreamingDataset and StreamingDataloader\n\n```python\nimport litdata as ld\n\ntrain_dataset = ld.StreamingDataset('s3://my-bucket/fast_data', shuffle=True, drop_last=True)\ntrain_dataloader = ld.StreamingDataLoader(train_dataset)\n\nfor sample in train_dataloader:\n    img, cls = sample['image'], sample['class']\n```\n\n**Key benefits:**\n\n✅ Accelerate training:       Optimized datasets load 20x faster.      \n✅ Stream cloud datasets:     Work with cloud data without downloading it.    \n✅ Pytorch-first:             Works with PyTorch libraries like PyTorch Lightning, Lightning Fabric, Hugging Face.    \n✅ Easy collaboration:        Share and access datasets in the cloud, streamlining team projects.     \n✅ Scale across GPUs:         Streamed data automatically scales to all GPUs.      \n✅ Flexible storage:          Use S3, GCS, Azure, or your own cloud account for data storage.    \n✅ Compression:               Reduce your data footprint by using advanced compression algorithms.  \n✅ Run local or cloud:        Run on your own machines or auto-scale to 1000s of cloud GPUs with Lightning Studios.         \n✅ Enterprise security:       Self host or process data on your cloud account with Lightning Studios.  \n\n\u0026nbsp;\n\n----\n\n# Transform datasets\nAccelerate data processing tasks (data scraping, image resizing, embedding creation, distributed inference) by parallelizing (map) the work across many machines at once.\n\nHere's an example that resizes and crops a large image dataset:\n\n```python\nfrom PIL import Image\nimport litdata as ld\n\n# use a local or S3 folder\ninput_dir = \"my_large_images\"     # or \"s3://my-bucket/my_large_images\"\noutput_dir = \"my_resized_images\"  # or \"s3://my-bucket/my_resized_images\"\n\ninputs = [os.path.join(input_dir, f) for f in os.listdir(input_dir)]\n\n# resize the input image\ndef resize_image(image_path, output_dir):\n  output_image_path = os.path.join(output_dir, os.path.basename(image_path))\n  Image.open(image_path).resize((224, 224)).save(output_image_path)\n\nld.map(\n    fn=resize_image,\n    inputs=inputs,\n    output_dir=\"output_dir\",\n)\n```\n\n**Key benefits:**\n\n✅ Parallelize processing:    Reduce processing time by transforming data across multiple machines simultaneously.    \n✅ Scale to large data:       Increase the size of datasets you can efficiently handle.    \n✅ Flexible usecases:         Resize images, create embeddings, scrape the internet, etc...    \n✅ Run local or cloud:        Run on your own machines or auto-scale to 1000s of cloud GPUs with Lightning Studios.         \n✅ Enterprise security:       Self host or process data on your cloud account with Lightning Studios.  \n\n\u0026nbsp;\n\n----\n\n# Key Features\n\n## Features for optimizing and streaming datasets for model training\n\n\u003cdetails\u003e\n  \u003csummary\u003e ✅ Stream large cloud datasets\u003c/summary\u003e\n\u0026nbsp;\n\nUse data stored on the cloud without needing to download it all to your computer, saving time and space.\n\nImagine you're working on a project with a huge amount of data stored online. Instead of waiting hours to download it all, you can start working with the data almost immediately by streaming it.\n\nOnce you've optimized the dataset with LitData, stream it as follows:\n```python\nfrom litdata import StreamingDataset, StreamingDataLoader\n\ndataset = StreamingDataset('s3://my-bucket/my-data', shuffle=True)\ndataloader = StreamingDataLoader(dataset, batch_size=64)\n\nfor batch in dataloader:\n    process(batch)  # Replace with your data processing logic\n\n```\n\n\nAdditionally, you can inject client connection settings for [S3](https://boto3.amazonaws.com/v1/documentation/api/latest/reference/core/session.html#boto3.session.Session.client) or GCP when initializing your dataset. This is useful for specifying custom endpoints and credentials per dataset.\n\n```python\nfrom litdata import StreamingDataset\n\n# boto3 compatible storage options for a custom S3-compatible endpoint\nstorage_options = {\n    \"endpoint_url\": \"your_endpoint_url\",\n    \"aws_access_key_id\": \"your_access_key_id\",\n    \"aws_secret_access_key\": \"your_secret_access_key\",\n}\n\ndataset = StreamingDataset('s3://my-bucket/my-data', storage_options=storage_options)\n\n# s5cmd compatible storage options for a custom S3-compatible endpoint\n# Note: If s5cmd is installed, it will be used by default for S3 operations. If you prefer not to use s5cmd, you can disable it by setting the environment variable: `DISABLE_S5CMD=1`\nstorage_options = {\n    \"AWS_ACCESS_KEY_ID\": \"your_access_key_id\",\n    \"AWS_SECRET_ACCESS_KEY\": \"your_secret_access_key\",\n    \"S3_ENDPOINT_URL\": \"your_endpoint_url\",  # Required only for custom endpoints\n}\n\n\ndataset = StreamingDataset('s3://my-bucket/my-data', storage_options=storage_options)\n```\n\nAlternative: Using `s5cmd` for S3 Operations\n\n\nAlso, you can specify a custom cache directory when initializing your dataset. This is useful when you want to store the cache in a specific location.\n```python\nfrom litdata import StreamingDataset\n\n# Initialize the StreamingDataset with the custom cache directory\ndataset = StreamingDataset('s3://my-bucket/my-data', cache_dir=\"/path/to/cache\")\n```\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\n  \u003csummary\u003e ✅ Stream Hugging Face 🤗 datasets\u003c/summary\u003e\n\n\u0026nbsp;\n\nTo use your favorite  Hugging Face dataset with LitData, simply pass its URL to `StreamingDataset`.\n\n\u003cdetails\u003e\n  \u003csummary\u003eHow to get HF dataset URI?\u003c/summary\u003e\n\nhttps://github.com/user-attachments/assets/3ba9e2ef-bf6b-41fc-a578-e4b4113a0e72\n\n\u003c/details\u003e\n\n**Prerequisites:**\n\nInstall the required dependencies to stream Hugging Face datasets:\n```sh\npip install \"litdata[extra]\" huggingface_hub\n\n# Optional: To speed up downloads on high-bandwidth networks\npip install hf_tansfer\nexport HF_HUB_ENABLE_HF_TRANSFER=1\n```\n\n**Stream Hugging Face dataset:**\n\n```python\nimport litdata as ld\n\n# Define the Hugging Face dataset URI\nhf_dataset_uri = \"hf://datasets/leonardPKU/clevr_cogen_a_train/data\"\n\n# Create a streaming dataset\ndataset = ld.StreamingDataset(hf_dataset_uri)\n\n# Print the first sample\nprint(\"Sample\", dataset[0])\n\n# Stream the dataset using StreamingDataLoader\ndataloader = ld.StreamingDataLoader(dataset, batch_size=4)\nfor sample in dataloader:\n    pass \n```\n\nYou don’t need to worry about indexing the dataset or any other setup. **LitData** will **handle all the necessary steps automatically** and `cache` the `index.json` file, so you won't have to index it again.\n\nThis ensures that the next time you stream the dataset, the indexing step is skipped..\n\n\u0026nbsp;\n\n### Indexing the HF dataset (Optional)\n\nIf the Hugging Face dataset hasn't been indexed yet, you can index it first using the `index_hf_dataset` method, and then stream it using the code above.\n\n```python\nimport litdata as ld\n\nhf_dataset_uri = \"hf://datasets/leonardPKU/clevr_cogen_a_train/data\"\n\nld.index_hf_dataset(hf_dataset_uri)\n```\n\n- Indexing the Hugging Face dataset ahead of time will make streaming abit faster, as it avoids the need for real-time indexing during streaming.\n\n- To use `HF gated dataset`, ensure the `HF_TOKEN` environment variable is set.\n\n**Note**: For HuggingFace datasets, `indexing` \u0026 `streaming` is supported only for datasets in **`Parquet format`**.\n\n\u0026nbsp;\n\n### Full Workflow for Hugging Face Datasets\n\nFor full control over the cache path(`where index.json file will be stored`) and other configurations, follow these steps:\n\n1. Index the Hugging Face dataset first:\n\n```python\nimport litdata as ld\n\nhf_dataset_uri = \"hf://datasets/open-thoughts/OpenThoughts-114k/data\"\n\nld.index_parquet_dataset(hf_dataset_uri, \"hf-index-dir\")\n```\n\n2. To stream HF datasets now, pass the `HF dataset URI`, the path where the `index.json` file is stored, and `ParquetLoader` as the `item_loader` to the **`StreamingDataset`**:\n\n```python\nimport litdata as ld\nfrom litdata.streaming.item_loader import ParquetLoader\n\nhf_dataset_uri = \"hf://datasets/open-thoughts/OpenThoughts-114k/data\"\n\ndataset = ld.StreamingDataset(hf_dataset_uri, item_loader=ParquetLoader(), index_path=\"hf-index-dir\")\n\nfor batch in ld.StreamingDataLoader(dataset, batch_size=4):\n  pass\n```\n\n\u0026nbsp;\n\n### LitData `Optimize` v/s `Parquet`\n\u003c!-- TODO: Update benchmark --\u003e\nBelow is the benchmark for the `Imagenet dataset (155 GB)`, demonstrating that **`optimizing the dataset using LitData is faster and results in smaller output size compared to raw Parquet files`**.\n\n| **Operation**                    | **Size (GB)** | **Time (seconds)** | **Throughput (images/sec)** |\n|-----------------------------------|---------------|---------------------|-----------------------------|\n| LitData Optimize Dataset          | 45            | 283.17             | 4000-4700                  |\n| Parquet Optimize Dataset          | 51            | 465.96             | 3600-3900                  |\n| Index Parquet Dataset (overhead)  | N/A           | 6                  | N/A                         |\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\n  \u003csummary\u003e ✅ Streams on multi-GPU, multi-node\u003c/summary\u003e\n\n\u0026nbsp;\n\nData optimized and loaded with Lightning automatically streams efficiently in distributed training across GPUs or multi-node.\n\nThe `StreamingDataset` and `StreamingDataLoader` automatically make sure each rank receives the same quantity of varied batches of data, so it works out of the box with your favorite frameworks ([PyTorch Lightning](https://lightning.ai/docs/pytorch/stable/), [Lightning Fabric](https://lightning.ai/docs/fabric/stable/), or [PyTorch](https://pytorch.org/docs/stable/index.html)) to do distributed training.\n\nHere you can see an illustration showing how the Streaming Dataset works with multi node / multi gpu under the hood.\n\n```python\nfrom litdata import StreamingDataset, StreamingDataLoader\n\n# For the training dataset, don't forget to enable shuffle and drop_last !!! \ntrain_dataset = StreamingDataset('s3://my-bucket/my-train-data', shuffle=True, drop_last=True)\ntrain_dataloader = StreamingDataLoader(train_dataset, batch_size=64)\n\nfor batch in train_dataloader:\n    process(batch)  # Replace with your data processing logic\n\nval_dataset = StreamingDataset('s3://my-bucket/my-val-data', shuffle=False, drop_last=False)\nval_dataloader = StreamingDataLoader(val_dataset, batch_size=64)\n\nfor batch in val_dataloader:\n    process(batch)  # Replace with your data processing logic\n```\n\n![An illustration showing how the Streaming Dataset works with multi node.](https://pl-flash-data.s3.amazonaws.com/streaming_dataset.gif)\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\n  \u003csummary\u003e ✅ Stream from multiple cloud providers\u003c/summary\u003e\n\n\u0026nbsp;\n\nThe `StreamingDataset` provides support for reading optimized datasets from common cloud storage providers like AWS S3, Google Cloud Storage (GCS), and Azure Blob Storage. Below are examples of how to use StreamingDataset with each cloud provider.\n\n```python\nimport os\nimport litdata as ld\n\n# Read data from AWS S3 using s5cmd\n# Note: If s5cmd is installed, it will be used by default for S3 operations. If you prefer not to use s5cmd, you can disable it by setting the environment variable: `DISABLE_S5CMD=1`\naws_storage_options={\n    \"AWS_ACCESS_KEY_ID\": os.environ['AWS_ACCESS_KEY_ID'],\n    \"AWS_SECRET_ACCESS_KEY\": os.environ['AWS_SECRET_ACCESS_KEY'],\n    \"S3_ENDPOINT_URL\": os.environ['AWS_ENDPOINT_URL'],  # Required only for custom endpoints\n}\ndataset = ld.StreamingDataset(\"s3://my-bucket/my-data\", storage_options=aws_storage_options)\n\n# Read Data from AWS S3 with Unsigned Request using s5cmd\naws_storage_options={\n  \"AWS_NO_SIGN_REQUEST\": \"Yes\" # Required for unsigned requests\n  \"S3_ENDPOINT_URL\": os.environ['AWS_ENDPOINT_URL'],  # Required only for custom endpoints\n}\ndataset = ld.StreamingDataset(\"s3://my-bucket/my-data\", storage_options=aws_storage_options)\n\n# Read data from AWS S3 using boto3\nos.environ[\"DISABLE_S5CMD\"] = \"1\"\naws_storage_options={\n    \"aws_access_key_id\": os.environ['AWS_ACCESS_KEY_ID'],\n    \"aws_secret_access_key\": os.environ['AWS_SECRET_ACCESS_KEY'],\n}\ndataset = ld.StreamingDataset(\"s3://my-bucket/my-data\", storage_options=aws_storage_options)\n\n\n# Read data from GCS\ngcp_storage_options={\n    \"project\": os.environ['PROJECT_ID'],\n}\ndataset = ld.StreamingDataset(\"gs://my-bucket/my-data\", storage_options=gcp_storage_options)\n\n# Read data from Azure\nazure_storage_options={\n    \"account_url\": f\"https://{os.environ['AZURE_ACCOUNT_NAME']}.blob.core.windows.net\",\n    \"credential\": os.environ['AZURE_ACCOUNT_ACCESS_KEY']\n}\ndataset = ld.StreamingDataset(\"azure://my-bucket/my-data\", storage_options=azure_storage_options)\n```\n\n\u003c/details\u003e  \n\n\u003cdetails\u003e\n  \u003csummary\u003e ✅ Pause, resume data streaming\u003c/summary\u003e\n\u0026nbsp;\n\nStream data during long training, if interrupted, pick up right where you left off without any issues.\n\nLitData provides a stateful `Streaming DataLoader` e.g. you can `pause` and `resume` your training whenever you want.\n\nInfo: The `Streaming DataLoader` was used by [Lit-GPT](https://github.com/Lightning-AI/lit-gpt/blob/main/pretrain/tinyllama.py) to pretrain LLMs. Restarting from an older checkpoint was critical to get to pretrain the full model due to several failures (network, CUDA Errors, etc..).\n\n```python\nimport os\nimport torch\nfrom litdata import StreamingDataset, StreamingDataLoader\n\ndataset = StreamingDataset(\"s3://my-bucket/my-data\", shuffle=True)\ndataloader = StreamingDataLoader(dataset, num_workers=os.cpu_count(), batch_size=64)\n\n# Restore the dataLoader state if it exists\nif os.path.isfile(\"dataloader_state.pt\"):\n    state_dict = torch.load(\"dataloader_state.pt\")\n    dataloader.load_state_dict(state_dict)\n\n# Iterate over the data\nfor batch_idx, batch in enumerate(dataloader):\n\n    # Store the state every 1000 batches\n    if batch_idx % 1000 == 0:\n        torch.save(dataloader.state_dict(), \"dataloader_state.pt\")\n```\n\n\u003c/details\u003e\n\n\n\u003cdetails\u003e\n  \u003csummary\u003e ✅ LLM Pre-training \u003c/summary\u003e\n\u0026nbsp;\n\nLitData is highly optimized for LLM pre-training. First, we need to tokenize the entire dataset and then we can consume it.\n\n```python\nimport json\nfrom pathlib import Path\nimport zstandard as zstd\nfrom litdata import optimize, TokensLoader\nfrom tokenizer import Tokenizer\nfrom functools import partial\n\n# 1. Define a function to convert the text within the jsonl files into tokens\ndef tokenize_fn(filepath, tokenizer=None):\n    with zstd.open(open(filepath, \"rb\"), \"rt\", encoding=\"utf-8\") as f:\n        for row in f:\n            text = json.loads(row)[\"text\"]\n            if json.loads(row)[\"meta\"][\"redpajama_set_name\"] == \"RedPajamaGithub\":\n                continue  # exclude the GitHub data since it overlaps with starcoder\n            text_ids = tokenizer.encode(text, bos=False, eos=True)\n            yield text_ids\n\nif __name__ == \"__main__\":\n    # 2. Generate the inputs (we are going to optimize all the compressed json files from SlimPajama dataset )\n    input_dir = \"./slimpajama-raw\"\n    inputs = [str(file) for file in Path(f\"{input_dir}/SlimPajama-627B/train\").rglob(\"*.zst\")]\n\n    # 3. Store the optimized data wherever you want under \"/teamspace/datasets\" or \"/teamspace/s3_connections\"\n    outputs = optimize(\n        fn=partial(tokenize_fn, tokenizer=Tokenizer(f\"{input_dir}/checkpoints/Llama-2-7b-hf\")), # Note: You can use HF tokenizer or any others\n        inputs=inputs,\n        output_dir=\"./slimpajama-optimized\",\n        chunk_size=(2049 * 8012),\n        # This is important to inform LitData that we are encoding contiguous 1D array (tokens). \n        # LitData skips storing metadata for each sample e.g all the tokens are concatenated to form one large tensor.\n        item_loader=TokensLoader(),\n    )\n```\n\n```python\nimport os\nfrom litdata import StreamingDataset, StreamingDataLoader, TokensLoader\nfrom tqdm import tqdm\n\n# Increase by one because we need the next word as well\ndataset = StreamingDataset(\n  input_dir=f\"./slimpajama-optimized/train\",\n  item_loader=TokensLoader(block_size=2048 + 1),\n  shuffle=True,\n  drop_last=True,\n)\n\ntrain_dataloader = StreamingDataLoader(dataset, batch_size=8, pin_memory=True, num_workers=os.cpu_count())\n\n# Iterate over the SlimPajama dataset\nfor batch in tqdm(train_dataloader):\n    pass\n```\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\n  \u003csummary\u003e ✅ Filter illegal data \u003c/summary\u003e\n\u0026nbsp;\n\nSometimes, you have bad data that you don't want to include in the optimized dataset. With LitData, yield only the good data sample to include. \n\n\n```python\nfrom litdata import optimize, StreamingDataset\n\ndef should_keep(index) -\u003e bool:\n  # Replace with your own logic\n  return index % 2 == 0\n\n\ndef fn(data):\n    if should_keep(data):\n        yield data\n\nif __name__ == \"__main__\":\n    optimize(\n        fn=fn,\n        inputs=list(range(1000)),\n        output_dir=\"only_even_index_optimized\",\n        chunk_bytes=\"64MB\",\n        num_workers=1\n    )\n\n    dataset = StreamingDataset(\"only_even_index_optimized\")\n    data = list(dataset)\n    print(data)\n    # [0, 2, 4, 6, 8, 10, ..., 992, 994, 996, 998]\n```\n\nYou can even use try/expect.  \n\n```python\nfrom litdata import optimize, StreamingDataset\n\ndef fn(data):\n    try:\n        yield 1 / data \n    except:\n        pass\n\nif __name__ == \"__main__\":\n    optimize(\n        fn=fn,\n        inputs=[0, 0, 0, 1, 2, 4, 0],\n        output_dir=\"only_defined_ratio_optimized\",\n        chunk_bytes=\"64MB\",\n        num_workers=1\n    )\n\n    dataset = StreamingDataset(\"only_defined_ratio_optimized\")\n    data = list(dataset)\n    # The 0 are filtered out as they raise a division by zero \n    print(data)\n    # [1.0, 0.5, 0.25] \n```\n\u003c/details\u003e\n\n\u003cdetails\u003e\n  \u003csummary\u003e ✅ Combine datasets\u003c/summary\u003e\n\u0026nbsp;\n\nMix and match different sets of data to experiment and create better models.\n\nCombine datasets with `CombinedStreamingDataset`.  As an example, this mixture of [Slimpajama](https://huggingface.co/datasets/cerebras/SlimPajama-627B) \u0026 [StarCoder](https://huggingface.co/datasets/bigcode/starcoderdata) was used in the [TinyLLAMA](https://github.com/jzhang38/TinyLlama) project to pretrain a 1.1B Llama model on 3 trillion tokens.\n\n```python\nfrom litdata import StreamingDataset, CombinedStreamingDataset, StreamingDataLoader, TokensLoader\nfrom tqdm import tqdm\nimport os\n\ntrain_datasets = [\n    StreamingDataset(\n        input_dir=\"s3://tinyllama-template/slimpajama/train/\",\n        item_loader=TokensLoader(block_size=2048 + 1), # Optimized loader for tokens used by LLMs\n        shuffle=True,\n        drop_last=True,\n    ),\n    StreamingDataset(\n        input_dir=\"s3://tinyllama-template/starcoder/\",\n        item_loader=TokensLoader(block_size=2048 + 1), # Optimized loader for tokens used by LLMs\n        shuffle=True,\n        drop_last=True,\n    ),\n]\n\n# Mix SlimPajama data and Starcoder data with these proportions:\nweights = (0.693584, 0.306416)\ncombined_dataset = CombinedStreamingDataset(datasets=train_datasets, seed=42, weights=weights, iterate_over_all=False)\n\ntrain_dataloader = StreamingDataLoader(combined_dataset, batch_size=8, pin_memory=True, num_workers=os.cpu_count())\n\n# Iterate over the combined datasets\nfor batch in tqdm(train_dataloader):\n    pass\n```\n\u003c/details\u003e\n\n\u003cdetails\u003e\n  \u003csummary\u003e ✅ Merge datasets\u003c/summary\u003e\n\u0026nbsp;\n\nMerge multiple optimized datasets into one.\n\n```python\nimport numpy as np\nfrom PIL import Image\n\nfrom litdata import StreamingDataset, merge_datasets, optimize\n\n\ndef random_images(index):\n    return {\n        \"index\": index,\n        \"image\": Image.fromarray(np.random.randint(0, 256, (32, 32, 3), dtype=np.uint8)),\n        \"class\": np.random.randint(10),\n    }\n\n\nif __name__ == \"__main__\":\n    out_dirs = [\"fast_data_1\", \"fast_data_2\", \"fast_data_3\", \"fast_data_4\"]  # or [\"s3://my-bucket/fast_data_1\", etc.]\"\n    for out_dir in out_dirs:\n        optimize(fn=random_images, inputs=list(range(250)), output_dir=out_dir, num_workers=4, chunk_bytes=\"64MB\")\n\n    merged_out_dir = \"merged_fast_data\" # or \"s3://my-bucket/merged_fast_data\"\n    merge_datasets(input_dirs=out_dirs, output_dir=merged_out_dir)\n\n    dataset = StreamingDataset(merged_out_dir)\n    print(len(dataset))\n    # out: 1000\n```\n\u003c/details\u003e\n\n\u003cdetails\u003e\n  \u003csummary\u003e ✅ Split datasets for train, val, test\u003c/summary\u003e\n\n\u0026nbsp;\n\nSplit a dataset into train, val, test splits with `train_test_split`.\n\n```python\nfrom litdata import StreamingDataset, train_test_split\n\ndataset = StreamingDataset(\"s3://my-bucket/my-data\") # data are stored in the cloud\n\nprint(len(dataset)) # display the length of your data\n# out: 100,000\n\ntrain_dataset, val_dataset, test_dataset = train_test_split(dataset, splits=[0.3, 0.2, 0.5])\n\nprint(train_dataset)\n# out: 30,000\n\nprint(val_dataset)\n# out: 20,000\n\nprint(test_dataset)\n# out: 50,000\n```\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\n  \u003csummary\u003e ✅ Load a subset of the remote dataset\u003c/summary\u003e\n\n\u0026nbsp;\nWork on a smaller, manageable portion of your data to save time and resources.\n\n\n```python\nfrom litdata import StreamingDataset, train_test_split\n\ndataset = StreamingDataset(\"s3://my-bucket/my-data\", subsample=0.01) # data are stored in the cloud\n\nprint(len(dataset)) # display the length of your data\n# out: 1000\n```\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\n  \u003csummary\u003e ✅ Upsample from your source datasets \u003c/summary\u003e\n\n\u0026nbsp;\nUse to control the size of one iteration of a StreamingDataset using repeats. Contains `floor(N)` possibly shuffled copies of the source data, then a subsampling of the remainder.\n\n\n```python\nfrom litdata import StreamingDataset\n\ndataset = StreamingDataset(\"s3://my-bucket/my-data\", subsample=2.5, shuffle=True)\n\nprint(len(dataset)) # display the length of your data\n# out: 250000\n```\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\n  \u003csummary\u003e ✅ Easily modify optimized cloud datasets\u003c/summary\u003e\n\u0026nbsp;\n\nAdd new data to an existing dataset or start fresh if needed, providing flexibility in data management.\n\nLitData optimized datasets are assumed to be immutable. However, you can make the decision to modify them by changing the mode to either `append` or `overwrite`.\n\n```python\nfrom litdata import optimize, StreamingDataset\n\ndef compress(index):\n    return index, index**2\n\nif __name__ == \"__main__\":\n    # Add some data\n    optimize(\n        fn=compress,\n        inputs=list(range(100)),\n        output_dir=\"./my_optimized_dataset\",\n        chunk_bytes=\"64MB\",\n    )\n\n    # Later on, you add more data\n    optimize(\n        fn=compress,\n        inputs=list(range(100, 200)),\n        output_dir=\"./my_optimized_dataset\",\n        chunk_bytes=\"64MB\",\n        mode=\"append\",\n    )\n\n    ds = StreamingDataset(\"./my_optimized_dataset\")\n    assert len(ds) == 200\n    assert ds[:] == [(i, i**2) for i in range(200)]\n```\n\nThe `overwrite` mode will delete the existing data and start from fresh.\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\n  \u003csummary\u003e ✅ Stream parquet datasets\u003c/summary\u003e\n\u0026nbsp;\n\nStream Parquet datasets directly with LitData—no need to convert them into LitData’s optimized binary format! If your dataset is already in Parquet format, you can efficiently index and stream it using `StreamingDataset` and `StreamingDataLoader`.\n\n**Assumption:**\n\nYour dataset directory contains one or more Parquet files.\n\n**Prerequisites:**\n\nInstall the required dependencies to stream Parquet datasets from cloud storage like **Amazon S3** or **Google Cloud Storage**:\n\n```bash\n# For Amazon S3\npip install \"litdata[extra]\" s3fs\n\n# For Google Cloud Storage\npip install \"litdata[extra]\" gcsfs\n```\n\n**Index Your Dataset**: \n\nIndex your Parquet dataset to create an index file that LitData can use to stream the dataset.\n\n```python\nimport litdata as ld\n\n# Point to your data stored in the cloud\npq_dataset_uri = \"s3://my-bucket/my-parquet-data\"  # or \"gs://my-bucket/my-parquet-data\"\n\nld.index_parquet_dataset(pq_dataset_uri)\n```\n\n**Stream the Dataset**\n\nUse `StreamingDataset` with `ParquetLoader` to load and stream the dataset efficiently:\n\n\n```python\nimport litdata as ld\nfrom litdata.streaming.item_loader import ParquetLoader\n\n# Specify your dataset location in the cloud\npq_dataset_uri = \"s3://my-bucket/my-parquet-data\"  # or \"gs://my-bucket/my-parquet-data\"\n\n# Set up the streaming dataset\ndataset = ld.StreamingDataset(pq_dataset_uri, item_loader=ParquetLoader())\n\n# print the first sample\nprint(\"Sample\", dataset[0])\n\n# Stream the dataset using StreamingDataLoader\ndataloader = ld.StreamingDataLoader(dataset, batch_size=4)\nfor sample in dataloader:\n    pass\n```\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\n  \u003csummary\u003e ✅ Use compression\u003c/summary\u003e\n\u0026nbsp;\n\nReduce your data footprint by using advanced compression algorithms.\n\n```python\nimport litdata as ld\n\ndef compress(index):\n    return index, index**2\n\nif __name__ == \"__main__\":\n    # Add some data\n    ld.optimize(\n        fn=compress,\n        inputs=list(range(100)),\n        output_dir=\"./my_optimized_dataset\",\n        chunk_bytes=\"64MB\",\n        num_workers=1,\n        compression=\"zstd\"\n    )\n```\n\nUsing [zstd](https://github.com/facebook/zstd), you can achieve high compression ratio like 4.34x for this simple example.\n\n| Without | With |\n| -------- | -------- | \n| 2.8kb | 646b |\n\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\n  \u003csummary\u003e ✅ Access samples without full data download\u003c/summary\u003e\n\u0026nbsp;\n\nLook at specific parts of a large dataset without downloading the whole thing or loading it on a local machine.\n\n```python\nfrom litdata import StreamingDataset\n\ndataset = StreamingDataset(\"s3://my-bucket/my-data\") # data are stored in the cloud\n\nprint(len(dataset)) # display the length of your data\n\nprint(dataset[42]) # show the 42th element of the dataset\n```\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\n  \u003csummary\u003e ✅ Use any data transforms\u003c/summary\u003e\n\u0026nbsp;\n\nCustomize how your data is processed to better fit your needs.\n\nSubclass the `StreamingDataset` and override its `__getitem__` method to add any extra data transformations.\n\n```python\nfrom litdata import StreamingDataset, StreamingDataLoader\nimport torchvision.transforms.v2.functional as F\n\nclass ImagenetStreamingDataset(StreamingDataset):\n\n    def __getitem__(self, index):\n        image = super().__getitem__(index)\n        return F.resize(image, (224, 224))\n\ndataset = ImagenetStreamingDataset(...)\ndataloader = StreamingDataLoader(dataset, batch_size=4)\n\nfor batch in dataloader:\n    print(batch.shape)\n    # Out: (4, 3, 224, 224)\n```\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\n  \u003csummary\u003e ✅ Profile data loading speed\u003c/summary\u003e\n\u0026nbsp;\n\nMeasure and optimize how fast your data is being loaded, improving efficiency.\n\nThe `StreamingDataLoader` supports profiling of your data loading process. Simply use the `profile_batches` argument to specify the number of batches you want to profile:\n\n```python\nfrom litdata import StreamingDataset, StreamingDataLoader\n\nStreamingDataLoader(..., profile_batches=5)\n```\n\nThis generates a Chrome trace called `result.json`. Then, visualize this trace by opening Chrome browser at the `chrome://tracing` URL and load the trace inside.\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\n  \u003csummary\u003e ✅ Reduce memory use for large files\u003c/summary\u003e\n\u0026nbsp;\n\nHandle large data files efficiently without using too much of your computer's memory.\n\nWhen processing large files like compressed [parquet files](https://en.wikipedia.org/wiki/Apache_Parquet), use the Python yield keyword to process and store one item at the time, reducing the memory footprint of the entire program.\n\n```python\nfrom pathlib import Path\nimport pyarrow.parquet as pq\nfrom litdata import optimize\nfrom tokenizer import Tokenizer\nfrom functools import partial\n\n# 1. Define a function to convert the text within the parquet files into tokens\ndef tokenize_fn(filepath, tokenizer=None):\n    parquet_file = pq.ParquetFile(filepath)\n    # Process per batch to reduce RAM usage\n    for batch in parquet_file.iter_batches(batch_size=8192, columns=[\"content\"]):\n        for text in batch.to_pandas()[\"content\"]:\n            yield tokenizer.encode(text, bos=False, eos=True)\n\n# 2. Generate the inputs\ninput_dir = \"/teamspace/s3_connections/tinyllama-template\"\ninputs = [str(file) for file in Path(f\"{input_dir}/starcoderdata\").rglob(\"*.parquet\")]\n\n# 3. Store the optimized data wherever you want under \"/teamspace/datasets\" or \"/teamspace/s3_connections\"\noutputs = optimize(\n    fn=partial(tokenize_fn, tokenizer=Tokenizer(f\"{input_dir}/checkpoints/Llama-2-7b-hf\")), # Note: Use HF tokenizer or any others\n    inputs=inputs,\n    output_dir=\"/teamspace/datasets/starcoderdata\",\n    chunk_size=(2049 * 8012), # Number of tokens to store by chunks. This is roughly 64MB of tokens per chunk.\n)\n```\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\n  \u003csummary\u003e ✅ Limit local cache space\u003c/summary\u003e\n\u0026nbsp;\n\nLimit the amount of disk space used by temporary files, preventing storage issues.\n\nAdapt the local caching limit of the `StreamingDataset`. This is useful to make sure the downloaded data chunks are deleted when used and the disk usage stays low.\n\n```python\nfrom litdata import StreamingDataset\n\ndataset = StreamingDataset(..., max_cache_size=\"10GB\")\n```\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\n  \u003csummary\u003e ✅ Change cache directory path\u003c/summary\u003e\n\u0026nbsp;\n\nSpecify the directory where cached files should be stored, ensuring efficient data retrieval and management. This is particularly useful for organizing your data storage and improving access times.\n\n```python\nfrom litdata import StreamingDataset\nfrom litdata.streaming.cache import Dir\n\ncache_dir = \"/path/to/your/cache\"\ndata_dir = \"s3://my-bucket/my_optimized_dataset\"\n\ndataset = StreamingDataset(input_dir=Dir(path=cache_dir, url=data_dir))\n```\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\n  \u003csummary\u003e ✅ Optimize loading on networked drives\u003c/summary\u003e\n\u0026nbsp;\n\nOptimize data handling for computers on a local network to improve performance for on-site setups.\n\nOn-prem compute nodes can mount and use a network drive. A network drive is a shared storage device on a local area network. In order to reduce their network overload, the `StreamingDataset` supports `caching` the data chunks.\n\n```python\nfrom litdata import StreamingDataset\n\ndataset = StreamingDataset(input_dir=\"local:/data/shared-drive/some-data\")\n```\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\n  \u003csummary\u003e ✅ Optimize dataset in distributed environment\u003c/summary\u003e\n\u0026nbsp;\n\nLightning can distribute large workloads across hundreds of machines in parallel. This can reduce the time to complete a data processing task from weeks to minutes by scaling to enough machines.\n\nTo apply the optimize operator across multiple machines, simply provide the num_nodes and machine arguments to it as follows:\n\n```python\nimport os\nfrom litdata import optimize, Machine\n\ndef compress(index):\n    return (index, index ** 2)\n\noptimize(\n    fn=compress,\n    inputs=list(range(100)),\n    num_workers=2,\n    output_dir=\"my_output\",\n    chunk_bytes=\"64MB\",\n    num_nodes=2,\n    machine=Machine.DATA_PREP, # You can select between dozens of optimized machines\n)\n```\n\nIf the `output_dir` is a local path, the optimized dataset will be present in: `/teamspace/jobs/{job_name}/nodes-0/my_output`. Otherwise, it will be stored in the specified `output_dir`.\n\nRead the optimized dataset:\n\n```python\nfrom litdata import StreamingDataset\n\noutput_dir = \"/teamspace/jobs/litdata-optimize-2024-07-08/nodes.0/my_output\"\n\ndataset = StreamingDataset(output_dir)\n\nprint(dataset[:])\n```\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\n  \u003csummary\u003e ✅ Encrypt, decrypt data at chunk/sample level\u003c/summary\u003e\n\u0026nbsp;\n\nSecure data by applying encryption to individual samples or chunks, ensuring sensitive information is protected during storage.\n\nThis example shows how to use the `FernetEncryption` class for sample-level encryption with a data optimization function.\n\n```python\nfrom litdata import optimize\nfrom litdata.utilities.encryption import FernetEncryption\nimport numpy as np\nfrom PIL import Image\n\n# Initialize FernetEncryption with a password for sample-level encryption\nfernet = FernetEncryption(password=\"your_secure_password\", level=\"sample\")\ndata_dir = \"s3://my-bucket/optimized_data\"\n\ndef random_image(index):\n    \"\"\"Generate a random image for demonstration purposes.\"\"\"\n    fake_img = Image.fromarray(np.random.randint(0, 255, (32, 32, 3), dtype=np.uint8))\n    return {\"image\": fake_img, \"class\": index}\n\n# Optimize data while applying encryption\noptimize(\n    fn=random_image,\n    inputs=list(range(5)),  # Example inputs: [0, 1, 2, 3, 4]\n    num_workers=1,\n    output_dir=data_dir,\n    chunk_bytes=\"64MB\",\n    encryption=fernet,\n)\n\n# Save the encryption key to a file for later use\nfernet.save(\"fernet.pem\")\n```\n\nLoad the encrypted data using the `StreamingDataset` class as follows:\n\n```python\nfrom litdata import StreamingDataset\nfrom litdata.utilities.encryption import FernetEncryption\n\n# Load the encryption key\nfernet = FernetEncryption(password=\"your_secure_password\", level=\"sample\")\nfernet.load(\"fernet.pem\")\n\n# Create a streaming dataset for reading the encrypted samples\nds = StreamingDataset(input_dir=data_dir, encryption=fernet)\n```\n\nImplement your own encryption method: Subclass the `Encryption` class and define the necessary methods:\n\n```python\nfrom litdata.utilities.encryption import Encryption\n\nclass CustomEncryption(Encryption):\n    def encrypt(self, data):\n        # Implement your custom encryption logic here\n        return data\n\n    def decrypt(self, data):\n        # Implement your custom decryption logic here\n        return data\n```\n\nThis allows the data to remain secure while maintaining flexibility in the encryption method.\n\u003c/details\u003e\n\n\u0026nbsp;\n\n## Features for transforming datasets\n\n\u003cdetails\u003e\n  \u003csummary\u003e ✅ Parallelize data transformations (map)\u003c/summary\u003e\n\u0026nbsp;\n\nApply the same change to different parts of the dataset at once to save time and effort.\n\nThe `map` operator can be used to apply a function over a list of inputs.\n\nHere is an example where the `map` operator is used to apply a `resize_image` function over a folder of large images.\n\n```python\nfrom litdata import map\nfrom PIL import Image\n\n# Note: Inputs could also refer to files on s3 directly.\ninput_dir = \"my_large_images\"\ninputs = [os.path.join(input_dir, f) for f in os.listdir(input_dir)]\n\n# The resize image takes one of the input (image_path) and the output directory.\n# Files written to output_dir are persisted.\ndef resize_image(image_path, output_dir):\n  output_image_path = os.path.join(output_dir, os.path.basename(image_path))\n  Image.open(image_path).resize((224, 224)).save(output_image_path)\n\nmap(\n    fn=resize_image,\n    inputs=inputs,\n    output_dir=\"s3://my-bucket/my_resized_images\",\n)\n```\n\n\u003c/details\u003e\n\n\u0026nbsp;\n\n----\n\n# Benchmarks\nIn this section we show benchmarks for speed to optimize a dataset and the resulting streaming speed ([Reproduce the benchmark](https://lightning.ai/lightning-ai/studios/benchmark-cloud-data-loading-libraries)).\n\n## Streaming speed\n\nData optimized and streamed with LitData achieves a 20x speed up over non optimized data and 2x speed up over other streaming solutions.\n\nSpeed to stream Imagenet 1.2M from AWS S3:\n\n| Framework | Images / sec  1st Epoch (float32)  | Images / sec   2nd Epoch (float32) | Images / sec 1st Epoch (torch16) | Images / sec 2nd Epoch (torch16) |\n|---|---|---|---|---|\n| LitData | **5839** | **6692**  | **6282**  | **7221**  |\n| Web Dataset  | 3134 | 3924 | 3343 | 4424 |\n| Mosaic ML  | 2898 | 5099 | 2809 | 5158 |\n\n\u003cdetails\u003e\n  \u003csummary\u003e Benchmark details\u003c/summary\u003e\n\u0026nbsp;\n\n- [Imagenet-1.2M dataset](https://www.image-net.org/) contains `1,281,167 images`.\n- To align with other benchmarks, we measured the streaming speed (`images per second`) loaded from [AWS S3](https://aws.amazon.com/s3/) for several frameworks.\n\n\u003c/details\u003e\n\n\u0026nbsp;\n\n## Time to optimize data\nLitData optimizes the Imagenet dataset for fast training 3-5x faster than other frameworks:\n\nTime to optimize 1.2 million ImageNet images (Faster is better):\n| Framework |Train Conversion Time | Val Conversion Time | Dataset Size | # Files |\n|---|---|---|---|---|\n| LitData  |  **10:05 min** | **00:30 min** | **143.1 GB**  | 2.339  |\n| Web Dataset  | 32:36 min | 01:22 min | 147.8 GB | 1.144 |\n| Mosaic ML  | 49:49 min | 01:04 min | **143.1 GB** | 2.298 |\n\n\u0026nbsp;\n\n----\n\n# Parallelize transforms and data optimization on cloud machines\n\u003cdiv align=\"center\"\u003e\n\u003cimg alt=\"Lightning\" src=\"https://pl-flash-data.s3.amazonaws.com/data-prep.jpg\" width=\"700px\"\u003e\n\u003c/div\u003e\n\n## Parallelize data transforms\n\nTransformations with LitData are linearly parallelizable across machines.\n\nFor example, let's say that it takes 56 hours to embed a dataset on a single A10G machine. With LitData,\nthis can be speed up by adding more machines in parallel\n\n| Number of machines | Hours |\n|-----------------|--------------|\n| 1               | 56           |\n| 2               | 28           |\n| 4               | 14           |\n| ...               | ...            |\n| 64              | 0.875        |\n\nTo scale the number of machines, run the processing script on [Lightning Studios](https://lightning.ai/):\n\n```python\nfrom litdata import map, Machine\n\nmap(\n  ...\n  num_nodes=32,\n  machine=Machine.DATA_PREP, # Select between dozens of optimized machines\n)\n```\n\n## Parallelize data optimization\nTo scale the number of machines for data optimization, use [Lightning Studios](https://lightning.ai/):\n\n```python\nfrom litdata import optimize, Machine\n\noptimize(\n  ...\n  num_nodes=32,\n  machine=Machine.DATA_PREP, # Select between dozens of optimized machines\n)\n```\n\n\u0026nbsp;\n\nExample: [Process the LAION 400 million image dataset in 2 hours on 32 machines, each with 32 CPUs](https://lightning.ai/lightning-ai/studios/use-or-explore-laion-400million-dataset).\n\n\u0026nbsp;\n\n----\n\n# Start from a template\nBelow are templates for real-world applications of LitData at scale.\n\n## Templates: Transform datasets\n\n| Studio | Data type | Time (minutes) | Machines | Dataset |\n| ------------------------------------ | ----------------- | ----------------- | -------------- | -------------- |\n| [Download LAION-400MILLION dataset](https://lightning.ai/lightning-ai/studios/use-or-explore-laion-400million-dataset) | Image \u0026 Text | 120 | 32 |[LAION-400M](https://laion.ai/blog/laion-400-open-dataset/) |\n| [Tokenize 2M Swedish Wikipedia Articles](https://lightning.ai/lightning-ai/studios/tokenize-2m-swedish-wikipedia-articles) | Text | 7 | 4 | [Swedish Wikipedia](https://huggingface.co/datasets/wikipedia) |\n| [Embed English Wikipedia under 5 dollars](https://lightning.ai/lightning-ai/studios/embed-english-wikipedia-under-5-dollars) | Text | 15 | 3 | [English Wikipedia](https://huggingface.co/datasets/wikipedia) |\n\n## Templates: Optimize + stream data\n\n| Studio | Data type | Time (minutes) | Machines | Dataset |\n| -------------------------------- | ----------------- | ----------------- | -------------- | -------------- |\n| [Benchmark cloud data-loading libraries](https://lightning.ai/lightning-ai/studios/benchmark-cloud-data-loading-libraries) | Image \u0026 Label | 10 | 1 | [Imagenet 1M](https://paperswithcode.com/sota/image-classification-on-imagenet?tag_filter=171) |\n| [Optimize GeoSpatial data for model training](https://lightning.ai/lightning-ai/studios/convert-spatial-data-to-lightning-streaming) | Image \u0026 Mask | 120 | 32 | [Chesapeake Roads Spatial Context](https://github.com/isaaccorley/chesapeakersc) |\n| [Optimize TinyLlama 1T dataset for training](https://lightning.ai/lightning-ai/studios/prepare-the-tinyllama-1t-token-dataset) | Text | 240 | 32 | [SlimPajama](https://huggingface.co/datasets/cerebras/SlimPajama-627B) \u0026 [StarCoder](https://huggingface.co/datasets/bigcode/starcoderdata) |\n| [Optimize parquet files for model training](https://lightning.ai/lightning-ai/studios/convert-parquets-to-lightning-streaming) | Parquet Files | 12 | 16 | Randomly Generated data |\n\n\u0026nbsp;\n\n----\n\n# Community\nLitData is a community project accepting contributions -  Let's make the world's most advanced AI data processing framework.\n\n💬 [Get help on Discord](https://discord.com/invite/XncpTy7DSt)    \n📋 [License: Apache 2.0](https://github.com/Lightning-AI/litdata/blob/main/LICENSE)\n\n----\n\n# Governance\n\n## Maintainers\n\n* Thomas Chaton ([tchaton](https://github.com/tchaton))\n* Luca Antiga ([lantiga](https://github.com/lantiga))\n* Justus Schock ([justusschock](https://github.com/justusschock))\n* Bhimraj Yadav ([bhimrazy](https://github.com/bhimrazy))\n* Deependu ([deependujha](https://github.com/deependujha))\n* Jirka Borda ([Borda](https://github.com/Borda))\n\n\n## Emeritus Maintainers\n* Adrian Wälchli ([awaelchli](https://github.com/awaelchli))\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FLightning-AI%2FlitData","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FLightning-AI%2FlitData","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FLightning-AI%2FlitData/lists"}