embedding_model.ipynb 14.3 KB
Newer Older
Chayenne's avatar
Chayenne committed
1
2
3
4
5
6
{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
Chayenne's avatar
Chayenne committed
7
8
9
10
11
12
    "# Embedding Model\n",
    "\n",
    "SGLang supports embedding models in the same way as completion models. Here are some example models:\n",
    "\n",
    "- [intfloat/e5-mistral-7b-instruct](https://huggingface.co/intfloat/e5-mistral-7b-instruct)\n",
    "- [Alibaba-NLP/gte-Qwen2-7B-instruct](https://huggingface.co/Alibaba-NLP/gte-Qwen2-7B-instruct)\n"
Chayenne's avatar
Chayenne committed
13
14
15
16
17
18
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
Chayenne's avatar
Chayenne committed
19
20
21
22
23
    "## Launch A Server\n",
    "\n",
    "The following code is equivalent to running this in the shell:\n",
    "```bash\n",
    "python -m sglang.launch_server --model-path Alibaba-NLP/gte-Qwen2-7B-instruct \\\n",
24
    "    --port 30010 --host 0.0.0.0 --is-embedding\n",
Chayenne's avatar
Chayenne committed
25
26
27
    "```\n",
    "\n",
    "Remember to add `--is-embedding` to the command."
Chayenne's avatar
Chayenne committed
28
29
30
31
   ]
  },
  {
   "cell_type": "code",
32
   "execution_count": 1,
Chayenne's avatar
Chayenne committed
33
34
35
36
37
38
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
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
      "/home/chenyang/miniconda3/envs/AlphaMeemory/lib/python3.11/site-packages/transformers/utils/hub.py:128: FutureWarning: Using `TRANSFORMERS_CACHE` is deprecated and will be removed in v5 of Transformers. Use `HF_HOME` instead.\n",
      "  warnings.warn(\n",
      "[2024-10-29 21:07:15] server_args=ServerArgs(model_path='Alibaba-NLP/gte-Qwen2-7B-instruct', tokenizer_path='Alibaba-NLP/gte-Qwen2-7B-instruct', tokenizer_mode='auto', skip_tokenizer_init=False, load_format='auto', trust_remote_code=False, dtype='auto', kv_cache_dtype='auto', quantization=None, context_length=None, device='cuda', served_model_name='Alibaba-NLP/gte-Qwen2-7B-instruct', chat_template=None, is_embedding=True, host='0.0.0.0', port=30010, mem_fraction_static=0.88, max_running_requests=None, max_total_tokens=None, chunked_prefill_size=8192, max_prefill_tokens=16384, schedule_policy='lpm', schedule_conservativeness=1.0, tp_size=1, stream_interval=1, random_seed=568040040, constrained_json_whitespace_pattern=None, log_level='info', log_level_http=None, log_requests=False, show_time_cost=False, api_key=None, file_storage_pth='SGLang_storage', enable_cache_report=False, watchdog_timeout=600, dp_size=1, load_balance_method='round_robin', dist_init_addr=None, nnodes=1, node_rank=0, json_model_override_args='{}', enable_double_sparsity=False, ds_channel_config_path=None, ds_heavy_channel_num=32, ds_heavy_token_num=256, ds_heavy_channel_type='qk', ds_sparse_decode_threshold=4096, lora_paths=None, max_loras_per_batch=8, attention_backend='flashinfer', sampling_backend='flashinfer', grammar_backend='outlines', disable_flashinfer=False, disable_flashinfer_sampling=False, disable_radix_cache=False, disable_regex_jump_forward=False, disable_cuda_graph=False, disable_cuda_graph_padding=False, disable_disk_cache=False, disable_custom_all_reduce=False, disable_mla=False, disable_penalizer=False, disable_nan_detection=False, enable_overlap_schedule=False, enable_mixed_chunk=False, enable_torch_compile=False, torch_compile_max_bs=32, cuda_graph_max_bs=160, torchao_config='', enable_p2p_check=False, triton_attention_reduce_in_fp32=False, num_continuous_decode_steps=1)\n",
      "/home/chenyang/miniconda3/envs/AlphaMeemory/lib/python3.11/site-packages/transformers/utils/hub.py:128: FutureWarning: Using `TRANSFORMERS_CACHE` is deprecated and will be removed in v5 of Transformers. Use `HF_HOME` instead.\n",
      "  warnings.warn(\n",
      "/home/chenyang/miniconda3/envs/AlphaMeemory/lib/python3.11/site-packages/transformers/utils/hub.py:128: FutureWarning: Using `TRANSFORMERS_CACHE` is deprecated and will be removed in v5 of Transformers. Use `HF_HOME` instead.\n",
      "  warnings.warn(\n",
      "[2024-10-29 21:07:20 TP0] Init torch distributed begin.\n",
      "[2024-10-29 21:07:20 TP0] Load weight begin. avail mem=47.27 GB\n",
      "[2024-10-29 21:07:21 TP0] lm_eval is not installed, GPTQ may not be usable\n",
      "INFO 10-29 21:07:22 weight_utils.py:243] Using model weights format ['*.safetensors']\n",
      "Loading safetensors checkpoint shards:   0% Completed | 0/7 [00:00<?, ?it/s]\n",
      "Loading safetensors checkpoint shards:  14% Completed | 1/7 [00:00<00:03,  1.65it/s]\n",
      "Loading safetensors checkpoint shards:  29% Completed | 2/7 [00:01<00:04,  1.02it/s]\n",
      "Loading safetensors checkpoint shards:  43% Completed | 3/7 [00:03<00:04,  1.24s/it]\n",
      "Loading safetensors checkpoint shards:  57% Completed | 4/7 [00:05<00:04,  1.47s/it]\n",
      "Loading safetensors checkpoint shards:  71% Completed | 5/7 [00:07<00:03,  1.62s/it]\n",
      "Loading safetensors checkpoint shards:  86% Completed | 6/7 [00:08<00:01,  1.64s/it]\n",
      "Loading safetensors checkpoint shards: 100% Completed | 7/7 [00:10<00:00,  1.63s/it]\n",
      "Loading safetensors checkpoint shards: 100% Completed | 7/7 [00:10<00:00,  1.49s/it]\n",
      "\n",
      "[2024-10-29 21:07:32 TP0] Load weight end. type=Qwen2ForCausalLM, dtype=torch.float16, avail mem=32.91 GB\n",
      "[2024-10-29 21:07:33 TP0] Memory pool end. avail mem=4.56 GB\n",
      "[2024-10-29 21:07:33 TP0] max_total_num_tokens=509971, max_prefill_tokens=16384, max_running_requests=2049, context_len=131072\n",
      "[2024-10-29 21:07:33] INFO:     Started server process [2650986]\n",
      "[2024-10-29 21:07:33] INFO:     Waiting for application startup.\n",
      "[2024-10-29 21:07:33] INFO:     Application startup complete.\n",
      "[2024-10-29 21:07:33] INFO:     Uvicorn running on http://0.0.0.0:30010 (Press CTRL+C to quit)\n",
      "[2024-10-29 21:07:34] INFO:     127.0.0.1:47812 - \"GET /v1/models HTTP/1.1\" 200 OK\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<strong style='color: #00008B;'><br>                            This cell combines server and notebook output. <br>                            <br>                            Typically, the server runs in a separate terminal, <br>                            but we combine the output of server and notebook to demonstrate the usage better.<br>                            <br>                            In our documentation, server output is in gray, notebook output is highlighted.<br>                            </strong>"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2024-10-29 21:07:34] INFO:     127.0.0.1:41780 - \"GET /get_model_info HTTP/1.1\" 200 OK\n",
      "[2024-10-29 21:07:34 TP0] Prefill batch. #new-seq: 1, #new-token: 6, #cached-token: 0, cache hit rate: 0.00%, token usage: 0.00, #running-req: 0, #queue-req: 0\n",
      "[2024-10-29 21:07:35] INFO:     127.0.0.1:41792 - \"POST /encode HTTP/1.1\" 200 OK\n",
      "[2024-10-29 21:07:35] The server is fired up and ready to roll!\n"
