Skip to content

Instantly share code, notes, and snippets.

@alfredplpl
Created August 5, 2025 02:45
Show Gist options
  • Save alfredplpl/5dd94043240ebf6f115f1bf560b0c5ac to your computer and use it in GitHub Desktop.
Save alfredplpl/5dd94043240ebf6f115f1bf560b0c5ac to your computer and use it in GitHub Desktop.
Qwen-Image.ipynb
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/alfredplpl/5dd94043240ebf6f115f1bf560b0c5ac/qwen-image.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "FHyQ_msUkkSf"
},
"outputs": [],
"source": [
"!pip install -U bitsandbytes\n",
"!pip install git+https://github.com/huggingface/diffusers\n",
"!pip install hf-transfer"
]
},
{
"cell_type": "code",
"source": [
"import os\n",
"os.environ[\"HF_HUB_ENABLE_HF_TRANSFER\"] = \"1\""
],
"metadata": {
"id": "r2bULHGQ6M2Y"
},
"execution_count": 2,
"outputs": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "K4e1zj3Gl4D8"
},
"outputs": [],
"source": [
"from diffusers import DiffusionPipeline, PipelineQuantizationConfig\n",
"import torch\n",
"\n",
"quant_config = PipelineQuantizationConfig(\n",
" quant_backend=\"bitsandbytes_4bit\",\n",
" quant_kwargs={\"load_in_4bit\": True, \"bnb_4bit_quant_type\": \"nf4\", \"bnb_4bit_compute_dtype\": torch.bfloat16},\n",
" components_to_quantize=[\"transformer\", \"text_encoder\"],\n",
")\n",
"pipe = DiffusionPipeline.from_pretrained(\n",
"\t\"Qwen/Qwen-Image\",\n",
"\tquantization_config=quant_config,\n",
"\ttorch_dtype=torch.bfloat16\n",
")\n",
"pipe.enable_model_cpu_offload()\n",
"\n",
"aspect_ratios = {\n",
" \"1:1\": (1328, 1328),\n",
" \"16:9\": (1664, 928),\n",
" \"9:16\": (928, 1664),\n",
" \"4:3\": (1472, 1140),\n",
" \"3:4\": (1140, 1472)\n",
"}\n",
"\n",
"width, height = aspect_ratios[\"9:16\"]\n",
"\n",
"prompt = \"Japanese anime style, a cat holding a sign that says hello world\"\n",
"negative_prompt=\"3d, cg, photo\"\n",
"image = pipe(\n",
" prompt=prompt,\n",
" negative_prompt=negative_prompt,\n",
" width=width,\n",
" height=height,\n",
" num_inference_steps=50,\n",
" true_cfg_scale=4.0,\n",
").images[0]\n",
"image.save(\"qwenimage_nf4.png\")"
]
}
],
"metadata": {
"accelerator": "GPU",
"colab": {
"gpuType": "L4",
"machine_shape": "hm",
"provenance": [],
"authorship_tag": "ABX9TyPGnm3ab2qQG/2j5SMxop91",
"include_colab_link": true
},
"kernelspec": {
"display_name": "Python 3",
"name": "python3"
},
"language_info": {
"name": "python"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
@alfredplpl
Copy link
Author

生成物
qwenimage_nf4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment