msong97 commited on
Commit
384859e
·
1 Parent(s): 2776aea

Add log for memory usage tracking and fix bug with torch.set_grad_enabled

Browse files
Files changed (1) hide show
  1. app.py +29 -14
app.py CHANGED
@@ -18,8 +18,6 @@ from factories import PhysicsWithGenerator, EvalModel, BaselineModel, EvalDatase
18
  ### Config
19
  # run model inference on NVIDIA gpu if available
20
  DEVICE_STR = 'cuda' if torch.cuda.is_available() else 'cpu'
21
- # stops tracking values for gradients
22
- torch.set_grad_enabled(False)
23
 
24
 
25
  ### Gradio Utils
@@ -62,17 +60,24 @@ def generate_imgs(x: torch.Tensor,
62
  physics: PhysicsWithGenerator, use_gen: bool,
63
  baseline: BaselineModel, model: EvalModel,
64
  metrics: List[Metric]):
65
- print(torch.cuda.memory_allocated() / 1024**2)
 
 
 
 
66
  ### Compute y
67
- y = physics(x, use_gen) # possible reduction in img shape due to Blurring
 
68
 
69
  ### Compute x_hat from RAM & DPIR
70
  ram_time = time.time()
71
- out = model(y=y, physics=physics.physics)
 
72
  ram_time = time.time() - ram_time
73
 
74
  dpir_time = time.time()
75
- out_baseline = baseline(y=y, physics=physics.physics)
 
76
  dpir_time = time.time() - dpir_time
77
 
78
  ### Process tensors before metric computation
