Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
OpenDAS
ktransformers
Commits
e645d847
Commit
e645d847
authored
Feb 27, 2025
by
Atream
Browse files
use generation config from json file in official repo
parent
9660b2cc
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
57 additions
and
21 deletions
+57
-21
ktransformers/local_chat.py
ktransformers/local_chat.py
+9
-9
ktransformers/operators/attention.py
ktransformers/operators/attention.py
+30
-1
ktransformers/operators/flashinfer_wrapper.py
ktransformers/operators/flashinfer_wrapper.py
+15
-9
ktransformers/util/utils.py
ktransformers/util/utils.py
+3
-2
No files found.
ktransformers/local_chat.py
View file @
e645d847
...
...
@@ -110,15 +110,15 @@ def local_chat(
optimize_and_load_gguf
(
model
,
optimize_config_path
,
gguf_path
,
config
)
try
:
model
.
generation_config
=
GenerationConfig
.
from_pretrained
(
model_path
)
except
:
gen_config
=
G
eneration
C
onfig
(
max_length
=
128
,
temperature
=
0.
7
,
top_p
=
0.9
,
do_sample
=
True
)
model
.
generation_config
=
gen_config
model
.
generation_config
=
GenerationConfig
.
from_pretrained
(
model_path
)
except
Exception
as
e
:
print
(
f
"g
eneration
c
onfig
can't auto create, make default. Message:
{
e
}
"
)
gen_config
=
GenerationConfig
(
temperature
=
0.
6
,
top_p
=
0.9
5
,
do_sample
=
True
)
model
.
generation_config
=
gen_config
# model.generation_config = GenerationConfig.from_pretrained(model_path)
if
model
.
generation_config
.
pad_token_id
is
None
:
model
.
generation_config
.
pad_token_id
=
model
.
generation_config
.
eos_token_id
...
...
ktransformers/operators/attention.py
View file @
e645d847
...
...
@@ -338,7 +338,7 @@ class KDeepseekV2Attention(BaseInjectedModule, DeepseekV2Attention):
attn_output
=
self
.
o_proj
(
attn_output
)
return
attn_output
,
None
,
past_key_value
def
forward_linux_flashinfer
(
def
forward_linux_flashinfer
_chunk
(
self
,
hidden_states
:
torch
.
Tensor
,
attention_mask
:
Optional
[
torch
.
Tensor
]
=
None
,
...
...
@@ -512,6 +512,35 @@ class KDeepseekV2Attention(BaseInjectedModule, DeepseekV2Attention):
attn_output
=
self
.
o_proj
(
attn_output
)
return
attn_output
,
None
,
past_key_value
def
forward_linux_flashinfer
(
self
,
hidden_states
:
torch
.
Tensor
,
attention_mask
:
Optional
[
torch
.
Tensor
]
=
None
,
position_ids
:
Optional
[
torch
.
Tensor
]
=
None
,
past_key_value
:
Optional
[
Cache
]
=
None
,
output_attentions
:
bool
=
False
,
use_cache
:
bool
=
False
,
cache_position
:
Optional
[
torch
.
Tensor
]
=
None
,
**
kwargs
,
)
->
Tuple
[
torch
.
Tensor
,
Optional
[
torch
.
Tensor
],
Optional
[
Tuple
[
torch
.
Tensor
]]]:
bsz
,
q_len
,
_
=
hidden_states
.
size
()
if
q_len
<=
self
.
chunck_size
or
not
self
.
absorb_for_prefill
:
return
self
.
forward_linux_flashinfer_chunk
(
hidden_states
,
attention_mask
,
position_ids
,
past_key_value
,
output_attentions
,
use_cache
,
cache_position
,
**
kwargs
,
)
assert
False
def
forward_windows
(
self
,
hidden_states
:
torch
.
Tensor
,
...
...
ktransformers/operators/flashinfer_wrapper.py
View file @
e645d847
...
...
@@ -122,7 +122,7 @@ class MLAWrapper():
if
kv_indices
is
None
:
assert
self
.
max_batch_size
==
1
kv_indices
=
self
.
kv_indices_buf
self
.
wrapper
.
plan
(
qo_indptr
,
kv_indptr
,
...
...
@@ -139,6 +139,11 @@ class MLAWrapper():
)
def
run
(
self
,
q_nope
,
q_pe
,
ckv
,
k_pe
,
return_lse
=
False
):
#print("run")
#print(self.wrapper._qo_indptr_buf)
#print(self.wrapper._kv_indptr_buf)
#print(self.wrapper._kv_indices_buf)
#print(self.wrapper._kv_len_arr_buf)
return
self
.
wrapper
.
run
(
q_nope
,
q_pe
,
ckv
,
k_pe
,
return_lse
=
return_lse
)
class
MLAWrapperSingleton
():
...
...
@@ -201,11 +206,12 @@ class MLAWrapperSingleton():
if
__name__
==
"__main__"
:
max_batch_size
=
1
max_pages
=
1
max_pages
=
1
28
page_size
=
64
num_heads
=
128
q_len
=
10
kv_len
=
2069
q_len
=
1
q_nope
=
torch
.
randn
((
q_len
,
num_heads
,
512
),
dtype
=
torch
.
bfloat16
,
device
=
"cuda"
)
q_pe
=
torch
.
randn
((
q_len
,
num_heads
,
64
),
dtype
=
torch
.
bfloat16
,
device
=
"cuda"
)
ckv
=
torch
.
randn
((
max_pages
,
page_size
,
512
),
dtype
=
torch
.
bfloat16
,
device
=
"cuda"
)
...
...
@@ -218,7 +224,7 @@ if __name__ == "__main__":
max_pages
,
)
kv_len_arr
=
torch
.
tensor
([
q
_len
],
dtype
=
torch
.
int32
,
device
=
"cuda"
)
kv_len_arr
=
torch
.
tensor
([
kv
_len
],
dtype
=
torch
.
int32
,
device
=
"cuda"
)
qo_indptr
=
torch
.
tensor
([
0
,
q_len
],
dtype
=
torch
.
int32
,
device
=
"cuda"
)
wrapper
.
plan
(
qo_indptr
,
...
...
@@ -244,15 +250,15 @@ if __name__ == "__main__":
)
v
=
ckv
.
view
(
-
1
,
1
,
512
).
repeat_interleave
(
num_heads
,
dim
=
1
)
print
(
k
[:
10
].
shape
)
print
(
v
[:
10
].
shape
)
print
(
k
[:
kv_len
].
shape
)
print
(
v
[:
kv_len
].
shape
)
attn_ref
,
lse_ref
=
attention_ref
(
max_batch_size
,
torch
.
cat
([
q_nope
,
q_pe
],
dim
=-
1
),
k
[:
10
],
v
[:
10
],
Fals
e
,
k
[:
kv_len
],
v
[:
kv_len
],
Tru
e
,
192
**
(
-
0.5
)
)
print
(
attn_ref
.
shape
)
...
...
ktransformers/util/utils.py
View file @
e645d847
...
...
@@ -184,8 +184,9 @@ def prefill_and_generate(model, tokenizer, inputs, max_new_tokens=10000, use_cud
inputs_embeds
=
inputs_embeds
,
cache_position
=
cache_position
,
past_key_values
=
past_key_values
,
return_dict
=
False
,
use_cache
=
True
)[
0
][:,
-
1
,:].
unsqueeze
(
0
).
clone
().
to
(
torch_device
)
generation_config
,
model_kwargs
=
model
.
_prepare_generation_config
(
None
,
max_length
=
max_new_tokens
,
do_sample
=
True
,
top_k
=
5
,
top_p
=
0.85
,
temperature
=
0.1
# change this to modify generate config
None
,
do_sample
=
True
# change this to modify generate config
#top_k=5, top_p=0.85, temperature=0.1
)
try
:
# transformers==4.43
logits_warper
=
(
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment