diff --git "a/lora_forward_pass_test.ipynb" "b/lora_forward_pass_test.ipynb" --- "a/lora_forward_pass_test.ipynb" +++ "b/lora_forward_pass_test.ipynb" @@ -4,9 +4,6 @@ "metadata": { "colab": { "provenance": [], - "collapsed_sections": [ - "u6BXluFgu5Rl" - ], "gpuType": "T4" }, "kernelspec": { @@ -19,7 +16,7 @@ "accelerator": "GPU", "widgets": { "application/vnd.jupyter.widget-state+json": { - "11539f142025414e804b5fdc9c48eda6": { + "2e1f425b2f3e4d18a5036fe961804b39": { "model_module": "@jupyter-widgets/controls", "model_name": "HBoxModel", "model_module_version": "1.5.0", @@ -34,14 +31,14 @@ "_view_name": "HBoxView", "box_style": "", "children": [ - "IPY_MODEL_72b20197d10041d48b038eaa8256183f", - "IPY_MODEL_557e328cf1a74de08084e897affd0e44", - "IPY_MODEL_c51efbe30cfa44f4a1635722aea0aa8a" + "IPY_MODEL_d818e77ae09443f999eb6fc249255b93", + "IPY_MODEL_858c08835bbc43d1b53c6fd091fdc52e", + "IPY_MODEL_b9a3b84c1bc74fc28d4e83654b4f26e5" ], - "layout": "IPY_MODEL_b28f6f41640a4c69a114324839b40b8e" + "layout": "IPY_MODEL_7002df0db93d41b39a2e27ceb6a7be58" } }, - "72b20197d10041d48b038eaa8256183f": { + "d818e77ae09443f999eb6fc249255b93": { "model_module": "@jupyter-widgets/controls", "model_name": "HTMLModel", "model_module_version": "1.5.0", @@ -56,13 +53,13 @@ "_view_name": "HTMLView", "description": "", "description_tooltip": null, - "layout": "IPY_MODEL_e628945c395249e38ab5988236460f4a", + "layout": "IPY_MODEL_36fcb14d4f45413094b1476c470767af", "placeholder": "​", - "style": "IPY_MODEL_e48a04cbfbab4ce3a9f8190e2a56aecc", - "value": "Epoch 1/30:   0%" + "style": "IPY_MODEL_55c5120ed2794c72919269e06317b567", + "value": "Loading pipeline components...: 100%" } }, - "557e328cf1a74de08084e897affd0e44": { + "858c08835bbc43d1b53c6fd091fdc52e": { "model_module": "@jupyter-widgets/controls", "model_name": "FloatProgressModel", "model_module_version": "1.5.0", @@ -75,18 +72,18 @@ "_view_module": "@jupyter-widgets/controls", "_view_module_version": "1.5.0", "_view_name": "ProgressView", - "bar_style": "danger", + "bar_style": "success", "description": "", "description_tooltip": null, - "layout": "IPY_MODEL_1004326b9b104a46a32aacd6c946df83", - "max": 2, + "layout": "IPY_MODEL_a43bc537526649fcad39b4adf96e620c", + "max": 5, "min": 0, "orientation": "horizontal", - "style": "IPY_MODEL_5ebbca15a97745dc95afcf5388c69def", - "value": 0 + "style": "IPY_MODEL_99256de0b03840c2a5072cf781b7f5ad", + "value": 5 } }, - "c51efbe30cfa44f4a1635722aea0aa8a": { + "b9a3b84c1bc74fc28d4e83654b4f26e5": { "model_module": "@jupyter-widgets/controls", "model_name": "HTMLModel", "model_module_version": "1.5.0", @@ -101,13 +98,13 @@ "_view_name": "HTMLView", "description": "", "description_tooltip": null, - "layout": "IPY_MODEL_f7f7a5dd82984c3cac21a394c5f6e9f0", + "layout": "IPY_MODEL_8b1fce564f334d38b978855fcfdba7f1", "placeholder": "​", - "style": "IPY_MODEL_86e499cc81b24d1c8d2aa6675d33095a", - "value": " 0/2 [00:01<?, ?it/s]" + "style": "IPY_MODEL_89aab0a940c04cad81dd77d373891892", + "value": " 5/5 [00:20<00:00,  6.44s/it]" } }, - "b28f6f41640a4c69a114324839b40b8e": { + "7002df0db93d41b39a2e27ceb6a7be58": { "model_module": "@jupyter-widgets/base", "model_name": "LayoutModel", "model_module_version": "1.2.0", @@ -159,7 +156,7 @@ "width": null } }, - "e628945c395249e38ab5988236460f4a": { + "36fcb14d4f45413094b1476c470767af": { "model_module": "@jupyter-widgets/base", "model_name": "LayoutModel", "model_module_version": "1.2.0", @@ -211,7 +208,7 @@ "width": null } }, - "e48a04cbfbab4ce3a9f8190e2a56aecc": { + "55c5120ed2794c72919269e06317b567": { "model_module": "@jupyter-widgets/controls", "model_name": "DescriptionStyleModel", "model_module_version": "1.5.0", @@ -226,7 +223,7 @@ "description_width": "" } }, - "1004326b9b104a46a32aacd6c946df83": { + "a43bc537526649fcad39b4adf96e620c": { "model_module": "@jupyter-widgets/base", "model_name": "LayoutModel", "model_module_version": "1.2.0", @@ -278,7 +275,7 @@ "width": null } }, - "5ebbca15a97745dc95afcf5388c69def": { + "99256de0b03840c2a5072cf781b7f5ad": { "model_module": "@jupyter-widgets/controls", "model_name": "ProgressStyleModel", "model_module_version": "1.5.0", @@ -294,7 +291,7 @@ "description_width": "" } }, - "f7f7a5dd82984c3cac21a394c5f6e9f0": { + "8b1fce564f334d38b978855fcfdba7f1": { "model_module": "@jupyter-widgets/base", "model_name": "LayoutModel", "model_module_version": "1.2.0", @@ -346,7 +343,7 @@ "width": null } }, - "86e499cc81b24d1c8d2aa6675d33095a": { + "89aab0a940c04cad81dd77d373891892": { "model_module": "@jupyter-widgets/controls", "model_name": "DescriptionStyleModel", "model_module_version": "1.5.0", @@ -361,7 +358,7 @@ "description_width": "" } }, - "16577fe1698040568ea4dba67a1fa7d0": { + "74e77df1e60d4cd2bef03fc9e09f6781": { "model_module": "@jupyter-widgets/controls", "model_name": "HBoxModel", "model_module_version": "1.5.0", @@ -376,14 +373,14 @@ "_view_name": "HBoxView", "box_style": "", "children": [ - "IPY_MODEL_b85487705df34c2e867fcbb7024b4b5b", - "IPY_MODEL_c0ab2e603303426f879e8f0abf4b7393", - "IPY_MODEL_fd28a908626645819424f1afe289375d" + "IPY_MODEL_9479d7198b934be8a2a0223b8518b3f7", + "IPY_MODEL_4d1d03b651774493a57e92d2e6139638", + "IPY_MODEL_1601e9e55e3040288dc729423d651537" ], - "layout": "IPY_MODEL_475e1827136247a6b58d353ea694bd19" + "layout": "IPY_MODEL_2a69c33f21f945f1b25c543e39058ea0" } }, - "b85487705df34c2e867fcbb7024b4b5b": { + "9479d7198b934be8a2a0223b8518b3f7": { "model_module": "@jupyter-widgets/controls", "model_name": "HTMLModel", "model_module_version": "1.5.0", @@ -398,13 +395,13 @@ "_view_name": "HTMLView", "description": "", "description_tooltip": null, - "layout": "IPY_MODEL_4d7f3b4f3fee49ac9092af94f2b2be1f", + "layout": "IPY_MODEL_4ffe3afdd90046f0b9fa91cc549797b0", "placeholder": "​", - "style": "IPY_MODEL_1a9301da3ba34b7fbd3c18f826b058fd", - "value": "model_index.json: 100%" + "style": "IPY_MODEL_bdbf3e3412fe401eb2442f02dd5d7ecc", + "value": "Loading weights: 100%" } }, - "c0ab2e603303426f879e8f0abf4b7393": { + "4d1d03b651774493a57e92d2e6139638": { "model_module": "@jupyter-widgets/controls", "model_name": "FloatProgressModel", "model_module_version": "1.5.0", @@ -420,15 +417,15 @@ "bar_style": "success", "description": "", "description_tooltip": null, - "layout": "IPY_MODEL_15999cdf364c48aaba6bc3d99dad3a63", - "max": 446, + "layout": "IPY_MODEL_80aaa28cfe4f47939ef5829e34e5513e", + "max": 902, "min": 0, "orientation": "horizontal", - "style": "IPY_MODEL_e953a19bb91b4ed5aa4e4244e3c91405", - "value": 446 + "style": "IPY_MODEL_4ffe3f2b4839461ab08f43b911744b24", + "value": 902 } }, - "fd28a908626645819424f1afe289375d": { + "1601e9e55e3040288dc729423d651537": { "model_module": "@jupyter-widgets/controls", "model_name": "HTMLModel", "model_module_version": "1.5.0", @@ -443,13 +440,13 @@ "_view_name": "HTMLView", "description": "", "description_tooltip": null, - "layout": "IPY_MODEL_9261c32d28854c9eb50a2e5581de4caf", + "layout": "IPY_MODEL_c4494ad824ab45639e047deb17477120", "placeholder": "​", - "style": "IPY_MODEL_f1083e6575824427be954bc52a119423", - "value": " 446/446 [00:00<00:00, 37.8kB/s]" + "style": "IPY_MODEL_91f1eb8615d24c359788a4ed9f9b899e", + "value": " 902/902 [00:03<00:00, 628.29it/s, Materializing param=model.norm.weight]" } }, - "475e1827136247a6b58d353ea694bd19": { + "2a69c33f21f945f1b25c543e39058ea0": { "model_module": "@jupyter-widgets/base", "model_name": "LayoutModel", "model_module_version": "1.2.0", @@ -501,7 +498,7 @@ "width": null } }, - "4d7f3b4f3fee49ac9092af94f2b2be1f": { + "4ffe3afdd90046f0b9fa91cc549797b0": { "model_module": "@jupyter-widgets/base", "model_name": "LayoutModel", "model_module_version": "1.2.0", @@ -553,7 +550,7 @@ "width": null } }, - "1a9301da3ba34b7fbd3c18f826b058fd": { + "bdbf3e3412fe401eb2442f02dd5d7ecc": { "model_module": "@jupyter-widgets/controls", "model_name": "DescriptionStyleModel", "model_module_version": "1.5.0", @@ -568,7 +565,7 @@ "description_width": "" } }, - "15999cdf364c48aaba6bc3d99dad3a63": { + "80aaa28cfe4f47939ef5829e34e5513e": { "model_module": "@jupyter-widgets/base", "model_name": "LayoutModel", "model_module_version": "1.2.0", @@ -620,7 +617,7 @@ "width": null } }, - "e953a19bb91b4ed5aa4e4244e3c91405": { + "4ffe3f2b4839461ab08f43b911744b24": { "model_module": "@jupyter-widgets/controls", "model_name": "ProgressStyleModel", "model_module_version": "1.5.0", @@ -636,7 +633,7 @@ "description_width": "" } }, - "9261c32d28854c9eb50a2e5581de4caf": { + "c4494ad824ab45639e047deb17477120": { "model_module": "@jupyter-widgets/base", "model_name": "LayoutModel", "model_module_version": "1.2.0", @@ -688,7 +685,7 @@ "width": null } }, - "f1083e6575824427be954bc52a119423": { + "91f1eb8615d24c359788a4ed9f9b899e": { "model_module": "@jupyter-widgets/controls", "model_name": "DescriptionStyleModel", "model_module_version": "1.5.0", @@ -703,7 +700,7 @@ "description_width": "" } }, - "f3d3ac18dc9c4ddeb1ed19f0fdfffe46": { + "02f03c321d5240308c81f7b3ce60c2d9": { "model_module": "@jupyter-widgets/controls", "model_name": "HBoxModel", "model_module_version": "1.5.0", @@ -718,14 +715,14 @@ "_view_name": "HBoxView", "box_style": "", "children": [ - "IPY_MODEL_2cb257c3b846484c90e27e38571252d0", - "IPY_MODEL_7c93c8bd0be445c68254272da976888f", - "IPY_MODEL_7d2113f0421f4880a12d5bb0308fef49" + "IPY_MODEL_2cc3f34f1a5348a78a213ec2216401f6", + "IPY_MODEL_28e0c0b76f7745e9af9870218bfc7e62", + "IPY_MODEL_211084c892f343c3b03a0022a09fb646" ], - "layout": "IPY_MODEL_b91eefa3c4e84d73bcaab6f0d8bd93bc" + "layout": "IPY_MODEL_15f4df00e7de480680c28c73e2907738" } }, - "2cb257c3b846484c90e27e38571252d0": { + "2cc3f34f1a5348a78a213ec2216401f6": { "model_module": "@jupyter-widgets/controls", "model_name": "HTMLModel", "model_module_version": "1.5.0", @@ -740,13 +737,13 @@ "_view_name": "HTMLView", "description": "", "description_tooltip": null, - "layout": "IPY_MODEL_37ffe4997f1647eb81e7a7d78107fdf1", + "layout": "IPY_MODEL_6a9ba0019d534f8e89b5cfe758690e1d", "placeholder": "​", - "style": "IPY_MODEL_1f19f094677e4e97ba4998adad062b69", - "value": "Download complete: " + "style": "IPY_MODEL_88e71a49e09241fe94c3ea1f9084385e", + "value": "Epoch 1/5:   0%" } }, - "7c93c8bd0be445c68254272da976888f": { + "28e0c0b76f7745e9af9870218bfc7e62": { "model_module": "@jupyter-widgets/controls", "model_name": "FloatProgressModel", "model_module_version": "1.5.0", @@ -759,18 +756,18 @@ "_view_module": "@jupyter-widgets/controls", "_view_module_version": "1.5.0", "_view_name": "ProgressView", - "bar_style": "success", + "bar_style": "danger", "description": "", "description_tooltip": null, - "layout": "IPY_MODEL_a6457a01fe5d47ff804d0d78a3fadd1e", - "max": 1, + "layout": "IPY_MODEL_fd5c96ad82e44cb09aa503d79419a142", + "max": 2, "min": 0, "orientation": "horizontal", - "style": "IPY_MODEL_8f8355073ff140f38077ab6a29c0ae0d", - "value": 1 + "style": "IPY_MODEL_2a2803f84f0d4443a657d5cc9e31d1a4", + "value": 0 } }, - "7d2113f0421f4880a12d5bb0308fef49": { + "211084c892f343c3b03a0022a09fb646": { "model_module": "@jupyter-widgets/controls", "model_name": "HTMLModel", "model_module_version": "1.5.0", @@ -785,13 +782,13 @@ "_view_name": "HTMLView", "description": "", "description_tooltip": null, - "layout": "IPY_MODEL_3dd5809a6c0c4c43b1d5166496a9fab6", + "layout": "IPY_MODEL_e79dd2d0a07448518e318fbcb7d01139", "placeholder": "​", - "style": "IPY_MODEL_23d0690c63194efa8d77311d9812eb96", - "value": " 5.92G/? [00:48<00:00, 233MB/s]" + "style": "IPY_MODEL_d3602489cf0844ce9f1f8217fcaa34e3", + "value": " 0/2 [00:01<?, ?it/s]" } }, - "b91eefa3c4e84d73bcaab6f0d8bd93bc": { + "15f4df00e7de480680c28c73e2907738": { "model_module": "@jupyter-widgets/base", "model_name": "LayoutModel", "model_module_version": "1.2.0", @@ -843,7 +840,7 @@ "width": null } }, - "37ffe4997f1647eb81e7a7d78107fdf1": { + "6a9ba0019d534f8e89b5cfe758690e1d": { "model_module": "@jupyter-widgets/base", "model_name": "LayoutModel", "model_module_version": "1.2.0", @@ -895,7 +892,7 @@ "width": null } }, - "1f19f094677e4e97ba4998adad062b69": { + "88e71a49e09241fe94c3ea1f9084385e": { "model_module": "@jupyter-widgets/controls", "model_name": "DescriptionStyleModel", "model_module_version": "1.5.0", @@ -910,7 +907,7 @@ "description_width": "" } }, - "a6457a01fe5d47ff804d0d78a3fadd1e": { + "fd5c96ad82e44cb09aa503d79419a142": { "model_module": "@jupyter-widgets/base", "model_name": "LayoutModel", "model_module_version": "1.2.0", @@ -959,10 +956,10 @@ "right": null, "top": null, "visibility": null, - "width": "20px" + "width": null } }, - "8f8355073ff140f38077ab6a29c0ae0d": { + "2a2803f84f0d4443a657d5cc9e31d1a4": { "model_module": "@jupyter-widgets/controls", "model_name": "ProgressStyleModel", "model_module_version": "1.5.0", @@ -978,7 +975,7 @@ "description_width": "" } }, - "3dd5809a6c0c4c43b1d5166496a9fab6": { + "e79dd2d0a07448518e318fbcb7d01139": { "model_module": "@jupyter-widgets/base", "model_name": "LayoutModel", "model_module_version": "1.2.0", @@ -1030,7 +1027,7 @@ "width": null } }, - "23d0690c63194efa8d77311d9812eb96": { + "d3602489cf0844ce9f1f8217fcaa34e3": { "model_module": "@jupyter-widgets/controls", "model_name": "DescriptionStyleModel", "model_module_version": "1.5.0", @@ -1044,2762 +1041,484 @@ "_view_name": "StyleView", "description_width": "" } + } + } + } + }, + "cells": [ + { + "cell_type": "code", + "execution_count": 22, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 309, + "referenced_widgets": [ + "2e1f425b2f3e4d18a5036fe961804b39", + "d818e77ae09443f999eb6fc249255b93", + "858c08835bbc43d1b53c6fd091fdc52e", + "b9a3b84c1bc74fc28d4e83654b4f26e5", + "7002df0db93d41b39a2e27ceb6a7be58", + "36fcb14d4f45413094b1476c470767af", + "55c5120ed2794c72919269e06317b567", + "a43bc537526649fcad39b4adf96e620c", + "99256de0b03840c2a5072cf781b7f5ad", + "8b1fce564f334d38b978855fcfdba7f1", + "89aab0a940c04cad81dd77d373891892", + "74e77df1e60d4cd2bef03fc9e09f6781", + "9479d7198b934be8a2a0223b8518b3f7", + "4d1d03b651774493a57e92d2e6139638", + "1601e9e55e3040288dc729423d651537", + "2a69c33f21f945f1b25c543e39058ea0", + "4ffe3afdd90046f0b9fa91cc549797b0", + "bdbf3e3412fe401eb2442f02dd5d7ecc", + "80aaa28cfe4f47939ef5829e34e5513e", + "4ffe3f2b4839461ab08f43b911744b24", + "c4494ad824ab45639e047deb17477120", + "91f1eb8615d24c359788a4ed9f9b899e" + ] }, - "fa76c5a087f34b53956918bea856e80b": { - "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_b7101eaa248c48a6ac29a28d44efdfdd", - "IPY_MODEL_fde9d91e04b24c04b27e6a9da56aeb61", - "IPY_MODEL_66a0f92f4bff492c937a9d32f60d0cb5" - ], - "layout": "IPY_MODEL_295e8356948745e28fe33dd29b690aab" - } - }, - "b7101eaa248c48a6ac29a28d44efdfdd": { - "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_9173730e1275476e8de4c9a7da30ae24", - "placeholder": "​", - "style": "IPY_MODEL_8c5932aebc2e4968bee9bf47eec8057a", - "value": "Fetching 11 files: 100%" - } - }, - "fde9d91e04b24c04b27e6a9da56aeb61": { - "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_66e49d85a7014dacbc318467ebf45be8", - "max": 11, - "min": 0, - "orientation": "horizontal", - "style": "IPY_MODEL_8d929c0f97504fb6af4c503d333e4512", - "value": 11 - } - }, - "66a0f92f4bff492c937a9d32f60d0cb5": { - "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_ac114e56e12b410f83cbaca782ea72f1", - "placeholder": "​", - "style": "IPY_MODEL_69c8a7fdc49145eca068262d61f37775", - "value": " 11/11 [00:47<00:00, 14.05s/it]" - } + "id": "9ul9xNLYEzTj", + "outputId": "e727ac39-cd53-435b-f7bf-094284282c45" + }, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Drive already mounted at /content/drive; to attempt to forcibly remount, call drive.mount(\"/content/drive\", force_remount=True).\n", + "✅ torchao updated to version: 0.17.0\n", + "✅ Dependencies installed\n", + "✅ Logged into Hugging Face\n", + " [GPU 0] Loading...\n" + ] }, - "295e8356948745e28fe33dd29b690aab": { - "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 - } + { + "output_type": "display_data", + "data": { + "text/plain": [ + "Loading pipeline components...: 0%| | 0/5 [00:00= 2.11.0 (found 2.10.0+cu128).\n" - ] - }, - { - "output_type": "stream", - "name": "stdout", - "text": [ - "✅ torchao updated to version: 0.17.0\n", - "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m113.2/113.2 kB\u001b[0m \u001b[31m5.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", - "\u001b[?25h✅ Dependencies installed\n", - "✅ Logged into Hugging Face\n" - ] - }, - { - "output_type": "stream", - "name": "stderr", - "text": [ - "Unable to import `torchao` Tensor objects. This may affect loading checkpoints serialized with `torchao`\n", - "Flax classes are deprecated and will be removed in Diffusers v1.0.0. We recommend migrating to PyTorch classes or pinning your version of Diffusers.\n", - "Flax classes are deprecated and will be removed in Diffusers v1.0.0. We recommend migrating to PyTorch classes or pinning your version of Diffusers.\n" - ] - } - ], - "source": [ - "#@markdown # CELL 1 — Setup environment + login\n", - "from google.colab import drive, userdata\n", - "from huggingface_hub import login\n", - "import torch, os, gc\n", - "drive.mount(\"/content/drive\")\n", - "hf_token = userdata.get(\"HF_TOKEN\")\n", - "if hf_token:\n", - " login(token=hf_token)\n", - "else:\n", - " raise ValueError(\"HF_TOKEN not found in Google Colab secrets\")\n", - "#----#\n", - "\n", - "!pip uninstall -q torchao -y\n", - "!pip install -q torchao==0.17.0 --no-deps\n", - "\n", - "import torchao\n", - "print(f\"✅ torchao updated to version: {torchao.__version__}\")\n", - "!pip install -q safetensors huggingface_hub diffusers transformers accelerate sdnq\n", - "\n", - "#images for testing\n", - "# Resize so longest side is no greater than 1024px prior to\n", - "#prior to encoding into VAE\n", - "image1_path = '/content/drive/MyDrive/image1.jpg'\n", - "image2_path = '/content/drive/MyDrive/image2.jpg'\n", - "\n", - "# Test prompts\n", - "prompt1= 'a cute dog on grass'\n", - "prompt2 = 'a golden retriever on a road'\n", - "#-----#\n", - "print(\"✅ Dependencies installed\")\n", - "print(\"✅ Logged into Hugging Face\")\n", - "\n", - "import gc\n", - "import torch\n", - "from diffusers import Flux2KleinPipeline\n", - "#----#\n", - "from sdnq import SDNQConfig\n", - "from sdnq.common import use_torch_compile as triton_is_available\n", - "from sdnq.training import convert_sdnq_model_to_training\n", - "from sdnq.training import sdnq_training_post_load_quant\n", - "from sdnq.loader import apply_sdnq_options_to_model\n", - "#----#\n", - "from functools import partial\n", - "from safetensors.torch import load_file, save_file\n", - "from huggingface_hub import hf_hub_download\n", - "import hashlib\n", - "from pathlib import Path\n", - "import torch\n", - "from transformers import BitsAndBytesConfig\n", - "\n", - "# Patch function\n", - "def patch_encode_prompt(pipe, cached_prompt_embeds, cached_text_ids, gpu_id):\n", - " def patched(self, prompt=None, device=None, num_images_per_prompt=1, prompt_embeds=None, **kwargs):\n", - " device = device or torch.device(f\"cuda:{gpu_id}\")\n", - " if prompt_embeds is None:\n", - " emb = cached_prompt_embeds.to(device)\n", - " emb = emb.repeat(1, num_images_per_prompt, 1)\n", - " emb = emb.view(emb.shape[0] * num_images_per_prompt, emb.shape[1], -1)\n", - " tids = cached_text_ids.to(device)\n", - " return emb, tids\n", - " return self._original_encode_prompt(prompt, device, num_images_per_prompt, prompt_embeds, **kwargs)\n", - "\n", - " if not hasattr(pipe, '_original_encode_prompt'):\n", - " pipe._original_encode_prompt = pipe.encode_prompt\n", - " pipe.encode_prompt = partial(patched, pipe)\n", - " return pipe\n", - "\n", - "# Load single pipe\n", - "def load_klein_pipe(gpu_id: int, pipe_id: int):\n", - " torch.cuda.set_device(gpu_id)\n", - " print(f\" [GPU {gpu_id} - Pipe {pipe_id}] Loading...\")\n", - " MODEL_ID = 'codeShare/Flux.2-Klein-4B-SDNQ-4bit'\n", - " #----#\n", - " edit_prompt = 'this is a test'\n", - " pipe = Flux2KleinPipeline.from_pretrained(\n", - " MODEL_ID,\n", - " torch_dtype=torch.float16,\n", - " low_cpu_mem_usage=True,\n", - " device_map=\"cpu\",\n", - " )\n", - "\n", - " # SDNQ + Prompt cache\n", - " pipe.transformer = apply_sdnq_options_to_model(pipe.transformer, use_quantized_matmul=False)\n", - "\n", - " # Prepare text_encoder\n", - " pipe.text_encoder = pipe.text_encoder.to(f\"cuda:{gpu_id}\")\n", - "\n", - " #if False:\n", - " #with torch.inference_mode():\n", - " # prompt_embeds, text_ids = pipe.encode_prompt(edit_prompt, device=f\"cuda:{gpu_id}\", num_images_per_prompt=1)\n", - "\n", - " #pipe.edit_prompt_embeds = prompt_embeds.cpu()\n", - " #pipe.edit_text_ids = text_ids.cpu()\n", - " #pipe = patch_encode_prompt(pipe, pipe.edit_prompt_embeds, pipe.edit_text_ids, gpu_id)\n", - " #del pipe.text_encoder\n", - " #del pipe.tokenizer\n", - " #-----#\n", - " gc.collect()\n", - " torch.cuda.empty_cache()\n", - " pipe.transformer.to(f\"cuda:{gpu_id}\")\n", - " # pipe.enable_model_cpu_offload(gpu_id=gpu_id) # Removed: This can interfere with VAE device placement during training\n", - "\n", - " # VAE stuffs\n", - " pipe.vae = pipe.vae.to(f\"cuda:{gpu_id}\")\n", - " #pipe.vae.enable_slicing()\n", - " #pipe.vae.enable_tiling()\n", - " #pipe.enable_attention_slicing(1)\n", - " print(f\" [GPU {gpu_id} - Pipe {pipe_id}] Ready | VRAM: {torch.cuda.memory_allocated(gpu_id)/1e9:.2f} GB\")\n", - " return pipe\n", - "#----#\n" - ] - }, - { - "cell_type": "code", - "source": [ - "gc.collect()\n", - "torch.cuda.empty_cache()\n", - "NUM_GPUS = torch.cuda.device_count()\n", - "#----#\n", - "PIPE_ID = 1\n", - "GPU_ID = 0\n", - "pipe = load_klein_pipe(GPU_ID , PIPE_ID)\n", - "print(f'loaded pipe to {pipe.device}')\n", - "#----#\n", - "pipe.transformer = pipe.transformer.to('cuda')\n", - "gc.collect()\n", - "torch.cuda.empty_cache()\n", - "from peft import prepare_model_for_kbit_training\n", - "pipe.transformer = prepare_model_for_kbit_training(pipe.transformer)\n", - "#----#\n", - "from peft import LoraConfig\n", - "config = LoraConfig(\n", - " r=16,\n", - " lora_alpha=8,\n", - " target_modules=[\n", - " \"attn.to_q\", \"attn.to_k\", \"attn.to_v\",\n", - " \"ff.linear_in\", \"ff.linear_out\",\n", - " \"ff_context.linear_in\", \"ff_context.linear_out\",\n", - " \"attn.to_qkv_mlp_proj\",\n", - "],\n", - " lora_dropout=0.05,\n", - " bias=\"none\",\n", - ")\n", - "#----#\n", - "from peft import get_peft_model\n", - "pipe.transformer = get_peft_model(pipe.transformer, config)\n", - "pipe.transformer.print_trainable_parameters()" - ], - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/", - "height": 266, - "referenced_widgets": [ - "16577fe1698040568ea4dba67a1fa7d0", - "b85487705df34c2e867fcbb7024b4b5b", - "c0ab2e603303426f879e8f0abf4b7393", - "fd28a908626645819424f1afe289375d", - "475e1827136247a6b58d353ea694bd19", - "4d7f3b4f3fee49ac9092af94f2b2be1f", - "1a9301da3ba34b7fbd3c18f826b058fd", - "15999cdf364c48aaba6bc3d99dad3a63", - "e953a19bb91b4ed5aa4e4244e3c91405", - "9261c32d28854c9eb50a2e5581de4caf", - "f1083e6575824427be954bc52a119423", - "f3d3ac18dc9c4ddeb1ed19f0fdfffe46", - "2cb257c3b846484c90e27e38571252d0", - "7c93c8bd0be445c68254272da976888f", - "7d2113f0421f4880a12d5bb0308fef49", - "b91eefa3c4e84d73bcaab6f0d8bd93bc", - "37ffe4997f1647eb81e7a7d78107fdf1", - "1f19f094677e4e97ba4998adad062b69", - "a6457a01fe5d47ff804d0d78a3fadd1e", - "8f8355073ff140f38077ab6a29c0ae0d", - "3dd5809a6c0c4c43b1d5166496a9fab6", - "23d0690c63194efa8d77311d9812eb96", - "fa76c5a087f34b53956918bea856e80b", - "b7101eaa248c48a6ac29a28d44efdfdd", - "fde9d91e04b24c04b27e6a9da56aeb61", - "66a0f92f4bff492c937a9d32f60d0cb5", - "295e8356948745e28fe33dd29b690aab", - "9173730e1275476e8de4c9a7da30ae24", - "8c5932aebc2e4968bee9bf47eec8057a", - "66e49d85a7014dacbc318467ebf45be8", - "8d929c0f97504fb6af4c503d333e4512", - "ac114e56e12b410f83cbaca782ea72f1", - "69c8a7fdc49145eca068262d61f37775", - "ea7307ba2daa475ca4e55214b16239a4", - "26da14b2a7d642ef9bbbe8ef15eaf8f5", - "75ad91ac5d3143379a808487fdffd264", - "a8ca808287bd4a6585186f9e9d49f4a3", - "78a66c7278dc4f27b949715a3dff75b9", - "3233901ab9aa474b9696a43707daa7cb", - "0e36ef86c40c4c3fb7893c9ee788772f", - "2fd194e0e12045aab0d91c73a399b2ce", - "adc12d3c20e74011a45127609dcb1639", - "38c495f402a942f88f1121c2b5c21c3c", - "26de88ff85364c44b8df7b9061f7e1e4", - "e06ac5a2b11f4e409eeb7e7cc19ae434", - "ac55b36af4644c84a81a19d70cc1335e", - "5772d2b30bf1470fbe7f9f5e9f25372b", - "1c243e051a8b4278b28c9c59677a0112", - "f3467296dd0140c2a84df2a037135795", - "ee48d14ef87b4c11adf44bf3daf76197", - "1e3e3d2a22e847698af5ee2f8f6ce22d", - "fb6380258dca43f691d06b4d30459ca4", - "379712892eba4a9d845520060b163a7b", - "0f515e64cbff4c19be9ca26e3ceb48df", - "c143b49f768c496181029f9be37dc492" - ] - }, - "id": "hI4jZjB3ywqi", - "outputId": "ec31f0f2-af05-4e3e-ca2c-29657e937928" - }, - "execution_count": 2, - "outputs": [ - { - "output_type": "stream", - "name": "stdout", - "text": [ - " [GPU 0 - Pipe 1] Loading...\n" - ] - }, - { - "output_type": "display_data", - "data": { - "text/plain": [ - "model_index.json: 0%| | 0.00/446 [00:00\u001b[0;34m()\u001b[0m\n\u001b[1;32m 111\u001b[0m \u001b[0mhidden_states\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mnoisy_latents\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 112\u001b[0m \u001b[0mtimestep\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mt\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;31m# [0,1] range\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 113\u001b[0;31m \u001b[0mencoder_hidden_states\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mtext_embeds\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mto\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mdevice\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mdevice\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[1;32m 114\u001b[0m \u001b[0mpooled_projections\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mpooled_embeds\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;31m# Pooled_embeds are not defined!\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 115\u001b[0m \u001b[0mtxt_ids\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mtxt_ids\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", - "\u001b[0;31mAttributeError\u001b[0m: 'CausalLMOutputWithPast' object has no attribute 'to'" + " [GPU 0] Ready | VRAM: 12.40 GB\n", + "loaded pipe to cuda:0\n", + "trainable params: 16,711,680 || all params: 2,167,021,056 || trainable%: 0.7712\n", + "Converting transformer for training (bfloat16)...\n", + "Trainable parameters: 3,697,213,440\n", + "trainable params: 3,697,213,440 || all params: 3,892,256,256 || trainable%: 94.9890\n" ] } - ] - }, - { - "cell_type": "markdown", - "source": [ - "# OLD CODE" ], - "metadata": { - "id": "u6BXluFgu5Rl" - } - }, - { - "cell_type": "code", "source": [ - "# Prepare the pipe.transformer for inference\n", - "from sdnq.loader import apply_sdnq_options_to_model\n", - "from sdnq import SDNQConfig\n", - "from sdnq.common import use_torch_compile as triton_is_available\n", - "from sdnq.training import convert_sdnq_model_to_training\n", - "pipe.transformer = apply_sdnq_options_to_model(pipe.transformer, use_quantized_matmul=False , dequantize_fp32=False)\n", - "\n", - "#Move SDNQ Components to VRAM\n", - "pipe.transformer = pipe.transformer.to('cuda')\n", - "pipe.text_encoder = pipe.text_encoder.to('cuda')\n", - "pipe.vae = pipe.vae.to('cuda')\n", - "\n", - "#Serialize the transformer weights in memory\n", - "pipe.transformer = convert_sdnq_model_to_training(pipe.transformer)\n", - "\n", - "import torch , gc\n", - "from PIL import Image\n", - "import torchvision.transforms as T\n", - "from diffusers.utils import load_image\n", - "\n", - "# ------------------- Load test images -------------------\n", - "def load_and_resize(img_path, max_side=1024):\n", - " img = Image.open(img_path).convert(\"RGB\")\n", - " # Resize maintaining aspect ratio\n", - " w, h = img.size\n", - " if max(w, h) > max_side:\n", - " scale = max_side / max(w, h)\n", - " new_w, new_h = int(w * scale), int(h * scale)\n", - " img = img.resize((new_w, new_h), Image.LANCZOS)\n", - " return img\n", + "#@markdown # CELL 1 — Setup environment + login\n", + "from google.colab import drive, userdata\n", + "from huggingface_hub import login\n", + "import torch, os, gc\n", + "drive.mount(\"/content/drive\")\n", + "hf_token = userdata.get(\"HF_TOKEN\")\n", + "if hf_token:\n", + " login(token=hf_token)\n", + "else:\n", + " raise ValueError(\"HF_TOKEN not found in Google Colab secrets\")\n", + "#----#\n", "\n", - "image1 = load_and_resize(image1_path)\n", - "image2 = load_and_resize(image2_path)\n", + "!pip uninstall -q torchao -y\n", + "!pip install -q torchao==0.17.0 --no-deps\n", "\n", - "print(f\"Image1 size: {image1.size}, Image2 size: {image2.size}\")\n", + "import torchao\n", + "print(f\"✅ torchao updated to version: {torchao.__version__}\")\n", + "!pip install -q safetensors huggingface_hub diffusers transformers accelerate sdnq\n", "\n", - "# Clear unbound variables from GPU RAM\n", - "gc.collect()\n", - "torch.cuda.empty_cache()\n" - ], - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "id": "sQamMz50JoXM", - "outputId": "fe17c7f2-6a9c-4c34-ab26-215c3cfe8f04" - }, - "execution_count": null, - "outputs": [ - { - "output_type": "stream", - "name": "stdout", - "text": [ - "Image1 size: (643, 360), Image2 size: (720, 360)\n" - ] - } - ] - }, - { - "cell_type": "code", - "source": [ - "#@markdown # CELL 3 — Training Setup\n", + "#images for testing\n", + "# Resize so longest side is no greater than 1024px prior to\n", + "#prior to encoding into VAE\n", + "image1_path = '/content/drive/MyDrive/image1.jpg'\n", + "image2_path = '/content/drive/MyDrive/image2.jpg'\n", "\n", - "!pip install -q peft datasets bitsandbytes\n", + "# Test prompts\n", + "prompt1= 'a cute dog on grass'\n", + "prompt2 = 'a golden retriever on a road'\n", + "#-----#\n", + "print(\"✅ Dependencies installed\")\n", + "print(\"✅ Logged into Hugging Face\")\n", "\n", - "import torch\n", - "from torch.utils.data import Dataset, DataLoader\n", - "from PIL import Image\n", - "import torchvision.transforms as T\n", - "from peft import LoraConfig, get_peft_model\n", "import gc\n", + "import torch\n", + "from diffusers import Flux2KleinPipeline\n", + "#----#\n", + "from sdnq import SDNQConfig\n", + "from sdnq.common import use_torch_compile as triton_is_available\n", + "from sdnq.training import convert_sdnq_model_to_training\n", + "from sdnq.training import sdnq_training_post_load_quant\n", + "from sdnq.loader import apply_sdnq_options_to_model\n", + "#----#\n", + "from functools import partial\n", + "from safetensors.torch import load_file, save_file\n", + "from huggingface_hub import hf_hub_download\n", + "import hashlib\n", + "from pathlib import Path\n", + "import torch\n", + "from transformers import BitsAndBytesConfig\n", "\n", - "# --- Dataset Class for Flux-style training ---\n", - "class FluxImageDataset(Dataset):\n", - " def __init__(self, images, prompts, size=1024):\n", - " self.images = images\n", - " self.prompts = prompts\n", - " self.transform = T.Compose([\n", - " T.Resize(size, interpolation=T.InterpolationMode.LANCZOS),\n", - " T.CenterCrop(size),\n", - " T.ToTensor(),\n", - " T.Normalize([0.5], [0.5]), # Flux expects -1 to 1 range usually\n", - " ])\n", + "# Patch function\n", + "def patch_encode_prompt(pipe, cached_prompt_embeds, cached_text_ids, gpu_id):\n", + " def patched(self, prompt=None, device=None, num_images_per_prompt=1, prompt_embeds=None, **kwargs):\n", + " device = device or torch.device(f\"cuda:{gpu_id}\")\n", + " if prompt_embeds is None:\n", + " emb = cached_prompt_embeds.to(device)\n", + " emb = emb.repeat(1, num_images_per_prompt, 1)\n", + " emb = emb.view(emb.shape[0] * num_images_per_prompt, emb.shape[1], -1)\n", + " tids = cached_text_ids.to(device)\n", + " return emb, tids\n", + " return self._original_encode_prompt(prompt, device, num_images_per_prompt, prompt_embeds, **kwargs)\n", "\n", - " def __len__(self):\n", - " return len(self.images)\n", + " if not hasattr(pipe, '_original_encode_prompt'):\n", + " pipe._original_encode_prompt = pipe.encode_prompt\n", + " pipe.encode_prompt = partial(patched, pipe)\n", + " return pipe\n", "\n", - " def __getitem__(self, idx):\n", - " img = self.transform(self.images[idx])\n", - " return {\"pixel_values\": img, \"text\": self.prompts[idx]}\n", + "# Load single pipe\n", + "def load_klein_pipe(gpu_id: int):\n", + " torch.cuda.set_device(gpu_id)\n", + " print(f\" [GPU {gpu_id}] Loading...\")\n", + " MODEL_ID = 'codeShare/Flux.2-Klein-4B-SDNQ-4bit'\n", + " #----#\n", + " edit_prompt = 'this is a test'\n", + " pipe = Flux2KleinPipeline.from_pretrained(\n", + " MODEL_ID,\n", + " torch_dtype=torch.float16,\n", + " low_cpu_mem_usage=True,\n", + " device_map=\"cpu\",\n", + " )\n", "\n", - "# Prepare data\n", - "train_images = [image1, image2]\n", - "train_prompts = [prompt1, prompt2]\n", + " # SDNQ + Prompt cache\n", + " pipe.transformer = apply_sdnq_options_to_model(pipe.transformer, use_quantized_matmul=False)\n", "\n", - "dataset = FluxImageDataset(train_images, train_prompts, size=1024)\n", - "dataloader = DataLoader(dataset, batch_size=1, shuffle=True) # Small batch for memory\n", + " # Prepare text_encoder\n", + " pipe.text_encoder = pipe.text_encoder.to(f\"cuda:{gpu_id}\")\n", "\n", - "print(f\"✅ Dataset ready with {len(dataset)} samples\")" - ], - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "id": "FNma-jSihzrP", - "outputId": "79dbfbee-948f-4f98-f385-913934996ee0" - }, - "execution_count": null, - "outputs": [ - { - "output_type": "stream", - "name": "stdout", - "text": [ - "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m60.7/60.7 MB\u001b[0m \u001b[31m6.2 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", - "\u001b[?25h✅ Dataset ready with 2 samples\n" - ] - } - ] - }, - { - "cell_type": "code", - "source": [ - "from sdnq.optim import Adafactor, AdamW, CAME, Lion, Muon\n", - "optimizer = AdamW(\n", - " parameters,\n", - " use_quantized_buffers=True,\n", - " quantized_buffers_dtype=\"uint8\",\n", - " quantized_buffers_hadamard_group_size=128,\n", - " quantized_buffers_group_size=32,\n", - " quantized_buffers_svd_rank=32,\n", - " final_norm_mode=\"clip\", # can be one of [\"none\", \"clip\", \"rms\", \"rms_clip\", \"relative\", \"muon\"]\n", - " use_kahan=False,\n", - " use_cautious=False,\n", - " use_stochastic_rounding=True,\n", - " use_stochastic_buffers=True,\n", - " quantized_buffers_use_svd=False,\n", - " quantized_buffers_use_hadamard=False,\n", - " use_torch_compile=False,\n", - " offload_buffers=False,\n", - " offload_non_blocking=True,\n", - ")" - ], - "metadata": { - "id": "F2i0abMYiMjp" - }, - "execution_count": null, - "outputs": [] - }, - { - "cell_type": "code", - "source": [], - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "id": "_fmTXBufjVLz", - "outputId": "69164b6a-9ea7-4c56-d2c8-078812c4e6ad" - }, - "execution_count": null, - "outputs": [ - { - "output_type": "stream", - "name": "stdout", - "text": [ - "\u001b[?25l \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m0.0/3.2 MB\u001b[0m \u001b[31m?\u001b[0m eta \u001b[36m-:--:--\u001b[0m\r\u001b[2K \u001b[91m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[91m╸\u001b[0m\u001b[90m━━\u001b[0m \u001b[32m3.0/3.2 MB\u001b[0m \u001b[31m90.7 MB/s\u001b[0m eta \u001b[36m0:00:01\u001b[0m\r\u001b[2K \u001b[91m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[91m╸\u001b[0m \u001b[32m3.2/3.2 MB\u001b[0m \u001b[31m87.6 MB/s\u001b[0m eta \u001b[36m0:00:01\u001b[0m\r\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m3.2/3.2 MB\u001b[0m \u001b[31m43.4 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", - "\u001b[?25h✅ torchao updated to version: 0.10.0\n" - ] - } - ] - }, - { - "cell_type": "code", - "source": [ - "#@markdown # CELL 4 — Add LoRA (SDNQ-Compatible Version)\n", + " gc.collect()\n", + " torch.cuda.empty_cache()\n", + " pipe.transformer.to(f\"cuda:{gpu_id}\")\n", + "\n", + " # VAE stuffs\n", + " pipe.vae = pipe.vae.to(f\"cuda:{gpu_id}\")\n", "\n", - "from peft import LoraConfig, get_peft_model\n", + " print(f\" [GPU {gpu_id}] Ready | VRAM: {torch.cuda.memory_allocated(gpu_id)/1e9:.2f} GB\")\n", + " return pipe\n", + "#----#\n", "\n", - "# Conservative & safe target modules for Flux.2 Klein + SDNQLinear\n", - "target_modules = [\n", + "gc.collect()\n", + "torch.cuda.empty_cache()\n", + "NUM_GPUS = torch.cuda.device_count()\n", + "#----#\n", + "PIPE_ID = 1\n", + "GPU_ID = 0\n", + "pipe = load_klein_pipe(GPU_ID)\n", + "print(f'loaded pipe to {pipe.device}')\n", + "#----#\n", + "pipe.transformer = pipe.transformer.to('cuda')\n", + "gc.collect()\n", + "torch.cuda.empty_cache()\n", + "from peft import prepare_model_for_kbit_training\n", + "pipe.transformer = prepare_model_for_kbit_training(pipe.transformer)\n", + "#----#\n", + "from peft import LoraConfig\n", + "config = LoraConfig(\n", + " r=16,\n", + " lora_alpha=8,\n", + " target_modules=[\n", " \"attn.to_q\", \"attn.to_k\", \"attn.to_v\",\n", " \"ff.linear_in\", \"ff.linear_out\",\n", " \"ff_context.linear_in\", \"ff_context.linear_out\",\n", " \"attn.to_qkv_mlp_proj\",\n", - "]\n", - "\n", - "lora_config = LoraConfig(\n", - " r=16,\n", - " lora_alpha=32,\n", - " target_modules=target_modules,\n", + "],\n", " lora_dropout=0.05,\n", " bias=\"none\",\n", ")\n", + "#----#\n", + "from peft import get_peft_model\n", + "pipe.transformer = get_peft_model(pipe.transformer, config)\n", + "pipe.transformer.print_trainable_parameters()\n", + "#----#\n", "\n", - "# Apply LoRA\n", - "pipe.transformer = get_peft_model(pipe.transformer, lora_config)\n", + "# ====================== SETUP ======================\n", + "device = torch.device(f\"cuda:{GPU_ID}\")\n", + "dtype = torch.bfloat16 # ← Changed to float16\n", "\n", - "# Show what was actually adapted\n", - "pipe.transformer.print_trainable_parameters()\n", + "print(\"Converting transformer for training (bfloat16)...\")\n", "\n", - "print(\"✅ LoRA applied with SDNQ-safe targets!\")" - ], - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "id": "jTb-ifqeh5OC", - "outputId": "61e228c3-85e7-40f8-c2f6-52bc68080f5d" - }, - "execution_count": null, - "outputs": [ - { - "output_type": "stream", - "name": "stdout", - "text": [ - "trainable params: 17,819,648 || all params: 3,893,364,224 || trainable%: 0.4577\n", - "✅ LoRA applied with SDNQ-safe targets!\n" - ] - } + "pipe.transformer = convert_sdnq_model_to_training(\n", + " pipe.transformer,\n", + " quantized_matmul_dtype=\"int8\",\n", + " use_grad_ckpt=True,\n", + " use_quantized_matmul=False,\n", + " use_stochastic_rounding=True,\n", + " dequantize_fp32=True,\n", + ")\n", + "\n", + "# Move to device + bfloat16\n", + "pipe.transformer = pipe.transformer.to(device)\n", + "\n", + "# Fix any remaining float32 layers (especially timestep embeddings)\n", + "for module in pipe.transformer.modules():\n", + " if isinstance(module, torch.nn.Linear):\n", + " if module.weight.dtype != dtype:\n", + " module.to(dtype)\n", + " if hasattr(module, 'timestep_embedder'):\n", + " module.timestep_embedder.to(dtype)\n", + " if hasattr(module, 'guidance_embedder') and module.guidance_embedder is not None:\n", + " module.guidance_embedder.to(dtype)\n", + "\n", + "# Set correct dtype\n", + "pipe.vae.to(dtype=dtype)\n", + "pipe.transformer.to(dtype= torch.float32)\n", + "\n", + "# LoRA parameters\n", + "trainable_params = [p for p in pipe.transformer.parameters() if p.requires_grad]\n", + "print(f\"Trainable parameters: {sum(p.numel() for p in trainable_params):,}\")\n", + "pipe.transformer.print_trainable_parameters()\n" ] }, { "cell_type": "code", "source": [ - "#@markdown # CELL 5 — Training Loop (Simplified but Functional)\n", - "\n", + "# ====================== FULL TRAINING SCRIPT - BFLOAT16 ======================\n", "import torch\n", - "import gc\n", + "import torch.nn.functional as F\n", "from torch.utils.data import Dataset, DataLoader\n", + "from PIL import Image\n", "import torchvision.transforms as T\n", - "from diffusers import FlowMatchEulerDiscreteScheduler\n", "from tqdm.auto import tqdm\n", + "import gc\n", + "import os\n", + "\n", + "dtype = torch.bfloat16 # ← Changed to bfloat16\n", + "\n", + "# ====================== DATA ======================\n", + "image_paths = ['/content/drive/MyDrive/image1.jpg', '/content/drive/MyDrive/image2.jpg']\n", + "instance_prompts = ['a cute dog on grass', 'a golden retriever on a road']\n", "\n", - "# ------------------- Dataset -------------------\n", - "class SimpleFluxDataset(Dataset):\n", - " def __init__(self, images, prompts, size=1024):\n", - " self.images = images\n", + "# ====================== DATASET ======================\n", + "class Flux2KleinDataset(Dataset):\n", + " def __init__(self, image_paths, prompts, resolution=512):\n", + " self.images = [Image.open(p).convert(\"RGB\") for p in image_paths]\n", " self.prompts = prompts\n", " self.transform = T.Compose([\n", - " T.Resize(size, interpolation=T.InterpolationMode.LANCZOS),\n", - " T.CenterCrop(size),\n", - " T.ToTensor(),\n", - " T.Normalize([0.5], [0.5]),\n", + " T.Resize(resolution, interpolation=T.InterpolationMode.BILINEAR),\n", + " T.CenterCrop(resolution),\n", + " T.ToTensor(), # [0, 1]\n", + " T.Normalize([0.5]*3, [0.5]*3), # → [-1, 1]\n", " ])\n", "\n", " def __len__(self):\n", " return len(self.images)\n", "\n", " def __getitem__(self, idx):\n", - " img = self.transform(self.images[idx])\n", - " return {\"pixel_values\": img, \"prompt\": self.prompts[idx]}\n", + " return self.transform(self.images[idx]), self.prompts[idx]\n", "\n", - "dataset = SimpleFluxDataset([image1, image2], [prompt1, prompt2])\n", + "dataset = Flux2KleinDataset(image_paths, instance_prompts, resolution=512)\n", "dataloader = DataLoader(dataset, batch_size=1, shuffle=True, num_workers=0)\n", "\n", - "# ------------------- Training Setup -------------------\n", - "pipe.transformer.train()\n", - "pipe.text_encoder.train() if hasattr(pipe.text_encoder, 'train') else None\n", - "\n", - "optimizer = torch.optim.AdamW(pipe.transformer.parameters(), lr=1e-4, weight_decay=1e-4)\n", - "scaler = torch.cuda.amp.GradScaler()\n", + "from sdnq.optim import Adafactor, AdamW, CAME, Lion, Muon\n", + "optimizer = AdamW(\n", + " trainable_params,\n", + " lr=1e-4,\n", + " use_quantized_buffers=True,\n", + " quantized_buffers_dtype=\"uint8\",\n", + " use_stochastic_rounding=True,\n", + " use_stochastic_buffers=True,\n", + ")\n", "\n", - "num_epochs = 30 # With only 2 images, more epochs help (but watch for overfitting)\n", - "device = torch.device(\"cuda\")\n", - "scheduler = FlowMatchEulerDiscreteScheduler.from_pretrained(MODEL_ID, subfolder=\"scheduler\")\n", + "# ====================== TRAINING LOOP ======================\n", + "num_epochs = 5\n", + "accumulation_steps = 2\n", "\n", - "print(\"🚀 Starting training... (Very small dataset - expect quick overfitting)\")\n", + "print(\"Starting training with bfloat16...\\n\")\n", "\n", "for epoch in range(num_epochs):\n", " epoch_loss = 0.0\n", - " progress = tqdm(dataloader, desc=f\"Epoch {epoch+1}/{num_epochs}\")\n", + " optimizer.zero_grad()\n", + "\n", + " for step, (pixel_values, prompts) in enumerate(tqdm(dataloader, desc=f\"Epoch {epoch+1}/{num_epochs}\")):\n", + " pixel_values = pixel_values.to(device, dtype=dtype)\n", + "\n", + " # ====================== VAE ENCODE ======================\n", + " with torch.no_grad():\n", + " posterior = pipe.vae.encode(pixel_values).latent_dist\n", + " latents = posterior.sample() # No scaling for Flux2 VAE\n", + "\n", + " # ====================== TEXT ENCODING ======================\n", + " with torch.no_grad():\n", + " if hasattr(pipe, '_original_encode_prompt'):\n", + " prompt_embeds, text_ids = pipe._original_encode_prompt(\n", + " prompts, device=device, num_images_per_prompt=1\n", + " )\n", + " else:\n", + " prompt_embeds, text_ids = pipe.encode_prompt(\n", + " prompts, device=device, num_images_per_prompt=1\n", + " )\n", + "\n", + " B, _, H, W = latents.shape\n", "\n", - " for batch in progress:\n", - " optimizer.zero_grad()\n", + " # ====================== POSITION IDS ======================\n", + " def get_img_ids(b, h, w, device):\n", + " h = h // 2\n", + " w = w // 2\n", + " img_ids = torch.zeros((b, h * w, 3), device=device, dtype=torch.long)\n", + " y = torch.arange(h, device=device).unsqueeze(1).expand(h, w).reshape(-1)\n", + " x = torch.arange(w, device=device).unsqueeze(0).expand(h, w).reshape(-1)\n", + " img_ids[0, :, 1] = y\n", + " img_ids[0, :, 2] = x\n", + " return img_ids\n", "\n", - " pixel_values = batch[\"pixel_values\"].to(device)\n", - " prompts = batch[\"prompt\"]\n", + " img_ids = get_img_ids(B, H, W, device)\n", + " txt_ids = torch.zeros((B, prompt_embeds.shape[1], 3), device=device, dtype=torch.long)\n", "\n", - " with torch.cuda.amp.autocast(dtype=torch.float16):\n", - " # Encode images to latents\n", - " latents = pipe.vae.encode(pixel_values).latent_dist.sample()\n", - " latents = latents * pipe.vae.config.scaling_factor\n", + " # ====================== NOISE & TARGET (Rectified Flow) ======================\n", + " noise = torch.randn_like(latents, dtype=dtype)\n", + " timesteps = torch.rand((B,), device=device, dtype=torch.float32) # keep float32 for timesteps\n", "\n", - " # Text conditioning\n", - " text_embeds = pipe.text_encoder(prompts, return_dict=True).last_hidden_state\n", + " noisy_latents = (1 - timesteps.view(-1, 1, 1, 1)) * latents + \\\n", + " timesteps.view(-1, 1, 1, 1) * noise\n", "\n", - " # Sample noise and timesteps (Flow Matching style)\n", - " noise = torch.randn_like(latents)\n", - " timesteps = torch.randint(0, scheduler.config.num_train_timesteps, (latents.shape[0],), device=device)\n", + " target = noise - latents\n", "\n", - " # Add noise\n", - " noisy_latents = scheduler.add_noise(latents, noise, timesteps)\n", + " # ====================== MODEL FORWARD ======================\n", + " prompt_embeds = prompt_embeds.to(dtype)\n", "\n", - " # Forward pass through transformer (this is the core part)\n", - " model_pred = pipe.transformer(\n", - " hidden_states=noisy_latents,\n", - " encoder_hidden_states=text_embeds,\n", - " timestep=timesteps,\n", - " return_dict=False,\n", - " )[0]\n", + " noise_pred = pipe.transformer(\n", + " hidden_states=noisy_latents,\n", + " encoder_hidden_states=prompt_embeds,\n", + " timestep=timesteps * 1000.0,\n", + " img_ids=img_ids,\n", + " txt_ids=txt_ids,\n", + " return_dict=False,\n", + " )[0]\n", "\n", - " # Simple MSE loss (Flow Matching uses velocity prediction)\n", - " loss = torch.nn.functional.mse_loss(model_pred, noise)\n", + " # ====================== LOSS ======================\n", + " loss = F.mse_loss(noise_pred.float(), target.float())\n", + " loss = loss / accumulation_steps\n", + " loss.backward()\n", "\n", - " scaler.scale(loss).backward()\n", - " scaler.unscale_(optimizer)\n", - " torch.nn.utils.clip_grad_norm_(pipe.transformer.parameters(), 1.0)\n", - " scaler.step(optimizer)\n", - " scaler.update()\n", + " epoch_loss += loss.item() * accumulation_steps\n", "\n", - " epoch_loss += loss.item()\n", - " progress.set_postfix({\"loss\": f\"{loss.item():.4f}\"})\n", + " # Gradient accumulation\n", + " if (step + 1) % accumulation_steps == 0 or (step + 1) == len(dataloader):\n", + " optimizer.step()\n", + " optimizer.zero_grad()\n", + " if step % 8 == 0:\n", + " torch.cuda.empty_cache()\n", + " gc.collect()\n", "\n", " avg_loss = epoch_loss / len(dataloader)\n", - " print(f\"✅ Epoch {epoch+1} completed | Avg Loss: {avg_loss:.4f}\")\n", - "\n", - " # Save checkpoint every 10 epochs\n", - " if (epoch + 1) % 10 == 0:\n", - " save_path = f\"/content/drive/MyDrive/flux_klein_lora/checkpoint-epoch-{epoch+1}\"\n", - " pipe.transformer.save_pretrained(save_path)\n", - " print(f\"💾 Saved checkpoint to {save_path}\")\n", + " print(f\"Epoch {epoch+1}/{num_epochs} - Average Loss: {avg_loss:.6f}\\n\")\n", "\n", - "# Final save\n", - "final_path = \"/content/drive/MyDrive/flux_klein_lora/final_lora\"\n", - "pipe.transformer.save_pretrained(final_path)\n", - "print(f\"🎉 Training finished! LoRA saved to: {final_path}\")" + "# ====================== SAVE LoRA ======================\n", + "save_path = \"/content/flux2_klein_4b_lora\"\n", + "os.makedirs(save_path, exist_ok=True)\n", + "pipe.transformer.save_pretrained(save_path)\n", + "print(f\"✅ Training finished! LoRA saved to: {save_path}\")" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/", - "height": 599, + "height": 423, "referenced_widgets": [ - "11539f142025414e804b5fdc9c48eda6", - "72b20197d10041d48b038eaa8256183f", - "557e328cf1a74de08084e897affd0e44", - "c51efbe30cfa44f4a1635722aea0aa8a", - "b28f6f41640a4c69a114324839b40b8e", - "e628945c395249e38ab5988236460f4a", - "e48a04cbfbab4ce3a9f8190e2a56aecc", - "1004326b9b104a46a32aacd6c946df83", - "5ebbca15a97745dc95afcf5388c69def", - "f7f7a5dd82984c3cac21a394c5f6e9f0", - "86e499cc81b24d1c8d2aa6675d33095a" + "02f03c321d5240308c81f7b3ce60c2d9", + "2cc3f34f1a5348a78a213ec2216401f6", + "28e0c0b76f7745e9af9870218bfc7e62", + "211084c892f343c3b03a0022a09fb646", + "15f4df00e7de480680c28c73e2907738", + "6a9ba0019d534f8e89b5cfe758690e1d", + "88e71a49e09241fe94c3ea1f9084385e", + "fd5c96ad82e44cb09aa503d79419a142", + "2a2803f84f0d4443a657d5cc9e31d1a4", + "e79dd2d0a07448518e318fbcb7d01139", + "d3602489cf0844ce9f1f8217fcaa34e3" ] }, - "id": "7p26uoeIjAe4", - "outputId": "cd9488cc-ab59-4ed6-e58c-92fba3aee489" + "id": "x-HYFzLdwj0x", + "outputId": "d5695231-d4d8-436c-ee4e-2b68594c5053" }, - "execution_count": null, + "execution_count": 23, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ - "🚀 Starting training... (Very small dataset - expect quick overfitting)\n" - ] - }, - { - "output_type": "stream", - "name": "stderr", - "text": [ - "/tmp/ipykernel_2860/3091037792.py:37: FutureWarning: `torch.cuda.amp.GradScaler(args...)` is deprecated. Please use `torch.amp.GradScaler('cuda', args...)` instead.\n", - " scaler = torch.cuda.amp.GradScaler()\n", - "/usr/local/lib/python3.12/dist-packages/huggingface_hub/utils/_validators.py:205: UserWarning: The `local_dir_use_symlinks` argument is deprecated and ignored in `hf_hub_download`. Downloading to a local directory does not use symlinks anymore.\n", - " warnings.warn(\n" + "Starting training with bfloat16...\n", + "\n" ] }, { "output_type": "display_data", "data": { "text/plain": [ - "Epoch 1/30: 0%| | 0/2 [00:00\u001b[0;34m()\u001b[0m\n\u001b[1;32m 55\u001b[0m \u001b[0;32mwith\u001b[0m \u001b[0mtorch\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcuda\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mamp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mautocast\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mdtype\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mtorch\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mfloat16\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 56\u001b[0m \u001b[0;31m# Encode images to latents\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 57\u001b[0;31m \u001b[0mlatents\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mpipe\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mvae\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mencode\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mpixel_values\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mlatent_dist\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msample\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[1;32m 58\u001b[0m \u001b[0mlatents\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mlatents\u001b[0m \u001b[0;34m*\u001b[0m \u001b[0mpipe\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mvae\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mconfig\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mscaling_factor\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 59\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n", - "\u001b[0;32m/usr/local/lib/python3.12/dist-packages/diffusers/utils/accelerate_utils.py\u001b[0m in \u001b[0;36mwrapper\u001b[0;34m(self, *args, **kwargs)\u001b[0m\n\u001b[1;32m 44\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mhasattr\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m\"_hf_hook\"\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32mand\u001b[0m \u001b[0mhasattr\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_hf_hook\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m\"pre_forward\"\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 45\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_hf_hook\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mpre_forward\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---> 46\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mmethod\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\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 47\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 48\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mwrapper\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/diffusers/models/autoencoders/autoencoder_kl_flux2.py\u001b[0m in \u001b[0;36mencode\u001b[0;34m(self, x, return_dict)\u001b[0m\n\u001b[1;32m 204\u001b[0m \u001b[0mh\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mtorch\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcat\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mencoded_slices\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 205\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 206\u001b[0;31m \u001b[0mh\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_encode\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mx\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 207\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 208\u001b[0m \u001b[0mposterior\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mDiagonalGaussianDistribution\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mh\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/diffusers/models/autoencoders/autoencoder_kl_flux2.py\u001b[0m in \u001b[0;36m_encode\u001b[0;34m(self, x)\u001b[0m\n\u001b[1;32m 178\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_tiled_encode\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mx\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 179\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 180\u001b[0;31m \u001b[0menc\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mencoder\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mx\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 181\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mquant_conv\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 182\u001b[0m \u001b[0menc\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mquant_conv\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0menc\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/torch/nn/modules/module.py\u001b[0m in \u001b[0;36m_wrapped_call_impl\u001b[0;34m(self, *args, **kwargs)\u001b[0m\n\u001b[1;32m 1774\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_compiled_call_impl\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;31m# type: ignore[misc]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1775\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 1776\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_call_impl\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 1777\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1778\u001b[0m \u001b[0;31m# torchrec tests the code consistency with the following code\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/torch/nn/modules/module.py\u001b[0m in \u001b[0;36m_call_impl\u001b[0;34m(self, *args, **kwargs)\u001b[0m\n\u001b[1;32m 1785\u001b[0m \u001b[0;32mor\u001b[0m \u001b[0m_global_backward_pre_hooks\u001b[0m \u001b[0;32mor\u001b[0m \u001b[0m_global_backward_hooks\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1786\u001b[0m or _global_forward_hooks or _global_forward_pre_hooks):\n\u001b[0;32m-> 1787\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mforward_call\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 1788\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1789\u001b[0m \u001b[0mresult\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;32mNone\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/diffusers/models/autoencoders/vae.py\u001b[0m in \u001b[0;36mforward\u001b[0;34m(self, sample)\u001b[0m\n\u001b[1;32m 165\u001b[0m \u001b[0;31m# down\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 166\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mdown_block\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdown_blocks\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 167\u001b[0;31m \u001b[0msample\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mdown_block\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0msample\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 168\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 169\u001b[0m \u001b[0;31m# middle\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;31mRuntimeError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m/tmp/ipykernel_1244/1950237892.py\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 103\u001b[0m \u001b[0mprompt_embeds\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mprompt_embeds\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mto\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mdtype\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 104\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 105\u001b[0;31m noise_pred = pipe.transformer(\n\u001b[0m\u001b[1;32m 106\u001b[0m \u001b[0mhidden_states\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mnoisy_latents\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 107\u001b[0m \u001b[0mencoder_hidden_states\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mprompt_embeds\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/torch/nn/modules/module.py\u001b[0m in \u001b[0;36m_wrapped_call_impl\u001b[0;34m(self, *args, **kwargs)\u001b[0m\n\u001b[1;32m 1774\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_compiled_call_impl\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;31m# type: ignore[misc]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1775\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 1776\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_call_impl\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 1777\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1778\u001b[0m \u001b[0;31m# torchrec tests the code consistency with the following code\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/torch/nn/modules/module.py\u001b[0m in \u001b[0;36m_call_impl\u001b[0;34m(self, *args, **kwargs)\u001b[0m\n\u001b[1;32m 1785\u001b[0m \u001b[0;32mor\u001b[0m \u001b[0m_global_backward_pre_hooks\u001b[0m \u001b[0;32mor\u001b[0m \u001b[0m_global_backward_hooks\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1786\u001b[0m or _global_forward_hooks or _global_forward_pre_hooks):\n\u001b[0;32m-> 1787\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mforward_call\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 1788\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1789\u001b[0m \u001b[0mresult\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;32mNone\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/diffusers/models/unets/unet_2d_blocks.py\u001b[0m in \u001b[0;36mforward\u001b[0;34m(self, hidden_states, *args, **kwargs)\u001b[0m\n\u001b[1;32m 1440\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1441\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mresnet\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mresnets\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 1442\u001b[0;31m \u001b[0mhidden_states\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mresnet\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mhidden_states\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mtemb\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 1443\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1444\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdownsamplers\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[0;32m/usr/local/lib/python3.12/dist-packages/peft/peft_model.py\u001b[0m in \u001b[0;36mforward\u001b[0;34m(self, *args, **kwargs)\u001b[0m\n\u001b[1;32m 943\u001b[0m \u001b[0;32mwith\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_enable_peft_forward_hooks\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[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 944\u001b[0m \u001b[0mkwargs\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m{\u001b[0m\u001b[0mk\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0mv\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mk\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mv\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mkwargs\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mitems\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mk\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mspecial_peft_forward_args\u001b[0m\u001b[0;34m}\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 945\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mget_base_model\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\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 946\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 947\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mgenerate\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\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[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;32m/usr/local/lib/python3.12/dist-packages/torch/nn/modules/module.py\u001b[0m in \u001b[0;36m_wrapped_call_impl\u001b[0;34m(self, *args, **kwargs)\u001b[0m\n\u001b[1;32m 1774\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_compiled_call_impl\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;31m# type: ignore[misc]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1775\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 1776\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_call_impl\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 1777\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1778\u001b[0m \u001b[0;31m# torchrec tests the code consistency with the following code\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/torch/nn/modules/module.py\u001b[0m in \u001b[0;36m_call_impl\u001b[0;34m(self, *args, **kwargs)\u001b[0m\n\u001b[1;32m 1785\u001b[0m \u001b[0;32mor\u001b[0m \u001b[0m_global_backward_pre_hooks\u001b[0m \u001b[0;32mor\u001b[0m \u001b[0m_global_backward_hooks\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1786\u001b[0m or _global_forward_hooks or _global_forward_pre_hooks):\n\u001b[0;32m-> 1787\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mforward_call\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 1788\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1789\u001b[0m \u001b[0mresult\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;32mNone\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/diffusers/models/resnet.py\u001b[0m in \u001b[0;36mforward\u001b[0;34m(self, input_tensor, temb, *args, **kwargs)\u001b[0m\n\u001b[1;32m 325\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 326\u001b[0m \u001b[0mhidden_states\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mnorm1\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mhidden_states\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 327\u001b[0;31m \u001b[0mhidden_states\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mnonlinearity\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mhidden_states\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 328\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 329\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mupsample\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[0;32m/usr/local/lib/python3.12/dist-packages/diffusers/utils/peft_utils.py\u001b[0m in \u001b[0;36mwrapper\u001b[0;34m(self, *args, **kwargs)\u001b[0m\n\u001b[1;32m 313\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 314\u001b[0m \u001b[0;31m# Execute the forward pass\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 315\u001b[0;31m \u001b[0mresult\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mforward_fn\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\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 316\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mresult\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 317\u001b[0m \u001b[0;32mfinally\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/diffusers/models/transformers/transformer_flux2.py\u001b[0m in \u001b[0;36mforward\u001b[0;34m(self, hidden_states, encoder_hidden_states, timestep, img_ids, txt_ids, guidance, joint_attention_kwargs, return_dict)\u001b[0m\n\u001b[1;32m 834\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 835\u001b[0m \u001b[0;31m# 2. Input projection for image (hidden_states) and conditioning text (encoder_hidden_states)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 836\u001b[0;31m \u001b[0mhidden_states\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mx_embedder\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mhidden_states\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 837\u001b[0m \u001b[0mencoder_hidden_states\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcontext_embedder\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mencoder_hidden_states\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 838\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;32m/usr/local/lib/python3.12/dist-packages/torch/nn/modules/module.py\u001b[0m in \u001b[0;36m_wrapped_call_impl\u001b[0;34m(self, *args, **kwargs)\u001b[0m\n\u001b[1;32m 1774\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_compiled_call_impl\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;31m# type: ignore[misc]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1775\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 1776\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_call_impl\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 1777\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1778\u001b[0m \u001b[0;31m# torchrec tests the code consistency with the following code\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/torch/nn/modules/module.py\u001b[0m in \u001b[0;36m_call_impl\u001b[0;34m(self, *args, **kwargs)\u001b[0m\n\u001b[1;32m 1785\u001b[0m \u001b[0;32mor\u001b[0m \u001b[0m_global_backward_pre_hooks\u001b[0m \u001b[0;32mor\u001b[0m \u001b[0m_global_backward_hooks\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1786\u001b[0m or _global_forward_hooks or _global_forward_pre_hooks):\n\u001b[0;32m-> 1787\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mforward_call\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 1788\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1789\u001b[0m \u001b[0mresult\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;32mNone\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/torch/nn/modules/activation.py\u001b[0m in \u001b[0;36mforward\u001b[0;34m(self, input)\u001b[0m\n\u001b[1;32m 470\u001b[0m \u001b[0mRuns\u001b[0m \u001b[0mthe\u001b[0m \u001b[0mforward\u001b[0m \u001b[0;32mpass\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 471\u001b[0m \"\"\"\n\u001b[0;32m--> 472\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mF\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msilu\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0minput\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0minplace\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0minplace\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 473\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 474\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mextra_repr\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m->\u001b[0m \u001b[0mstr\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/torch/nn/functional.py\u001b[0m in \u001b[0;36msilu\u001b[0;34m(input, inplace)\u001b[0m\n\u001b[1;32m 2393\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0minplace\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2394\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mtorch\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_C\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_nn\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msilu_\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0minput\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 2395\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mtorch\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_C\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_nn\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msilu\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0minput\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 2396\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2397\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n", - "\u001b[0;31mOutOfMemoryError\u001b[0m: CUDA out of memory. Tried to allocate 128.00 MiB. GPU 0 has a total capacity of 14.56 GiB of which 119.81 MiB is free. Including non-PyTorch memory, this process has 14.44 GiB memory in use. Of the allocated memory 14.08 GiB is allocated by PyTorch, and 252.42 MiB is reserved by PyTorch but unallocated. If reserved but unallocated memory is large try setting PYTORCH_ALLOC_CONF=expandable_segments:True to avoid fragmentation. See documentation for Memory Management (https://pytorch.org/docs/stable/notes/cuda.html#environment-variables)" - ] - } - ] - }, - { - "cell_type": "code", - "source": [ - "#@markdown # CELL 5 — Run Training\n", - "\n", - "num_epochs = 20 # Increase if you add more images\n", - "save_steps = 10\n", - "output_dir = \"/content/drive/MyDrive/flux_klein_lora\"\n", - "\n", - "os.makedirs(output_dir, exist_ok=True)\n", - "\n", - "pipe.transformer.train()\n", - "device = torch.device('cuda')\n", - "\n", - "for epoch in range(num_epochs):\n", - " epoch_loss = 0\n", - " for batch in dataloader:\n", - " optimizer.zero_grad()\n", - "\n", - " pixel_values = batch[\"pixel_values\"].to(device)\n", - " prompts = batch[\"text\"]\n", - "\n", - " # Forward pass (this is simplified — real Flux training uses noise prediction)\n", - " with torch.cuda.amp.autocast():\n", - " # You may need to use the pipeline's components more directly\n", - " # or a full training script for proper noise scheduling\n", - " latents = pipe.vae.encode(pixel_values).latent_dist.sample() * pipe.vae.config.scaling_factor\n", - "\n", - " # Add noise and predict (basic placeholder — expand this)\n", - " noise = torch.randn_like(latents)\n", - " timesteps = torch.randint(0, 1000, (latents.shape[0],), device=device)\n", - "\n", - " # This part is highly simplified. For proper Flux training,\n", - " # consider using diffusers' train_dreambooth_lora_flux script adapted for Klein.\n", - " noisy_latents = latents + noise # placeholder\n", - " # model_output = pipe.transformer(...) # needs full setup\n", - "\n", - " loss = torch.nn.functional.mse_loss(noisy_latents, latents) # dummy loss for demo\n", - "\n", - " scaler.scale(loss).backward()\n", - " scaler.step(optimizer)\n", - " scaler.update()\n", - "\n", - " epoch_loss += loss.item()\n", - "\n", - " print(f\"Epoch {epoch+1}/{num_epochs}, Loss: {epoch_loss/len(dataloader):.4f}\")\n", - "\n", - " if (epoch + 1) % save_steps == 0:\n", - " pipe.transformer.save_pretrained(f\"{output_dir}/checkpoint-{epoch+1}\")\n", - " print(f\"✅ Saved checkpoint at epoch {epoch+1}\")\n", - "\n", - "# Final save\n", - "pipe.transformer.save_pretrained(f\"{output_dir}/final_lora\")\n", - "print(\"✅ Training completed! LoRA saved to Google Drive.\")" - ], - "metadata": { - "id": "1xckXr-lieMq" - }, - "execution_count": null, - "outputs": [] - }, - { - "cell_type": "markdown", - "source": [], - "metadata": { - "id": "4MRDxrA5hZus" - } - }, - { - "cell_type": "code", - "source": [ - "\n", - "\n", - "print(\"✅ Pipeline loaded on CPU\")\n", - "\n", - "\n", - "\n", - "pipe.text_encoder = pipe.text_encoder.to(\"cuda\")\n", - "\n", - "# ------------------- Text Encoding -------------------\n", - "# Encode both prompts (Flux often uses one main prompt)\n", - "with torch.no_grad():\n", - " text_inputs1 = pipe.tokenizer(prompt1, padding=\"max_length\", max_length=512, truncation=True, return_tensors=\"pt\")\n", - " text_embeds1 = pipe.text_encoder(text_inputs1.input_ids.to(pipe.device))[0] # shape: (1, seq_len, hidden_size)\n", - "\n", - " text_inputs2 = pipe.tokenizer(prompt2, padding=\"max_length\", max_length=512, truncation=True, return_tensors=\"pt\")\n", - " text_embeds2 = pipe.text_encoder(text_inputs2.input_ids.to(pipe.device))[0]\n", - "\n", - "print(f\"Text embeds shape: {text_embeds1.shape}\")\n", - "\n", - "# ------------------- Image Encoding (VAE) -------------------\n", - "# Move VAE to GPU temporarily\n", - "pipe.vae = pipe.vae.to(\"cuda\", dtype=torch.float16)\n", - "pipe.vae.eval()\n", - "\n", - "def encode_image(img):\n", - " with torch.no_grad():\n", - " img_tensor = T.ToTensor()(img).unsqueeze(0).to(\"cuda\", dtype=torch.float16) * 2 - 1 # [-1, 1]\n", - " latents = pipe.vae.encode(img_tensor).latent_dist.sample()\n", - " return latents\n", - "\n", - "latent_image1 = encode_image(image1)\n", - "latent_image2 = encode_image(image2) # This will be your Y1_hat target\n", - "\n", - "print(f\"Latent shape: {latent_image1.shape}\") # Should be (1, 4, h/8, w/8) or similar for Flux\n", - "\n", - "# Clean up VAE VRAM if needed\n", - "torch.cuda.empty_cache()\n", - "gc.collect()" - ], - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "id": "Gz6zaIR9GPY-", - "outputId": "74cea392-32ce-4cf9-b07f-e14a9a53453e" - }, - "execution_count": null, - "outputs": [ - { - "output_type": "stream", - "name": "stdout", - "text": [ - "✅ Pipeline loaded on CPU\n", - "Image1 size: (643, 360), Image2 size: (720, 360)\n" - ] - }, - { - "output_type": "stream", - "name": "stderr", - "text": [ - "W0522 16:15:30.574000 691 torch/_inductor/utils.py:1679] [2/0] Not enough SMs to use max_autotune_gemm mode\n" - ] - }, - { - "output_type": "stream", - "name": "stdout", - "text": [ - "Text embeds shape: torch.Size([1, 512, 151936])\n", - "Latent shape: torch.Size([1, 32, 45, 80])\n" - ] - }, - { - "output_type": "execute_result", - "data": { - "text/plain": [ - "8923" - ] - }, - "metadata": {}, - "execution_count": 3 - } - ] - }, - { - "cell_type": "code", - "source": [ - "# ====================== DEFINE X1 and X2 ======================\n", - "# X1 = latent_image1 (from image1) → will be combined with prompt1 conditioning\n", - "# X2 = latent_image2 (from image2) → this is your target Y1_hat\n", - "\n", - "X1 = latent_image1.clone().detach().to(\"cuda\", dtype=torch.float16)\n", - "X2 = latent_image2.clone().detach().to(\"cuda\", dtype=torch.float16)\n", - "\n", - "print(f\"X1 (input latent) shape: {X1.shape}\")\n", - "print(f\"X2 (target latent) shape: {X2.shape}\")\n", - "\n", - "# Save them\n", - "torch.save(X1, \"/content/X1_latent.pt\")\n", - "torch.save(X2, \"/content/X2_latent.pt\")\n", - "print(\"✅ X1 and X2 saved\")" - ], - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "id": "O76DmAIFL_rL", - "outputId": "81655137-8cd0-41f6-a1dc-cf3ae9eb2bfd" - }, - "execution_count": null, - "outputs": [ - { - "output_type": "stream", - "name": "stdout", - "text": [ - "X1 (input latent) shape: torch.Size([1, 32, 45, 80])\n", - "X2 (target latent) shape: torch.Size([1, 32, 45, 90])\n", - "✅ X1 and X2 saved\n" - ] - } - ] - }, - { - "cell_type": "code", - "source": [ - "# Unload the text_encoder\n", - "pipe.text_encoder = pipe.text_encoder.to(\"cpu\")\n", - "gc.collect()\n", - "torch.cuda.empty_cache()" - ], - "metadata": { - "id": "xy9HRVAqMJBf" - }, - "execution_count": null, - "outputs": [] - }, - { - "cell_type": "code", - "source": [ - "# Move transformer to GPU\n", - "pipe.transformer = pipe.transformer.to(\"cuda\")\n", - "pipe.transformer.eval()\n", - "\n", - "print(\"✅ Transformer on GPU\")\n", - "\n", - "# ====================== PREPARE INPUTS ======================\n", - "with torch.no_grad():\n", - " # 1. Start from clean X1 latent (32 channels)\n", - " noise = torch.randn_like(X1) * 0.02\n", - " hidden_states = (X1 + noise).to(torch.float16) # shape: [1, 32, 45, 80]\n", - "\n", - " # === CRITICAL FIX: Project from 32 → 128 channels ===\n", - " # We use a simple linear projection (you can also try nearest-neighbor repeat or zero-pad later)\n", - " b, c, h, w = hidden_states.shape\n", - " hidden_states = hidden_states.permute(0, 2, 3, 1) # [B, H, W, C=32]\n", - " hidden_states = hidden_states.reshape(b, h * w, c) # [B, tokens=3600, 32]\n", - "\n", - " # Project channels 32 → 128\n", - " channel_proj = torch.nn.Linear(32, 128, bias=False, dtype=torch.float16, device=\"cuda\")\n", - " hidden_states = channel_proj(hidden_states) # now [B, 3600, 128]\n", - "\n", - " encoder_hidden_states = text_embeds1.to(torch.float16)\n", - "\n", - " # Timestep\n", - " timestep = torch.tensor([0.15], device=\"cuda\", dtype=torch.float16)\n", - "\n", - " # 2. IDs\n", - " img_ids = torch.zeros((b, h * w, 3), device=\"cuda\", dtype=torch.float16)\n", - " y_coords = torch.arange(h, device=\"cuda\").repeat_interleave(w).float()\n", - " x_coords = torch.arange(w, device=\"cuda\").repeat(h).float()\n", - " img_ids[0, :, 0] = x_coords\n", - " img_ids[0, :, 1] = y_coords\n", - " img_ids[0, :, 2] = 0.0\n", - "\n", - " txt_seq_len = encoder_hidden_states.shape[1]\n", - " txt_ids = torch.zeros((b, txt_seq_len, 3), device=\"cuda\", dtype=torch.float16)\n", - " txt_ids[0, :, 0] = torch.arange(txt_seq_len, device=\"cuda\").float()\n", - " txt_ids[0, :, 1] = 0.0\n", - " txt_ids[0, :, 2] = 1.0\n", - "\n", - " # ====================== FORWARD PASS ======================\n", - " try:\n", - " output = pipe.transformer(\n", - " hidden_states=hidden_states,\n", - " encoder_hidden_states=encoder_hidden_states,\n", - " timestep=timestep,\n", - " img_ids=img_ids,\n", - " txt_ids=txt_ids,\n", - " return_dict=False\n", - " )\n", - "\n", - " if isinstance(output, tuple):\n", - " Y1 = output[0]\n", - " else:\n", - " Y1 = output\n", - "\n", - " print(f\"✅ Forward pass successful!\")\n", - " print(f\"Y1 shape (flattened): {Y1.shape}\")\n", - "\n", - " # Optional: reshape back toward spatial (approximate)\n", - " # Y1 is currently [B, tokens, 128] → we can project back if needed\n", - "\n", - " except Exception as e:\n", - " print(\"❌ Error:\")\n", - " print(str(e))" - ], - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "id": "gXdtTyDqMIN_", - "outputId": "7f181167-269e-46e7-bdf5-616debd658ff" - }, - "execution_count": null, - "outputs": [ - { - "output_type": "stream", - "name": "stdout", - "text": [ - "✅ Transformer on GPU\n", - "❌ Error:\n", - "mat1 and mat2 shapes cannot be multiplied (512x151936 and 7680x3072)\n" + "\u001b[0;32m/usr/local/lib/python3.12/dist-packages/torch/nn/modules/linear.py\u001b[0m in \u001b[0;36mforward\u001b[0;34m(self, input)\u001b[0m\n\u001b[1;32m 132\u001b[0m \u001b[0mRuns\u001b[0m \u001b[0mthe\u001b[0m \u001b[0mforward\u001b[0m \u001b[0;32mpass\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 133\u001b[0m \"\"\"\n\u001b[0;32m--> 134\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mF\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mlinear\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0minput\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mweight\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mbias\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 135\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 136\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mextra_repr\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m->\u001b[0m \u001b[0mstr\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;31mRuntimeError\u001b[0m: mat1 and mat2 shapes cannot be multiplied (2048x64 and 128x3072)" ] } ] - }, - { - "cell_type": "code", - "source": [ - "# Move transformer + VAE to GPU (or use offload if VRAM is tight)\n", - "pipe.transformer = pipe.transformer.to(\"cuda\", dtype=torch.float16)\n", - "pipe.transformer.eval()\n", - "print(\"✅ Transformer moved to GPU\")" - ], - "metadata": { - "id": "71y6xaSMKbUH" - }, - "execution_count": null, - "outputs": [] - }, - { - "cell_type": "code", - "source": [ - "\n", - "\n", - "# For Flux/Klein, the transformer typically takes:\n", - "# - noisy_latents (or clean for single forward)\n", - "# - timestep\n", - "# - text embeddings (encoder_hidden_states)\n", - "# - image_ids / txt_ids (positional)\n", - "\n", - "# Prepare a simple forward pass (single step, no full denoising loop)\n", - "with torch.no_grad():\n", - " # Start with latent of image1 as input (X1)\n", - " # Add small noise or use as-is depending on your experiment\n", - " noise = torch.randn_like(latent_image1) * 0.1\n", - " x1 = latent_image1 + noise # X1: latent input from prompt1 + image1\n", - "\n", - " # Timestep (for rectified flow / Flow Matching models like Flux, often normalized 0-1)\n", - " timestep = torch.tensor([0.5], device=\"cuda\", dtype=torch.float16) # adjust as needed\n", - "\n", - " # Forward through transformer\n", - " # Note: Exact signature depends on Flux2KleinTransformer. Common args:\n", - " Y1 = pipe.transformer(\n", - " hidden_states=x1,\n", - " encoder_hidden_states=text_embeds1, # text conditioning\n", - " timestep=timestep,\n", - " # img_ids / txt_ids may be needed — check model.forward signature\n", - " return_dict=False,\n", - " )[0] # Usually returns tuple with noise prediction / velocity\n", - "\n", - "print(f\"Transformer output Y1 shape: {Y1.shape}\")\n", - "\n", - "# Your target Y1_hat\n", - "Y1_hat = latent_image2\n", - "print(f\"Target Y1_hat (latent image2) shape: {Y1_hat.shape}\")\n", - "\n", - "# Example loss (for debugging / training signal)\n", - "loss = torch.nn.functional.mse_loss(Y1, Y1_hat)\n", - "print(f\"Simple MSE between Y1 and Y1_hat: {loss.item():.6f}\")" - ], - "metadata": { - "id": "3CFZ93NfI1Uj" - }, - "execution_count": null, - "outputs": [] } ] } \ No newline at end of file