-
Notifications
You must be signed in to change notification settings - Fork 3k
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
[Feature] Support float8 dtype storage. #9906
Open
ZHUI
wants to merge
3
commits into
PaddlePaddle:develop
Choose a base branch
from
ZHUI:support_fp8_storage
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+129
−19
Open
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,4 +27,5 @@ regex | |
numpy<=1.26.4 | ||
tiktoken | ||
tokenizers>=0.21,<0.22 | ||
ml_dtypes | ||
omegaconf |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,17 +17,119 @@ | |
import unittest | ||
|
||
import numpy as np | ||
import paddle | ||
from safetensors.numpy import load_file, save_file | ||
|
||
from paddlenlp.utils.safetensors import fast_load_file, fast_safe_open | ||
|
||
from ..testing_utils import skip_platform | ||
|
||
paddle.set_device("cpu") | ||
|
||
|
||
def enhanced_to_tensor(tensor): | ||
if tensor.dtype == np.bfloat16: | ||
return paddle.to_tensor(tensor.view(np.uint16)) | ||
if tensor.dtype == np.float8_e5m2: | ||
t = paddle.to_tensor(tensor.view(np.int8)) | ||
new_t = paddle.empty(t.shape, dtype=paddle.float8_e5m2) | ||
new_t.get_tensor()._share_data_with(t.get_tensor()) | ||
return new_t | ||
if tensor.dtype == np.float8_e4m3fn: | ||
t = paddle.to_tensor(tensor.view(np.int8)) | ||
new_t = paddle.empty(t.shape, dtype=paddle.float8_e4m3fn) | ||
new_t.get_tensor()._share_data_with(t.get_tensor()) | ||
return new_t | ||
# return paddle.to_tensor(tensor.view(np.int8), dtype=paddle.float8_e4m3fn) | ||
raise ValueError() | ||
return paddle.to_tensor(tensor) | ||
|
||
|
||
class EextendDtypeNumpySafe(unittest.TestCase): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Extend |
||
def setUp(self): | ||
super().setUp() | ||
self.weight_map = {} | ||
self.tensors = [ | ||
([10, 1, 10], "float32"), | ||
([1, 1, 10], "float32"), | ||
([1, 1, 1, 10], "float32"), | ||
([10, 10], "float32"), | ||
([8], "float16"), | ||
([5, 5, 5], "int32"), | ||
] | ||
|
||
def get_target_dtype(self, dtype="float32"): | ||
count = 0 | ||
weight_map = {} | ||
for shape, _ in self.tensors: | ||
weight_map[f"weight_{count}"] = (np.random.random(shape) * 100).astype(dtype) | ||
count += 1 | ||
return weight_map | ||
|
||
def get_paddle_target_dtype(self, dtype="float32"): | ||
weight_map = self.get_target_dtype(dtype) | ||
for k, v in list(weight_map.items()): | ||
weight_map[k] = enhanced_to_tensor(v) | ||
return weight_map | ||
|
||
@skip_platform("win32", "cygwin") | ||
def test_save_load_file_paddle(self): | ||
with tempfile.TemporaryDirectory() as tmpdirname: | ||
for dtype in ["bfloat16", "float8_e5m2", "float8_e4m3fn"]: | ||
weight_map = self.get_paddle_target_dtype(dtype) | ||
path = os.path.join(tmpdirname, "test.safetensors") | ||
shard = {} | ||
for k in list(weight_map.keys()): | ||
if isinstance(weight_map[k], paddle.Tensor): | ||
shard[k] = weight_map[k].cpu().numpy() | ||
else: | ||
shard[k] = weight_map[k] | ||
|
||
save_file(shard, path, metadata={"format": "np"}) | ||
sf_load = load_file(path) | ||
fs_sf_load = fast_load_file(path) | ||
|
||
for k, v in self.weight_map.items(): | ||
paddle.allclose(v, enhanced_to_tensor(sf_load[k])) | ||
paddle.allclose(v, enhanced_to_tensor(fs_sf_load[k])) | ||
|
||
@skip_platform("win32", "cygwin") | ||
def test_save_load_file(self): | ||
with tempfile.TemporaryDirectory() as tmpdirname: | ||
for dtype in ["bfloat16", "float8_e4m3fn", "float8_e5m2"]: | ||
weight_map = self.get_target_dtype(dtype) | ||
path = os.path.join(tmpdirname, "test.safetensors") | ||
save_file(weight_map, path, metadata={"format": "np"}) | ||
sf_load = load_file(path) | ||
fs_sf_load = fast_load_file(path) | ||
for k, v in self.weight_map.items(): | ||
np.testing.assert_equal(v, sf_load[k]) | ||
np.testing.assert_equal(v, fs_sf_load[k]) | ||
|
||
@skip_platform("win32", "cygwin") | ||
def test_dtype_safe_open(self): | ||
with tempfile.TemporaryDirectory() as tmpdirname: | ||
for dtype in ["float32", "int32", "bfloat16", "float8_e4m3fn", "float8_e5m2"]: | ||
weight_map = self.get_target_dtype(dtype) | ||
path = os.path.join(tmpdirname, "test.safetensors") | ||
save_file(weight_map, path, metadata={"format": "np"}) | ||
|
||
with fast_safe_open(path, framework="np") as f: | ||
for key in f.keys(): | ||
safe_slice = f.get_slice(key) | ||
# np.testing.assert_equal(self.weight_map[key][2:1, ...], safe_slice[2:1, ...]) | ||
np.testing.assert_equal(weight_map[key][0, ...], safe_slice[0, ...]) | ||
np.testing.assert_equal(weight_map[key][0:1, ...], safe_slice[0:1, ...]) | ||
np.testing.assert_equal(weight_map[key][..., 2:], safe_slice[..., 2:]) | ||
np.testing.assert_equal(weight_map[key][..., 1], safe_slice[..., 1]) | ||
np.testing.assert_equal(weight_map[key][:2, ...], safe_slice[:2, ...]) | ||
np.testing.assert_equal(weight_map[key][..., :4], safe_slice[..., :4]) | ||
|
||
|
||
class FastSafetensors(unittest.TestCase): | ||
def setUp(self): | ||
super().setUp() | ||
self.weigth_map = {} | ||
self.weight_map = {} | ||
tensors = [ | ||
([10, 1, 10], "float32"), | ||
([1, 1, 10], "float32"), | ||
|
@@ -38,34 +140,33 @@ def setUp(self): | |
] | ||
count = 0 | ||
for shape, dtype in tensors: | ||
self.weigth_map[f"weight_{count}"] = (np.random.random(shape) * 100).astype(dtype) | ||
self.weight_map[f"weight_{count}"] = (np.random.random(shape) * 100).astype(dtype) | ||
count += 1 | ||
print(self.weigth_map) | ||
|
||
@skip_platform("win32", "cygwin") | ||
def test_load_file(self): | ||
with tempfile.TemporaryDirectory() as tmpdirname: | ||
path = os.path.join(tmpdirname, "test.safetensors") | ||
save_file(self.weigth_map, path, metadata={"format": "np"}) | ||
save_file(self.weight_map, path, metadata={"format": "np"}) | ||
sf_load = load_file(path) | ||
fs_sf_load = fast_load_file(path) | ||
for k, v in self.weigth_map.items(): | ||
for k, v in self.weight_map.items(): | ||
np.testing.assert_equal(v, sf_load[k]) | ||
np.testing.assert_equal(v, fs_sf_load[k]) | ||
|
||
@skip_platform("win32", "cygwin") | ||
def test_safe_open(self): | ||
with tempfile.TemporaryDirectory() as tmpdirname: | ||
path = os.path.join(tmpdirname, "test.safetensors") | ||
save_file(self.weigth_map, path, metadata={"format": "np"}) | ||
save_file(self.weight_map, path, metadata={"format": "np"}) | ||
|
||
with fast_safe_open(path, framework="np") as f: | ||
for key in f.keys(): | ||
safe_slice = f.get_slice(key) | ||
# np.testing.assert_equal(self.weigth_map[key][2:1, ...], safe_slice[2:1, ...]) | ||
np.testing.assert_equal(self.weigth_map[key][0, ...], safe_slice[0, ...]) | ||
np.testing.assert_equal(self.weigth_map[key][0:1, ...], safe_slice[0:1, ...]) | ||
np.testing.assert_equal(self.weigth_map[key][..., 2:], safe_slice[..., 2:]) | ||
np.testing.assert_equal(self.weigth_map[key][..., 1], safe_slice[..., 1]) | ||
np.testing.assert_equal(self.weigth_map[key][:2, ...], safe_slice[:2, ...]) | ||
np.testing.assert_equal(self.weigth_map[key][..., :4], safe_slice[..., :4]) | ||
# np.testing.assert_equal(self.weight_map[key][2:1, ...], safe_slice[2:1, ...]) | ||
np.testing.assert_equal(self.weight_map[key][0, ...], safe_slice[0, ...]) | ||
np.testing.assert_equal(self.weight_map[key][0:1, ...], safe_slice[0:1, ...]) | ||
np.testing.assert_equal(self.weight_map[key][..., 2:], safe_slice[..., 2:]) | ||
np.testing.assert_equal(self.weight_map[key][..., 1], safe_slice[..., 1]) | ||
np.testing.assert_equal(self.weight_map[key][:2, ...], safe_slice[:2, ...]) | ||
np.testing.assert_equal(self.weight_map[key][..., :4], safe_slice[..., :4]) |
Oops, something went wrong.
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.
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.
trick 绕过fp8赋值。