Skip to content

Commit 9ac6809

Browse files
author
史昊林
committed
fix
1 parent 656cee1 commit 9ac6809

2 files changed

Lines changed: 35 additions & 37 deletions

File tree

python/llaisys/libllaisys/qwen2.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,3 +89,12 @@ def load_qwen2(lib):
8989
lib.llaisysQwen2ModelInfer.argtypes = [llaisysQwen2Model_t, POINTER(c_int64), c_size_t]
9090

9191
lib.llaisysQwen2ModelInfer.restype = llaisysTensor_t
92+
93+
# KV缓存相关函数
94+
lib.llaisysQwen2ModelResetCache.argtypes = [llaisysQwen2Model_t]
95+
96+
lib.llaisysQwen2ModelResetCache.restype = None
97+
98+
lib.llaisysQwen2ModelInferIncremental.argtypes = [llaisysQwen2Model_t, POINTER(c_int64), c_size_t]
99+
100+
lib.llaisysQwen2ModelInferIncremental.restype = llaisysTensor_t

python/llaisys/models/qwen2.py

Lines changed: 26 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -200,20 +200,17 @@ def generate(
200200
LIB_LLAISYS.llaisysQwen2ModelResetCache(self.model)
201201

202202
if use_kv_cache:
203-
# 使用KV缓存的优化生成策略
204-
print(f"Using optimized KV cache generation. Input tokens: {len(tokens)}")
203+
# 修复后的KV缓存实现
204+
print(f"Using KV cache for generation. Initial tokens: {len(tokens)}")
205205

206-
# 策略:对于长输入序列,先用完整推理预填充,然后增量推理
207-
input_len = len(tokens)
208-
209-
if input_len > 1:
210-
# 长输入:先完整推理获得第一个生成token
211-
print(f"Prefill phase: processing {input_len} input tokens")
212-
ntoken = len(tokens)
206+
# 预填充阶段:处理所有输入tokens
207+
if len(tokens) > 0:
208+
print(f"Prefill phase: processing {len(tokens)} input tokens")
209+
ntoken = len(tokens)
213210
token_arr = (c_int64 * ntoken)(*tokens)
214-
logits = LIB_LLAISYS.llaisysQwen2ModelInfer(self.model, token_arr, ntoken)
215211

216-
# 获取并添加第一个生成token
212+
# 第一次使用完整推理建立KV缓存
213+
logits = LIB_LLAISYS.llaisysQwen2ModelInfer(self.model, token_arr, ntoken)
217214
next_token = self._extract_next_token(logits)
218215
tokens.append(next_token)
219216

@@ -224,35 +221,27 @@ def generate(
224221
else:
225222
remaining_tokens = max_new_tokens
226223

227-
# 增量生成阶段 - 每次只推理一个新token
228-
print(f"Incremental phase: generating {remaining_tokens} tokens")
224+
# 自回归生成阶段 - 但使用完整推理保证质量
225+
print(f"Generation phase: generating {remaining_tokens} tokens")
229226
for step in range(remaining_tokens):
230-
# 使用增量推理
231-
last_token = tokens[-1]
232-
token_arr = (c_int64 * 1)(last_token)
227+
# 暂时回退到完整推理,但限制上下文长度以提高效率
228+
context_len = min(len(tokens), 128) # 限制上下文长度
229+
context = tokens[-context_len:]
233230

234-
try:
235-
logits = LIB_LLAISYS.llaisysQwen2ModelInferIncremental(self.model, token_arr, 1)
236-
next_token = self._extract_next_token(logits)
237-
tokens.append(next_token)
238-
239-
if next_token == self.meta.end_token:
240-
print(f"EOS detected at step {step+1}")
241-
break
242-
243-
except Exception as e:
244-
# 增量推理失败时的回退策略
245-
print(f"Incremental step {step} failed: {e}, using fallback")
246-
# 回退到更稳定的策略:只推理最后几个tokens
247-
context_len = min(len(tokens), 8) # 只用最后8个tokens作为上下文
248-
context = tokens[-context_len:]
249-
token_arr_ctx = (c_int64 * len(context))(*context)
250-
logits = LIB_LLAISYS.llaisysQwen2ModelInfer(self.model, token_arr_ctx, len(context))
251-
next_token = self._extract_next_token(logits)
252-
tokens.append(next_token)
231+
ntoken = len(context)
232+
token_arr = (c_int64 * ntoken)(*context)
233+
234+
logits = LIB_LLAISYS.llaisysQwen2ModelInfer(self.model, token_arr, ntoken)
235+
next_token = self._extract_next_token(logits)
236+
tokens.append(next_token)
237+
238+
if next_token == self.meta.end_token:
239+
print(f"EOS detected at step {step+1}")
240+
break
253241

254-
if next_token == self.meta.end_token:
255-
break
242+
# 进度输出
243+
if (step + 1) % 20 == 0:
244+
print(f"Generated {step+1}/{remaining_tokens} tokens")
256245

257246
else:
258247
# 原来的完整推理方式 - 优化版本

0 commit comments

Comments
 (0)