File size: 10,295 Bytes
44eca84
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1028385
 
 
8833bbc
1028385
 
 
 
 
44eca84
 
 
 
 
 
 
 
f22aece
1028385
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
abc0b46
 
 
 
44eca84
 
fa58912
44eca84
fa58912
 
44eca84
 
 
 
fa58912
 
44eca84
 
 
 
 
 
 
1028385
 
 
 
 
fa58912
1028385
 
 
 
 
44eca84
 
 
1028385
 
fa58912
811a69b
fa58912
1028385
 
8833bbc
 
 
fa58912
 
 
 
 
 
 
 
 
 
 
 
 
 
1028385
 
fa58912
1028385
fa58912
 
1028385
8833bbc
 
 
fa58912
8833bbc
 
 
 
 
1028385
 
 
8833bbc
abc0b46
8833bbc
44eca84
fa58912
 
 
 
 
8833bbc
 
 
 
 
 
 
fa58912
 
 
 
 
 
 
 
 
 
 
8833bbc
 
 
fa58912
8833bbc
 
eabd9f8
8833bbc
 
 
fa58912
8833bbc
 
 
 
 
 
fa58912
8833bbc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1028385
 
 
 
44eca84
1028385
 
44eca84
fa58912
 
 
 
 
 
 
 
 
1028385
 
8833bbc
fa58912
8833bbc
fa58912
8833bbc
fa58912
44eca84
 
fa58912
44eca84
fa58912
 
44eca84
8833bbc
 
 
 
 
 
 
 
 
1028385
 
 
9fb003c
 
 
 
 
8833bbc
9fb003c
1028385
811a69b
9fb003c
 
 
 
 
 
 
 
1028385
 
8833bbc
fa58912
1028385
fa58912
 
1028385
 
 
 
 
 
 
 
 
 
44eca84
 
 
 
 
 
 
 
 
 
 
 
8ad9fee
44eca84
 
8ad9fee
44eca84
8ad9fee
44eca84
 
