-
Notifications
You must be signed in to change notification settings - Fork 234
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support FP8 model fallback KVCache to bfloat16 #1505
base: main
Are you sure you want to change the base?
Conversation
@@ -628,10 +628,14 @@ def pre_attn_forward( | |||
else: | |||
if past_key_value is None: | |||
past_key = torch.zeros( | |||
key_states.shape, dtype=self.get_k_proj_weight_dtype(), device=key_states.device | |||
key_states.shape, | |||
dtype=torch.bfloat16 if isinstance(self.k_cache, KVCache) else self.get_k_proj_weight_dtype(), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not use the function?
The default value is:
self.k_proj.weight.dtype
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
for recipes FP8Config(allowlist={"types": ["Linear"], "names": []}, blocklist=blocklist = {"types": [], "names": []})
, self.k_proj.weight.dtype
is torch.float8_e4m3fn, but the past_key
dtype should be torch.bfloat16
) | ||
past_value = torch.zeros( | ||
key_states.shape, dtype=self.get_k_proj_weight_dtype(), device=key_states.device | ||
key_states.shape, | ||
dtype=torch.bfloat16 if isinstance(self.v_cache, KVCache) else self.get_k_proj_weight_dtype(), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same
@@ -628,10 +628,14 @@ def pre_attn_forward( | |||
else: | |||
if past_key_value is None: | |||
past_key = torch.zeros( | |||
key_states.shape, dtype=self.get_k_proj_weight_dtype(), device=key_states.device | |||
key_states.shape, | |||
dtype=torch.bfloat16 if isinstance(self.k_cache, KVCache) else self.get_k_proj_weight_dtype(), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
in the init(), self.k_proj is initialized as a KVCache(). isn't isinstance(self.k_cache, KVCache) always true? when does it hit the else part?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the self.k_proj is the kind of linear,
no, self.k_cache is KVCache for bf16, self.k_cache is PatchedKVCache for fp8.
Scenario: self.k_proj's weight is fp8, but the past_key_value need use bf16 datatype. ( for example,
When the accuracy does not reach 1% loss, we will selectively fallback operator, such as kv_cache.
When I want to load neuralmagic model(https://huggingface.co/neuralmagic/Meta-Llama-3.1-8B-Instruct-FP8), there are only linear fp8 weights in the checkpoints, but no kv cache scale.)
@changwangss can you please resolve conflicts? |
I plan to load fp8 model with the following config, Linear is fp8 and kvcache and others op are bf16.
when use
run_generation.py
do model.generate, the error raised.