@@ -29,259 +29,6 @@ def add_virtal_op_for_mutiple_finished_ops(
2929 graph .add_dependency (op .op_id , op_id )
3030 return graph , op .op_id
3131
32- def create_read_graph_cpu_storage (
33- gpu_blocks : torch .Tensor ,
34- cpu_blocks : torch .Tensor ,
35- ssd_blocks : torch .Tensor ,
36- gpu_device_id : int = 0 ,
37- layer_num : int = 1 ,
38- graph : Optional [TransferOpGraph ] = None ,
39- )-> Tuple [TransferOpGraph , List [int ]]:
40- """
41- Create a read transfer graph with (REMOTE_STORAGE / SSD)->CPU->GPU operations
42- ssd_blocks: the blocks of ssd that are used as a lower-level storage backend,
43- including ssd or remote storage. This can be empty, which means cpu-only kvcache.
44- Returns:
45- graph: TransferOpGraph
46- ops_to_be_tracked: List[int]: a list of transfer ops that can indicate
47- the completion of some key operations
48- """
49- assert len (gpu_blocks ) == len (cpu_blocks )
50- if graph is None :
51- graph = TransferOpGraph ()
52- assert len (gpu_blocks ) > 0
53- if len (ssd_blocks ) == 0 :
54- op = TransferOp (
55- graph_id = graph .graph_id ,
56- transfer_type = TransferType .H2D ,
57- src_block_ids = cpu_blocks ,
58- dst_block_ids = gpu_blocks ,
59- layer_id = 0 ,
60- layer_granularity = layer_num ,
61- )
62- graph .add_transfer_op (op )
63- return graph , [op .op_id ]
64- elif len (ssd_blocks ) < len (cpu_blocks ):
65- task_end_ops_ids = []
66- if len (ssd_blocks ) > 0 :
67- op1 = TransferOp (
68- graph_id = graph .graph_id ,
69- transfer_type = TransferType .DISK2H ,
70- src_block_ids = ssd_blocks ,
71- dst_block_ids = cpu_blocks [- len (ssd_blocks ):],
72- layer_id = 0 ,
73- layer_granularity = layer_num
74- )
75- graph .add_transfer_op (op1 )
76- op2 = TransferOp (
77- graph_id = graph .graph_id ,
78- transfer_type = TransferType .H2D ,
79- src_block_ids = cpu_blocks [- len (ssd_blocks ):],
80- dst_block_ids = gpu_blocks [- len (ssd_blocks ):],
81- layer_id = 0 ,
82- layer_granularity = layer_num
83- )
84- graph .add_transfer_op (op2 )
85- graph .add_dependency (op2 .op_id , op1 .op_id )
86- task_end_ops_ids .append (op2 .op_id )
87- op3 = TransferOp (
88- graph_id = graph .graph_id ,
89- transfer_type = TransferType .H2D ,
90- src_block_ids = cpu_blocks [:len (cpu_blocks ) - len (ssd_blocks )],
91- dst_block_ids = gpu_blocks [:len (cpu_blocks ) - len (ssd_blocks )],
92- layer_id = 0 ,
93- layer_granularity = layer_num
94- )
95- graph .add_transfer_op (op3 )
96- task_end_ops_ids .append (op3 .op_id )
97- return graph , task_end_ops_ids
98- else :
99- op1 = TransferOp (
100- graph_id = graph .graph_id ,
101- transfer_type = TransferType .DISK2H ,
102- src_block_ids = ssd_blocks ,
103- dst_block_ids = cpu_blocks ,
104- layer_id = 0 ,
105- layer_granularity = layer_num
106- )
107- graph .add_transfer_op (op1 )
108- op2 = TransferOp (
109- graph_id = graph .graph_id ,
110- transfer_type = TransferType .H2D ,
111- src_block_ids = cpu_blocks ,
112- dst_block_ids = gpu_blocks ,
113- layer_id = 0 ,
114- layer_granularity = layer_num
115- )
116- graph .add_transfer_op (op2 )
117- graph .add_dependency (op2 .op_id , op1 .op_id )
118- return graph , [op2 .op_id ]
119-
120- def create_read_graph_cpu_ssd_remote (
121- gpu_blocks : torch .Tensor ,
122- cpu_blocks : torch .Tensor ,
123- ssd_blocks : torch .Tensor ,
124- remote_blocks : torch .Tensor ,
125- gpu_device_id : int = 0 ,
126- layer_num : int = 1 ,
127- write_back_to_ssd : bool = True ,
128- )-> Tuple [TransferOpGraph , List [int ]]:
129- """
130- Create a read transfer graph with (REMOTE_STORAGE + SSD)->CPU->GPU operations
131- Returns:
132- graph: TransferOpGraph
133- finished_ops_ids: List[int]: a list of transfer ops that can indicate
134- the completion of each layer or each layer for each tp rank
135- """
136- graph = TransferOpGraph ()
137- finished_ops_ids : List [int ] = []
138- if len (remote_blocks ) == 0 :
139- graph , finished_ops_ids = create_read_graph_cpu_storage (gpu_blocks = gpu_blocks ,
140- cpu_blocks = cpu_blocks ,
141- ssd_blocks = ssd_blocks ,
142- gpu_device_id = gpu_device_id ,
143- layer_num = layer_num ,
144- graph = graph )
145- if len (finished_ops_ids ) > 0 :
146- graph , finished_ops_ids = add_virtal_op_for_mutiple_finished_ops (graph , finished_ops_ids )
147- assert len (finished_ops_ids ) > 0
148- return graph , finished_ops_ids
149- else :
150- if len (remote_blocks ) < len (gpu_blocks ):
151- graph , finished_ops_ids = create_read_graph_cpu_storage (gpu_blocks = gpu_blocks [:- len (remote_blocks )],
152- cpu_blocks = cpu_blocks [:- len (remote_blocks )],
153- ssd_blocks = ssd_blocks [:- len (remote_blocks )],
154- gpu_device_id = gpu_device_id ,
155- layer_num = layer_num ,
156- graph = graph )
157- op_r2h = TransferOp (
158- graph_id = graph .graph_id ,
159- transfer_type = TransferType .REMOTE2H ,
160- src_block_ids = remote_blocks ,
161- dst_block_ids = cpu_blocks [- len (remote_blocks ):],
162- layer_id = 0 ,
163- layer_granularity = layer_num
164- )
165- graph .add_transfer_op (op_r2h )
166- op_h2d = TransferOp (
167- graph_id = graph .graph_id ,
168- transfer_type = TransferType .H2D ,
169- src_block_ids = cpu_blocks [- len (remote_blocks ):],
170- dst_block_ids = gpu_blocks [- len (remote_blocks ):],
171- layer_id = 0 ,
172- layer_granularity = layer_num
173- )
174- graph .add_transfer_op (op_h2d )
175- graph .add_dependency (op_h2d .op_id , op_r2h .op_id )
176- if write_back_to_ssd :
177- op_h2disk = TransferOp (
178- graph_id = graph .graph_id ,
179- transfer_type = TransferType .H2DISK ,
180- src_block_ids = cpu_blocks [- len (remote_blocks ):],
181- dst_block_ids = ssd_blocks [- len (remote_blocks ):],
182- layer_id = 0 ,
183- layer_granularity = layer_num
184- )
185- graph .add_transfer_op (op_h2disk )
186- graph .add_dependency (op_h2disk .op_id , op_r2h .op_id )
187- finished_ops_ids .append (op_h2d .op_id )
188- if len (finished_ops_ids ) > 0 :
189- graph , finished_ops_ids = add_virtal_op_for_mutiple_finished_ops (graph , finished_ops_ids )
190- return graph , finished_ops_ids
191-
192- def create_write_graph_cpu_storage (
193- gpu_blocks : torch .Tensor ,
194- cpu_blocks : torch .Tensor ,
195- ssd_blocks : torch .Tensor ,
196- gpu_device_id : int = 0 ,
197- layer_num : int = 1 ,
198- graph : Optional [TransferOpGraph ] = None ,
199- )-> Tuple [TransferOpGraph , List [int ]]:
200- """
201- Create a write transfer graph with CPU->REMOTE_STORAGE / SSD operations
202- ssd_blocks: the blocks of ssd that are used as a lower-level storage backend,
203- including ssd or remote storage. This can be empty, which means cpu-only kvcache.
204- Write op granularity is larger: gpu->cpu is put into the same op.
205- Returns:
206- graph: TransferOpGraph
207- layer_wise_ops: List[int]: a list of transfer ops that can indicate
208- the completion of each layer or each layer for each tp rank
209- """
210- if graph is None :
211- graph = TransferOpGraph ()
212- op_d2h = TransferOp (
213- graph_id = graph .graph_id ,
214- transfer_type = TransferType .D2H ,
215- src_block_ids = gpu_blocks ,
216- dst_block_ids = cpu_blocks [- len (gpu_blocks ):],
217- layer_id = 0 ,
218- layer_granularity = layer_num
219- )
220- graph .add_transfer_op (op_d2h )
221- if len (ssd_blocks ) == 0 :
222- return graph , [op_d2h .op_id ]
223- else :
224- op_h2disk = TransferOp (
225- graph_id = graph .graph_id ,
226- transfer_type = TransferType .H2DISK ,
227- src_block_ids = cpu_blocks [- len (ssd_blocks ):],
228- dst_block_ids = ssd_blocks ,
229- layer_id = 0 ,
230- layer_granularity = layer_num
231- )
232- graph .add_transfer_op (op_h2disk )
233- graph .add_dependency (op_h2disk .op_id , op_d2h .op_id )
234- return graph , [op_d2h .op_id ]
235-
236- def create_write_graph_cpu_ssd_remote (
237- gpu_blocks : torch .Tensor ,
238- cpu_blocks : torch .Tensor ,
239- ssd_blocks : torch .Tensor ,
240- remote_blocks : torch .Tensor ,
241- gpu_device_id : int = 0 ,
242- layer_num : int = 1 ,
243- )-> Tuple [TransferOpGraph , List [int ]]:
244- """
245- Create a write transfer graph with CPU->REMOTE_STORAGE + SSD operations
246- Returns:
247- graph: TransferOpGraph
248- layer_wise_ops: List[int]: a list of transfer ops that can indicate
249- the completion of each layer or each layer for each tp rank
250- """
251- graph = TransferOpGraph ()
252- op_d2h = TransferOp (
253- graph_id = graph .graph_id ,
254- transfer_type = TransferType .D2H ,
255- src_block_ids = gpu_blocks ,
256- dst_block_ids = cpu_blocks [- len (gpu_blocks ):],
257- layer_id = 0 ,
258- layer_granularity = layer_num
259- )
260- graph .add_transfer_op (op_d2h )
261- if len (ssd_blocks ) != 0 :
262- op_h2disk = TransferOp (
263- graph_id = graph .graph_id ,
264- transfer_type = TransferType .H2DISK ,
265- src_block_ids = cpu_blocks [- len (gpu_blocks ):],
266- dst_block_ids = ssd_blocks ,
267- layer_id = 0 ,
268- layer_granularity = layer_num
269- )
270- graph .add_transfer_op (op_h2disk )
271- graph .add_dependency (op_h2disk .op_id , op_d2h .op_id )
272- if len (remote_blocks ) != 0 :
273- op_h2remote = TransferOp (
274- graph_id = graph .graph_id ,
275- transfer_type = TransferType .H2REMOTE ,
276- src_block_ids = cpu_blocks [- len (remote_blocks ):],
277- dst_block_ids = remote_blocks ,
278- layer_id = 0 ,
279- layer_granularity = layer_num
280- )
281- graph .add_transfer_op (op_h2remote )
282- graph .add_dependency (op_h2remote .op_id , op_d2h .op_id )
283- return graph , [op_d2h .op_id ]
284-
28532def convert_read_graph_to_layer_wise_graph (
28633 transfer_graph : TransferOpGraph ,
28734 finished_ops_ids : List [int ],
0 commit comments