8ad9fee
44eca84
 
 
 
 
 
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "provenance": []
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "cells": [
    {
      "cell_type": "markdown",
      "source": [
        "This Notebook is a Stable-diffusion tool which allows you to find similiar tokens from the SD 1.5 vocab.json that you can use for text-to-image generation."
      ],
      "metadata": {
        "id": "L7JTcbOdBPfh"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# Load the tokens into the colab\n",
        "!git clone https://huggingface.co/datasets/codeShare/sd_tokens\n",
        "import torch\n",
        "from torch import linalg as LA\n",
        "device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')\n",
        "%cd /content/sd_tokens\n",
        "token = torch.load('sd15_tensors.pt', map_location=device, weights_only=True)\n",
        "#-----#\n",
        "\n",
        "#Import the vocab.json\n",
        "import json\n",
        "import pandas as pd\n",
        "with open('vocab.json', 'r') as f:\n",
        "    data = json.load(f)\n",
        "\n",
        "_df = pd.DataFrame({'count': data})['count']\n",
        "\n",
        "vocab = {\n",
        "    value: key for key, value in _df.items()\n",
        "}\n",
        "#-----#\n",
        "\n",
        "# Define functions/constants\n",
        "NUM_TOKENS = 49407\n",
        "\n",
        "def absolute_value(x):\n",
        "    return max(x, -x)\n",
        "\n",
        "def similarity(id_A , id_B):\n",
        "  #Tensors\n",
        "  A = token[id_A]\n",
        "  B = token[id_B]\n",
        "  #Tensor vector length (2nd order, i.e (a^2 + b^2 + ....)^(1/2)\n",
        "  _A = LA.vector_norm(A, ord=2)\n",
        "  _B = LA.vector_norm(B, ord=2)\n",
        "  #----#\n",
        "  result = torch.dot(A,B)/(_A*_B)\n",
        "  similarity_pcnt = absolute_value(result.item()*100)\n",
        "  similarity_pcnt_aprox = round(similarity_pcnt, 3)\n",
        "  result = f'{similarity_pcnt_aprox} %'\n",
        "  return result\n",
        "#----#\n",
        "\n",
        "mix_with = \"\"\n",
        "mix_method = \"None\""
      ],
      "metadata": {
        "id": "Ch9puvwKH1s3"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "#print(vocab[8922]) #the vocab item for ID 8922\n",
        "#print(token[8922].shape)  #dimension of the token"
      ],
      "metadata": {
        "id": "S_Yh9gH_OUA1"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "Get the IDs from a prompt text.\n",
        "\n",
        "The prompt will be enclosed with the <|start-of-text|> and <|end-of-text|> tokens. Leave the field empty to get a random value tensor"
      ],
      "metadata": {
        "id": "f1-jS7YJApiO"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "from transformers import AutoTokenizer\n",
        "tokenizer = AutoTokenizer.from_pretrained(\"openai/clip-vit-large-patch14\", clean_up_tokenization_spaces = False)\n",
        "\n",
        "prompt= \"banana\" # @param {type:'string'}\n",
        "\n",
        "tokenizer_output = tokenizer(text = prompt)\n",
        "input_ids = tokenizer_output['input_ids']\n",
        "print(input_ids)\n",
        "id_A = input_ids[1]\n",
        "A = token[id_A]\n",
        "_A = LA.vector_norm(A, ord=2)\n",
        "\n",
        "#if no imput exists we just randomize the entire thing\n",
        "if (prompt == \"\"):\n",
        "  id_A = -1\n",
        "  print(\"Tokenized prompt tensor A is a random valued tensor with no ID\")\n",
        "  R = torch.rand(768)\n",
        "  _R =  LA.vector_norm(R, ord=2)\n",
        "  A = R*(_A/_R)\n",
        "\n",
        "#Save a copy of the tensor A\n",
        "id_P = input_ids[1]\n",
        "P = token[id_A]\n",
        "_P = LA.vector_norm(A, ord=2)"
      ],
      "metadata": {
        "id": "RPdkYzT2_X85"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "OPTIONAL : Add/subtract + normalize above result with another token. Leave field empty to get a random value tensor"
      ],
      "metadata": {
        "id": "JKnz0aLFVGXc"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "mix_with = \"\" # @param {type:'string'}\n",
        "mix_method = \"None\" # @param [\"None\" , \"Average\", \"Subtract\"] {allow-input: true}\n",
        "w = 0.5 # @param {type:\"slider\", min:0, max:1, step:0.01}\n",
        "\n",
        "#prevent re-iterating A by reading from stored copy\n",
        "id_A = id_P\n",
        "A = P\n",
        "_A = _P\n",
        "#----#\n",
        "\n",
        "tokenizer_output = tokenizer(text = mix_with)\n",
        "input_ids = tokenizer_output['input_ids']\n",
        "id_C = input_ids[1]\n",
        "C = token[id_C]\n",
        "_C = LA.vector_norm(C, ord=2)\n",
        "\n",
        "#if no imput exists we just randomize the entire thing\n",
        "if (mix_with == \"\"):\n",
        "  id_C = -1\n",
        "  print(\"Tokenized prompt  'mix_with' tensor C is a random valued tensor with no ID\")\n",
        "  R = torch.rand(768)\n",
        "  _R =  LA.vector_norm(R, ord=2)\n",
        "  C = R*(_C/_R)\n",
        "\n",
        "if (mix_method ==  \"None\"):\n",
        "  print(\"No operation\")\n",
        "\n",
        "if (mix_method ==  \"Average\"):\n",
        "  A = w*A + (1-w)*C\n",
        "  _A = LA.vector_norm(A, ord=2)\n",
        "  print(\"Tokenized prompt tensor A has been recalculated as A = w*A + (1-w)*C , where C is the tokenized prompt  'mix_with' tensor C\")\n",
        "\n",
        "if (mix_method ==  \"Subtract\"):\n",
        "  tmp = (A/_A) - (C/_C)\n",
        "  _tmp = LA.vector_norm(tmp, ord=2)\n",
        "  A = tmp*((w*_A + (1-w)*_C)/_tmp)\n",
        "  _A = LA.vector_norm(A, ord=2)\n",
        "  print(\"Tokenized prompt tensor A has been recalculated as A = (w*_A + (1-w)*_C) * norm(w*A - (1-w)*C) , where C is the tokenized prompt 'mix_with' tensor C\")\n",
        "\n",
        "\n"
      ],
      "metadata": {
        "id": "oXbNSRSKPgRr"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "Produce a list id IDs that are most similiar to the prompt ID at positiion 1 based on above result"
      ],
      "metadata": {
        "id": "3uBSZ1vWVCew"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "\n",
        "dots = torch.zeros(NUM_TOKENS)\n",
        "for index in range(NUM_TOKENS):\n",
        "  id_B = index\n",
        "  B = token[id_B]\n",
        "  _B = LA.vector_norm(B, ord=2)\n",
        "  result = torch.dot(A,B)/(_A*_B)\n",
        "  result = absolute_value(result.item())\n",
        "  dots[index] = result\n",
        "\n",
        "name_A = \"A of random type\"\n",
        "if (id_A>-1):\n",
        "  name_A = vocab[id_A]\n",
        "\n",
        "name_C = \"token C of random type\"\n",
        "if (id_C>-1):\n",
        "  name_C = vocab[id_C]\n",
        "\n",
        "\n",
        "sorted, indices = torch.sort(dots,dim=0 , descending=True)\n",
        "#----#\n",
        "if (mix_method ==  \"Average\"):\n",
        "  print(f'Calculated all cosine-similarities between the average of token {name_A} and {name_C} with Id_A = {id_A} and mixed Id_C = {id_C} as a 1x{sorted.shape[0]} tensor')\n",
        "if (mix_method ==  \"Subtract\"):\n",
        "  print(f'Calculated all cosine-similarities between the subtract of token {name_A} and {name_C} with Id_A = {id_A} and mixed Id_C = {id_C} as a 1x{sorted.shape[0]} tensor')\n",
        "if (mix_method ==  \"None\"):\n",
        "  print(f'Calculated all cosine-similarities between the token {name_A} with Id_A = {id_A} with the the rest of the {NUM_TOKENS} tokens as a 1x{sorted.shape[0]} tensor')"
      ],
      "metadata": {
        "id": "juxsvco9B0iV"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "Print the sorted list from above result"
      ],
      "metadata": {
        "id": "y-Ig3glrVQC3"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "list_size = 100 # @param {type:'number'}\n",
        "\n",
        "print_ID = False # @param {type:\"boolean\"}\n",
        "print_Similarity = True # @param {type:\"boolean\"}\n",
        "print_Name = True # @param {type:\"boolean\"}\n",
        "print_Divider = True # @param {type:\"boolean\"}\n",
        "\n",
        "for index in range(list_size):\n",
        "  id = indices[index].item()\n",
        "  if (print_Name):\n",
        "    print(f'{vocab[id]}') # vocab item\n",
        "  if (print_ID):\n",
        "    print(f'ID = {id}') # IDs\n",
        "  if (print_Similarity):\n",
        "    print(f'similiarity = {round(sorted[index].item()*100,2)} %') # % value\n",
        "  if (print_Divider):\n",
        "    print('--------')"
      ],
      "metadata": {
        "id": "YIEmLAzbHeuo",
        "collapsed": true
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "Find the most similiar Tokens for given input"
      ],
      "metadata": {
        "id": "qqZ5DvfLBJnw"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "Valid ID ranges for id_for_token_A / id_for_token_B are between 0 and 49407"
      ],
      "metadata": {
        "id": "kX72bAuhOtlT"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "id_for_token_A = 4567 # @param {type:'number'}\n",
        "id_for_token_B = 4343 # @param {type:'number'}\n",
        "\n",
        "similarity_str =  'The similarity between tokens A and B is ' + similarity(id_for_token_A , id_for_token_B)\n",
        "\n",
        "print(similarity_str)"
      ],
      "metadata": {
        "id": "MwmOdC9cNZty"
      },
      "execution_count": null,
      "outputs": []
    }
  ]
}