Chayenne's avatar
Chayenne committed
90
91
92
93
     ]
    }
   ],
   "source": [
94
95
96
97
98
99
    "from sglang.utils import (\n",
    "    execute_shell_command,\n",
    "    wait_for_server,\n",
    "    terminate_process,\n",
    "    print_highlight,\n",
    ")\n",
100
    "\n",
Chayenne's avatar
Chayenne committed
101
102
    "embedding_process = execute_shell_command(\n",
    "    \"\"\"\n",
103
    "python -m sglang.launch_server --model-path Alibaba-NLP/gte-Qwen2-7B-instruct \\\n",
104
    "    --port 30010 --host 0.0.0.0 --is-embedding\n",
Chayenne's avatar
Chayenne committed
105
106
    "\"\"\"\n",
    ")\n",
Chayenne's avatar
Chayenne committed
107
    "\n",
108
    "wait_for_server(\"http://localhost:30010\")"
Chayenne's avatar
Chayenne committed
109
110
111
112
113
114
115
116
117
118
119
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Use Curl"
   ]
  },
  {
   "cell_type": "code",
120
   "execution_count": 2,
Chayenne's avatar
Chayenne committed
121
122
123
124
125
126
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
127
      "[2024-10-28 02:10:30 TP0] Prefill batch. #new-seq: 1, #new-token: 4, #cached-token: 0, cache hit rate: 0.00%, token usage: 0.00, #running-req: 0, #queue-req: 0\n"
Chayenne's avatar
Chayenne committed
128
     ]
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2024-10-28 02:10:31] INFO:     127.0.0.1:48094 - \"POST /v1/embeddings HTTP/1.1\" 200 OK\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<strong style='color: #00008B;'>Text embedding (first 10): [0.0083160400390625, 0.0006804466247558594, -0.00809478759765625, -0.0006995201110839844, 0.0143890380859375, -0.0090179443359375, 0.01238250732421875, 0.00209808349609375, 0.0062103271484375, -0.003047943115234375]</strong>"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
Chayenne's avatar
Chayenne committed
148
149
150
    }
   ],
   "source": [
Chayenne's avatar
Chayenne committed
151
152
153
    "import subprocess, json\n",
    "\n",
    "text = \"Once upon a time\"\n",
Chayenne's avatar
Chayenne committed
154
    "\n",
Chayenne's avatar
Chayenne committed
155
    "curl_text = f\"\"\"curl -s http://localhost:30010/v1/embeddings \\\n",
Chayenne's avatar
Chayenne committed
156
157
    "  -H \"Content-Type: application/json\" \\\n",
    "  -H \"Authorization: Bearer None\" \\\n",
Chayenne's avatar
Chayenne committed
158
159
160
161
162
163
    "  -d '{{\"model\": \"Alibaba-NLP/gte-Qwen2-7B-instruct\", \"input\": \"{text}\"}}'\"\"\"\n",
    "\n",
    "text_embedding = json.loads(subprocess.check_output(curl_text, shell=True))[\"data\"][0][\n",
    "    \"embedding\"\n",
    "]\n",
    "\n",
164
    "print_highlight(f\"Text embedding (first 10): {text_embedding[:10]}\")"
Chayenne's avatar
Chayenne committed
165
166
167
168
169
170
171
172
173
174
175
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Using OpenAI Compatible API"
   ]
  },
  {
   "cell_type": "code",
176
   "execution_count": 3,
Chayenne's avatar
Chayenne committed
177
178
179
180
181
182
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
183
184
185
186
187
188
      "[2024-10-28 02:10:31] INFO:     127.0.0.1:48110 - \"GET /get_model_info HTTP/1.1\" 200 OK\n",
      "[2024-10-28 02:10:31 TP0] Prefill batch. #new-seq: 1, #new-token: 6, #cached-token: 0, cache hit rate: 0.00%, token usage: 0.00, #running-req: 0, #queue-req: 0\n",
      "[2024-10-28 02:10:31] INFO:     127.0.0.1:48114 - \"POST /encode HTTP/1.1\" 200 OK\n",
      "[2024-10-28 02:10:31] The server is fired up and ready to roll!\n",
      "[2024-10-28 02:10:31 TP0] Prefill batch. #new-seq: 1, #new-token: 1, #cached-token: 3, cache hit rate: 21.43%, token usage: 0.00, #running-req: 0, #queue-req: 0\n",
      "[2024-10-28 02:10:31] INFO:     127.0.0.1:48118 - \"POST /v1/embeddings HTTP/1.1\" 200 OK\n"
Chayenne's avatar
Chayenne committed
189
     ]
