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