Skip to content

Commit 8b1c38e

Browse files
fix image cache overhead (#930)
Co-authored-by: hiworldwzj <[email protected]> Co-authored-by: wangzaijun <[email protected]>
1 parent 4ca8b78 commit 8b1c38e

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

lightllm/server/embed_cache/utils.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,14 @@ def tensor2bytes(t: torch.Tensor):
88
# t = t.cpu().numpy().tobytes()
99
# return t
1010
buf = BytesIO()
11-
torch.save(t.detach().cpu(), buf)
11+
t = t.detach().cpu()
12+
# 这个地方进行新的empty并复制是因为,torch的tensor save的机制存在问题
13+
# 如果 t 是从一个大 tensor 上切片复制下来的的tensor, 在save的时候,其
14+
# 会保存大tensor的所有数据,所以会导致存储开销较大,需要申请一个新的tensor
15+
# 并进行复制,来打断这种联系。
16+
dest = torch.empty_like(t)
17+
dest.copy_(t)
18+
torch.save(dest, buf, _use_new_zipfile_serialization=False, pickle_protocol=4)
1219
buf.seek(0)
1320
return buf.read()
1421

0 commit comments

Comments
 (0)