-
Notifications
You must be signed in to change notification settings - Fork 586
Expand file tree
/
Copy pathutils.py
More file actions
67 lines (58 loc) · 2.58 KB
/
utils.py
File metadata and controls
67 lines (58 loc) · 2.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import torch
import matplotlib.pyplot as plt
from torch.utils.cpp_extension import load_inline
import re
import subprocess
def show_img(x, figsize=(4,3), **kwargs):
"Display HW or CHW format image `x`"
plt.figure(figsize=figsize)
plt.axis('off')
if len(x.shape)==3: x = x.permute(1,2,0) # CHW -> HWC
plt.imshow(x.cpu(), **kwargs)
cuda_begin = r'''
#include <torch/extension.h>
#include <stdio.h>
#include <c10/cuda/CUDAException.h>
#define CHECK_CUDA(x) TORCH_CHECK(x.device().is_cuda(), #x " must be a CUDA tensor")
#define CHECK_CONTIGUOUS(x) TORCH_CHECK(x.is_contiguous(), #x " must be contiguous")
#define CHECK_INPUT(x) CHECK_CUDA(x); CHECK_CONTIGUOUS(x)
#define CUDA_ERR(ans) { gpuAssert((ans), __FILE__, __LINE__); }
inline void gpuAssert(cudaError_t code, const char *file, int line, bool abort=true)
{
if (code != cudaSuccess)
{
fprintf(stderr,"GPUassert: %s %s %d\n", cudaGetErrorString(code), file, line);
if (abort) exit(code);
}
}
__host__ __device__ inline unsigned int cdiv(unsigned int a, unsigned int b) { return (a+b-1)/b;}
'''
def load_cuda(cuda_src, cpp_src, funcs, opt=True, verbose=False, name=None):
"Simple wrapper for torch.utils.cpp_extension.load_inline"
if name is None: name = funcs[0]
flags = "-O3 -Xptxas -O3 -Xcompiler -O3" if opt else "-O0 -Xptxas -O0 -Xcompiler -O0"
return load_inline(cuda_sources=[cuda_src], cpp_sources=[cpp_src], functions=funcs,
extra_cuda_cflags=[flags], verbose=verbose, name=name)
def cdiv(a,b):
"Int ceiling division of `a` over `b`"
return (a+b-1)//b
def get_sig(fname, src):
res = re.findall(rf'^(.+\s+{fname}\(.*?\))\s*{{?\s*$', src, re.MULTILINE)
return res[0]+';' if res else None
def print_cuda_info():
print("=== PyTorch CUDA Info ===")
print(f"PyTorch version: {torch.__version__}")
print(f"CUDA available: {torch.cuda.is_available()}")
print(f"CUDA version: {torch.version.cuda}")
print(f"cuDNN version: {torch.backends.cudnn.version()}")
print(f"Number of GPUs: {torch.cuda.device_count()}")
for i in range(torch.cuda.device_count()):
print(f" GPU {i}: {torch.cuda.get_device_name(i)}")
print(f" Current device: {torch.cuda.current_device()}")
print(f" Memory allocated: {torch.cuda.memory_allocated(i)/1e6:.2f} MB")
print(f" Memory cached : {torch.cuda.memory_reserved(i)/1e6:.2f} MB")
print("\n=== nvidia-smi Info (if available) ===")
try:
subprocess.run(["nvidia-smi"], check=True)
except Exception as e:
print(f"nvidia-smi not available: {e}")