MCPcopy
hub / github.com/huggingface/transformers / flush_memory

Function flush_memory

benchmark_v2/framework/benchmark_runner.py:82–109  ·  view source on GitHub ↗

Flush GPU memory and run garbage collection. If the flush_compile flag is set, we also clear the everything related to compile cache.

(flush_compile: bool = True)

Source from the content-addressed store, hash-verified

80
81
82def flush_memory(flush_compile: bool = True) -> None:
83 """Flush GPU memory and run garbage collection. If the flush_compile flag is set, we also clear the everything
84 related to compile cache."""
85 gc.collect()
86 # If needed, flush everything related to torch.compile
87 if flush_compile:
88 # Dynamo resets
89 torch._dynamo.reset()
90 torch._dynamo.reset_code_caches()
91 if hasattr(torch._inductor, "codecache"):
92 # Clear FX graph cache
93 if hasattr(torch._inductor.codecache, "FxGraphCache"):
94 torch._inductor.codecache.FxGraphCache.clear()
95 # Clear PyCodeCache
96 if hasattr(torch._inductor.codecache, "PyCodeCache"):
97 torch._inductor.codecache.PyCodeCache.cache_clear()
98 # Clear TritonFuture cache (for async compilation)
99 if hasattr(torch._inductor.codecache, "TritonFuture"):
100 if hasattr(torch._inductor.codecache.TritonFuture, "_compile_cache"):
101 torch._inductor.codecache.TritonFuture._compile_cache.clear()
102 # Clear device cache
103 if torch.cuda.is_available():
104 torch.cuda.empty_cache()
105 torch.cuda.synchronize()
106 elif is_torch_xpu_available():
107 torch.xpu.empty_cache()
108 torch.xpu.synchronize()
109 gc.collect()
110
111
112class BenchmarkStreamer(BaseStreamer):

Callers 3

cleanupMethod · 0.70
run_benchmarkMethod · 0.70
time_generateMethod · 0.70

Calls 5

is_torch_xpu_availableFunction · 0.90
collectMethod · 0.80
resetMethod · 0.45
clearMethod · 0.45
is_availableMethod · 0.45

Tested by

no test coverage detected