Mirror worker 2
Browse files- assets/dsv4_performance.png +3 -0
- encoding/tests/test_output_3.txt +38 -0
- inference/generate.py +155 -0
- model-00002-of-00046.safetensors +3 -0
- model-00010-of-00046.safetensors +3 -0
- model-00018-of-00046.safetensors +3 -0
- model-00026-of-00046.safetensors +3 -0
- model-00034-of-00046.safetensors +3 -0
- model-00042-of-00046.safetensors +3 -0
assets/dsv4_performance.png
ADDED
|
Git LFS Details
|
encoding/tests/test_output_3.txt
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<|begin▁of▁sentence|>该助手为DeepSeek,由深度求索公司创造。<|latest_reminder|>2026-02-21,星期六,广州,App,中文<|User|>小柴胡冲剂和布洛芬能一起吃吗?
|
| 2 |
+
|
| 3 |
+
CITATION FORMAT: 【{cursor_id}†L{start_line_id}(-L{end_line_id})?】
|
| 4 |
+
|
| 5 |
+
## Tools
|
| 6 |
+
|
| 7 |
+
You have access to a set of tools to help answer the user's question. You can invoke tools by writing a "<|DSML|tool_calls>" block like the following:
|
| 8 |
+
|
| 9 |
+
<|DSML|tool_calls>
|
| 10 |
+
<|DSML|invoke name="$TOOL_NAME">
|
| 11 |
+
<|DSML|parameter name="$PARAMETER_NAME" string="true|false">$PARAMETER_VALUE</|DSML|parameter>
|
| 12 |
+
...
|
| 13 |
+
</|DSML|invoke>
|
| 14 |
+
<|DSML|invoke name="$TOOL_NAME2">
|
| 15 |
+
...
|
| 16 |
+
</|DSML|invoke>
|
| 17 |
+
</|DSML|tool_calls>
|
| 18 |
+
|
| 19 |
+
String parameters should be specified as is and set `string="true"`. For all other types (numbers, booleans, arrays, objects), pass the value in JSON format and set `string="false"`.
|
| 20 |
+
|
| 21 |
+
If thinking_mode is enabled (triggered by <think>), you MUST output your complete reasoning inside <think>...</think> BEFORE any tool calls or final response.
|
| 22 |
+
|
| 23 |
+
Otherwise, output directly after </think> with tool calls or final response.
|
| 24 |
+
|
| 25 |
+
### Available Tool Schemas
|
| 26 |
+
|
| 27 |
+
{"name": "search", "description": "Web search. Split multiple queries with '||'.", "parameters": {"type": "object", "properties": {"queries": {"type": "string", "description": "query1||query2"}}, "required": ["queries"], "additionalProperties": false, "$schema": "http://json-schema.org/draft-07/schema#"}}
|
| 28 |
+
{"name": "open", "description": "Batch open IDs (format 【{id}†...】) or URLs.", "parameters": {"type": "object", "properties": {"open_list": {"type": "array", "items": {"type": "object", "properties": {"id": {"description": "ID or URL", "anyOf": [{"type": "integer"}, {"type": "string"}], "default": -1}, "cursor": {"type": "integer", "description": "", "default": -1}, "loc": {"type": "integer", "description": "Start line", "default": -1}, "num_lines": {"type": "integer", "description": "", "default": -1}, "view_source": {"type": "boolean", "description": "", "default": false}}, "additionalProperties": false}, "description": ""}}, "required": ["open_list"], "additionalProperties": false, "$schema": "http://json-schema.org/draft-07/schema#"}}
|
| 29 |
+
{"name": "find", "description": "Find exact text pattern in pages.", "parameters": {"type": "object", "properties": {"find_list": {"type": "array", "items": {"type": "object", "properties": {"pattern": {"type": "string", "description": ""}, "cursor": {"type": "integer", "description": "", "default": -1}}, "required": ["pattern"], "additionalProperties": false}, "description": ""}}, "required": ["find_list"], "additionalProperties": false, "$schema": "http://json-schema.org/draft-07/schema#"}}
|
| 30 |
+
|
| 31 |
+
You MUST strictly follow the above defined tool name and parameter schemas to invoke tool calls.
|
| 32 |
+
<|Assistant|><think>用户想知道小柴胡冲剂和布洛芬能否一起服用。</think>
|
| 33 |
+
|
| 34 |
+
<|DSML|tool_calls>
|
| 35 |
+
<|DSML|invoke name="search">
|
| 36 |
+
<|DSML|parameter name="queries" string="true">小柴胡冲剂 布洛芬 相互作用 一起吃</|DSML|parameter>
|
| 37 |
+
</|DSML|invoke>
|
| 38 |
+
</|DSML|tool_calls><|end▁of▁sentence|><|User|><tool_result>[0]</tool_result><|Assistant|><think>现在开始组织回答。</think>请及时就医。<|end▁of▁sentence|>
|
inference/generate.py
ADDED
|
@@ -0,0 +1,155 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import json
|
| 3 |
+
import sys
|
| 4 |
+
from argparse import ArgumentParser
|
| 5 |
+
from typing import List
|
| 6 |
+
|
| 7 |
+
import torch
|
| 8 |
+
import torch.distributed as dist
|
| 9 |
+
from transformers import AutoTokenizer
|
| 10 |
+
from safetensors.torch import load_model
|
| 11 |
+
|
| 12 |
+
from model import Transformer, ModelArgs
|
| 13 |
+
current_dir = os.path.dirname(os.path.abspath(__file__))
|
| 14 |
+
encoding_dir = os.path.join(current_dir, '../encoding')
|
| 15 |
+
sys.path.insert(0, os.path.abspath(encoding_dir))
|
| 16 |
+
from encoding_dsv4 import encode_messages, parse_message_from_completion_text
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
def sample(logits, temperature: float = 1.0):
|
| 20 |
+
"""Gumbel-max trick: equivalent to multinomial sampling but faster on GPU,
|
| 21 |
+
since it avoids the GPU-to-CPU sync in torch.multinomial."""
|
| 22 |
+
logits = logits / max(temperature, 1e-5)
|
| 23 |
+
probs = torch.softmax(logits, dim=-1, dtype=torch.float32)
|
| 24 |
+
return probs.div_(torch.empty_like(probs).exponential_(1)).argmax(dim=-1)
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
@torch.inference_mode()
|
| 28 |
+
def generate(
|
| 29 |
+
model: Transformer,
|
| 30 |
+
prompt_tokens: List[List[int]],
|
| 31 |
+
max_new_tokens: int,
|
| 32 |
+
eos_id: int,
|
| 33 |
+
temperature: float = 1.0
|
| 34 |
+
) -> List[List[int]]:
|
| 35 |
+
"""Batch generation with left-padded prompts.
|
| 36 |
+
|
| 37 |
+
The first forward pass processes [min_prompt_len:] tokens (prefill phase).
|
| 38 |
+
Subsequent passes generate one token at a time (decode phase). For positions
|
| 39 |
+
still within a prompt, the ground-truth token overrides the model's prediction.
|
| 40 |
+
"""
|
| 41 |
+
prompt_lens = [len(t) for t in prompt_tokens]
|
| 42 |
+
assert max(prompt_lens) <= model.max_seq_len, f"Prompt length exceeds model maximum sequence length (max_seq_len={model.max_seq_len})"
|
| 43 |
+
total_len = min(model.max_seq_len, max_new_tokens + max(prompt_lens))
|
| 44 |
+
tokens = torch.full((len(prompt_tokens), total_len), -1, dtype=torch.long)
|
| 45 |
+
for i, t in enumerate(prompt_tokens):
|
| 46 |
+
tokens[i, :len(t)] = torch.tensor(t, dtype=torch.long)
|
| 47 |
+
prev_pos = 0
|
| 48 |
+
finished = torch.tensor([False] * len(prompt_tokens))
|
| 49 |
+
prompt_mask = tokens != -1
|
| 50 |
+
for cur_pos in range(min(prompt_lens), total_len):
|
| 51 |
+
logits = model.forward(tokens[:, prev_pos:cur_pos], prev_pos)
|
| 52 |
+
if temperature > 0:
|
| 53 |
+
next_token = sample(logits, temperature)
|
| 54 |
+
else:
|
| 55 |
+
next_token = logits.argmax(dim=-1)
|
| 56 |
+
next_token = torch.where(prompt_mask[:, cur_pos], tokens[:, cur_pos], next_token)
|
| 57 |
+
tokens[:, cur_pos] = next_token
|
| 58 |
+
finished |= torch.logical_and(~prompt_mask[:, cur_pos], next_token == eos_id)
|
| 59 |
+
prev_pos = cur_pos
|
| 60 |
+
if finished.all():
|
| 61 |
+
break
|
| 62 |
+
completion_tokens = []
|
| 63 |
+
for i, toks in enumerate(tokens.tolist()):
|
| 64 |
+
toks = toks[prompt_lens[i]:prompt_lens[i]+max_new_tokens]
|
| 65 |
+
if eos_id in toks:
|
| 66 |
+
toks = toks[:toks.index(eos_id)]
|
| 67 |
+
toks.append(eos_id)
|
| 68 |
+
completion_tokens.append(toks)
|
| 69 |
+
return completion_tokens
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
def main(
|
| 73 |
+
ckpt_path: str,
|
| 74 |
+
config: str,
|
| 75 |
+
input_file: str = "",
|
| 76 |
+
interactive: bool = True,
|
| 77 |
+
max_new_tokens: int = 100,
|
| 78 |
+
temperature: float = 1.0,
|
| 79 |
+
) -> None:
|
| 80 |
+
world_size = int(os.getenv("WORLD_SIZE", "1"))
|
| 81 |
+
rank = int(os.getenv("RANK", "0"))
|
| 82 |
+
local_rank = int(os.getenv("LOCAL_RANK", "0"))
|
| 83 |
+
if world_size > 1:
|
| 84 |
+
dist.init_process_group("nccl")
|
| 85 |
+
global print
|
| 86 |
+
if rank != 0:
|
| 87 |
+
print = lambda *_, **__: None
|
| 88 |
+
torch.cuda.set_device(local_rank)
|
| 89 |
+
torch.cuda.memory._set_allocator_settings("expandable_segments:True")
|
| 90 |
+
torch.set_default_dtype(torch.bfloat16)
|
| 91 |
+
torch.set_num_threads(8)
|
| 92 |
+
torch.manual_seed(33377335)
|
| 93 |
+
with open(config) as f:
|
| 94 |
+
args = ModelArgs(**json.load(f))
|
| 95 |
+
if interactive:
|
| 96 |
+
args.max_batch_size = 1
|
| 97 |
+
print(args)
|
| 98 |
+
with torch.device("cuda"):
|
| 99 |
+
model = Transformer(args)
|
| 100 |
+
tokenizer = AutoTokenizer.from_pretrained(ckpt_path)
|
| 101 |
+
print("load model")
|
| 102 |
+
load_model(model, os.path.join(ckpt_path, f"model{rank}-mp{world_size}.safetensors"), strict=False)
|
| 103 |
+
torch.set_default_device("cuda")
|
| 104 |
+
print("I'm DeepSeek 👋")
|
| 105 |
+
|
| 106 |
+
if interactive:
|
| 107 |
+
messages = []
|
| 108 |
+
while True:
|
| 109 |
+
if world_size == 1:
|
| 110 |
+
prompt = input(">>> ")
|
| 111 |
+
elif rank == 0:
|
| 112 |
+
prompt = input(">>> ")
|
| 113 |
+
objects = [prompt]
|
| 114 |
+
dist.broadcast_object_list(objects, 0)
|
| 115 |
+
else:
|
| 116 |
+
objects = [None]
|
| 117 |
+
dist.broadcast_object_list(objects, 0)
|
| 118 |
+
prompt = objects[0]
|
| 119 |
+
if prompt == "/exit":
|
| 120 |
+
break
|
| 121 |
+
elif prompt == "/clear":
|
| 122 |
+
messages.clear()
|
| 123 |
+
continue
|
| 124 |
+
messages.append({"role": "user", "content": prompt})
|
| 125 |
+
prompt_tokens = tokenizer.encode(encode_messages(messages, thinking_mode="chat"))
|
| 126 |
+
completion_tokens = generate(model, [prompt_tokens], max_new_tokens, tokenizer.eos_token_id, temperature)
|
| 127 |
+
completion = tokenizer.decode(completion_tokens[0])
|
| 128 |
+
print(completion)
|
| 129 |
+
messages.append(parse_message_from_completion_text(completion, thinking_mode="chat"))
|
| 130 |
+
else:
|
| 131 |
+
with open(input_file) as f:
|
| 132 |
+
prompts = f.read().split("\n\n")
|
| 133 |
+
prompt_tokens = [tokenizer.encode(encode_messages([{"role": "user", "content": prompt}], thinking_mode="chat")) for prompt in prompts]
|
| 134 |
+
completion_tokens = generate(model, prompt_tokens, max_new_tokens, tokenizer.eos_token_id, temperature)
|
| 135 |
+
completions = tokenizer.batch_decode(completion_tokens)
|
| 136 |
+
for prompt, completion in zip(prompts, completions):
|
| 137 |
+
print("Prompt:", prompt)
|
| 138 |
+
print("Completion:", completion)
|
| 139 |
+
print()
|
| 140 |
+
|
| 141 |
+
if world_size > 1:
|
| 142 |
+
dist.destroy_process_group()
|
| 143 |
+
|
| 144 |
+
|
| 145 |
+
if __name__ == "__main__":
|
| 146 |
+
parser = ArgumentParser()
|
| 147 |
+
parser.add_argument("--ckpt-path", type=str, required=True)
|
| 148 |
+
parser.add_argument("--config", type=str, required=True)
|
| 149 |
+
parser.add_argument("--input-file", type=str, default="")
|
| 150 |
+
parser.add_argument("--interactive", action="store_true")
|
| 151 |
+
parser.add_argument("--max-new-tokens", type=int, default=300)
|
| 152 |
+
parser.add_argument("--temperature", type=float, default=0.6)
|
| 153 |
+
args = parser.parse_args()
|
| 154 |
+
assert args.input_file or args.interactive, "Either input-file or interactive mode must be specified"
|
| 155 |
+
main(args.ckpt_path, args.config, args.input_file, args.interactive, args.max_new_tokens, args.temperature)
|
model-00002-of-00046.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:f04048189d3b472b26d7d02331edd0412e85b184725041c978fdce793a286a0d
|
| 3 |
+
size 3566321192
|
model-00010-of-00046.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:c9cef4200444326f8d802bc1bb5be7c1d15af4bfdec0426db7e841a7bd516a28
|
| 3 |
+
size 3590024776
|
model-00018-of-00046.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:1298a07452a409ddbcc5172a4f57a011193bea4f16a6a964c3dd8531cbc06b49
|
| 3 |
+
size 3590026352
|
model-00026-of-00046.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:fdde6791ab713c93cb256d05d9560d5ecfd2b913563658b62d32af9a17a91546
|
| 3 |
+
size 3590026352
|
model-00034-of-00046.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:666b77201ec6946fb1f299f2ebcb462bce53575a97a91fb30db806bbcd099100
|
| 3 |
+
size 3590026352
|
model-00042-of-00046.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:bd3f5b898b041559a534c81a2f9bb53a8f9744ee8db37ed11b3a5c9fcf848882
|
| 3 |
+
size 3590026352
|