-
Notifications
You must be signed in to change notification settings - Fork 631
[Docs]: Add guide for update weights #4151
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
Merged
Merged
Changes from all commits
Commits
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
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| # Update Weights | ||
|
|
||
| LMDeploy supports update model weights online for scenes such as RL training. Here are the steps to do so. | ||
|
|
||
| ## Step 1: Launch server | ||
|
|
||
| For pytorch backend you have to add `--distributed-executor-backend ray`. | ||
|
|
||
| ```shell | ||
| lmdeploy serve api_server internlm/internlm2_5-7b-chat --server-port 23333 --distributed-executor-backend ray # for pytorch backend | ||
| ``` | ||
|
|
||
| ## Step 2: Offloads weights & kv cache | ||
|
|
||
| Before update model weights, the server should offloads weights and kv cache. | ||
|
|
||
| ```python | ||
| from lmdeploy.utils import serialize_state_dict | ||
| import requests | ||
|
|
||
| BASE_URL = 'http://0.0.0.0:23333' | ||
| api_key = 'sk-xxx' | ||
|
|
||
| headers = { | ||
| "Content-Type": "application/json", | ||
| "Authorization": f"Bearer {api_key}", | ||
| } | ||
|
|
||
| # offloads weights and kv cache with level=2 | ||
| response = requests.post(f"{BASE_URL}/sleep", headers=headers, params=dict(tags=['weights', 'kv_cache'], level=2)) | ||
| assert response.status_code == 200, response.status_code | ||
|
|
||
| # wake up weights, the server is ready for update weights | ||
| response = requests.post(f"{BASE_URL}/wakeup", headers=headers, params=dict(tags=['weights'])) | ||
| assert response.status_code == 200, response.status_code | ||
| ``` | ||
|
|
||
| ## Step 3: Update weights | ||
|
|
||
| Split model weights into multi segments and update through `update_weights` endpoint. | ||
|
|
||
| ```python | ||
| segmented_state_dict: List[Dict[str, torch.Tensor]] = ... | ||
| num_segment = len(segmented_state_dict) | ||
| for seg_idx in range(num_segment): | ||
| serialized_data = serialize_state_dict(segmented_state_dict[seg_idx]) | ||
| data = dict(serialized_named_tensors=serialized_data, finished=seg_idx == num_segment-1) | ||
| response = requests.post(f"{BASE_URL}/update_weights", headers=headers, json=data) | ||
| assert response.status_code == 200, f"response.status_code = {response.status_code}" | ||
|
|
||
| ``` | ||
|
|
||
| **Note**: For pytorch backend, lmdeploy also supports flattened bucket tensors: | ||
|
|
||
| ```python | ||
| from lmdeploy.utils import serialize_state_dict, FlattenedTensorBucket, FlattenedTensorMetadata | ||
|
|
||
| segmented_state_dict: List[Dict[str, torch.Tensor]] = ... | ||
| num_segment = len(segmented_state_dict) | ||
| for seg_idx in range(num_segment): | ||
| named_tensors = list(segmented_state_dict[seg_idx].items()) | ||
| bucket = FlattenedTensorBucket(named_tensors=named_tensors) | ||
| metadata = bucket.get_metadata() | ||
| flattened_tensor_data = dict(flattened_tensor=bucket.get_flattened_tensor(), metadata=metadata) | ||
| serialized_data = serialize_state_dict(flattened_tensor_data) | ||
| data = dict(serialized_named_tensors=serialized_data, finished=seg_idx == num_segment-1, load_format='flattened_bucket') | ||
| response = requests.post(f"{BASE_URL}/update_weights", headers=headers, json=data) | ||
| assert response.status_code == 200, f"response.status_code = {response.status_code}" | ||
| ``` | ||
|
|
||
| ## Step 4: Wakeup server | ||
|
|
||
| After update model weights, the server should onloads kv cache and provide serving again with the new updated weights. | ||
|
|
||
| ```python | ||
| response = requests.post(f"{BASE_URL}/wakeup", headers=headers, params=dict(tags=['kv_cache'])) | ||
| assert response.status_code == 200, response.status_code | ||
| ``` | ||
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| # 权重更新 | ||
|
|
||
| LMDeploy支持在线权重更新,方便RL训练等场景下的使用。以下是权重更新的步骤: | ||
|
|
||
| ## 步骤 1: 启动服务 | ||
|
|
||
| For pytorch backend you have to add `--distributed-executor-backend ray`. | ||
|
|
||
| ```shell | ||
| lmdeploy serve api_server internlm/internlm2_5-7b-chat --server-port 23333 --distributed-executor-backend ray # for pytorch backend | ||
| ``` | ||
|
|
||
| ## 步骤 2: 卸载权重和KV缓存 | ||
|
|
||
| 在权重更新前,需要调用API卸载权重和KV缓存,使推理引擎处于可更新状态: | ||
|
|
||
| ```python | ||
| from lmdeploy.utils import serialize_state_dict | ||
| import requests | ||
|
|
||
| BASE_URL = 'http://0.0.0.0:23333' | ||
| api_key = 'sk-xxx' | ||
|
|
||
| headers = { | ||
| "Content-Type": "application/json", | ||
| "Authorization": f"Bearer {api_key}", | ||
| } | ||
|
|
||
| # offloads weights and kv cache with level=2 | ||
| response = requests.post(f"{BASE_URL}/sleep", headers=headers, params=dict(tags=['weights', 'kv_cache'], level=2)) | ||
| assert response.status_code == 200, response.status_code | ||
|
|
||
| # wake up weights, the server is ready for update weights | ||
| response = requests.post(f"{BASE_URL}/wakeup", headers=headers, params=dict(tags=['weights'])) | ||
| assert response.status_code == 200, response.status_code | ||
| ``` | ||
|
|
||
| ## 步骤 3: 更新权重 | ||
|
|
||
| 将模型权重切分后调用`update_weights`API进行更新。 | ||
|
|
||
| ```python | ||
| segmented_state_dict: List[Dict[str, torch.Tensor]] = ... | ||
| num_segment = len(segmented_state_dict) | ||
| for seg_idx in range(num_segment): | ||
| serialized_data = serialize_state_dict(segmented_state_dict[seg_idx]) | ||
| data = dict(serialized_named_tensors=serialized_data, finished=seg_idx == num_segment-1) | ||
| response = requests.post(f"{BASE_URL}/update_weights", headers=headers, json=data) | ||
| assert response.status_code == 200, f"response.status_code = {response.status_code}" | ||
|
|
||
| ``` | ||
|
|
||
| **注意**: 对于pytorch推理后端,lmdeploy还支持扁平化桶张量(flattened bucket tensor)传输方式: | ||
|
|
||
| ```python | ||
| from lmdeploy.utils import serialize_state_dict, FlattenedTensorBucket, FlattenedTensorMetadata | ||
|
|
||
| segmented_state_dict: List[Dict[str, torch.Tensor]] = ... | ||
| num_segment = len(segmented_state_dict) | ||
| for seg_idx in range(num_segment): | ||
| named_tensors = list(segmented_state_dict[seg_idx].items()) | ||
| bucket = FlattenedTensorBucket(named_tensors=named_tensors) | ||
| metadata = bucket.get_metadata() | ||
| flattened_tensor_data = dict(flattened_tensor=bucket.get_flattened_tensor(), metadata=metadata) | ||
| serialized_data = serialize_state_dict(flattened_tensor_data) | ||
| data = dict(serialized_named_tensors=serialized_data, finished=seg_idx == num_segment-1, load_format='flattened_bucket') | ||
| response = requests.post(f"{BASE_URL}/update_weights", headers=headers, json=data) | ||
| assert response.status_code == 200, f"response.status_code = {response.status_code}" | ||
| ``` | ||
|
|
||
| ## 步骤 4: 唤醒引擎 | ||
|
|
||
| 权重更新后,调用API构建KV缓存,唤醒引擎,重新提供推理服务。 | ||
|
|
||
| ```python | ||
| response = requests.post(f"{BASE_URL}/wakeup", headers=headers, params=dict(tags=['kv_cache'])) | ||
| assert response.status_code == 200, response.status_code | ||
| ``` |
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
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
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
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.
Uh oh!
There was an error while loading. Please reload this page.