-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.py
136 lines (111 loc) · 4.45 KB
/
utils.py
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import torch
import pdb
import tensorrt as trt
def torch_dtype_to_trt(dtype):
if dtype == torch.bool:
return trt.bool
elif dtype == torch.int8:
return trt.int8
elif dtype == torch.int32:
return trt.int32
elif dtype == torch.float16:
return trt.float16
elif dtype == torch.float32:
return trt.float32
else:
raise TypeError("%s is not supported by tensorrt" % dtype)
def torch_dtype_from_trt(dtype):
if dtype == trt.int8:
return torch.int8
elif dtype == trt.bool:
return torch.bool
elif dtype == trt.int32:
return torch.int32
elif dtype == trt.float16:
return torch.float16
elif dtype == trt.float32:
return torch.float32
else:
raise TypeError("%s is not supported by torch" % dtype)
def torch_device_to_trt(device):
if device.type == torch.device("cuda").type:
return trt.TensorLocation.DEVICE
elif device.type == torch.device("cpu").type:
return trt.TensorLocation.HOST
else:
return TypeError("%s is not supported by tensorrt" % device)
def torch_device_from_trt(device):
if device == trt.TensorLocation.DEVICE:
return torch.device("cuda")
elif device == trt.TensorLocation.HOST:
return torch.device("cpu")
else:
return TypeError("%s is not supported by torch" % device)
class TRTModule(torch.nn.Module):
def __init__(self, engine=None, input_names=None, output_names=None):
super(TRTModule, self).__init__()
self.engine = engine
if self.engine is not None:
self.context = self.engine.create_execution_context()
self.input_names = input_names
self.output_names = output_names
def forward(self, *inputs):
batch_size = inputs[0].shape[0]
bindings = [None] * (len(self.input_names) + len(self.output_names))
for i, input_name in enumerate(self.input_names):
idx = self.engine.get_binding_index(input_name)
self.context.set_binding_shape(idx, tuple(inputs[i].shape))
bindings[idx] = inputs[i].contiguous().data_ptr()
# create output tensors
outputs = [None] * len(self.output_names)
for i, output_name in enumerate(self.output_names):
idx = self.engine.get_binding_index(output_name)
dtype = torch_dtype_from_trt(self.engine.get_binding_dtype(idx))
shape = tuple(self.context.get_binding_shape(idx))
device = torch_device_from_trt(self.engine.get_location(idx))
output = torch.empty(size=shape, dtype=dtype, device=device)
outputs[i] = output
bindings[idx] = output.data_ptr()
self.context.execute_async_v2(bindings,
torch.cuda.current_stream().cuda_stream)
outputs = tuple(outputs)
if len(outputs) == 1:
outputs = outputs[0]
return outputs
def extract_model_state_dict(ckpt_path, model_name='model', prefixes_to_ignore=[], type='NGP'):
checkpoint = torch.load(ckpt_path, map_location='cpu')
checkpoint_ = {}
if 'state_dict' in checkpoint: # if it's a pytorch-lightning checkpoint
checkpoint = checkpoint['state_dict']
for k, v in checkpoint.items():
# pdb.set_trace()
if not k.startswith(model_name):
continue
if type=='NGP':
k = k[len(model_name)+1:]
for prefix in prefixes_to_ignore:
# pdb.set_trace()
if k.startswith(prefix):
break
else:
# Hard code: to rename key from SR model(Rec_v4)
k = k.replace('model.2.', 'model.rec_net.')
checkpoint_[k] = v
return checkpoint_
def load_ckpt(model, ckpt_path, model_name='model', prefixes_to_ignore=[], type='NGP'):
if not ckpt_path: return
model_dict = model.state_dict()
checkpoint_ = extract_model_state_dict(ckpt_path, model_name, prefixes_to_ignore, type)
model_dict.update(checkpoint_)
model.load_state_dict(model_dict)
def slim_ckpt(ckpt_path, save_poses=False):
ckpt = torch.load(ckpt_path, map_location='cpu')
# pop unused parameters
keys_to_pop = ['directions', 'model.density_grid', 'model.grid_coords']
if not save_poses: keys_to_pop += ['poses']
for k in ckpt['state_dict']:
if k.startswith('val_lpips'):
keys_to_pop += [k]
for k in keys_to_pop:
ckpt['state_dict'].pop(k, None)
return ckpt['state_dict']