@@ -87,13 +92,15 @@ def generate_imgs(x: torch.Tensor,
87
 
88
  ### Metrics
89
  metrics_y = ""
90
- metrics_out = f"Inference time = {ram_time:.3f}s" + "\n"
91
- metrics_out_baseline = f"Inference time = {dpir_time:.3f}s" + "\n"
92
  for metric in metrics:
93
  if y.shape == x.shape:
94
  metrics_y += f"{metric.name} = {metric(y, x).item():.4f}" + "\n"
95
  metrics_out += f"{metric.name} = {metric(out, x).item():.4f}" + "\n"
96
  metrics_out_baseline += f"{metric.name} = {metric(out_baseline, x).item():.4f}" + "\n"
 
 
97
 
98
  ### Process y when y shape is different from x shape
99
  if physics.name == "MRI":
@@ -118,7 +125,10 @@ def generate_imgs(x: torch.Tensor,
118
  # Free memory
119
  del x, y, out, out_baseline, y_plot
120
  torch.cuda.empty_cache()
121
- print(torch.cuda.memory_allocated() / 1024**2)
 
 
 
122
 
123
  return x_pil, y_pil, out_pil, out_baseline_pil, physics.display_saved_params(), metrics_y, metrics_out, metrics_out_baseline
124
 
@@ -150,13 +160,17 @@ def get_dataset(dataset_name):
150
 
151
  # global variables shared by all users
152
  ram_model = EvalModel("unext_emb_physics_config_C", device_str=DEVICE_STR)
 
153
  psnr = Metric.get_list_metrics(["PSNR"], device_str=DEVICE_STR)
154
 
155
  generate_imgs_from_user_partial = partial(generate_imgs_from_user, model=ram_model, metrics=psnr)
156
  generate_imgs_from_dataset_partial = partial(generate_imgs_from_dataset, model=ram_model, metrics=psnr)
157
  generate_random_imgs_from_dataset_partial = partial(generate_random_imgs_from_dataset, model=ram_model, metrics=psnr)
158
 
159
- print(torch.cuda.memory_allocated() / 1024**2)
 
 
 
160
  ### Gradio Blocks interface
161
 
162
  title = "Inverse problem playground" # displayed on gradio tab and in the gradio page
@@ -172,7 +186,8 @@ with gr.Blocks(title=title, theme=gr.themes.Glass()) as interface:
172
  physics_placeholder = gr.State(lambda: get_physics_on_DEVICE_STR("MotionBlur_easy"))
173
  model_b_placeholder = gr.State(lambda: get_baseline_model_on_DEVICE_STR("DPIR"))
174
 
175
- print(torch.cuda.memory_allocated() / 1024**2)
 
176
  @gr.render(inputs=[dataset_placeholder, physics_placeholder, available_physics_placeholder])
177
  def dynamic_layout(dataset, physics, available_physics):
178
  ### LAYOUT
@@ -196,11 +211,11 @@ with gr.Blocks(title=title, theme=gr.themes.Glass()) as interface:
196
  load_button = gr.Button("Run on index image from dataset", size='md')
197
  load_random_button = gr.Button("Run on random image from dataset", size='md')
198
  with gr.Column(scale=1, min_width=160):
199
- observed_metrics = gr.Textbox(label="Observed metric", lines=3, key='metrics')
200
  with gr.Column(scale=1, min_width=160):
201
- out_a_metric = gr.Textbox(label="RAM output metrics", lines=3, key='ram_metrics')
202
  with gr.Column(scale=1, min_width=160):
203
- out_b_metric = gr.Textbox(label="DPIR output metrics", lines=3, key='dpir_metrics')
204
 
205
  # Manage physics
206
  with gr.Row():
 
18
  ### Config
19
  # run model inference on NVIDIA gpu if available
20
  DEVICE_STR = 'cuda' if torch.cuda.is_available() else 'cpu'
 
 
21
 
22
 
23
  ### Gradio Utils
 
60
  physics: PhysicsWithGenerator, use_gen: bool,
61
  baseline: BaselineModel, model: EvalModel,
62
  metrics: List[Metric]):
63
+ print(f"[Before inference] CUDA current allocated: {torch.cuda.memory_allocated() / 1024**2:.2f} MB")
64
+ print(f"[Before inference] CUDA current reserved: {torch.cuda.memory_reserved() / 1024**2:.2f} MB")
65
+ print(f"[Before inference] CUDA max allocated: {torch.cuda.max_memory_allocated() / 1024**2:.2f} MB")
66
+ print(f"[Before inference] CUDA max reserved: {torch.cuda.max_memory_reserved() / 1024**2:.2f} MB")
67
+
68
  ### Compute y
69
+ with torch.no_grad():
70
+ y = physics(x, use_gen) # possible reduction in img shape due to Blurring
71
 
72
  ### Compute x_hat from RAM & DPIR
73
  ram_time = time.time()
74
+ with torch.no_grad():
75
+ out = model(y=y, physics=physics.physics)
76
  ram_time = time.time() - ram_time
77
 
78
  dpir_time = time.time()
79
+ with torch.no_grad():
80
+ out_baseline = baseline(y=y, physics=physics.physics)
81
  dpir_time = time.time() - dpir_time
82
 
83
  ### Process tensors before metric computation
 
92
 
93
  ### Metrics
94
  metrics_y = ""
95
+ metrics_out = ""
96
+ metrics_out_baseline = ""
97
  for metric in metrics:
98
  if y.shape == x.shape:
99
  metrics_y += f"{metric.name} = {metric(y, x).item():.4f}" + "\n"
100
  metrics_out += f"{metric.name} = {metric(out, x).item():.4f}" + "\n"
101
  metrics_out_baseline += f"{metric.name} = {metric(out_baseline, x).item():.4f}" + "\n"
102
+ metrics_out += f"Inference time = {ram_time:.3f}s"
103
+ metrics_out_baseline += f"Inference time = {dpir_time:.3f}s"
104
 
105
  ### Process y when y shape is different from x shape
106
  if physics.name == "MRI":
 
125
  # Free memory
126
  del x, y, out, out_baseline, y_plot
127
  torch.cuda.empty_cache()
128
+ print(f"[After inference] CUDA current allocated: {torch.cuda.memory_allocated() / 1024**2:.2f} MB")
129
+ print(f"[After inference] CUDA current reserved: {torch.cuda.memory_reserved() / 1024**2:.2f} MB")
130
+ print(f"[After inference] CUDA max allocated: {torch.cuda.max_memory_allocated() / 1024**2:.2f} MB")
131
+ print(f"[After inference] CUDA max reserved: {torch.cuda.max_memory_reserved() / 1024**2:.2f} MB")
132
 
133
  return x_pil, y_pil, out_pil, out_baseline_pil, physics.display_saved_params(), metrics_y, metrics_out, metrics_out_baseline
134
 
 
160
 
161
  # global variables shared by all users
162
  ram_model = EvalModel("unext_emb_physics_config_C", device_str=DEVICE_STR)
163
+ ram_model.eval()
164
  psnr = Metric.get_list_metrics(["PSNR"], device_str=DEVICE_STR)
165
 
166
  generate_imgs_from_user_partial = partial(generate_imgs_from_user, model=ram_model, metrics=psnr)
167
  generate_imgs_from_dataset_partial = partial(generate_imgs_from_dataset, model=ram_model, metrics=psnr)
168
  generate_random_imgs_from_dataset_partial = partial(generate_random_imgs_from_dataset, model=ram_model, metrics=psnr)
169
 
170
+
171
+ print(f"[Init] CUDA max allocated: {torch.cuda.max_memory_allocated() / 1024**2:.2f} MB")
172
+ print(f"[Init] CUDA max reserved: {torch.cuda.max_memory_reserved() / 1024**2:.2f} MB")
173
+
174
  ### Gradio Blocks interface
175
 
176
  title = "Inverse problem playground" # displayed on gradio tab and in the gradio page
 
186
  physics_placeholder = gr.State(lambda: get_physics_on_DEVICE_STR("MotionBlur_easy"))
187
  model_b_placeholder = gr.State(lambda: get_baseline_model_on_DEVICE_STR("DPIR"))
188
 
189
+ print(f"[Render] CUDA max allocated: {torch.cuda.max_memory_allocated() / 1024**2:.2f} MB")
190
+ print(f"[Render] CUDA max reserved: {torch.cuda.max_memory_reserved() / 1024**2:.2f} MB")
191
  @gr.render(inputs=[dataset_placeholder, physics_placeholder, available_physics_placeholder])
192
  def dynamic_layout(dataset, physics, available_physics):
193
  ### LAYOUT
 
211
  load_button = gr.Button("Run on index image from dataset", size='md')
212
  load_random_button = gr.Button("Run on random image from dataset", size='md')
213
  with gr.Column(scale=1, min_width=160):
214
+ observed_metrics = gr.Textbox(label="Observed metric", lines=2, key='metrics')
215
  with gr.Column(scale=1, min_width=160):
216
+ out_a_metric = gr.Textbox(label="RAM output metrics", lines=2, key='ram_metrics')
217
  with gr.Column(scale=1, min_width=160):
218
+ out_b_metric = gr.Textbox(label="DPIR output metrics", lines=2, key='dpir_metrics')
219
 
220
  # Manage physics
221
  with gr.Row():