Jumpr/67 / LM_train (1).ipynb
Jumpr's picture
download
raw
68.4 kB
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"provenance": [],
"gpuType": "T4"
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
},
"accelerator": "GPU",
"widgets": {
"application/vnd.jupyter.widget-state+json": {
"a2028495dc4743a180f31e9c191f6a59": {
"model_module": "@jupyter-widgets/controls",
"model_name": "HBoxModel",
"model_module_version": "1.5.0",
"state": {
"_dom_classes": [],
"_model_module": "@jupyter-widgets/controls",
"_model_module_version": "1.5.0",
"_model_name": "HBoxModel",
"_view_count": null,
"_view_module": "@jupyter-widgets/controls",
"_view_module_version": "1.5.0",
"_view_name": "HBoxView",
"box_style": "",
"children": [
"IPY_MODEL_988a7701f3134ef18196fe2f70509987",
"IPY_MODEL_78456e7017ef48a18a7e443c6a575b01",
"IPY_MODEL_555c1c78b2bb4df9adfd31879c708c62"
],
"layout": "IPY_MODEL_8c3ecfcedf4342af8e62add4897211e8"
}
},
"988a7701f3134ef18196fe2f70509987": {
"model_module": "@jupyter-widgets/controls",
"model_name": "HTMLModel",
"model_module_version": "1.5.0",
"state": {
"_dom_classes": [],
"_model_module": "@jupyter-widgets/controls",
"_model_module_version": "1.5.0",
"_model_name": "HTMLModel",
"_view_count": null,
"_view_module": "@jupyter-widgets/controls",
"_view_module_version": "1.5.0",
"_view_name": "HTMLView",
"description": "",
"description_tooltip": null,
"layout": "IPY_MODEL_cf462213ab294dc9bf75f4594cd69891",
"placeholder": "​",
"style": "IPY_MODEL_3b2cf5b575cf4350887fe072a2d84311",
"value": "Resolving data files: 100%"
}
},
"78456e7017ef48a18a7e443c6a575b01": {
"model_module": "@jupyter-widgets/controls",
"model_name": "FloatProgressModel",
"model_module_version": "1.5.0",
"state": {
"_dom_classes": [],
"_model_module": "@jupyter-widgets/controls",
"_model_module_version": "1.5.0",
"_model_name": "FloatProgressModel",
"_view_count": null,
"_view_module": "@jupyter-widgets/controls",
"_view_module_version": "1.5.0",
"_view_name": "ProgressView",
"bar_style": "success",
"description": "",
"description_tooltip": null,
"layout": "IPY_MODEL_a3d72c32318d447b89bb489e02f58dc9",
"max": 2410,
"min": 0,
"orientation": "horizontal",
"style": "IPY_MODEL_2d9fac91f8514fd2856d07fd81880b9f",
"value": 2410
}
},
"555c1c78b2bb4df9adfd31879c708c62": {
"model_module": "@jupyter-widgets/controls",
"model_name": "HTMLModel",
"model_module_version": "1.5.0",
"state": {
"_dom_classes": [],
"_model_module": "@jupyter-widgets/controls",
"_model_module_version": "1.5.0",
"_model_name": "HTMLModel",
"_view_count": null,
"_view_module": "@jupyter-widgets/controls",
"_view_module_version": "1.5.0",
"_view_name": "HTMLView",
"description": "",
"description_tooltip": null,
"layout": "IPY_MODEL_8e401f470b2046efa750fd3cf9749c05",
"placeholder": "",
"style": "IPY_MODEL_9dc69fdae068418989ca4b24f796002a",
"value": "2410/2410 [00:00<00:00,  4.94it/s]"
}
},
"8c3ecfcedf4342af8e62add4897211e8": {
"model_module": "@jupyter-widgets/base",
"model_name": "LayoutModel",
"model_module_version": "1.2.0",
"state": {
"_model_module": "@jupyter-widgets/base",
"_model_module_version": "1.2.0",
"_model_name": "LayoutModel",
"_view_count": null,
"_view_module": "@jupyter-widgets/base",
"_view_module_version": "1.2.0",
"_view_name": "LayoutView",
"align_content": null,
"align_items": null,
"align_self": null,
"border": null,
"bottom": null,
"display": null,
"flex": null,
"flex_flow": null,
"grid_area": null,
"grid_auto_columns": null,
"grid_auto_flow": null,
"grid_auto_rows": null,
"grid_column": null,
"grid_gap": null,
"grid_row": null,
"grid_template_areas": null,
"grid_template_columns": null,
"grid_template_rows": null,
"height": null,
"justify_content": null,
"justify_items": null,
"left": null,
"margin": null,
"max_height": null,
"max_width": null,
"min_height": null,
"min_width": null,
"object_fit": null,
"object_position": null,
"order": null,
"overflow": null,
"overflow_x": null,
"overflow_y": null,
"padding": null,
"right": null,
"top": null,
"visibility": null,
"width": null
}
},
"cf462213ab294dc9bf75f4594cd69891": {
"model_module": "@jupyter-widgets/base",
"model_name": "LayoutModel",
"model_module_version": "1.2.0",
"state": {
"_model_module": "@jupyter-widgets/base",
"_model_module_version": "1.2.0",
"_model_name": "LayoutModel",
"_view_count": null,
"_view_module": "@jupyter-widgets/base",
"_view_module_version": "1.2.0",
"_view_name": "LayoutView",
"align_content": null,
"align_items": null,
"align_self": null,
"border": null,
"bottom": null,
"display": null,
"flex": null,
"flex_flow": null,
"grid_area": null,
"grid_auto_columns": null,
"grid_auto_flow": null,
"grid_auto_rows": null,
"grid_column": null,
"grid_gap": null,
"grid_row": null,
"grid_template_areas": null,
"grid_template_columns": null,
"grid_template_rows": null,
"height": null,
"justify_content": null,
"justify_items": null,
"left": null,
"margin": null,
"max_height": null,
"max_width": null,
"min_height": null,
"min_width": null,
"object_fit": null,
"object_position": null,
"order": null,
"overflow": null,
"overflow_x": null,
"overflow_y": null,
"padding": null,
"right": null,
"top": null,
"visibility": null,
"width": null
}
},
"3b2cf5b575cf4350887fe072a2d84311": {
"model_module": "@jupyter-widgets/controls",
"model_name": "DescriptionStyleModel",
"model_module_version": "1.5.0",
"state": {
"_model_module": "@jupyter-widgets/controls",
"_model_module_version": "1.5.0",
"_model_name": "DescriptionStyleModel",
"_view_count": null,
"_view_module": "@jupyter-widgets/base",
"_view_module_version": "1.2.0",
"_view_name": "StyleView",
"description_width": ""
}
},
"a3d72c32318d447b89bb489e02f58dc9": {
"model_module": "@jupyter-widgets/base",
"model_name": "LayoutModel",
"model_module_version": "1.2.0",
"state": {
"_model_module": "@jupyter-widgets/base",
"_model_module_version": "1.2.0",
"_model_name": "LayoutModel",
"_view_count": null,
"_view_module": "@jupyter-widgets/base",
"_view_module_version": "1.2.0",
"_view_name": "LayoutView",
"align_content": null,
"align_items": null,
"align_self": null,
"border": null,
"bottom": null,
"display": null,
"flex": null,
"flex_flow": null,
"grid_area": null,
"grid_auto_columns": null,
"grid_auto_flow": null,
"grid_auto_rows": null,
"grid_column": null,
"grid_gap": null,
"grid_row": null,
"grid_template_areas": null,
"grid_template_columns": null,
"grid_template_rows": null,
"height": null,
"justify_content": null,
"justify_items": null,
"left": null,
"margin": null,
"max_height": null,
"max_width": null,
"min_height": null,
"min_width": null,
"object_fit": null,
"object_position": null,
"order": null,
"overflow": null,
"overflow_x": null,
"overflow_y": null,
"padding": null,
"right": null,
"top": null,
"visibility": null,
"width": null
}
},
"2d9fac91f8514fd2856d07fd81880b9f": {
"model_module": "@jupyter-widgets/controls",
"model_name": "ProgressStyleModel",
"model_module_version": "1.5.0",
"state": {
"_model_module": "@jupyter-widgets/controls",
"_model_module_version": "1.5.0",
"_model_name": "ProgressStyleModel",
"_view_count": null,
"_view_module": "@jupyter-widgets/base",
"_view_module_version": "1.2.0",
"_view_name": "StyleView",
"bar_color": null,
"description_width": ""
}
},
"8e401f470b2046efa750fd3cf9749c05": {
"model_module": "@jupyter-widgets/base",
"model_name": "LayoutModel",
"model_module_version": "1.2.0",
"state": {
"_model_module": "@jupyter-widgets/base",
"_model_module_version": "1.2.0",
"_model_name": "LayoutModel",
"_view_count": null,
"_view_module": "@jupyter-widgets/base",
"_view_module_version": "1.2.0",
"_view_name": "LayoutView",
"align_content": null,
"align_items": null,
"align_self": null,
"border": null,
"bottom": null,
"display": null,
"flex": null,
"flex_flow": null,
"grid_area": null,
"grid_auto_columns": null,
"grid_auto_flow": null,
"grid_auto_rows": null,
"grid_column": null,
"grid_gap": null,
"grid_row": null,
"grid_template_areas": null,
"grid_template_columns": null,
"grid_template_rows": null,
"height": null,
"justify_content": null,
"justify_items": null,
"left": null,
"margin": null,
"max_height": null,
"max_width": null,
"min_height": null,
"min_width": null,
"object_fit": null,
"object_position": null,
"order": null,
"overflow": null,
"overflow_x": null,
"overflow_y": null,
"padding": null,
"right": null,
"top": null,
"visibility": null,
"width": null
}
},
"9dc69fdae068418989ca4b24f796002a": {
"model_module": "@jupyter-widgets/controls",
"model_name": "DescriptionStyleModel",
"model_module_version": "1.5.0",
"state": {
"_model_module": "@jupyter-widgets/controls",
"_model_module_version": "1.5.0",
"_model_name": "DescriptionStyleModel",
"_view_count": null,
"_view_module": "@jupyter-widgets/base",
"_view_module_version": "1.2.0",
"_view_name": "StyleView",
"description_width": ""
}
},
"04dc40a15f8e46bfb63e071ff04a9f6c": {
"model_module": "@jupyter-widgets/controls",
"model_name": "HBoxModel",
"model_module_version": "1.5.0",
"state": {
"_dom_classes": [],
"_model_module": "@jupyter-widgets/controls",
"_model_module_version": "1.5.0",
"_model_name": "HBoxModel",
"_view_count": null,
"_view_module": "@jupyter-widgets/controls",
"_view_module_version": "1.5.0",
"_view_name": "HBoxView",
"box_style": "",
"children": [
"IPY_MODEL_ed4d63ca46584dfbbd9b3d2591ff28d6",
"IPY_MODEL_93c49d86eea7415bb6bba9678fcceb2a",
"IPY_MODEL_1c21c4be4b70449d982972d15a44aa25"
],
"layout": "IPY_MODEL_7aa4a191402445b998898ff0f383a953"
}
},
"ed4d63ca46584dfbbd9b3d2591ff28d6": {
"model_module": "@jupyter-widgets/controls",
"model_name": "HTMLModel",
"model_module_version": "1.5.0",
"state": {
"_dom_classes": [],
"_model_module": "@jupyter-widgets/controls",
"_model_module_version": "1.5.0",
"_model_name": "HTMLModel",
"_view_count": null,
"_view_module": "@jupyter-widgets/controls",
"_view_module_version": "1.5.0",
"_view_name": "HTMLView",
"description": "",
"description_tooltip": null,
"layout": "IPY_MODEL_b968f8835c054cf69e3119b6643b7913",
"placeholder": "",
"style": "IPY_MODEL_2eab38a0837540529052e2bf58f3eb06",
"value": "Resolving data files: 100%"
}
},
"93c49d86eea7415bb6bba9678fcceb2a": {
"model_module": "@jupyter-widgets/controls",
"model_name": "FloatProgressModel",
"model_module_version": "1.5.0",
"state": {
"_dom_classes": [],
"_model_module": "@jupyter-widgets/controls",
"_model_module_version": "1.5.0",
"_model_name": "FloatProgressModel",
"_view_count": null,
"_view_module": "@jupyter-widgets/controls",
"_view_module_version": "1.5.0",
"_view_name": "ProgressView",
"bar_style": "success",
"description": "",
"description_tooltip": null,
"layout": "IPY_MODEL_8f3447bc345d4860a56506f19282561e",
"max": 2410,
"min": 0,
"orientation": "horizontal",
"style": "IPY_MODEL_235e2fed74de42118e35e5bc6dce707d",
"value": 2410
}
},
"1c21c4be4b70449d982972d15a44aa25": {
"model_module": "@jupyter-widgets/controls",
"model_name": "HTMLModel",
"model_module_version": "1.5.0",
"state": {
"_dom_classes": [],
"_model_module": "@jupyter-widgets/controls",
"_model_module_version": "1.5.0",
"_model_name": "HTMLModel",
"_view_count": null,
"_view_module": "@jupyter-widgets/controls",
"_view_module_version": "1.5.0",
"_view_name": "HTMLView",
"description": "",
"description_tooltip": null,
"layout": "IPY_MODEL_01074671af2b433a864c371988b2de07",
"placeholder": "​",
"style": "IPY_MODEL_f3c3753cd34b44efadd9034546f43ea7",
"value": " 2410/2410 [00:00<00:00,  6.59it/s]"
}
},
"7aa4a191402445b998898ff0f383a953": {
"model_module": "@jupyter-widgets/base",
"model_name": "LayoutModel",
"model_module_version": "1.2.0",
"state": {
"_model_module": "@jupyter-widgets/base",
"_model_module_version": "1.2.0",
"_model_name": "LayoutModel",
"_view_count": null,
"_view_module": "@jupyter-widgets/base",
"_view_module_version": "1.2.0",
"_view_name": "LayoutView",
"align_content": null,
"align_items": null,
"align_self": null,
"border": null,
"bottom": null,
"display": null,
"flex": null,
"flex_flow": null,
"grid_area": null,
"grid_auto_columns": null,
"grid_auto_flow": null,
"grid_auto_rows": null,
"grid_column": null,
"grid_gap": null,
"grid_row": null,
"grid_template_areas": null,
"grid_template_columns": null,
"grid_template_rows": null,
"height": null,
"justify_content": null,
"justify_items": null,
"left": null,
"margin": null,
"max_height": null,
"max_width": null,
"min_height": null,
"min_width": null,
"object_fit": null,
"object_position": null,
"order": null,
"overflow": null,
"overflow_x": null,
"overflow_y": null,
"padding": null,
"right": null,
"top": null,
"visibility": null,
"width": null
}
},
"b968f8835c054cf69e3119b6643b7913": {
"model_module": "@jupyter-widgets/base",
"model_name": "LayoutModel",
"model_module_version": "1.2.0",
"state": {
"_model_module": "@jupyter-widgets/base",
"_model_module_version": "1.2.0",
"_model_name": "LayoutModel",
"_view_count": null,
"_view_module": "@jupyter-widgets/base",
"_view_module_version": "1.2.0",
"_view_name": "LayoutView",
"align_content": null,
"align_items": null,
"align_self": null,
"border": null,
"bottom": null,
"display": null,
"flex": null,
"flex_flow": null,
"grid_area": null,
"grid_auto_columns": null,
"grid_auto_flow": null,
"grid_auto_rows": null,
"grid_column": null,
"grid_gap": null,
"grid_row": null,
"grid_template_areas": null,
"grid_template_columns": null,
"grid_template_rows": null,
"height": null,
"justify_content": null,
"justify_items": null,
"left": null,
"margin": null,
"max_height": null,
"max_width": null,
"min_height": null,
"min_width": null,
"object_fit": null,
"object_position": null,
"order": null,
"overflow": null,
"overflow_x": null,
"overflow_y": null,
"padding": null,
"right": null,
"top": null,
"visibility": null,
"width": null
}
},
"2eab38a0837540529052e2bf58f3eb06": {
"model_module": "@jupyter-widgets/controls",
"model_name": "DescriptionStyleModel",
"model_module_version": "1.5.0",
"state": {
"_model_module": "@jupyter-widgets/controls",
"_model_module_version": "1.5.0",
"_model_name": "DescriptionStyleModel",
"_view_count": null,
"_view_module": "@jupyter-widgets/base",
"_view_module_version": "1.2.0",
"_view_name": "StyleView",
"description_width": ""
}
},
"8f3447bc345d4860a56506f19282561e": {
"model_module": "@jupyter-widgets/base",
"model_name": "LayoutModel",
"model_module_version": "1.2.0",
"state": {
"_model_module": "@jupyter-widgets/base",
"_model_module_version": "1.2.0",
"_model_name": "LayoutModel",
"_view_count": null,
"_view_module": "@jupyter-widgets/base",
"_view_module_version": "1.2.0",
"_view_name": "LayoutView",
"align_content": null,
"align_items": null,
"align_self": null,
"border": null,
"bottom": null,
"display": null,
"flex": null,
"flex_flow": null,
"grid_area": null,
"grid_auto_columns": null,
"grid_auto_flow": null,
"grid_auto_rows": null,
"grid_column": null,
"grid_gap": null,
"grid_row": null,
"grid_template_areas": null,
"grid_template_columns": null,
"grid_template_rows": null,
"height": null,
"justify_content": null,
"justify_items": null,
"left": null,
"margin": null,
"max_height": null,
"max_width": null,
"min_height": null,
"min_width": null,
"object_fit": null,
"object_position": null,
"order": null,
"overflow": null,
"overflow_x": null,
"overflow_y": null,
"padding": null,
"right": null,
"top": null,
"visibility": null,
"width": null
}
},
"235e2fed74de42118e35e5bc6dce707d": {
"model_module": "@jupyter-widgets/controls",
"model_name": "ProgressStyleModel",
"model_module_version": "1.5.0",
"state": {
"_model_module": "@jupyter-widgets/controls",
"_model_module_version": "1.5.0",
"_model_name": "ProgressStyleModel",
"_view_count": null,
"_view_module": "@jupyter-widgets/base",
"_view_module_version": "1.2.0",
"_view_name": "StyleView",
"bar_color": null,
"description_width": ""
}
},
"01074671af2b433a864c371988b2de07": {
"model_module": "@jupyter-widgets/base",
"model_name": "LayoutModel",
"model_module_version": "1.2.0",
"state": {
"_model_module": "@jupyter-widgets/base",
"_model_module_version": "1.2.0",
"_model_name": "LayoutModel",
"_view_count": null,
"_view_module": "@jupyter-widgets/base",
"_view_module_version": "1.2.0",
"_view_name": "LayoutView",
"align_content": null,
"align_items": null,
"align_self": null,
"border": null,
"bottom": null,
"display": null,
"flex": null,
"flex_flow": null,
"grid_area": null,
"grid_auto_columns": null,
"grid_auto_flow": null,
"grid_auto_rows": null,
"grid_column": null,
"grid_gap": null,
"grid_row": null,
"grid_template_areas": null,
"grid_template_columns": null,
"grid_template_rows": null,
"height": null,
"justify_content": null,
"justify_items": null,
"left": null,
"margin": null,
"max_height": null,
"max_width": null,
"min_height": null,
"min_width": null,
"object_fit": null,
"object_position": null,
"order": null,
"overflow": null,
"overflow_x": null,
"overflow_y": null,
"padding": null,
"right": null,
"top": null,
"visibility": null,
"width": null
}
},
"f3c3753cd34b44efadd9034546f43ea7": {
"model_module": "@jupyter-widgets/controls",
"model_name": "DescriptionStyleModel",
"model_module_version": "1.5.0",
"state": {
"_model_module": "@jupyter-widgets/controls",
"_model_module_version": "1.5.0",
"_model_name": "DescriptionStyleModel",
"_view_count": null,
"_view_module": "@jupyter-widgets/base",
"_view_module_version": "1.2.0",
"_view_name": "StyleView",
"description_width": ""
}
}
}
}
},
"cells": [
{
"cell_type": "code",
"execution_count": 21,
"metadata": {
"id": "xuZYSBUI62Ve"
},
"outputs": [],
"source": [
"!pip install -q lightning transformers datasets wandb"
]
},
{
"cell_type": "code",
"source": [
"!pip install -q liger-kernel # if using liger-kernel on CUDA device"
],
"metadata": {
"id": "pPpNmtB8AsAz"
},
"execution_count": 22,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"# **Dataset**"
],
"metadata": {
"id": "I6dgmqcy9htj"
}
},
{
"cell_type": "code",
"source": [
"# @title\n",
"import lightning as L\n",
"\n",
"from datasets import load_dataset\n",
"from transformers import AutoTokenizer\n",
"\n",
"from torch.utils.data import IterableDataset, DataLoader\n",
"\n",
"class HFStreamingDataset(IterableDataset):\n",
" def __init__(self, dataset_ckpt, tokenizer_ckpt, seq_len):\n",
" super().__init__()\n",
" self.seq_len = seq_len\n",
" self.tokenizer = AutoTokenizer.from_pretrained(tokenizer_ckpt)\n",
" self.dataset = load_dataset(dataset_ckpt, split='train', streaming=True)\n",
"\n",
" def __iter__(self):\n",
" for seq in self.dataset:\n",
" text_seq = seq['text']\n",
" enc = self.tokenizer(\n",
" text_seq,\n",
" max_length=self.seq_len + 1,\n",
" truncation=True,\n",
" return_tensors='pt'\n",
" )\n",
" enc_seq = enc['input_ids'].squeeze(0)\n",
"\n",
" if len(enc_seq) < self.seq_len + 1:\n",
" continue\n",
"\n",
" yield enc_seq[:-1], enc_seq[1:]\n",
"\n",
"class LightningDataLoader(L.LightningDataModule):\n",
" def __init__(self, tokenizer_ckpt, dataset_ckpt, batch_size, seq_len, num_workers):\n",
" super().__init__()\n",
" self.tokenizer_ckpt = tokenizer_ckpt\n",
" self.dataset_ckpt = dataset_ckpt\n",
" self.batch_size = batch_size\n",
" self.seq_len = seq_len\n",
" self.num_workers = num_workers\n",
"\n",
" def setup(self, stage=None):\n",
" self.train_dataset = HFStreamingDataset(self.tokenizer_ckpt, self.dataset_ckpt, self.seq_len)\n",
"\n",
" def train_dataloader(self):\n",
" return DataLoader(\n",
" self.train_dataset,\n",
" batch_size=self.batch_size,\n",
" num_workers=self.num_workers,\n",
" pin_memory=True\n",
" )"
],
"metadata": {
"cellView": "form",
"id": "GK3ja1ST9a68"
},
"execution_count": 23,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"# **Liger Kernel SwiGLU MLP Config**"
],
"metadata": {
"id": "7BVHeHQY-gsY"
}
},
{
"cell_type": "code",
"source": [
"# @title\n",
"class SwiGLUMLP_Config():\n",
" def __init__(self, hidden_size, hidden_act, exp_factor):\n",
" self.hidden_size = hidden_size\n",
" self.intermediate_size = hidden_size*exp_factor\n",
" self.hidden_act = hidden_act"
],
"metadata": {
"cellView": "form",
"id": "GbhHEtfz-llC"
},
"execution_count": 24,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"# **SwiGLU Fallback Torch Implementation**"
],
"metadata": {
"id": "7msxah4h9pWY"
}
},
{
"cell_type": "code",
"source": [
"# @title\n",
"import torch\n",
"import torch.nn as nn\n",
"import torch.nn.functional as F\n",
"\n",
"class SwiGLU(nn.Module):\n",
" def __init__(self, embed_dims, exp_factor):\n",
" super().__init__()\n",
"\n",
" self.proj_up = nn.Linear(embed_dims, embed_dims*exp_factor*2)\n",
" self.proj_down = nn.Linear(embed_dims*exp_factor, embed_dims)\n",
"\n",
" def forward(self, x):\n",
" a, b = self.proj_up(x).chunk(2, dim=-1)\n",
" y = F.silu(a) * b\n",
" return self.proj_down(y)"
],
"metadata": {
"cellView": "form",
"id": "1m1CELQA9elE"
},
"execution_count": 25,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"# ** Transformer implementation Fails!!!! RoPE Liger Kernel and Transformers Fallback implementations**"
],
"metadata": {
"id": "sntMBWdV92wa"
}
},
{
"cell_type": "code",
"source": [
"\n",
"import torch\n",
"import torch.nn as nn\n",
"import importlib.util\n",
"from transformers.models.llama.modeling_llama import (\n",
" LlamaRotaryEmbedding,\n",
" LlamaConfig,\n",
" apply_rotary_pos_emb\n",
")\n",
"\n",
"if importlib.util.find_spec('liger_kernel'):\n",
" import liger_kernel.transformers as liger\n",
"\n",
"class RoPE(nn.Module):\n",
" def __init__(self, seq_len, num_heads, head_size, use_liger):\n",
" super().__init__()\n",
" config = LlamaConfig(\n",
" hidden_size=num_heads * head_size,\n",
" num_attention_heads=num_heads,\n",
" num_key_value_heads=num_heads,\n",
" max_position_embeddings=seq_len,\n",
" vocab_size=6767,\n",
" )\n",
" self.use_liger = True # force set true for testing\n",
" self.rotary_emb = LlamaRotaryEmbedding(config)\n",
"\n",
" def forward(self, q, k):\n",
" batch_size, seq_len = q.shape[0], q.shape[1]\n",
" # position_ids must be (batch_size, seq_len)\n",
" position_ids = torch.arange(seq_len, dtype=torch.long, device=q.device).unsqueeze(0).expand(batch_size, -1)\n",
"\n",
" cos, sin = self.rotary_emb(k, position_ids)\n",
"\n",
" if self.use_liger:\n",
" tt_q, tt_k = liger.liger_rotary_pos_emb(q, k, cos, sin)\n",
" else:\n",
" tt_q, tt_k = apply_rotary_pos_emb(q, k, cos, sin)\n",
" return tt_q, tt_k"
],
"metadata": {
"id": "u_ORtF2V9blG"
},
"execution_count": 26,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"# **Self Attention Implementation**"
],
"metadata": {
"id": "PoTg1Qn7-BQT"
}
},
{
"cell_type": "code",
"source": [
"# @title\n",
"import torch.nn as nn\n",
"import torch.nn.functional as F\n",
"\n",
"class Attention_Head(nn.Module):\n",
" def __init__(self, seq_len, embed_dims, head_size, num_heads, use_liger):\n",
" super().__init__()\n",
" self.embed_dims = embed_dims\n",
" self.num_heads = num_heads\n",
" self.head_size = head_size\n",
" self.total_heads = head_size * num_heads\n",
"\n",
" self.q_proj = nn.Linear(embed_dims, self.total_heads)\n",
" self.k_proj = nn.Linear(embed_dims, self.total_heads)\n",
" self.v_proj = nn.Linear(embed_dims, self.total_heads)\n",
" self.o_proj = nn.Linear(self.total_heads, embed_dims)\n",
" self.pe = RoPE(seq_len, num_heads, head_size, use_liger)\n",
"\n",
" def forward(self, logits, batch_size, seq_len):\n",
" q = self.q_proj(logits).view(batch_size, seq_len, self.num_heads, self.head_size)\n",
" k = self.k_proj(logits).view(batch_size, seq_len, self.num_heads, self.head_size)\n",
"\n",
" q_pe, k_pe = self.pe.forward(q, k)\n",
"\n",
" q_pe = q_pe.transpose(1, 2)\n",
" k_pe = k_pe.transpose(1, 2)\n",
"\n",
" v = (\n",
" self.v_proj(logits)\n",
" .view(batch_size, seq_len, self.num_heads, self.head_size)\n",
" .transpose(1, 2)\n",
" )\n",
"\n",
" attention_out = F.scaled_dot_product_attention(q_pe, k_pe, v, is_causal=True)\n",
" out = (\n",
" attention_out.transpose(1, 2)\n",
" .contiguous()\n",
" .view(batch_size, seq_len, self.total_heads)\n",
" )\n",
" return self.o_proj(out)"
],
"metadata": {
"cellView": "form",
"id": "hZ16HXCM98xZ"
},
"execution_count": 27,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"# **Attention Block**"
],
"metadata": {
"id": "0NtrrBGr-njZ"
}
},
{
"cell_type": "code",
"source": [
"# @title\n",
"import torch.nn as nn\n",
"\n",
"class Block(nn.Module):\n",
" def __init__(self, seq_len, embed_dims, head_size, num_heads, use_liger, exp_factor=3):\n",
" super().__init__()\n",
" self.embed_dims = embed_dims\n",
" self.head_size = head_size\n",
"\n",
" if use_liger:\n",
" self.rms_Norm1 = liger.LigerRMSNorm(embed_dims)\n",
" self.rms_Norm2 = liger.LigerRMSNorm(embed_dims)\n",
"\n",
" config = SwiGLUMLP_Config(embed_dims, 'swish', exp_factor)\n",
" self.FFN = liger.LigerSwiGLUMLP(config)\n",
"\n",
" else:\n",
" self.rms_Norm1 = nn.RMSNorm(embed_dims)\n",
" self.rms_Norm2 = nn.RMSNorm(embed_dims)\n",
"\n",
" self.FFN = SwiGLU(embed_dims, exp_factor)\n",
"\n",
" self.Attention_Head = Attention_Head(seq_len, embed_dims, head_size, num_heads, use_liger)\n",
"\n",
" def forward(self, logits, batch_size, seq_len):\n",
" x = self.Attention_Head(self.rms_Norm1(logits), batch_size, seq_len)\n",
" x = x + logits\n",
" out = self.FFN(self.rms_Norm2(x))\n",
" out = out + x\n",
" return out"
],
"metadata": {
"cellView": "form",
"id": "6u5YJvd_-qyp"
},
"execution_count": 28,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"# **Lightning Transformer**"
],
"metadata": {
"id": "ZfbC5BoE-SuB"
}
},
{
"cell_type": "code",
"source": [
"# @title\n",
"import torch\n",
"import torch.nn as nn\n",
"import lightning as L\n",
"\n",
"from torch.optim import AdamW\n",
"from torch.optim.lr_scheduler import SequentialLR, LinearLR, ConstantLR, CosineAnnealingLR\n",
"from huggingface_hub import PyTorchModelHubMixin\n",
"\n",
"class LightningTransformer(L.LightningModule, PyTorchModelHubMixin):\n",
" def __init__(\n",
" self,\n",
" batch_size,\n",
" seq_len,\n",
" embed_dims,\n",
" head_size,\n",
" num_heads,\n",
" block_num,\n",
" vocab_size,\n",
" lr,\n",
" iterations,\n",
" warmup_steps=2000,\n",
" decay_ratio=0.1,\n",
" use_liger=False,\n",
" tie_weights=False\n",
" ):\n",
" super().__init__()\n",
" self.save_hyperparameters() # Logs hyperparameters to WandB\n",
" self.batch_size = batch_size\n",
" self.seq_len = seq_len\n",
" self.embed_dims = embed_dims\n",
" self.head_size = head_size\n",
" self.num_heads = num_heads\n",
" self.vocab_size = vocab_size\n",
"\n",
" self.block_list = nn.ModuleList(\n",
" [Block(seq_len, embed_dims, head_size, num_heads, use_liger) for _ in range(block_num)]\n",
" )\n",
"\n",
" self.lr = lr\n",
" self.iterations = iterations\n",
" self.warmup_steps = warmup_steps\n",
" self.decay_ratio = decay_ratio\n",
"\n",
" self.token_embed = nn.Embedding(vocab_size, embed_dims)\n",
" self.embed_proj = nn.Linear(embed_dims, vocab_size)\n",
"\n",
" # Set both layers to same weights if using weight tying (Torch auto-transposes)\n",
" if tie_weights:\n",
" self.token_embed.weight = self.embed_proj.weight\n",
"\n",
" # use Liger kernel if CUDA is available and LigerKernel is installed\n",
" if use_liger:\n",
" self.softmax = liger.LigerSoftmax()\n",
" self.cross_entropy = liger.LigerCrossEntropyLoss()\n",
" self.rms_Norm_embed = liger.LigerRMSNorm(embed_dims)\n",
"\n",
" # fallback to Pytorch and Transformers\n",
" else:\n",
" self.softmax = nn.Softmax(dim=-1)\n",
" self.cross_entropy = nn.CrossEntropyLoss()\n",
" self.rms_Norm_embed = nn.RMSNorm(embed_dims)\n",
"\n",
" def _init_weights(self, module):\n",
" if isinstance(module, nn.Linear):\n",
" torch.nn.init.normal_(module.weight, mean=0.0, std=0.02)\n",
" if module.bias is not None:\n",
" torch.nn.init.zeros_(module.bias)\n",
" elif isinstance(module, nn.Embedding):\n",
" torch.nn.init.normal_(\n",
" module.weight,\n",
" mean=0.0,\n",
" std=0.02 * (self.embed_dims ** 0.5)\n",
" )\n",
" elif isinstance(module, nn.RMSNorm):\n",
" torch.nn.init.ones_(module.weight)\n",
" pass\n",
"\n",
" def configure_optimizers(self):\n",
" optimizer = AdamW(self.parameters(), lr=self.lr)\n",
"\n",
" warmup_scheduler = LinearLR(\n",
" optimizer,\n",
" start_factor=0.1,\n",
" end_factor=1.0,\n",
" total_iters=self.warmup_steps\n",
" )\n",
"\n",
" stable_scheduler = ConstantLR(\n",
" optimizer,\n",
" factor=1.0\n",
" )\n",
"\n",
" cosine_decay_scheduler = CosineAnnealingLR(\n",
" optimizer,\n",
" T_max=self.iterations*self.decay_ratio\n",
" )\n",
"\n",
" wsd_scheduler = SequentialLR(\n",
" optimizer,\n",
" schedulers=[warmup_scheduler, stable_scheduler, cosine_decay_scheduler],\n",
" milestones=[self.warmup_steps, self.iterations * (1 - self.decay_ratio)]\n",
" )\n",
"\n",
" return {\n",
" \"optimizer\": optimizer,\n",
" \"lr_scheduler\": {\"scheduler\": wsd_scheduler, \"interval\": \"step\"},\n",
" }\n",
"\n",
" def training_step(self, batch, batch_idx):\n",
" x, y = batch\n",
" loss = self(x, y)\n",
" self.log(\"train_loss\", loss)\n",
" return loss\n",
"\n",
" def forward(self, inputs, target=None):\n",
" batch_size, seq_len = inputs.shape\n",
" logits = self.token_embed(inputs)\n",
"\n",
" for block in self.block_list:\n",
" logits = block(logits, batch_size, seq_len)\n",
"\n",
" unembed_out = self.embed_proj(self.rms_Norm_embed(logits))\n",
"\n",
" if target is not None:\n",
" preds = unembed_out.view(batch_size * seq_len, -1)\n",
" target = target.view(-1)\n",
"\n",
" loss_fn = self.cross_entropy(preds, target)\n",
" return loss_fn\n",
"\n",
" return unembed_out\n",
"\n",
" def generate(self, input_tokens, max_tokens):\n",
" for _ in range(max_tokens):\n",
" last_seq = input_tokens[:, -self.seq_len :]\n",
" logits = self(last_seq)\n",
" logits = logits[:, -1, :]\n",
" probs = self.softmax(logits)\n",
" next_tok = torch.multinomial(probs, num_samples=1)\n",
" input_tokens = torch.cat((input_tokens, next_tok), dim=1)\n",
" return input_tokens"
],
"metadata": {
"id": "TCFRaH-N-FHd"
},
"execution_count": 29,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"# **Train Script**"
],
"metadata": {
"id": "O_6kMEpT-zqG"
}
},
{
"cell_type": "code",
"source": [
"# @title\n",
"dataset_ckpt = \"HuggingFaceFW/fineweb-edu\"\n",
"tokenizer_ckpt = \"HuggingFaceTB/SmolLM-1.7B\"\n",
"save_ckpt = \"hf://buckets/Jumpr/67/model_ckpts\" # \"model_ckpts\"\n",
"pretrain_ckpt = None\n",
"enable_liger_kernel = True\n",
"\n",
"run_id = \"colab_test2_liger_enabled\"\n",
"save_every_n_train_steps = 50\n",
"save_top_k = -1\n",
"log_every_n_steps = 1\n",
"\n",
"precision = \"bf16-mixed\"\n",
"gradient_clip_val = 1.0\n",
"devices = 1\n",
"\n",
"batch_size = 1\n",
"batch_acc = 1\n",
"lr = 3e-4\n",
"iterations = 10000\n",
"max_epochs = 1\n",
"num_workers = 2\n",
"\n",
"seq_len = 64\n",
"embed_dims = 64\n",
"head_size = 8\n",
"num_heads = 8\n",
"block_num = 4\n",
"vocab_size = 49152"
],
"metadata": {
"id": "yt7GcTZJAWNe"
},
"execution_count": 30,
"outputs": []
},
{
"cell_type": "code",
"source": [
"# @title\n",
"import torch\n",
"from lightning.pytorch.loggers import WandbLogger\n",
"import lightning as L\n",
"\n",
"import importlib.util\n",
"\n",
"def main():\n",
"\n",
" wandb_logger = WandbLogger(\n",
" log_model='false',\n",
" resume='allow',\n",
" id=run_id\n",
" )\n",
"\n",
" # checks if CUDA available on device\n",
" use_liger = False\n",
" if torch.cuda.is_available() and importlib.util.find_spec('liger_kernel') and enable_liger_kernel:\n",
" use_liger = True\n",
"\n",
" model = LightningTransformer(\n",
" batch_size=batch_size,\n",
" seq_len=seq_len,\n",
" embed_dims=embed_dims,\n",
" head_size=head_size,\n",
" num_heads=num_heads,\n",
" block_num=block_num,\n",
" vocab_size=vocab_size,\n",
" lr=lr,\n",
" iterations=iterations,\n",
" use_liger=use_liger\n",
" )\n",
"\n",
" dataloader = LightningDataLoader(\n",
" dataset_ckpt,\n",
" tokenizer_ckpt,\n",
" batch_size,\n",
" seq_len,\n",
" num_workers\n",
" )\n",
"\n",
" trainer = L.Trainer(\n",
" logger=wandb_logger,\n",
" max_epochs=max_epochs,\n",
" limit_train_batches=iterations,\n",
" precision=precision,\n",
" gradient_clip_val=gradient_clip_val,\n",
" accumulate_grad_batches=batch_acc,\n",
" log_every_n_steps=log_every_n_steps,\n",
" enable_checkpointing=True,\n",
" devices=devices,\n",
" strategy='auto',\n",
" # profiler='advanced',\n",
" callbacks=[\n",
" L.pytorch.callbacks.ModelCheckpoint(\n",
" dirpath=save_ckpt,\n",
" every_n_train_steps=save_every_n_train_steps,\n",
" save_top_k=save_top_k,\n",
"\n",
" #save_weights_only=True\n",
" ),\n",
" L.pytorch.callbacks.LearningRateMonitor(\n",
" logging_interval='step'\n",
" ),\n",
" # L.pytorch.callbacks.ThroughputMonitor(\n",
" # batch_size_fn=lambda batch: batch[0].size(0),\n",
" # length_fn=lambda batch: batch[0].size(1)\n",
" # ),\n",
"\n",
" ],\n",
" )\n",
"\n",
" # if pretrain_ckpt is not None:\n",
" # trainer.fit(model, datamodule=dataloader, ckpt_path=pretrain_ckpt) # doesnt work\n",
" # else:\n",
" trainer.fit(model, datamodule=dataloader)"
],
"metadata": {
"id": "CVllHbI6-3cj"
},
"execution_count": 31,
"outputs": []
},
{
"cell_type": "code",
"source": [
"# @title\n",
"import os\n",
"from google.colab import userdata\n",
"os.environ['WANDB_API_KEY'] = userdata.get('WANDB_API_KEY') # must have a wandb secret WANDB_API_KEY set as your wandb api key\n",
"main()"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 579,
"referenced_widgets": [
"a2028495dc4743a180f31e9c191f6a59",
"988a7701f3134ef18196fe2f70509987",
"78456e7017ef48a18a7e443c6a575b01",
"555c1c78b2bb4df9adfd31879c708c62",
"8c3ecfcedf4342af8e62add4897211e8",
"cf462213ab294dc9bf75f4594cd69891",
"3b2cf5b575cf4350887fe072a2d84311",
"a3d72c32318d447b89bb489e02f58dc9",
"2d9fac91f8514fd2856d07fd81880b9f",
"8e401f470b2046efa750fd3cf9749c05",
"9dc69fdae068418989ca4b24f796002a",
"04dc40a15f8e46bfb63e071ff04a9f6c",
"ed4d63ca46584dfbbd9b3d2591ff28d6",
"93c49d86eea7415bb6bba9678fcceb2a",
"1c21c4be4b70449d982972d15a44aa25",
"7aa4a191402445b998898ff0f383a953",
"b968f8835c054cf69e3119b6643b7913",
"2eab38a0837540529052e2bf58f3eb06",
"8f3447bc345d4860a56506f19282561e",
"235e2fed74de42118e35e5bc6dce707d",
"01074671af2b433a864c371988b2de07",
"f3c3753cd34b44efadd9034546f43ea7"
]
},
"cellView": "form",
"id": "P6lmizu_A9Qb",
"outputId": "ffba1560-5ad2-44fc-a02a-15c2569a0865"
},
"execution_count": 32,
"outputs": [
{
"output_type": "stream",
"name": "stderr",
"text": [
"INFO: Using bfloat16 Automatic Mixed Precision (AMP)\n",
"INFO:lightning.pytorch.utilities.rank_zero:Using bfloat16 Automatic Mixed Precision (AMP)\n",
"INFO: GPU available: True (cuda), used: True\n",
"INFO:lightning.pytorch.utilities.rank_zero:GPU available: True (cuda), used: True\n",
"INFO: TPU available: False, using: 0 TPU cores\n",
"INFO:lightning.pytorch.utilities.rank_zero:TPU available: False, using: 0 TPU cores\n",
"INFO: 💡 Tip: For seamless cloud logging and experiment tracking, try installing [litlogger](https://pypi.org/project/litlogger/) to enable LitLogger, which logs metrics and artifacts automatically to the Lightning Experiments platform.\n",
"INFO:lightning.pytorch.utilities.rank_zero:💡 Tip: For seamless cloud logging and experiment tracking, try installing [litlogger](https://pypi.org/project/litlogger/) to enable LitLogger, which logs metrics and artifacts automatically to the Lightning Experiments platform.\n",
"/usr/local/lib/python3.12/dist-packages/lightning/pytorch/loggers/wandb.py:400: There is a wandb run already in progress and newly created instances of `WandbLogger` will reuse this run. If this is not desired, call `wandb.finish()` before instantiating `WandbLogger`.\n"
]
},
{
"output_type": "display_data",
"data": {
"text/plain": [
"Resolving data files: 0%| | 0/2410 [00:00<?, ?it/s]"
],
"application/vnd.jupyter.widget-view+json": {
"version_major": 2,
"version_minor": 0,
"model_id": "a2028495dc4743a180f31e9c191f6a59"
}
},
"metadata": {}
},
{
"output_type": "display_data",
"data": {
"text/plain": [
"Resolving data files: 0%| | 0/2410 [00:00<?, ?it/s]"
],
"application/vnd.jupyter.widget-view+json": {
"version_major": 2,
"version_minor": 0,
"model_id": "04dc40a15f8e46bfb63e071ff04a9f6c"
}
},
"metadata": {}
},
{
"output_type": "error",
"ename": "FileNotFoundError",
"evalue": "buckets/Jumpr/67",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mFileNotFoundError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m/tmp/ipykernel_8418/1439823157.py\u001b[0m in \u001b[0;36m<cell line: 0>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0;32mfrom\u001b[0m \u001b[0mgoogle\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcolab\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0muserdata\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0mos\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0menviron\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m'WANDB_API_KEY'\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0muserdata\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mget\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'WANDB_API_KEY'\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;31m# must have a wandb secret WANDB_API_KEY set as your wandb api key\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 5\u001b[0;31m \u001b[0mmain\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;32m/tmp/ipykernel_8418/3964967622.py\u001b[0m in \u001b[0;36mmain\u001b[0;34m()\u001b[0m\n\u001b[1;32m 74\u001b[0m \u001b[0;31m# trainer.fit(model, datamodule=dataloader, ckpt_path=pretrain_ckpt) # doesnt work\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 75\u001b[0m \u001b[0;31m# else:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 76\u001b[0;31m \u001b[0mtrainer\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mfit\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmodel\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mdatamodule\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mdataloader\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;32m/usr/local/lib/python3.12/dist-packages/lightning/pytorch/trainer/trainer.py\u001b[0m in \u001b[0;36mfit\u001b[0;34m(self, model, train_dataloaders, val_dataloaders, datamodule, ckpt_path, weights_only)\u001b[0m\n\u001b[1;32m 582\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mtraining\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;32mTrue\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 583\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mshould_stop\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;32mFalse\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 584\u001b[0;31m call._call_and_handle_interrupt(\n\u001b[0m\u001b[1;32m 585\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 586\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_fit_impl\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m/usr/local/lib/python3.12/dist-packages/lightning/pytorch/trainer/call.py\u001b[0m in \u001b[0;36m_call_and_handle_interrupt\u001b[0;34m(trainer, trainer_fn, *args, **kwargs)\u001b[0m\n\u001b[1;32m 47\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mtrainer\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mstrategy\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mlauncher\u001b[0m \u001b[0;32mis\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 48\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mtrainer\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mstrategy\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mlauncher\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mlaunch\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mtrainer_fn\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mtrainer\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mtrainer\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 49\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mtrainer_fn\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 50\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 51\u001b[0m \u001b[0;32mexcept\u001b[0m \u001b[0m_TunerExitException\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m/usr/local/lib/python3.12/dist-packages/lightning/pytorch/trainer/trainer.py\u001b[0m in \u001b[0;36m_fit_impl\u001b[0;34m(self, model, train_dataloaders, val_dataloaders, datamodule, ckpt_path, weights_only)\u001b[0m\n\u001b[1;32m 628\u001b[0m \u001b[0mmodel_connected\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mlightning_module\u001b[0m \u001b[0;32mis\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 629\u001b[0m )\n\u001b[0;32m--> 630\u001b[0;31m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_run\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmodel\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mckpt_path\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mckpt_path\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mweights_only\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mweights_only\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 631\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 632\u001b[0m \u001b[0;32massert\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mstate\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mstopped\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m/usr/local/lib/python3.12/dist-packages/lightning/pytorch/trainer/trainer.py\u001b[0m in \u001b[0;36m_run\u001b[0;34m(self, model, ckpt_path, weights_only)\u001b[0m\n\u001b[1;32m 1037\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_data_connector\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mprepare_data\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1038\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 1039\u001b[0;31m \u001b[0mcall\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_call_setup_hook\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;31m# allow user to set up LightningModule in accelerator environment\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 1040\u001b[0m \u001b[0mlog\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdebug\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34mf\"{self.__class__.__name__}: configuring model\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1041\u001b[0m \u001b[0mcall\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_call_configure_model\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m/usr/local/lib/python3.12/dist-packages/lightning/pytorch/trainer/call.py\u001b[0m in \u001b[0;36m_call_setup_hook\u001b[0;34m(trainer)\u001b[0m\n\u001b[1;32m 107\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mtrainer\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdatamodule\u001b[0m \u001b[0;32mis\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 108\u001b[0m \u001b[0m_call_lightning_datamodule_hook\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mtrainer\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m\"setup\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mstage\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mfn\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 109\u001b[0;31m \u001b[0m_call_callback_hooks\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mtrainer\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m\"setup\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mstage\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mfn\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 110\u001b[0m \u001b[0m_call_lightning_module_hook\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mtrainer\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m\"setup\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mstage\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mfn\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 111\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m/usr/local/lib/python3.12/dist-packages/lightning/pytorch/trainer/call.py\u001b[0m in \u001b[0;36m_call_callback_hooks\u001b[0;34m(trainer, hook_name, monitoring_callbacks, *args, **kwargs)\u001b[0m\n\u001b[1;32m 226\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mcallable\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mfn\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 227\u001b[0m \u001b[0;32mwith\u001b[0m \u001b[0mtrainer\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mprofiler\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mprofile\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34mf\"[Callback]{callback.state_key}.{hook_name}\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 228\u001b[0;31m \u001b[0mfn\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mtrainer\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mtrainer\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mlightning_module\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 229\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 230\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mpl_module\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m/usr/local/lib/python3.12/dist-packages/lightning/pytorch/callbacks/model_checkpoint.py\u001b[0m in \u001b[0;36msetup\u001b[0;34m(self, trainer, pl_module, stage)\u001b[0m\n\u001b[1;32m 327\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_fs\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mget_filesystem\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdirpath\u001b[0m \u001b[0;32mor\u001b[0m \u001b[0;34m\"\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 328\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mtrainer\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mis_global_zero\u001b[0m \u001b[0;32mand\u001b[0m \u001b[0mstage\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0;34m\"fit\"\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 329\u001b[0;31m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m__warn_if_dir_not_empty\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdirpath\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 330\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msave_last\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0;34m\"link\"\u001b[0m \u001b[0;32mand\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0m_is_local_file_protocol\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdirpath\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 331\u001b[0m raise ValueError(\n",
"\u001b[0;32m/usr/local/lib/python3.12/dist-packages/lightning/pytorch/callbacks/model_checkpoint.py\u001b[0m in \u001b[0;36m__warn_if_dir_not_empty\u001b[0;34m(self, dirpath)\u001b[0m\n\u001b[1;32m 878\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 879\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0m__warn_if_dir_not_empty\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mdirpath\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0m_PATH\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m->\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 880\u001b[0;31m \u001b[0;32mif\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msave_top_k\u001b[0m \u001b[0;34m!=\u001b[0m \u001b[0;36m0\u001b[0m \u001b[0;32mand\u001b[0m \u001b[0m_is_dir\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_fs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mdirpath\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mstrict\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mTrue\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32mand\u001b[0m \u001b[0mlen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_fs\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mls\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mdirpath\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m>\u001b[0m \u001b[0;36m0\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 881\u001b[0m \u001b[0mrank_zero_warn\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34mf\"Checkpoint directory {dirpath} exists and is not empty.\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 882\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m/usr/local/lib/python3.12/dist-packages/huggingface_hub/hf_file_system.py\u001b[0m in \u001b[0;36mls\u001b[0;34m(self, path, detail, refresh, revision, **kwargs)\u001b[0m\n\u001b[1;32m 503\u001b[0m \u001b[0;31m# Path could be a file\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 504\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0mresolved_path\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mpath\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 505\u001b[0;31m \u001b[0m_raise_file_not_found\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mpath\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 506\u001b[0m \u001b[0;32mtry\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 507\u001b[0m \u001b[0mout\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_ls_tree\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_parent\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mpath\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mrefresh\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mrefresh\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mrevision\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mrevision\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m/usr/local/lib/python3.12/dist-packages/huggingface_hub/hf_file_system.py\u001b[0m in \u001b[0;36m_raise_file_not_found\u001b[0;34m(path, err)\u001b[0m\n\u001b[1;32m 1407\u001b[0m \u001b[0;32melif\u001b[0m \u001b[0misinstance\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0merr\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mHFValidationError\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1408\u001b[0m \u001b[0mmsg\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34mf\"{path} (invalid repository id)\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 1409\u001b[0;31m \u001b[0;32mraise\u001b[0m \u001b[0mFileNotFoundError\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmsg\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32mfrom\u001b[0m \u001b[0merr\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 1410\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1411\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mFileNotFoundError\u001b[0m: buckets/Jumpr/67"
]
}
]
},
{
"cell_type": "code",
"source": [],
"metadata": {
"id": "GkvWXEHtHbib"
},
"execution_count": null,
"outputs": []
}
]
}

Xet Storage Details

Size:
68.4 kB
·
Xet hash:
146f03d41314d6f13a0b3fe389a4970a54a2f27102c300874d38701268cd3ec6

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.