stream-ai-runtime 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- stream_ai_runtime-0.1.0.dist-info/METADATA +5 -0
- stream_ai_runtime-0.1.0.dist-info/RECORD +8 -0
- stream_ai_runtime-0.1.0.dist-info/WHEEL +5 -0
- stream_ai_runtime-0.1.0.dist-info/top_level.txt +1 -0
- stream_runtime/__init__.py +3 -0
- stream_runtime/buffer_manager.py +30 -0
- stream_runtime/config.py +6 -0
- stream_runtime/engine.py +26 -0
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
stream_runtime/__init__.py,sha256=X0UZqQqGaUhxkpCvrNDKkRjeckrARZdBp6Ac5RDdZok,120
|
|
2
|
+
stream_runtime/buffer_manager.py,sha256=f_zPpsT57-62o_LqhNDAEYIdG-C8OW1nefOTmoqsHx0,1385
|
|
3
|
+
stream_runtime/config.py,sha256=i8Nhp-RUN-tQA2AYYBb5e9wrf1cDwi7XHaqa7RsElmM,127
|
|
4
|
+
stream_runtime/engine.py,sha256=r2UW3X1-dtlXeTV-OHH_D4siJ4qxUfJ9vFdyiX57lOY,1040
|
|
5
|
+
stream_ai_runtime-0.1.0.dist-info/METADATA,sha256=QEVWm5rB-GL_LF_ckG6zQgfCC5os65aSMq-rFr84MJw,128
|
|
6
|
+
stream_ai_runtime-0.1.0.dist-info/WHEEL,sha256=mOdXR9C3wMVov7cSY8PY-XCzo3SmmWCHvGQr5csJoao,91
|
|
7
|
+
stream_ai_runtime-0.1.0.dist-info/top_level.txt,sha256=a8-xOiPZcRLIs3shDUsk0O6-OrhFewv5TOpKFLSHONQ,15
|
|
8
|
+
stream_ai_runtime-0.1.0.dist-info/RECORD,,
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
stream_runtime
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import torch
|
|
2
|
+
|
|
3
|
+
class DoubleBufferManager:
|
|
4
|
+
def __init__(self, shape, dtype=torch.float32, device="cuda"):
|
|
5
|
+
self.device = device
|
|
6
|
+
self.buffer_ping = torch.empty(shape, dtype=dtype, device=device)
|
|
7
|
+
self.buffer_pong = torch.empty(shape, dtype=dtype, device=device)
|
|
8
|
+
self.current_slot = 0
|
|
9
|
+
self.compute_stream = torch.cuda.Stream()
|
|
10
|
+
self.copy_stream = torch.cuda.Stream()
|
|
11
|
+
self.copy_done = [torch.cuda.Event(), torch.cuda.Event()]
|
|
12
|
+
self.compute_done = [torch.cuda.Event(), torch.cuda.Event()]
|
|
13
|
+
|
|
14
|
+
def prefetch_next_chunk(self, cpu_tensor):
|
|
15
|
+
next_slot = 1 - self.current_slot
|
|
16
|
+
with torch.cuda.stream(self.copy_stream):
|
|
17
|
+
self.compute_done[next_slot].wait(self.copy_stream)
|
|
18
|
+
target = self.buffer_pong if next_slot == 1 else self.buffer_ping
|
|
19
|
+
target.copy_(cpu_tensor, non_blocking=True)
|
|
20
|
+
self.copy_done[next_slot].record(self.copy_stream)
|
|
21
|
+
|
|
22
|
+
def get_ready_buffer(self):
|
|
23
|
+
with torch.cuda.stream(self.compute_stream):
|
|
24
|
+
self.copy_done[self.current_slot].wait(self.compute_stream)
|
|
25
|
+
ready_buf = self.buffer_ping if self.current_slot == 0 else self.buffer_pong
|
|
26
|
+
return ready_buf
|
|
27
|
+
|
|
28
|
+
def mark_compute_complete(self):
|
|
29
|
+
self.compute_done[self.current_slot].record(self.compute_stream)
|
|
30
|
+
self.current_slot = 1 - self.current_slot
|
stream_runtime/config.py
ADDED
stream_runtime/engine.py
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import torch
|
|
2
|
+
import torch.nn as nn
|
|
3
|
+
from .buffer_manager import DoubleBufferManager
|
|
4
|
+
|
|
5
|
+
class StreamModel:
|
|
6
|
+
def __init__(self, num_layers=32, dim=2048):
|
|
7
|
+
self.num_layers = num_layers
|
|
8
|
+
self.dim = dim
|
|
9
|
+
self.cpu_weights = [torch.randn(dim, dim, pin_memory=True) for _ in range(num_layers)]
|
|
10
|
+
self.buffer_mgr = DoubleBufferManager((dim, dim))
|
|
11
|
+
|
|
12
|
+
def forward(self, x):
|
|
13
|
+
with torch.cuda.stream(self.buffer_mgr.copy_stream):
|
|
14
|
+
self.buffer_mgr.buffer_ping.copy_(self.cpu_weights[0], non_blocking=True)
|
|
15
|
+
self.buffer_mgr.copy_done[0].record(self.buffer_mgr.copy_stream)
|
|
16
|
+
|
|
17
|
+
for i in range(self.num_layers):
|
|
18
|
+
if i + 1 < self.num_layers:
|
|
19
|
+
self.buffer_mgr.prefetch_next_chunk(self.cpu_weights[i + 1])
|
|
20
|
+
buf = self.buffer_mgr.get_ready_buffer()
|
|
21
|
+
with torch.cuda.stream(self.buffer_mgr.compute_stream):
|
|
22
|
+
x = torch.matmul(x, buf)
|
|
23
|
+
self.buffer_mgr.mark_compute_complete()
|
|
24
|
+
|
|
25
|
+
torch.cuda.synchronize()
|
|
26
|
+
return x
|