Skip to content

Commit e29858a

Browse files
committed
Reuse memory_pool
Signed-off-by: Hui Gao <[email protected]>
1 parent 948b8b9 commit e29858a

File tree

4 files changed

+71
-28
lines changed

4 files changed

+71
-28
lines changed

tensorrt_llm/_torch/memory_buffer_utils.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44

55
import torch
66

7+
from tensorrt_llm.logger import logger
8+
9+
from .utils import get_graph_pool
10+
711

812
@dataclass
913
class BufferBlock:
@@ -80,9 +84,20 @@ def get_buffer(self, tensor_shape: list[int], dtype: torch.dtype,
8084

8185
# No suitable buffer was found, so allocate a new one.
8286
# The new buffer is created with uint8 to represent raw bytes.
83-
new_buffer_tensor = torch.zeros((required_memory_size, ),
84-
device='cuda',
85-
dtype=torch.uint8)
87+
new_buffer_tensor = None
88+
try:
89+
with torch.cuda.memory.use_mem_pool(get_graph_pool()):
90+
new_buffer_tensor = torch.zeros((required_memory_size, ),
91+
device='cuda',
92+
dtype=torch.uint8)
93+
except Exception:
94+
# Need to check if this is an OOM exception
95+
logger.debug(
96+
f"Exception happened to create tensor from given memory pool")
97+
new_buffer_tensor = torch.zeros((required_memory_size, ),
98+
device='cuda',
99+
dtype=torch.uint8)
100+
86101
new_block = BufferBlock(buffer=new_buffer_tensor,
87102
is_reserved=reserve_buffer)
88103

tensorrt_llm/_torch/pyexecutor/cuda_graph_runner.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,9 @@ def needs_capture(self, key: Tuple[int, int, int]):
194194

195195
return key not in self.graph_outputs
196196

197+
def get_graph_pool(self):
198+
return self.memory_pool
199+
197200
def capture(self,
198201
key: Tuple[int, int, int],
199202
forward_fn: Callable,
@@ -255,6 +258,7 @@ def _setup_spec_decoding_and_forward(key: Tuple[int, int, int],
255258
capture_inputs)
256259
if postprocess_fn is not None:
257260
postprocess_fn(capture_inputs)
261+
258262
with torch.cuda.graph(graph, pool=self.memory_pool):
259263
output = _setup_spec_decoding_and_forward(
260264
key, forward_fn, capture_inputs)

tensorrt_llm/_torch/pyexecutor/model_engine.py

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@
4848
from ..speculative.mtp import SampleStateTensorsMTP
4949
from ..utils import (get_model_extra_attrs,
5050
set_per_request_piecewise_cuda_graph_flag,
51-
set_torch_compiling, with_model_extra_attrs)
51+
set_prefer_mem_pool, set_torch_compiling,
52+
with_model_extra_attrs)
5253
from .config import PyTorchConfig
5354
from .config_utils import is_mla
5455
from .cuda_graph_runner import CUDAGraphRunner
@@ -2186,35 +2187,35 @@ def forward(
21862187
new_tensors_device, cache_indirection_buffer)
21872188

21882189
self.iter_counter += 1
2190+
with set_prefer_mem_pool(self.cuda_graph_runner.get_graph_pool()):
2191+
if not maybe_graph:
2192+
# Fallback to eager execution if graph was not used
2193+
with MoeLoadBalancerIterContext(moe_load_balancer):
2194+
outputs = self._forward_step(inputs, gather_ids,
2195+
gather_context_logits)
2196+
else:
2197+
if self.cuda_graph_runner.needs_capture(key):
21892198

2190-
if not maybe_graph:
2191-
# Fallback to eager execution if graph was not used
2192-
with MoeLoadBalancerIterContext(moe_load_balancer):
2193-
outputs = self._forward_step(inputs, gather_ids,
2194-
gather_context_logits)
2195-
else:
2196-
if self.cuda_graph_runner.needs_capture(key):
2197-
2198-
def capture_forward_fn(inputs: Dict[str, Any]):
2199-
with MoeLoadBalancerIterContext(moe_load_balancer):
2200-
return self._forward_step(
2201-
inputs,
2202-
gather_ids=gather_ids,
2203-
gather_context_logits=gather_context_logits)
2199+
def capture_forward_fn(inputs: Dict[str, Any]):
2200+
with MoeLoadBalancerIterContext(moe_load_balancer):
2201+
return self._forward_step(
2202+
inputs,
2203+
gather_ids=gather_ids,
2204+
gather_context_logits=gather_context_logits)
22042205

2205-
def capture_postprocess_fn(inputs: Dict[str, Any]):
2206-
self._postprocess_inputs(inputs)
2206+
def capture_postprocess_fn(inputs: Dict[str, Any]):
2207+
self._postprocess_inputs(inputs)
22072208

2208-
self.cuda_graph_runner.capture(key, capture_forward_fn,
2209-
inputs,
2210-
capture_postprocess_fn)
2209+
self.cuda_graph_runner.capture(key, capture_forward_fn,
2210+
inputs,
2211+
capture_postprocess_fn)
22112212

2212-
# here we don't need to use context since cuda graph capture didn't run kernel.
2213-
# maybe we need a cleaner way to do this.
2214-
outputs = self.cuda_graph_runner.replay(key, inputs)
2215-
else:
2216-
with MoeLoadBalancerIterContext(moe_load_balancer):
2213+
# here we don't need to use context since cuda graph capture didn't run kernel.
2214+
# maybe we need a cleaner way to do this.
22172215
outputs = self.cuda_graph_runner.replay(key, inputs)
2216+
else:
2217+
with MoeLoadBalancerIterContext(moe_load_balancer):
2218+
outputs = self.cuda_graph_runner.replay(key, inputs)
22182219

22192220
self._execute_logit_post_processors(scheduled_requests, outputs)
22202221

tensorrt_llm/_torch/utils.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,3 +312,26 @@ def create_lm_head_tp_mapping(mapping: Mapping, token_count: int) -> Mapping:
312312
# It's here so that unit tests can mock it and turn it off.
313313
def _get_allow_chain_drafter() -> bool:
314314
return True
315+
316+
317+
_buffer_pool = None
318+
319+
320+
def set_mem_pool(buffer_pool):
321+
global _buffer_pool
322+
_buffer_pool = buffer_pool
323+
324+
325+
def get_graph_pool():
326+
global _buffer_pool
327+
return _buffer_pool
328+
329+
330+
@contextlib.contextmanager
331+
def set_prefer_mem_pool(mem_pool):
332+
old_buffer_pool = get_graph_pool()
333+
set_mem_pool(mem_pool)
334+
try:
335+
yield
336+
finally:
337+
set_mem_pool(old_buffer_pool)

0 commit comments

Comments
 (0)