190
191
192
193
194
195
196
197
198
199
200
201
    },
    {
     "data": {
      "text/html": [
       "<strong style='color: #00008B;'>Text embedding (first 10): [0.00829315185546875, 0.0007004737854003906, -0.00809478759765625, -0.0006799697875976562, 0.01438140869140625, -0.00897979736328125, 0.0123748779296875, 0.0020923614501953125, 0.006195068359375, -0.0030498504638671875]</strong>"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
Chayenne's avatar
Chayenne committed
202
203
204
205
206
    }
   ],
   "source": [
    "import openai\n",
    "\n",
Chayenne's avatar
Chayenne committed
207
    "client = openai.Client(base_url=\"http://127.0.0.1:30010/v1\", api_key=\"None\")\n",
Chayenne's avatar
Chayenne committed
208
209
210
211
    "\n",
    "# Text embedding example\n",
    "response = client.embeddings.create(\n",
    "    model=\"Alibaba-NLP/gte-Qwen2-7B-instruct\",\n",
Chayenne's avatar
Chayenne committed
212
    "    input=text,\n",
Chayenne's avatar
Chayenne committed
213
214
215
    ")\n",
    "\n",
    "embedding = response.data[0].embedding[:10]\n",
216
    "print_highlight(f\"Text embedding (first 10): {embedding}\")"
Chayenne's avatar
Chayenne committed
217
218
219
220
221
222
223
224
225
226
227
228
229
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Using Input IDs\n",
    "\n",
    "SGLang also supports `input_ids` as input to get the embedding."
   ]
  },
  {
   "cell_type": "code",
230
   "execution_count": 4,
Chayenne's avatar
Chayenne committed
231
232
   "metadata": {},
   "outputs": [
233
234
235
236
237
238
239
240
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/chenyang/miniconda3/envs/AlphaMeemory/lib/python3.11/site-packages/transformers/utils/hub.py:127: FutureWarning: Using `TRANSFORMERS_CACHE` is deprecated and will be removed in v5 of Transformers. Use `HF_HOME` instead.\n",
      "  warnings.warn(\n"
     ]
    },
