Adds support for loading F8 e5m2 weights#460
Merged
leejet merged 1 commit intoleejet:masterfrom Nov 23, 2024
LostRuins:pr_add_e5m2
Merged
Adds support for loading F8 e5m2 weights#460leejet merged 1 commit intoleejet:masterfrom LostRuins:pr_add_e5m2
leejet merged 1 commit intoleejet:masterfrom
LostRuins:pr_add_e5m2
Conversation
LostRuins
added a commit
to LostRuins/koboldcpp
that referenced
this pull request
Nov 13, 2024
stduhpf
reviewed
Nov 13, 2024
Comment on lines
+618
to
+658
| uint16_t f8_e5m2_to_f16(uint8_t fp8) { | ||
| uint8_t sign = (fp8 >> 7) & 0x1; | ||
| uint8_t exponent = (fp8 >> 2) & 0x1F; | ||
| uint8_t mantissa = fp8 & 0x3; | ||
|
|
||
| uint16_t fp16_sign = sign << 15; | ||
| uint16_t fp16_exponent; | ||
| uint16_t fp16_mantissa; | ||
|
|
||
| if (exponent == 0 && mantissa == 0) { //zero | ||
| return fp16_sign; | ||
| } | ||
|
|
||
| if (exponent == 0x1F) { //NAN and INF | ||
| fp16_exponent = 0x1F; | ||
| fp16_mantissa = mantissa ? (mantissa << 8) : 0; | ||
| return fp16_sign | (fp16_exponent << 10) | fp16_mantissa; | ||
| } | ||
|
|
||
| if (exponent == 0) { //subnormal numbers | ||
| fp16_exponent = 0; | ||
| fp16_mantissa = (mantissa << 8); | ||
| return fp16_sign | fp16_mantissa; | ||
| } | ||
|
|
||
| //normal numbers | ||
| int16_t true_exponent = (int16_t)exponent - 15 + 15; | ||
| if (true_exponent <= 0) { | ||
| fp16_exponent = 0; | ||
| fp16_mantissa = (mantissa << 8); | ||
| } else if (true_exponent >= 0x1F) { | ||
| fp16_exponent = 0x1F; | ||
| fp16_mantissa = 0; | ||
| } else { | ||
| fp16_exponent = (uint16_t)true_exponent; | ||
| fp16_mantissa = mantissa << 8; | ||
| } | ||
|
|
||
| return fp16_sign | (fp16_exponent << 10) | fp16_mantissa; | ||
| } | ||
|
|
Contributor
There was a problem hiding this comment.
I might be mistaken, but can't this whole thing be replaced with just return (uint16_t)fp8<<8;, since fp8_e5m2 is basically truncated fp16?
Contributor
There was a problem hiding this comment.
(or rather return static_cast<uint16_t>(fp8) << 8;)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Adds support for loading F8 e5m2 weights, which is an alternative of f8 e4m3. Added in the similar manner as #359 by @Green-Sky converting to f16.
Tested and seems to work.