Skip to content

Commit 8b528a9

Browse files
author
wangzaijun
committed
remove tensor2bytes byte2tensor
1 parent 832b445 commit 8b528a9

File tree

5 files changed

+1
-49
lines changed

5 files changed

+1
-49
lines changed

lightllm/models/gemma3/layer_infer/pre_layer_infer.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
from lightllm.common.basemodel.triton_kernel.multimodal_emb import multimodal_emb
33
from lightllm.distributed.communication_op import all_reduce
44
from lightllm.models.qwen_vl.layer_infer.pre_layer_infer import LlamaMultimodalPreLayerInfer
5-
from lightllm.server.embed_cache.utils import bytes2tensor, get_shm_name_embed, read_shm
65

76

87
class Gemma3PreLayerInfer(LlamaMultimodalPreLayerInfer):

lightllm/models/qwen3_vl/layer_infer/pre_layer_infer.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,9 @@
22
import torch.distributed as dist
33

44
from lightllm.models.llama.layer_weights.pre_and_post_layer_weight import LlamaPreAndPostLayerWeight
5-
from lightllm.models.llama.infer_struct import LlamaInferStateInfo
65
from lightllm.models.qwen3_vl.infer_struct import Qwen3VLInferStateInfo
7-
8-
from lightllm.server.embed_cache.utils import (
9-
bytes2tensor,
10-
read_shm,
11-
get_shm_name_embed,
12-
)
136
from lightllm.common.basemodel.triton_kernel.multimodal_emb import multimodal_emb
147
from lightllm.distributed.communication_op import all_reduce
15-
168
from lightllm.models.qwen_vl.layer_infer.pre_layer_infer import LlamaMultimodalPreLayerInfer
179

1810

lightllm/models/qwen_vl/layer_infer/pre_layer_infer.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,7 @@
33

44
from lightllm.models.llama.layer_weights.pre_and_post_layer_weight import LlamaPreAndPostLayerWeight
55
from lightllm.models.llama.infer_struct import LlamaInferStateInfo
6-
76
from lightllm.models.llama.layer_infer.pre_layer_infer import LlamaPreLayerInfer
8-
from lightllm.utils.infer_utils import mark_cost_time
9-
from lightllm.server.embed_cache.utils import bytes2tensor, read_shm, get_shm_name_embed
107
from lightllm.common.basemodel.triton_kernel.multimodal_emb import multimodal_emb
118
from lightllm.distributed.communication_op import all_reduce
129

lightllm/models/qwen_vl/qwen_visual.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from PIL import Image
1212
from typing import Callable, Optional, Sequence, Tuple, List, Union
1313
import numpy as np
14-
from lightllm.server.embed_cache.utils import tensor2bytes, read_shm, create_shm, get_shm_name_data, get_shm_name_embed
14+
from lightllm.server.embed_cache.utils import read_shm, get_shm_name_data
1515
import torch
1616
from torch import nn
1717
from torch.nn import functional as F
Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,4 @@
1-
import torch
2-
import numpy as np
3-
from io import BytesIO
41
import multiprocessing.shared_memory as shm
5-
import time
6-
7-
8-
def tensor2bytes(t: torch.Tensor):
9-
# 转换为 numpy array,使用 contiguous 确保内存连续
10-
print(f"tensor2bytes shape: {t.shape} {t.is_contiguous()}")
11-
memory_size = t.numel() * t.element_size()
12-
out = torch.empty(memory_size, dtype=torch.uint8, device="cpu", pin_memory=True).copy_(t.view(torch.uint8).view(-1))
13-
return out.numpy().tobytes()
14-
15-
16-
def bytes2tensor(b):
17-
# 直接返回二进制数据的 uint8 tensor,外部自己转 dtype 和 view
18-
# 避免 numpy 不支持 bfloat16 等问题
19-
return torch.frombuffer(b, dtype=torch.uint8)
20-
21-
22-
def create_shm_and_dump(name, data: torch.Tensor):
23-
try:
24-
data_size = data.numel() * data.element_size()
25-
shared_memory = shm.SharedMemory(name=name, create=True, size=data_size)
26-
tensor = torch.frombuffer(shared_memory.buf, dtype=torch.uint8)
27-
out = torch.empty(data_size, dtype=torch.uint8, device="cpu", pin_memory=True).copy_(
28-
data.view(torch.uint8).view(-1)
29-
)
30-
tensor.copy_(out)
31-
return tensor
32-
except FileExistsError:
33-
print("Warning create shm {} failed because of FileExistsError!".format(name))
342

353

364
def create_shm(name, data):
@@ -57,7 +25,3 @@ def free_shm(name):
5725

5826
def get_shm_name_data(uid):
5927
return str(uid) + "-data"
60-
61-
62-
def get_shm_name_embed(uid):
63-
return str(uid) + "-embed"

0 commit comments

Comments
 (0)