Spaces:
Running
Running
File size: 8,728 Bytes
95d2faf |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 |
{
"cells": [
{
"cell_type": "markdown",
"id": "bf8fb38a",
"metadata": {},
"source": [
"# Data Pipeline"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "9b83dcb9",
"metadata": {},
"outputs": [],
"source": [
"from dataclasses import dataclass, field\n",
"from pathlib import Path\n",
"\n",
"import datasets\n",
"from datasets import Dataset, load_dataset\n",
"import numpy as np\n",
"\n",
"from transformers import BartTokenizer\n",
"\n",
"from tqdm import tqdm\n",
"\n",
"import jax\n",
"import jax.numpy as jnp\n",
"\n",
"from flax.training.common_utils import shard"
]
},
{
"cell_type": "markdown",
"id": "a661a89e",
"metadata": {},
"source": [
"File containing image paths, captions and VQGAN-encoded indices."
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "0e84e889",
"metadata": {},
"outputs": [],
"source": [
"datafile = '/data/CC12M/images-encoded-10000.tsv' # 9999 encoded images from CC12M"
]
},
{
"cell_type": "markdown",
"id": "7fdc640b",
"metadata": {},
"source": [
"TODO: generate train/test splits if necessary."
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "cc6789b4",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"Using custom data configuration default-91833df78e844785\n",
"Reusing dataset csv (/home/pedro/.cache/huggingface/datasets/csv/default-91833df78e844785/0.0.0/e138af468cb14e747fb46a19c787ffcfa5170c821476d20d5304287ce12bbc23)\n"
]
}
],
"source": [
"dataset = load_dataset('csv', delimiter='\\t', data_files=[datafile])"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "f3ed4919",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"DatasetDict({\n",
" train: Dataset({\n",
" features: ['image_file', 'caption', 'encoding'],\n",
" num_rows: 9999\n",
" })\n",
"})"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"dataset"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "a70c7354",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Dataset({\n",
" features: ['image_file', 'caption', 'encoding'],\n",
" num_rows: 9999\n",
"})"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"dataset = dataset[\"train\"]\n",
"dataset"
]
},
{
"cell_type": "markdown",
"id": "a73454cf",
"metadata": {},
"source": [
"We don't really need the `image_file` field for training. We'll drop it during pre-processing because we won't be able to numericalize it to a `jnp.array`, which would be required in JAX."
]
},
{
"cell_type": "markdown",
"id": "7c0fa992",
"metadata": {},
"source": [
"## Preprocessing"
]
},
{
"cell_type": "markdown",
"id": "a0e36582",
"metadata": {},
"source": [
"The `encoding` field contains a string representation of the encoded indices. We'll convert them to numbers. We also need to tokenize the captions."
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "d46f6ac5",
"metadata": {},
"outputs": [],
"source": [
"# Setting padding=\"max_length\" as we need fixed length inputs for jitted functions\n",
"max_length = 256 # Read from data_args.max_source_length\n",
"tokenizer = BartTokenizer.from_pretrained('facebook/bart-large-cnn')"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "4cac6643",
"metadata": {},
"outputs": [],
"source": [
"def preprocess_function(examples):\n",
" inputs = examples[\"caption\"]\n",
"# inputs = [prefix + inp for inp in inputs] # Do we need this?\n",
" model_inputs = tokenizer(\n",
" inputs, max_length=max_length, padding=\"max_length\", truncation=True, return_tensors=\"np\"\n",
" )\n",
"\n",
" model_inputs[\"eval_encoding\"] = [eval(indices) for indices in examples['encoding']]\n",
"\n",
" return model_inputs"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "e6a4cb91",
"metadata": {},
"outputs": [],
"source": [
"num_workers = 48 # We have 96 processors in the TPU\n",
"column_names = dataset.column_names\n",
"dataset = dataset.map(preprocess_function,\n",
" remove_columns=column_names,\n",
" batched=True,\n",
" num_proc=48\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "a9b1b467",
"metadata": {},
"outputs": [],
"source": [
"def data_loader(rng: jax.random.PRNGKey, dataset: Dataset, batch_size: int, shuffle: bool = False):\n",
" \"\"\"\n",
" Returns batches of size `batch_size` from truncated `dataset`, sharded over all local devices.\n",
" Shuffle batches if `shuffle` is `True`.\n",
" \"\"\"\n",
" steps_per_epoch = len(dataset) // batch_size\n",
"\n",
" if shuffle:\n",
" batch_idx = jax.random.permutation(rng, len(dataset))\n",
" else:\n",
" batch_idx = jnp.arange(len(dataset))\n",
"\n",
" batch_idx = batch_idx[: steps_per_epoch * batch_size] # Skip incomplete batch.\n",
" batch_idx = batch_idx.reshape((steps_per_epoch, batch_size))\n",
"\n",
" for idx in batch_idx:\n",
" batch = dataset[idx] \n",
" batch = {k: jnp.array(v) for k, v in batch.items()}\n",
" batch = shard(batch)\n",
" yield batch"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "0a628505",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"INFO:absl:Starting the local TPU driver.\n",
"INFO:absl:Unable to initialize backend 'tpu_driver': Not found: Unable to find driver in registry given worker: local://\n",
"INFO:absl:Unable to initialize backend 'gpu': Not found: Could not find registered platform with name: \"cuda\". Available platform names are: Interpreter TPU Host\n"
]
}
],
"source": [
"rng = jax.random.PRNGKey(23) # Use training_args.seed\n",
"batch_size = 64 # Per device\n",
"super_batch_size = batch_size * jax.device_count()"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "b3a5ce7d",
"metadata": {},
"outputs": [],
"source": [
"loader = data_loader(rng, dataset, batch_size=super_batch_size)"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "67aa8f9c",
"metadata": {},
"outputs": [],
"source": [
"superbatch = next(iter(loader))"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "7cd99402",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"dict_keys(['attention_mask', 'eval_encoding', 'input_ids'])"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"superbatch.keys()"
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "652a4a9e",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"8"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"len(superbatch[\"eval_encoding\"])"
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "de7de4e8",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(8, 64, 256)"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"superbatch[\"eval_encoding\"].shape"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "cfe23a71",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.10"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
|