Stable-Diffusion-Vertex/Workbench/diffusers_nbexecutor.ipynb (143 lines of code) (raw):
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"id": "644ad25f",
"metadata": {},
"outputs": [],
"source": [
"# Copyright 2022 Google LLC\n",
"#\n",
"# Licensed under the Apache License, Version 2.0 (the \"License\");\n",
"# you may not use this file except in compliance with the License.\n",
"# You may obtain a copy of the License at\n",
"#\n",
"# https://www.apache.org/licenses/LICENSE-2.0\n",
"#\n",
"# Unless required by applicable law or agreed to in writing, software\n",
"# distributed under the License is distributed on an \"AS IS\" BASIS,\n",
"# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
"# See the License for the specific language governing permissions and\n",
"# limitations under the License."
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "7abfd671",
"metadata": {},
"source": [
"## Write training code here and Click \"Execute\" for a workbench execute job\n",
"- Use custom container built in Cloud Build and stored in Artifact Registry\n",
"- Cloud Build command: gcloud builds submit --config cloud-build.yaml .\n",
"- input and output directory can be /gcs/bucket_name/folder for Cloud Storage path"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2243ab9f-d7db-4db5-836a-154d9616a628",
"metadata": {
"id": "2243ab9f-d7db-4db5-836a-154d9616a628"
},
"outputs": [],
"source": [
"MODEL_NAME=\"runwayml/stable-diffusion-v1-5\"\n",
"INSTANCE_DIR=\"/gcs/bucket_name/input_dog\"\n",
"OUTPUT_DIR=\"/gcs/bucket_name/dog_lora_output\"\n",
"\n",
"! accelerate launch ./diffusers/examples/dreambooth/train_dreambooth_lora.py \\\n",
" --pretrained_model_name_or_path=$MODEL_NAME \\\n",
" --instance_data_dir=$INSTANCE_DIR \\\n",
" --output_dir=$OUTPUT_DIR \\\n",
" --instance_prompt=\"a photo of sks dog\" \\\n",
" --resolution=512 \\\n",
" --train_batch_size=1 \\\n",
" --use_8bit_adam \\\n",
" --mixed_precision=\"fp16\" \\\n",
" --gradient_accumulation_steps=1 \\\n",
" --learning_rate=1e-4 \\\n",
" --lr_scheduler=\"constant\" \\\n",
" --lr_warmup_steps=0 \\\n",
" --max_train_steps=500"
]
},
{
"cell_type": "markdown",
"id": "O25rkc78ggqL",
"metadata": {
"id": "O25rkc78ggqL"
},
"source": [
"Convert the lora .bin file to safetensor file, for used in WebUI"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d52e7698-122a-4864-ad8c-55d4562c2a94",
"metadata": {
"id": "d52e7698-122a-4864-ad8c-55d4562c2a94"
},
"outputs": [],
"source": [
"import os;\n",
"import re;\n",
"import torch;\n",
"from safetensors.torch import save_file;\n",
"\n",
"newDict = dict();\n",
"checkpoint = torch.load(OUTPUT_DIR + '/pytorch_lora_weights.bin');\n",
"for idx, key in enumerate(checkpoint):\n",
" newKey = re.sub('\\.processor\\.', '_', key);\n",
" newKey = re.sub('mid_block\\.', 'mid_block_', newKey);\n",
" newKey = re.sub('_lora.up.', '.lora_up.', newKey);\n",
" newKey = re.sub('_lora.down.', '.lora_down.', newKey);\n",
" newKey = re.sub('\\.(\\d+)\\.', '_\\\\1_', newKey);\n",
" newKey = re.sub('to_out', 'to_out_0', newKey);\n",
" newKey = 'lora_unet_'+newKey;\n",
"\n",
" newDict[newKey] = checkpoint[key];\n",
"\n",
"newLoraName = 'pytorch_lora_weights.safetensors';\n",
"print(\"Saving \" + newLoraName);\n",
"save_file(newDict, OUTPUT_DIR + '/' + newLoraName);"
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "bd0880a8",
"metadata": {},
"source": [
"***It's supported to configure NFS using Executor*"
]
}
],
"metadata": {
"colab": {
"provenance": []
},
"gpuClass": "standard",
"kernelspec": {
"display_name": "Pytorch (Local)",
"language": "python",
"name": "local-pytorch"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.12"
}
},
"nbformat": 4,
"nbformat_minor": 5
}