Skip to content
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

mergekit gpu 1226 #9702

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from

Conversation

Mangodadada
Copy link
Contributor

PR types

PR changes

Description

mergekit gpu

Copy link

paddle-bot bot commented Dec 26, 2024

Thanks for your contribution!

@@ -30,7 +31,7 @@ def merge(self, tensor_list):
elif self.merge_config.merge_type == "ties":
return self.ties(tensor_list)
else:
raise NotImplementedError(f"{self.merge_config.merge_type} is not supported yet.")
raise NotImplementedError("Unsupported tensor type.")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

????为啥要修改所有的NotImplementedError("Unsupported tensor type.")

else:
raise NotImplementedError("Paddle Tensor is not supported yet.")
raise NotImplementedError(f"Tensor type {self.merge_config.tensor_type} is not supported yet.")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

raise NotImplementedError(f"Tensor type {self.merge_config.tensor_type} is not supported yet.") 改成 raise raise ValueError(f"Unkonwn tensor type {self.merge_config.tensor_type}")

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

其他地方类似问题也做同样的修改!

stacked_signs = paddle.sign(stacked_tensors).astype(mask_dtype)
majority_sign = (paddle.sum(stacked_signs, axis=0) >= 0).astype(mask_dtype) * 2 - 1
else:
raise NotImplementedError("Unsupported tensor type.")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

????????

elif self.merge_config.tensor_type == "pd":
mask = paddle.cast(
paddle.bernoulli(paddle.full(tensor.shape, self.merge_config.reserve_p, dtype=tensor.dtype)),
tensor.dtype,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

不用再cast了,输出tensor和输入tensor形状和数据类型相同https://www.paddlepaddle.org.cn/documentation/docs/zh/2.6/api/paddle/bernoulli_cn.html#bernoulli

raise ValueError(f"Unsupported tensor type: {self.tensor_type}. Support 'np' only.")
if self.device != "cpu":
logger.warning(f"Currently only support cpu device, but got {self.device}. Setting `device` to `cpu`.")
if self.tensor_type not in ["np", "pd"]:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://github.com/PaddlePaddle/PaddleNLP/blob/develop/tests/llm/test_mergekit.py 测试mergekit包括cpu numpy、cpu paddle tensor、gpu paddle tensor三种case

tensor_list = []

for i, model_path in enumerate(self.merge_config.model_path_list):
with fast_safe_open(os.path.join(model_path, index_list[i]["weight_map"][k]), framework="np") as w:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

意识到这里的写法可能有问题,因为不同model可能存储的文件对应位置不一致

with fast_safe_open(os.path.join(model_path, index_list[i]["weight_map"][k]), framework="np") as w:
tensor = w.get_tensor(k)
# 将 numpy 转为 Paddle Tensor
tensor = paddle.to_tensor(tensor, dtype="float32")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

不要加dtype float32,读出来是什么dtype就用什么dtype

framework="np",
) as w:
base_tensor = w.get_tensor(k)
base_tensor = paddle.to_tensor(base_tensor, dtype="float32")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里也是

del tensor_list
if self.merge_config.base_model_path is not None:
del base_tensor

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

del后加上paddle.device.cuda.empty_cache()保证显存释放

metadata={"format": "np"},
)
del merge_state_dict
gc.collect()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

del后加上paddle.device.cuda.empty_cache()保证显存释放

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants