差别
这里会显示出您选择的修订版和当前版本之间的差别。
| 后一修订版 | 前一修订版 | ||
| linux:gpu:nvidia显卡:常用脚本:操作gpu显存 [2025/10/22 07:38] – 创建 ctbots | linux:gpu:nvidia显卡:常用脚本:操作gpu显存 [2025/10/23 11:10] (当前版本) – ctbots | ||
|---|---|---|---|
| 行 1: | 行 1: | ||
| ====== 强行占用20G的显存 ====== | ====== 强行占用20G的显存 ====== | ||
| - | 因为某些特定的原因,我们必须占用20G的显存 | + | 因为某些特定的原因,我们必须占用  | 
| <code python use_gpu.py> | <code python use_gpu.py> | ||
| import torch | import torch | ||
| - | import  | + | import  | 
| - | from math import ceil | + | device  | 
| - | + | total_memory  | |
| - | def parse_memory_size(memory_str): | + | target_memory  | 
| - |     memory_str  | + | num_elements  | 
| - |     if memory_str.endswith(' | + | tensor = torch.randn(num_elements, dtype=torch.float32, | 
| - |         gb = float(memory_str[: | + | time.sleep(3600000000) | 
| - | return int(gb * 1024**3) | + | del tensor | 
| - |     elif memory_str.endswith(' | + | torch.cuda.empty_cache() | 
| - |         mb = float(memory_str[: | + | |
| - | return int(mb * 1024**2) | + | |
| - | else: | + | |
| - |         raise ValueError("Unsupported unit! Use ' | + | |
| - | + | ||
| - | def allocate_vram(memory_str=" | + | |
| - |     if not torch.cuda.is_available(): | + | |
| - |         raise RuntimeError(" | + | |
| - | + | ||
| - | target_bytes = parse_memory_size(memory_str) | + | |
| - | target_gb = target_bytes / (1024**3) | + | |
| - |     print(f" | + | |
| - | + | ||
| - |     gpu_props  | + | |
| - |      | + | |
| - |      | + | |
| - | + | ||
| - |     if target_gb > total_vram_gb: | + | |
| - |         raise RuntimeError(f" | + | |
| - | + | ||
| - |     element_size  | + | |
| - |     total_elements = ceil(target_bytes  | + | |
| - |     print(f" | + | |
| - | + | ||
| - | chunk_size = 10**9 | + | |
| - | tensors = [] | + | |
| - | allocated_elements = 0 | + | |
| - | + | ||
| - |     while allocated_elements < total_elements: | + | |
| - |         current_chunk = min(chunk_size, | + | |
| - | tensor = torch.randn( | + | |
| - | current_chunk, | + | |
| - |              | + | |
| - |              | + | |
| - | requires_grad=False | + | |
| - |          | + | |
| - |          | + | |
| - | allocated_elements += current_chunk | + | |
| - | + | ||
| - | allocated_gb = (allocated_elements * element_size) / (1024**3) | + | |
| - |         print(f" | + | |
| - | + | ||
| - | used_vram = torch.cuda.memory_allocated(gpu_id) / (1024**3) | + | |
| - | cached_vram = torch.cuda.memory_reserved(gpu_id) / (1024**3) | + | |
| - |     print(f" | + | |
| - |     print(f" | + | |
| - |     print(f" | + | |
| - | + | ||
| - |     input(" | + | |
| - | del tensors | + | |
| - | torch.cuda.empty_cache() | + | |
| - |     print(" | + | |
| - | + | ||
| - | if __name__ == " | + | |
| - |     parser = argparse.ArgumentParser(description=" | + | |
| - | parser.add_argument( | + | |
| - |         " | + | |
| - |         default=" | + | |
| - |         help=" | + | |
| - | ) | + | |
| - | parser.add_argument( | + | |
| - |         " | + | |
| - | type=int, | + | |
| - |         default=0, | + | |
| - |         help=" | + | |
| - | ) | + | |
| - | args = parser.parse_args() | + | |
| - |     allocate_vram(args.memory, | + | |
| </ | </ | ||