Skip to content

Commit 9b28ee2

Browse files
committed
docs: update for finetunes
1 parent 6ee8a4d commit 9b28ee2

File tree

5 files changed

+310
-2
lines changed

5 files changed

+310
-2
lines changed

README.md

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -510,11 +510,37 @@ TODO
510510
511511
## 步骤 3:围绕意图的数据工程与模型演进
512512
513-
### IDE 指令设计
513+
### IDE 指令设计与演化
514+
515+
#### 模板指令
516+
517+
如下是在 AutoDev 中精简化后的 Prompt 示例:
518+
519+
Write unit test for following code.
520+
521+
${context.testFramework}
522+
${context.coreFramework}
523+
${context.testSpec}
524+
525+
```${context.language}
526+
${context.related_model}
527+
${context.selection}
528+
```
529+
530+
其中包含了:
531+
532+
- 技术栈上下文
533+
- 测试技术栈上下文
534+
- 代码块(类、函数)的输入和输出信息
514535
515536
### 高质量数据集生成
516537
517-
## 相关资源
538+
- 统一提示词(Prompt)。统一工具-微调-评估底层的提示词。
539+
- 代码质量管道。诸如于代码复杂性、代码坏味道、测试坏味道、API 设计味道等。
540+
- 可扩展的质量阈。自定义规则、自定义阈值、自定义质量类型等。
541+
542+
543+
## 附:相关资源
518544
519545
### 开源 AI 辅助工具
520546

code/finetune/finetune.ipynb

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": null,
6+
"id": "3bf0068d-9f6f-4c61-bc08-47b9594254d2",
7+
"metadata": {
8+
"tags": []
9+
},
10+
"outputs": [],
11+
"source": [
12+
"!git clone https://github.com/deepseek-ai/DeepSeek-Coder"
13+
]
14+
},
15+
{
16+
"cell_type": "code",
17+
"execution_count": null,
18+
"id": "14a9e9c5-07b0-4d82-9dde-06559bb14ea2",
19+
"metadata": {
20+
"tags": []
21+
},
22+
"outputs": [],
23+
"source": [
24+
"!pip install deepspeed"
25+
]
26+
},
27+
{
28+
"cell_type": "code",
29+
"execution_count": null,
30+
"outputs": [],
31+
"source": [
32+
"# if you want to clone without large files – just their pointers\n",
33+
"!apt install git-lfs"
34+
],
35+
"metadata": {
36+
"collapsed": false
37+
},
38+
"id": "8fd2eb721d14953a"
39+
},
40+
{
41+
"cell_type": "code",
42+
"execution_count": null,
43+
"id": "f8817387-03de-4fb7-9d6f-63d41e1db328",
44+
"metadata": {},
45+
"outputs": [],
46+
"source": [
47+
"# Make sure you have git-lfs installed (https://git-lfs.com)\n",
48+
"!git lfs install\n",
49+
"!git clone https://huggingface.co/deepseek-ai/deepseek-coder-6.7b-instruct"
50+
]
51+
},
52+
{
53+
"cell_type": "code",
54+
"execution_count": null,
55+
"outputs": [],
56+
"source": [
57+
"!cd DeepSeek-Coder/finetune && pip install -r requirements.txt"
58+
],
59+
"metadata": {
60+
"collapsed": false
61+
},
62+
"id": "8ee1f87d90fc2a8e"
63+
},
64+
{
65+
"cell_type": "code",
66+
"execution_count": null,
67+
"id": "f42cbcf2-09a1-4560-9a61-33a16ad220ea",
68+
"metadata": {
69+
"tags": []
70+
},
71+
"outputs": [],
72+
"source": [
73+
"DATA_PATH=\"/openbayes/home/summary.jsonl\"\n",
74+
"OUTPUT_PATH=\"/openbayes/home/output\"\n",
75+
"\n",
76+
"# NotImplementedError: Using RTX 3090 or 4000 series doesn't support faster communication broadband via P2P or IB. Please\n",
77+
"# set `NCCL_P2P_DISABLE=\"1\"` and `NCCL_IB_DISABLE=\"1\" or use `accelerate launch` which will do this automatically.\n",
78+
"# \n",
79+
"# !NCCL_P2P_DISABLE=1\n",
80+
"# !NCCL_IB_DISABLE=1\n",
81+
"\n",
82+
"MODEL_PATH=\"/openbayes/home/deepseek-coder-6.7b-instruct\"\n",
83+
"\n",
84+
"!cd DeepSeek-Coder/finetune && deepspeed finetune_deepseekcoder.py \\\n",
85+
" --model_name_or_path $MODEL_PATH \\\n",
86+
" --data_path $DATA_PATH \\\n",
87+
" --output_dir $OUTPUT_PATH \\\n",
88+
" --num_train_epochs 1 \\\n",
89+
" --model_max_length 768 \\\n",
90+
" --per_device_train_batch_size 16 \\\n",
91+
" --per_device_eval_batch_size 1 \\\n",
92+
" --gradient_accumulation_steps 4 \\\n",
93+
" --evaluation_strategy \"no\" \\\n",
94+
" --save_strategy \"no\" \\\n",
95+
" --save_steps 50 \\\n",
96+
" --save_total_limit 10 \\\n",
97+
" --learning_rate 4e-5 \\\n",
98+
" --warmup_steps 10 \\\n",
99+
" --logging_steps 1 \\\n",
100+
" --lr_scheduler_type \"cosine\" \\\n",
101+
" --gradient_checkpointing True \\\n",
102+
" --report_to \"tensorboard\" \\\n",
103+
" --deepspeed configs/ds_config_zero3.json \\\n",
104+
" --bf16 True"
105+
]
106+
}
107+
],
108+
"metadata": {
109+
"kernelspec": {
110+
"display_name": "Python 3 (ipykernel)",
111+
"language": "python",
112+
"name": "python3"
113+
},
114+
"language_info": {
115+
"codemirror_mode": {
116+
"name": "ipython",
117+
"version": 3
118+
},
119+
"file_extension": ".py",
120+
"mimetype": "text/x-python",
121+
"name": "python",
122+
"nbconvert_exporter": "python",
123+
"pygments_lexer": "ipython3",
124+
"version": "3.8.18"
125+
}
126+
},
127+
"nbformat": 4,
128+
"nbformat_minor": 5
129+
}

