RangiLyu commited on
Commit
2e9cfbf
·
verified ·
1 Parent(s): 22acd83

Create README.md

Browse files
Files changed (1) hide show
  1. README.md +423 -3
README.md CHANGED
@@ -1,3 +1,423 @@
1
- ---
2
- license: apache-2.0
3
- ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: apache-2.0
3
+ pipeline_tag: image-text-to-text
4
+ ---
5
+
6
+
7
+ ## Intern-S1-mini
8
+
9
+ <div align="center">
10
+ <img src="https://cdn-uploads.huggingface.co/production/uploads/642695e5274e7ad464c8a5ba/E43cgEXBRWjVJlU_-hdh6.png" />
11
+
12
+ <div>&nbsp;</div>
13
+
14
+ [💻Github Repo](https://github.com/InternLM/Intern-S1) • [🤗Model Collections](https://huggingface.co/collections/internlm/intern-s1-6882e325e8ac1c58ba108aa5) • [📜Technical Report (coming soon)]() • [💬Online Chat](https://chat.intern-ai.org.cn/)
15
+
16
+ </div>
17
+
18
+ <p align="center">
19
+ 👋 join us on <a href="https://discord.gg/xa29JuW87d" target="_blank">Discord</a> and <a href="https://cdn.vansin.top/intern-s1.jpg" target="_blank">WeChat</a>
20
+ </p>
21
+
22
+
23
+
24
+ ## Introduction
25
+
26
+ We introduce **Intern-S1-mini**, a lightweight open-source multimodal reasoning model based on the same techniques as **[Intern-S1](https://huggingface.co/internlm/Intern-S1)**.
27
+ Built upon a 8B dense language model (Qwen3) and a 400M Vision encoder (InternViT), Intern-S1-mini has been further pretrained on **5 trillion tokens** of multimodal data, including over **2.5 trillion scientific-domain tokens**. This enables the model to retain strong general capabilities while excelling in specialized scientific domains such as **interpreting chemical structures, understanding protein sequences, and planning compound synthesis routes**, making Intern-S1-mini to be a capable research assistant for real-world scientific applications.
28
+
29
+ ## Features
30
+
31
+ - Strong performance across language and vision reasoning benchmarks, especially scientific tasks.
32
+
33
+ - Continuously pretrained on a massive 5T token dataset, with over 50% specialized scientific data, embedding deep domain expertise.
34
+
35
+ - Dynamic tokenizer enables native understanding of molecular formulas, protein sequences, and seismic signals.
36
+
37
+ ## Performance
38
+
39
+ We evaluate the Intern-S1-mini on various benchmarks including general datasets and scientifc datasets. We report the performance comparsion with the recent VLMs and LLMs below.
40
+
41
+
42
+ > **Note**: ✅ means the best performance among open-sourced models, 👑 indicates the best performance among all models.
43
+
44
+ We use the [OpenCompass](https://github.com/open-compass/OpenCompass/) and [VLMEvalkit](https://github.com/open-compass/vlmevalkit) to evaluate all models.
45
+
46
+
47
+ ## Quick Start
48
+
49
+ ### Sampling Parameters
50
+
51
+ We recommend using the following hyperparameters to ensure better results
52
+
53
+ ```python
54
+ top_p = 1.0
55
+ top_k = 50
56
+ min_p = 0.0
57
+ temperature = 0.8
58
+ ```
59
+
60
+ ### Transformers
61
+
62
+ The following provides demo code illustrating how to generate based on text and multimodal inputs.
63
+
64
+ > **Please use transformers>=4.53.0 to ensure the model works normally.**
65
+
66
+ #### Text input
67
+
68
+ ```python
69
+ from transformers import AutoProcessor, AutoModelForCausalLM
70
+ import torch
71
+
72
+ model_name = "internlm/Intern-S1-mini"
73
+ processor = AutoProcessor.from_pretrained(model_name, trust_remote_code=True)
74
+ model = AutoModelForCausalLM.from_pretrained(model_name, device_map="auto", torch_dtype="auto", trust_remote_code=True)
75
+
76
+ messages = [
77
+ {
78
+ "role": "user",
79
+ "content": [
80
+ {"type": "text", "text": "tell me about an interesting physical phenomenon."},
81
+ ],
82
+ }
83
+ ]
84
+
85
+ inputs = processor.apply_chat_template(messages, add_generation_prompt=True, tokenize=True, return_dict=True, return_tensors="pt").to(model.device, dtype=torch.bfloat16)
86
+
87
+ generate_ids = model.generate(**inputs, max_new_tokens=32768)
88
+ decoded_output = processor.decode(generate_ids[0, inputs["input_ids"].shape[1] :], skip_special_tokens=True)
89
+ print(decoded_output)
90
+ ```
91
+
92
+ #### Image input
93
+
94
+ ```python
95
+ from transformers import AutoProcessor, AutoModelForCausalLM
96
+ import torch
97
+
98
+ model_name = "internlm/Intern-S1-mini"
99
+ processor = AutoProcessor.from_pretrained(model_name, trust_remote_code=True)
100
+ model = AutoModelForCausalLM.from_pretrained(model_name, device_map="auto", torch_dtype="auto", trust_remote_code=True)
101
+
102
+ messages = [
103
+ {
104
+ "role": "user",
105
+ "content": [
106
+ {"type": "image", "url": "http://images.cocodataset.org/val2017/000000039769.jpg"},
107
+ {"type": "text", "text": "Please describe the image explicitly."},
108
+ ],
109
+ }
110
+ ]
111
+
112
+ inputs = processor.apply_chat_template(messages, add_generation_prompt=True, tokenize=True, return_dict=True, return_tensors="pt").to(model.device, dtype=torch.bfloat16)
113
+
114
+ generate_ids = model.generate(**inputs, max_new_tokens=32768)
115
+ decoded_output = processor.decode(generate_ids[0, inputs["input_ids"].shape[1] :], skip_special_tokens=True)
116
+ print(decoded_output)
117
+ ```
118
+
119
+ #### Video input
120
+
121
+ Please ensure that the decord video decoding library is installed via `pip install decord`.
122
+
123
+ ```python
124
+ from transformers import AutoProcessor, AutoModelForCausalLM
125
+ import torch
126
+
127
+ model_name = "internlm/Intern-S1-mini"
128
+ processor = AutoProcessor.from_pretrained(model_name, trust_remote_code=True)
129
+ model = AutoModelForCausalLM.from_pretrained(model_name, device_map="auto", torch_dtype="auto", trust_remote_code=True)
130
+
131
+ messages = [
132
+ {
133
+ "role": "user",
134
+ "content": [
135
+ {
136
+ "type": "video",
137
+ "url": "https://huggingface.co/datasets/hf-internal-testing/fixtures_videos/resolve/main/tennis.mp4",
138
+ },
139
+ {"type": "text", "text": "What type of shot is the man performing?"},
140
+ ],
141
+ }
142
+ ]
143
+
144
+ inputs = processor.apply_chat_template(
145
+ messages,
146
+ return_tensors="pt",
147
+ add_generation_prompt=True,
148
+ video_load_backend="decord",
149
+ tokenize=True,
150
+ return_dict=True,
151
+ ).to(model.device, dtype=torch.float16)
152
+
153
+ generate_ids = model.generate(**inputs, max_new_tokens=32768)
154
+ decoded_output = processor.decode(generate_ids[0, inputs["input_ids"].shape[1] :], skip_special_tokens=True)
155
+ print(decoded_output)
156
+ ```
157
+
158
+ ### Serving
159
+
160
+ The minimum hardware requirements for deploying Intern-S1 series models are:
161
+
162
+ | Model | A100(GPUs) | H800(GPUs) | H100(GPUs) | H200(GPUs) |
163
+ | :---------------------------------------------------------------------: | :--------: | :--------: | :--------: | :--------: |
164
+ | [internlm/Intern-S1-mini](https://huggingface.co/internlm/Intern-S1-mini) | 1 | 1 | 1 | 1 |
165
+ | [internlm/Intern-S1-mini-FP8](https://huggingface.co/internlm/Intern-S1-mini-FP8) | - | 1 | 1 | 1 |
166
+
167
+ You can utilize one of the following LLM inference frameworks to create an OpenAI compatible server:
168
+
169
+ #### [lmdeploy(>=0.9.2)](https://github.com/InternLM/lmdeploy)
170
+
171
+ ```bash
172
+ lmdeploy serve api_server internlm/Intern-S1-mini --reasoning-parser intern-s1 --tool-call-parser intern-s1
173
+ ```
174
+
175
+ #### [vllm](https://github.com/vllm-project/vllm)
176
+
177
+ ```bash
178
+ vllm serve internlm/Intern-S1-mini --trust-remote-code
179
+ ```
180
+
181
+ #### [sglang](https://github.com/sgl-project/sglang)
182
+
183
+ ```bash
184
+ python3 -m sglang.launch_server \
185
+ --model-path internlm/Intern-S1-mini \
186
+ --trust-remote-code \
187
+ --grammar-backend none
188
+ ```
189
+
190
+ #### ollama for local deployment:
191
+
192
+ ```bash
193
+ # install ollama
194
+ curl -fsSL https://ollama.com/install.sh | sh
195
+ # fetch model
196
+ ollama pull internlm/interns1-mini
197
+ # run model
198
+ ollama run internlm/interns1-mini
199
+ # then use openai client to call on http://localhost:11434/v1
200
+ ```
201
+
202
+ ## Advanced Usage
203
+
204
+ ### Tool Calling
205
+
206
+ Many Large Language Models (LLMs) now feature **Tool Calling**, a powerful capability that allows them to extend their functionality by interacting with external tools and APIs. This enables models to perform tasks like fetching up-to-the-minute information, running code, or calling functions within other applications.
207
+
208
+ A key advantage for developers is that a growing number of open-source LLMs are designed to be compatible with the OpenAI API. This means you can leverage the same familiar syntax and structure from the OpenAI library to implement tool calling with these open-source models. As a result, the code demonstrated in this tutorial is versatile—it works not just with OpenAI models, but with any model that follows the same interface standard.
209
+
210
+ To illustrate how this works, let's dive into a practical code example that uses tool calling to get the latest weather forecast (based on lmdeploy api server).
211
+
212
+ ```python
213
+
214
+ from openai import OpenAI
215
+ import json
216
+
217
+
218
+ def get_current_temperature(location: str, unit: str = "celsius"):
219
+ """Get current temperature at a location.
220
+
221
+ Args:
222
+ location: The location to get the temperature for, in the format "City, State, Country".
223
+ unit: The unit to return the temperature in. Defaults to "celsius". (choices: ["celsius", "fahrenheit"])
224
+
225
+ Returns:
226
+ the temperature, the location, and the unit in a dict
227
+ """
228
+ return {
229
+ "temperature": 26.1,
230
+ "location": location,
231
+ "unit": unit,
232
+ }
233
+
234
+
235
+ def get_temperature_date(location: str, date: str, unit: str = "celsius"):
236
+ """Get temperature at a location and date.
237
+
238
+ Args:
239
+ location: The location to get the temperature for, in the format "City, State, Country".
240
+ date: The date to get the temperature for, in the format "Year-Month-Day".
241
+ unit: The unit to return the temperature in. Defaults to "celsius". (choices: ["celsius", "fahrenheit"])
242
+
243
+ Returns:
244
+ the temperature, the location, the date and the unit in a dict
245
+ """
246
+ return {
247
+ "temperature": 25.9,
248
+ "location": location,
249
+ "date": date,
250
+ "unit": unit,
251
+ }
252
+
253
+ def get_function_by_name(name):
254
+ if name == "get_current_temperature":
255
+ return get_current_temperature
256
+ if name == "get_temperature_date":
257
+ return get_temperature_date
258
+
259
+ tools = [{
260
+ 'type': 'function',
261
+ 'function': {
262
+ 'name': 'get_current_temperature',
263
+ 'description': 'Get current temperature at a location.',
264
+ 'parameters': {
265
+ 'type': 'object',
266
+ 'properties': {
267
+ 'location': {
268
+ 'type': 'string',
269
+ 'description': 'The location to get the temperature for, in the format \'City, State, Country\'.'
270
+ },
271
+ 'unit': {
272
+ 'type': 'string',
273
+ 'enum': [
274
+ 'celsius',
275
+ 'fahrenheit'
276
+ ],
277
+ 'description': 'The unit to return the temperature in. Defaults to \'celsius\'.'
278
+ }
279
+ },
280
+ 'required': [
281
+ 'location'
282
+ ]
283
+ }
284
+ }
285
+ }, {
286
+ 'type': 'function',
287
+ 'function': {
288
+ 'name': 'get_temperature_date',
289
+ 'description': 'Get temperature at a location and date.',
290
+ 'parameters': {
291
+ 'type': 'object',
292
+ 'properties': {
293
+ 'location': {
294
+ 'type': 'string',
295
+ 'description': 'The location to get the temperature for, in the format \'City, State, Country\'.'
296
+ },
297
+ 'date': {
298
+ 'type': 'string',
299
+ 'description': 'The date to get the temperature for, in the format \'Year-Month-Day\'.'
300
+ },
301
+ 'unit': {
302
+ 'type': 'string',
303
+ 'enum': [
304
+ 'celsius',
305
+ 'fahrenheit'
306
+ ],
307
+ 'description': 'The unit to return the temperature in. Defaults to \'celsius\'.'
308
+ }
309
+ },
310
+ 'required': [
311
+ 'location',
312
+ 'date'
313
+ ]
314
+ }
315
+ }
316
+ }]
317
+
318
+
319
+
320
+ messages = [
321
+ {'role': 'user', 'content': 'Today is 2024-11-14, What\'s the temperature in San Francisco now? How about tomorrow?'}
322
+ ]
323
+
324
+ openai_api_key = "EMPTY"
325
+ openai_api_base = "http://0.0.0.0:23333/v1"
326
+ client = OpenAI(
327
+ api_key=openai_api_key,
328
+ base_url=openai_api_base,
329
+ )
330
+ model_name = client.models.list().data[0].id
331
+ response = client.chat.completions.create(
332
+ model=model_name,
333
+ messages=messages,
334
+ max_tokens=32768,
335
+ temperature=0.8,
336
+ top_p=0.8,
337
+ stream=False,
338
+ extra_body=dict(spaces_between_special_tokens=False, enable_thinking=False),
339
+ tools=tools)
340
+ print(response.choices[0].message)
341
+ messages.append(response.choices[0].message)
342
+
343
+ for tool_call in response.choices[0].message.tool_calls:
344
+ tool_call_args = json.loads(tool_call.function.arguments)
345
+ tool_call_result = get_function_by_name(tool_call.function.name)(**tool_call_args)
346
+ tool_call_result = json.dumps(tool_call_result, ensure_ascii=False)
347
+ messages.append({
348
+ 'role': 'tool',
349
+ 'name': tool_call.function.name,
350
+ 'content': tool_call_result,
351
+ 'tool_call_id': tool_call.id
352
+ })
353
+
354
+ response = client.chat.completions.create(
355
+ model=model_name,
356
+ messages=messages,
357
+ temperature=0.8,
358
+ top_p=0.8,
359
+ stream=False,
360
+ extra_body=dict(spaces_between_special_tokens=False, enable_thinking=False),
361
+ tools=tools)
362
+ print(response.choices[0].message.content)
363
+ ```
364
+
365
+ ### Switching Between Thinking and Non-Thinking Modes
366
+
367
+ Intern-S1 enables thinking mode by default, enhancing the model's reasoning capabilities to generate higher-quality responses. This feature can be disabled by setting `enable_thinking=False` in `tokenizer.apply_chat_template`
368
+
369
+ ```python
370
+ text = tokenizer.apply_chat_template(
371
+ messages,
372
+ tokenize=False,
373
+ add_generation_prompt=True,
374
+ enable_thinking=False # think mode indicator
375
+ )
376
+ ```
377
+
378
+ With LMDeploy serving Intern-S1 models, you can dynamically control the thinking mode by adjusting the `enable_thinking` parameter in your requests.
379
+
380
+ ```python
381
+ from openai import OpenAI
382
+ import json
383
+
384
+ messages = [
385
+ {
386
+ 'role': 'user',
387
+ 'content': 'who are you'
388
+ }, {
389
+ 'role': 'assistant',
390
+ 'content': 'I am an AI'
391
+ }, {
392
+ 'role': 'user',
393
+ 'content': 'AGI is?'
394
+ }]
395
+
396
+ openai_api_key = "EMPTY"
397
+ openai_api_base = "http://0.0.0.0:23333/v1"
398
+ client = OpenAI(
399
+ api_key=openai_api_key,
400
+ base_url=openai_api_base,
401
+ )
402
+ model_name = client.models.list().data[0].id
403
+
404
+ response = client.chat.completions.create(
405
+ model=model_name,
406
+ messages=messages,
407
+ temperature=0.8,
408
+ top_p=0.8,
409
+ max_tokens=2048,
410
+ extra_body={
411
+ "enable_thinking": False,
412
+ }
413
+ )
414
+ print(json.dumps(response.model_dump(), indent=2, ensure_ascii=False))
415
+ ```
416
+
417
+ For vllm and sglang users, configure this through,
418
+
419
+ ```python
420
+ extra_body={
421
+ "chat_template_kwargs": {"enable_thinking": False}
422
+ }
423
+ ```