Chayenne's avatar
Chayenne committed
241
242
243
244
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
245
246
      "[2024-10-28 02:10:32 TP0] Prefill batch. #new-seq: 1, #new-token: 1, #cached-token: 3, cache hit rate: 33.33%, token usage: 0.00, #running-req: 0, #queue-req: 0\n",
      "[2024-10-28 02:10:32] INFO:     127.0.0.1:48124 - \"POST /v1/embeddings HTTP/1.1\" 200 OK\n"
Chayenne's avatar
Chayenne committed
247
     ]
248
249
250
251
252
253
254
255
256
257
258
259
    },
    {
     "data": {
      "text/html": [
       "<strong style='color: #00008B;'>Input IDs embedding (first 10): [0.00829315185546875, 0.0007004737854003906, -0.00809478759765625, -0.0006799697875976562, 0.01438140869140625, -0.00897979736328125, 0.0123748779296875, 0.0020923614501953125, 0.006195068359375, -0.0030498504638671875]</strong>"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
Chayenne's avatar
Chayenne committed
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
    }
   ],
   "source": [
    "import json\n",
    "import os\n",
    "from transformers import AutoTokenizer\n",
    "\n",
    "os.environ[\"TOKENIZERS_PARALLELISM\"] = \"false\"\n",
    "\n",
    "tokenizer = AutoTokenizer.from_pretrained(\"Alibaba-NLP/gte-Qwen2-7B-instruct\")\n",
    "input_ids = tokenizer.encode(text)\n",
    "\n",
    "curl_ids = f\"\"\"curl -s http://localhost:30010/v1/embeddings \\\n",
    "  -H \"Content-Type: application/json\" \\\n",
    "  -H \"Authorization: Bearer None\" \\\n",
    "  -d '{{\"model\": \"Alibaba-NLP/gte-Qwen2-7B-instruct\", \"input\": {json.dumps(input_ids)}}}'\"\"\"\n",
    "\n",
    "input_ids_embedding = json.loads(subprocess.check_output(curl_ids, shell=True))[\"data\"][\n",
    "    0\n",
    "][\"embedding\"]\n",
    "\n",
281
    "print_highlight(f\"Input IDs embedding (first 10): {input_ids_embedding[:10]}\")"
Chayenne's avatar
Chayenne committed
282
   ]
283
284
285
  },
  {
   "cell_type": "code",
286
   "execution_count": 5,
287
   "metadata": {},
288
289
290
291
292
293
294
295
296
297
298
299
300
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2024-10-28 02:10:32] INFO:     Shutting down\n",
      "[2024-10-28 02:10:32] INFO:     Waiting for application shutdown.\n",
      "[2024-10-28 02:10:32] INFO:     Application shutdown complete.\n",
      "[2024-10-28 02:10:32] INFO:     Finished server process [1188896]\n",
      "W1028 02:10:32.490000 140389363193408 torch/_inductor/compile_worker/subproc_pool.py:126] SubprocPool unclean exit\n"
     ]
    }
   ],
301
302
303
   "source": [
    "terminate_process(embedding_process)"
   ]
Chayenne's avatar
Chayenne committed
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "AlphaMeemory",
   "language": "python",
   "name": "python3"
  },
  "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.11.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}