code/ide/.gitkeep

Whitespace-only changes.

code/server/requirements.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
accelerate==0.23.0
2+
bitsandbytes==0.41.1
3+
gradio==3.48.0
4+
protobuf==3.20.3
5+
# scipy==1.11.2
6+
sentencepiece==0.1.99
7+
spaces==0.16.1
8+
torch==2.0.0
9+
transformers==4.34.0
10+
fastapi
11+
uvicorn
12+
asyncio
13+
async_timeout

code/server/server-python38.py

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
import os
2+
from threading import Thread
3+
from typing import Iterator, List, Tuple
4+
from urllib.request import Request
5+
6+
import uvicorn
7+
from fastapi import FastAPI, Response
8+
from fastapi.responses import StreamingResponse
9+
from fastapi.exceptions import RequestValidationError
10+
11+
import torch
12+
from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
13+
import requests
14+
from pydantic import BaseModel
15+
from starlette import status
16+
from starlette.responses import JSONResponse
17+
import async_timeout
18+
import asyncio
19+
import time
20+
21+
MAX_MAX_NEW_TOKENS = 4096
22+
DEFAULT_MAX_NEW_TOKENS = 1024
23+
total_count = 0
24+
MAX_INPUT_TOKEN_LENGTH = int(os.getenv("MAX_INPUT_TOKEN_LENGTH", "4096"))
25+
26+
if torch.cuda.is_available():
27+
model_id = "/openbayes/input/input0/"
28+
model = AutoModelForCausalLM.from_pretrained(model_id, torch_dtype=torch.bfloat16, device_map="auto")
29+
tokenizer = AutoTokenizer.from_pretrained(model_id)
30+
tokenizer.use_default_system_prompt = False
31+
32+
33+
class Message(BaseModel):
34+
role: str
35+
content: str
36+
37+
38+
class MessageInResponseChat(BaseModel):
39+
message: Message
40+
41+
42+
class ChatResponse(BaseModel):
43+
choices: List[MessageInResponseChat]
44+
model: str
45+
46+
47+
class SimpleOpenAIBody(BaseModel):
48+
messages: List[Message]
49+
temperature: float
50+
stream: bool
51+
52+
53+
GENERATION_TIMEOUT_SEC = 480
54+
55+
56+
async def stream_generate(
57+
chat_history: List[Message],
58+
max_new_tokens: int = 512,
59+
temperature: float = 0.1,
60+
top_p: float = 0.9,
61+
top_k: int = 50,
62+
repetition_penalty: float = 1,
63+
):
64+
async with async_timeout.timeout(GENERATION_TIMEOUT_SEC):
65+
try:
66+
global total_count
67+
total_count += 1
68+
if total_count % 50 == 0:
69+
os.system("nvidia-smi")
70+
71+
conversation = chat_history
72+
73+
input_ids = tokenizer.apply_chat_template(conversation, return_tensors="pt")
74+
if input_ids.shape[1] > MAX_INPUT_TOKEN_LENGTH:
75+
input_ids = input_ids[:, -MAX_INPUT_TOKEN_LENGTH:]
76+
input_ids = input_ids.to(model.device)
77+
78+
streamer = TextIteratorStreamer(tokenizer, timeout=20.0, skip_prompt=True, skip_special_tokens=True)
79+
generate_kwargs = dict(
80+
{"input_ids": input_ids},
81+
streamer=streamer,
82+
max_new_tokens=max_new_tokens,
83+
do_sample=False,
84+
top_p=top_p,
85+
top_k=top_k,
86+
num_beams=1,
87+
temperature=temperature,
88+
repetition_penalty=repetition_penalty,
89+
eos_token_id=32021
90+
)
91+
t = Thread(target=model.generate, kwargs=generate_kwargs)
92+
t.start()
93+
94+
result = ""
95+
outputs = []
96+
for text in streamer:
97+
outputs.append(text)
98+
result = "".join(outputs)
99+
# result = "".join(outputs).replace("<|EOT|>", "")
100+
101+
yield 'data:' + ChatResponse(
102+
choices=[MessageInResponseChat(message=Message(role='assistant', content=result))],
103+
model="autodev-deepseek").model_dump_json()
104+
105+
yield '\n\n'
106+
time.sleep(0.2)
107+
yield 'data:[DONE]'
108+
print(result)
109+
110+
except asyncio.TimeoutError:
111+
raise HTTPException(status_code=504, detail="Stream timed out")
112+
113+
114+
app = FastAPI()
115+
116+
117+
@app.exception_handler(RequestValidationError)
118+
async def validation_exception_handler(request: Request, exc: RequestValidationError):
119+
exc_str = f'{exc}'.replace('\n', ' ').replace(' ', ' ')
120+
print(f"{request}: {exc_str}")
121+
content = {'status_code': 10422, 'message': exc_str, 'data': None}
122+
return JSONResponse(content=content, status_code=status.HTTP_422_UNPROCESSABLE_ENTITY)
123+
124+
125+
@app.post("/api/chat", response_class=Response)
126+
async def root(body: SimpleOpenAIBody) -> StreamingResponse:
127+
return StreamingResponse(stream_generate(body.messages, temperature=body.temperature),
128+
media_type="text/event-stream")
129+
130+
131+
if __name__ == "__main__":
132+
try:
133+
meta = requests.get('http://localhost:21999/gear-status', timeout=5).json()
134+
url = meta['links'].get('auxiliary')
135+
if url:
136+
print("打开该链接访问:", url)
137+
except Exception:
138+
pass
139+
140+
uvicorn.run(app, host="0.0.0.0", port=8080)

0 commit comments

Comments
 (0)