p1atdev commited on
Commit
22aa404
1 Parent(s): 4c3e29d

Upload quantize_fp8.ipynb

Browse files
Files changed (1) hide show
  1. quantize_fp8.ipynb +141 -0
quantize_fp8.ipynb ADDED
@@ -0,0 +1,141 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "nbformat": 4,
3
+ "nbformat_minor": 0,
4
+ "metadata": {
5
+ "colab": {
6
+ "provenance": []
7
+ },
8
+ "kernelspec": {
9
+ "name": "python3",
10
+ "display_name": "Python 3"
11
+ },
12
+ "language_info": {
13
+ "name": "python"
14
+ }
15
+ },
16
+ "cells": [
17
+ {
18
+ "cell_type": "code",
19
+ "execution_count": null,
20
+ "metadata": {
21
+ "id": "3eDO2Vp7y94X"
22
+ },
23
+ "outputs": [],
24
+ "source": [
25
+ "!pip install huggingface_hub hf_transfer"
26
+ ]
27
+ },
28
+ {
29
+ "cell_type": "code",
30
+ "source": [
31
+ "!export HF_HUB_ENABLE_HF_TRANSFER=1"
32
+ ],
33
+ "metadata": {
34
+ "id": "j4X2v5yh0qJC"
35
+ },
36
+ "execution_count": null,
37
+ "outputs": []
38
+ },
39
+ {
40
+ "cell_type": "code",
41
+ "source": [
42
+ "import torch\n",
43
+ "from huggingface_hub import notebook_login, HfApi\n",
44
+ "from safetensors.torch import load_file, save_file"
45
+ ],
46
+ "metadata": {
47
+ "id": "kPX5fBF5zHYN"
48
+ },
49
+ "execution_count": null,
50
+ "outputs": []
51
+ },
52
+ {
53
+ "cell_type": "code",
54
+ "source": [
55
+ "notebook_login() # WRITE token"
56
+ ],
57
+ "metadata": {
58
+ "id": "50ZzcvkI1F0W"
59
+ },
60
+ "execution_count": null,
61
+ "outputs": []
62
+ },
63
+ {
64
+ "cell_type": "code",
65
+ "source": [
66
+ "api = HfApi()"
67
+ ],
68
+ "metadata": {
69
+ "id": "zTFJTeRHzJ6a"
70
+ },
71
+ "execution_count": null,
72
+ "outputs": []
73
+ },
74
+ {
75
+ "cell_type": "code",
76
+ "source": [
77
+ "def download(repo_id: str, filename: str, save_name: str, dtype = torch.float8_e4m3fn):\n",
78
+ " ckpt_path = api.hf_hub_download(\n",
79
+ " repo_id,\n",
80
+ " filename=filename,\n",
81
+ " )\n",
82
+ "\n",
83
+ " state_dict = load_file(ckpt_path)\n",
84
+ " for key, value in state_dict.items():\n",
85
+ " state_dict[key] = value.to(dtype)\n",
86
+ "\n",
87
+ " save_file(state_dict, f\"./{save_name}\")\n",
88
+ " return f\"./{save_name}\""
89
+ ],
90
+ "metadata": {
91
+ "id": "IDgia3bzzJBJ"
92
+ },
93
+ "execution_count": null,
94
+ "outputs": []
95
+ },
96
+ {
97
+ "cell_type": "code",
98
+ "source": [
99
+ "def push(repo_id: str, local_file: str):\n",
100
+ " api.upload_file(\n",
101
+ " path_or_fileobj=local_file,\n",
102
+ " path_in_repo=local_file.split(\"/\")[-1],\n",
103
+ " repo_id=repo_id,\n",
104
+ " repo_type=\"model\",\n",
105
+ " )"
106
+ ],
107
+ "metadata": {
108
+ "id": "o1-kDCqY1yZY"
109
+ },
110
+ "execution_count": null,
111
+ "outputs": []
112
+ },
113
+ {
114
+ "cell_type": "code",
115
+ "source": [
116
+ "model_path = download(\n",
117
+ " \"OnomaAIResearch/Illustrious-xl-early-release-v0\",\n",
118
+ " \"Illustrious-XL-v0.1.safetensors\",\n",
119
+ " \"Illustrious-XL-v0.1.float8_e4m3fn.safetensors\",\n",
120
+ " torch.float8_e4m3fn, # float8_e5m2\n",
121
+ ")"
122
+ ],
123
+ "metadata": {
124
+ "id": "dOBdDRTpzLcV"
125
+ },
126
+ "execution_count": null,
127
+ "outputs": []
128
+ },
129
+ {
130
+ "cell_type": "code",
131
+ "source": [
132
+ "push(YOUR_REPO_NAME, model_path)"
133
+ ],
134
+ "metadata": {
135
+ "id": "jYGONemw2Lj_"
136
+ },
137
+ "execution_count": null,
138
+ "outputs": []
139
+ }
140
+ ]
141
+ }