Skip to content

Commit

Permalink
feature/1124 Fixed memory consumption in PL tests
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexandreBrown committed Jul 22, 2022
1 parent 8f39eff commit 8216315
Show file tree
Hide file tree
Showing 19 changed files with 40 additions and 0 deletions.
2 changes: 2 additions & 0 deletions tests/models/efficient_det/lightning/test_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ def test_lightining_efficientdet_finalizes_metrics_on_test_epoch_end(
fridge_efficientdet_model, light_model_cls, metrics
):
light_model = light_model_cls(fridge_efficientdet_model, metrics=metrics)
torch.set_grad_enabled(False)

light_model.test_epoch_end(None)

Expand All @@ -63,6 +64,7 @@ def test_lightining_efficientdet_logs_losses_during_test_step(
light_model.convert_raw_predictions = lambda *args: None
light_model.compute_loss = lambda *args: None
light_model.accumulate_metrics = lambda *args: None
torch.set_grad_enabled(False)

light_model.test_step(batch, 0)

Expand Down
2 changes: 2 additions & 0 deletions tests/models/efficient_det/lightning/test_train.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ def fake_compute_loss(self, *args):
return expected_loss

light_model.compute_loss = fake_compute_loss
torch.set_grad_enabled(False)

loss = light_model.training_step(batch, 0)

Expand All @@ -66,6 +67,7 @@ def test_lightining_efficientdet_logs_losses_during_training_step(
light_model = light_model_cls(model=fridge_efficientdet_model, metrics=None)
for batch in train_dl:
break
torch.set_grad_enabled(False)

light_model.training_step(batch, 0)

Expand Down
2 changes: 2 additions & 0 deletions tests/models/efficient_det/lightning/test_validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ def test_lightining_efficientdet_finalizes_metrics_on_validation_epoch_end(
):
light_model = light_model_cls(fridge_efficientdet_model, metrics=metrics)
light_model.convert_raw_predictions = lambda *args: None
torch.set_grad_enabled(False)

light_model.validation_epoch_end(None)

Expand All @@ -65,6 +66,7 @@ def test_lightining_efficientdet_logs_losses_during_validation_step(
light_model.convert_raw_predictions = lambda *args: None
light_model.compute_loss = lambda *args: None
light_model.accumulate_metrics = lambda *args: None
torch.set_grad_enabled(False)

light_model.validation_step(batch, 0)

Expand Down
1 change: 1 addition & 0 deletions tests/models/fastai/unet/lightning/test_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ def log(self, key, value, **args):
for batch in valid_dl:
break
light_model = LightModel(model)
torch.set_grad_enabled(False)

light_model.test_step(batch, 0)

Expand Down
2 changes: 2 additions & 0 deletions tests/models/fastai/unet/lightning/test_train.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ def configure_optimizers(self):
expected_loss = random.randint(0, 1000)
light_model = LightModel(model)
light_model.compute_loss = lambda *args: expected_loss
torch.set_grad_enabled(False)

loss = light_model.training_step(batch, 0)

Expand Down Expand Up @@ -83,6 +84,7 @@ def log(self, key, value, **args):
for batch in train_dl:
break
light_model = LightModel(model)
torch.set_grad_enabled(False)

light_model.training_step(batch, 0)

Expand Down
1 change: 1 addition & 0 deletions tests/models/fastai/unet/lightning/test_validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ def log(self, key, value, **args):
for batch in valid_dl:
break
light_model = LightModel(model)
torch.set_grad_enabled(False)

light_model.validation_step(batch, 0)

Expand Down
6 changes: 6 additions & 0 deletions tests/models/mmdet/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ def configure_optimizers(self):
for batch in train_dl:
batch
break
torch.set_grad_enabled(False)

loss = light_model.training_step(batch, 0)

Expand Down Expand Up @@ -123,6 +124,7 @@ def log(self, key, value, **args):
for batch in train_dl:
batch
break
torch.set_grad_enabled(False)

light_model.training_step(batch, 0)

Expand Down Expand Up @@ -178,6 +180,7 @@ def log(self, key, value, **args):
for batch in valid_dl:
batch
break
torch.set_grad_enabled(False)

light_model.validation_step(batch, 0)

Expand All @@ -202,6 +205,7 @@ def finalize_metrics(self):
self.was_finalize_metrics_called = True

light_model = LitModel(model)
torch.set_grad_enabled(False)

light_model.validation_epoch_end(None)

Expand Down Expand Up @@ -257,6 +261,7 @@ def log(self, key, value, **args):
for batch in valid_dl:
batch
break
torch.set_grad_enabled(False)

light_model.test_step(batch, 0)

Expand All @@ -281,6 +286,7 @@ def finalize_metrics(self):
self.was_finalize_metrics_called = True

light_model = LitModel(model)
torch.set_grad_enabled(False)

light_model.test_epoch_end(None)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ def test_lightining_faster_rcnn_finalizes_metrics_on_test_epoch_end(
fridge_faster_rcnn_model, light_model_cls, metrics
):
light_model = light_model_cls(fridge_faster_rcnn_model, metrics=metrics)
torch.set_grad_enabled(False)

light_model.test_epoch_end(None)

Expand All @@ -61,6 +62,7 @@ def test_lightining_faster_rcnn_logs_losses_during_test_step(
break
light_model.convert_raw_predictions = lambda **args: None
light_model.accumulate_metrics = lambda **args: None
torch.set_grad_enabled(False)

light_model.test_step(batch, 0)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ def test_lightining_faster_rcnn_training_step_returns_loss(
light_model.compute_loss = lambda *args: expected_loss
for batch in train_dl:
break
torch.set_grad_enabled(False)

loss = light_model.training_step(batch, 0)

Expand All @@ -61,6 +62,7 @@ def test_lightining_faster_rcnn_logs_losses_during_training_step(
light_model = light_model_cls(fridge_faster_rcnn_model, metrics=None)
for batch in train_dl:
break
torch.set_grad_enabled(False)

light_model.training_step(batch, 0)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ def test_lightining_faster_rcnn_finalizes_metrics_on_validation_epoch_end(
fridge_faster_rcnn_model, light_model_cls, metrics
):
light_model = light_model_cls(fridge_faster_rcnn_model, metrics=metrics)
torch.set_grad_enabled(False)

light_model.validation_epoch_end(None)

Expand All @@ -61,6 +62,7 @@ def test_lightining_faster_rcnn_logs_losses_during_validation_step(
break
light_model.convert_raw_predictions = lambda **args: None
light_model.accumulate_metrics = lambda **args: None
torch.set_grad_enabled(False)

light_model.validation_step(batch, 0)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ def test_lightining_keypoints_rcnn_test(ochuman_keypoints_dls, light_model_cls):
def test_lightining_keypoints_finalizes_metrics_on_test_epoch_end(light_model_cls):
model = keypoint_rcnn.model(num_keypoints=19)
light_model = light_model_cls(model)
torch.set_grad_enabled(False)

light_model.test_epoch_end(None)

Expand All @@ -58,6 +59,7 @@ def test_lightining_keypoints_rcnn_logs_losses_during_test_step(
break
light_model.convert_raw_predictions = lambda **args: None
light_model.accumulate_metrics = lambda **args: None
torch.set_grad_enabled(False)

light_model.test_step(batch, 0)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ def test_lightining_keypoints_rcnn_training_step_returns_loss(
light_model.compute_loss = lambda *args: expected_loss
for batch in train_dl:
break
torch.set_grad_enabled(False)

loss = light_model.training_step(batch, 0)

Expand All @@ -60,6 +61,7 @@ def test_lightining_keypoints_rcnn_logs_losses_during_training_step(
light_model = light_model_cls(model)
for batch in train_dl:
break
torch.set_grad_enabled(False)

light_model.training_step(batch, 0)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ def test_lightining_keypoints_finalizes_metrics_on_validation_epoch_end(
):
model = keypoint_rcnn.model(num_keypoints=19)
light_model = light_model_cls(model)
torch.set_grad_enabled(False)

light_model.validation_epoch_end(None)

Expand All @@ -60,6 +61,7 @@ def test_lightining_keypoints_rcnn_logs_losses_during_validation_step(
break
light_model.convert_raw_predictions = lambda **args: None
light_model.accumulate_metrics = lambda **args: None
torch.set_grad_enabled(False)

light_model.validation_step(batch, 0)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ def test_lightining_retinanet_finalizes_metrics_on_test_epoch_end(
fridge_retinanet_model, light_model_cls, metrics
):
light_model = light_model_cls(fridge_retinanet_model, metrics=metrics)
torch.set_grad_enabled(False)

light_model.test_epoch_end(None)

Expand All @@ -61,6 +62,7 @@ def test_lightining_retinanet_logs_losses_during_test_step(
break
light_model.convert_raw_predictions = lambda **args: None
light_model.accumulate_metrics = lambda **args: None
torch.set_grad_enabled(False)

light_model.test_step(batch, 0)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ def test_lightining_retinanet_training_step_returns_loss(
light_model.compute_loss = lambda *args: expected_loss
for batch in train_dl:
break
torch.set_grad_enabled(False)

loss = light_model.training_step(batch, 0)

Expand All @@ -59,6 +60,7 @@ def test_lightining_retinanet_logs_losses_during_training_step(
light_model = light_model_cls(fridge_retinanet_model)
for batch in train_dl:
break
torch.set_grad_enabled(False)

light_model.training_step(batch, 0)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ def test_lightining_retinanet_finalizes_metrics_on_validation_epoch_end(
fridge_retinanet_model, light_model_cls, metrics
):
light_model = light_model_cls(fridge_retinanet_model, metrics=metrics)
torch.set_grad_enabled(False)

light_model.validation_epoch_end(None)

Expand All @@ -61,6 +62,7 @@ def test_lightining_retinanet_logs_losses_during_validation_step(
break
light_model.convert_raw_predictions = lambda **args: None
light_model.accumulate_metrics = lambda **args: None
torch.set_grad_enabled(False)

light_model.validation_step(batch, 0)

Expand Down
2 changes: 2 additions & 0 deletions tests/models/ultralytics/yolov5/lightning/test_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ def test_lightning_yolo_finalizes_metrics_on_test_epoch_end(backbone, light_mode
)
metrics = [COCOMetric(metric_type=COCOMetricType.bbox)]
light_model = light_model_cls(model, metrics=metrics)
torch.set_grad_enabled(False)

light_model.test_epoch_end(None)

Expand Down Expand Up @@ -88,6 +89,7 @@ def log(self, key, value, **args):
for batch in valid_dl:
batch
break
torch.set_grad_enabled(False)

light_model.test_step(batch, 0)

Expand Down
2 changes: 2 additions & 0 deletions tests/models/ultralytics/yolov5/lightning/test_train.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ def configure_optimizers(self):
for batch in train_dl:
batch
break
torch.set_grad_enabled(False)

loss = light_model.training_step(batch, 0)

Expand Down Expand Up @@ -94,6 +95,7 @@ def log(self, key, value, **args):
for batch in train_dl:
batch
break
torch.set_grad_enabled(False)

light_model.training_step(batch, 0)

Expand Down
2 changes: 2 additions & 0 deletions tests/models/ultralytics/yolov5/lightning/test_validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ def test_lightning_yolo_finalizes_metrics_on_validation_epoch_end(
)
metrics = [COCOMetric(metric_type=COCOMetricType.bbox)]
light_model = light_model_cls(model, metrics=metrics)
torch.set_grad_enabled(False)

light_model.validation_epoch_end(None)

Expand Down Expand Up @@ -90,6 +91,7 @@ def log(self, key, value, **args):
for batch in valid_dl:
batch
break
torch.set_grad_enabled(False)

light_model.validation_step(batch, 0)

Expand Down

0 comments on commit 8216315

Please sign in to comment.