2020-12-17 11:03:45 +00:00
|
|
|
# Copyright The PyTorch Lightning team.
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
2020-12-23 19:38:57 +00:00
|
|
|
import gc
|
2021-07-20 21:23:53 +00:00
|
|
|
import os
|
2020-03-30 22:16:32 +00:00
|
|
|
import time
|
|
|
|
|
|
|
|
import numpy as np
|
|
|
|
import pytest
|
|
|
|
import torch
|
2020-12-17 11:03:45 +00:00
|
|
|
from tqdm import tqdm
|
2020-03-30 22:16:32 +00:00
|
|
|
|
2020-12-23 19:38:57 +00:00
|
|
|
from pytorch_lightning import LightningModule, seed_everything, Trainer
|
2021-07-20 21:23:53 +00:00
|
|
|
from tests.helpers.advanced_models import ParityModuleCIFAR, ParityModuleMNIST, ParityModuleRNN
|
|
|
|
|
2021-07-26 11:37:35 +00:00
|
|
|
_EXTEND_BENCHMARKS = os.getenv("PL_RUNNING_BENCHMARKS", "0") == "1"
|
2021-07-20 21:23:53 +00:00
|
|
|
_SHORT_BENCHMARKS = not _EXTEND_BENCHMARKS
|
|
|
|
_MARK_SHORT_BM = pytest.mark.skipif(_SHORT_BENCHMARKS, reason="Only run during Benchmarking")
|
2020-03-30 22:16:32 +00:00
|
|
|
|
|
|
|
|
2020-12-23 19:38:57 +00:00
|
|
|
def assert_parity_relative(pl_values, pt_values, norm_by: float = 1, max_diff: float = 0.1):
|
|
|
|
# assert speeds
|
|
|
|
diffs = np.asarray(pl_values) - np.mean(pt_values)
|
|
|
|
# norm by vanilla time
|
|
|
|
diffs = diffs / norm_by
|
|
|
|
# relative to mean reference value
|
|
|
|
diffs = diffs / np.mean(pt_values)
|
|
|
|
assert np.mean(diffs) < max_diff, f"Lightning diff {diffs} was worse than vanilla PT (threshold {max_diff})"
|
|
|
|
|
|
|
|
|
|
|
|
def assert_parity_absolute(pl_values, pt_values, norm_by: float = 1, max_diff: float = 0.55):
|
|
|
|
# assert speeds
|
|
|
|
diffs = np.asarray(pl_values) - np.mean(pt_values)
|
|
|
|
# norm by event count
|
|
|
|
diffs = diffs / norm_by
|
|
|
|
assert np.mean(diffs) < max_diff, f"Lightning {diffs} was worse than vanilla PT (threshold {max_diff})"
|
|
|
|
|
|
|
|
|
2020-11-27 18:36:50 +00:00
|
|
|
# ParityModuleMNIST runs with num_workers=1
|
2021-01-28 02:58:33 +00:00
|
|
|
@pytest.mark.parametrize(
|
2021-07-26 11:37:35 +00:00
|
|
|
"cls_model,max_diff_speed,max_diff_memory,num_epochs,num_runs",
|
2021-01-28 02:58:33 +00:00
|
|
|
[
|
2021-07-20 21:23:53 +00:00
|
|
|
(ParityModuleRNN, 0.05, 0.001, 4, 3),
|
|
|
|
(ParityModuleMNIST, 0.25, 0.001, 4, 3), # todo: lower this thr
|
|
|
|
pytest.param(ParityModuleCIFAR, 4.0, 0.0002, 2, 2, marks=_MARK_SHORT_BM),
|
2021-07-26 11:37:35 +00:00
|
|
|
],
|
2021-01-28 02:58:33 +00:00
|
|
|
)
|
2020-03-30 22:16:32 +00:00
|
|
|
@pytest.mark.skipif(not torch.cuda.is_available(), reason="test requires GPU machine")
|
2020-12-23 19:38:57 +00:00
|
|
|
def test_pytorch_parity(
|
2021-07-20 21:23:53 +00:00
|
|
|
tmpdir, cls_model: LightningModule, max_diff_speed: float, max_diff_memory: float, num_epochs: int, num_runs: int
|
2020-12-23 19:38:57 +00:00
|
|
|
):
|
2020-03-30 22:16:32 +00:00
|
|
|
"""
|
|
|
|
Verify that the same pytorch and lightning models achieve the same results
|
|
|
|
"""
|
2020-12-23 19:38:57 +00:00
|
|
|
lightning = measure_loops(cls_model, kind="PT Lightning", num_epochs=num_epochs, num_runs=num_runs)
|
|
|
|
vanilla = measure_loops(cls_model, kind="Vanilla PT", num_epochs=num_epochs, num_runs=num_runs)
|
2020-03-30 22:16:32 +00:00
|
|
|
|
|
|
|
# make sure the losses match exactly to 5 decimal places
|
2020-12-23 19:38:57 +00:00
|
|
|
print(f"Losses are for... \n vanilla: {vanilla['losses']} \n lightning: {lightning['losses']}")
|
2021-07-26 11:37:35 +00:00
|
|
|
for pl_out, pt_out in zip(lightning["losses"], vanilla["losses"]):
|
2020-03-30 22:16:32 +00:00
|
|
|
np.testing.assert_almost_equal(pl_out, pt_out, 5)
|
|
|
|
|
2020-12-23 19:38:57 +00:00
|
|
|
# drop the first run for initialize dataset (download & filter)
|
|
|
|
assert_parity_absolute(
|
2021-07-26 11:37:35 +00:00
|
|
|
lightning["durations"][1:], vanilla["durations"][1:], norm_by=num_epochs, max_diff=max_diff_speed
|
2020-12-17 11:03:45 +00:00
|
|
|
)
|
2020-04-15 00:23:36 +00:00
|
|
|
|
2021-07-26 11:37:35 +00:00
|
|
|
assert_parity_relative(lightning["memory"], vanilla["memory"], max_diff=max_diff_memory)
|
2020-03-30 22:16:32 +00:00
|
|
|
|
2020-12-23 19:38:57 +00:00
|
|
|
|
|
|
|
def _hook_memory():
|
|
|
|
if torch.cuda.is_available():
|
|
|
|
torch.cuda.synchronize()
|
|
|
|
used_memory = torch.cuda.max_memory_allocated()
|
|
|
|
else:
|
|
|
|
used_memory = np.nan
|
|
|
|
return used_memory
|
|
|
|
|
|
|
|
|
|
|
|
def measure_loops(cls_model, kind, num_runs=10, num_epochs=10):
|
2020-03-30 22:16:32 +00:00
|
|
|
"""
|
|
|
|
Returns an array with the last loss from each epoch for each run
|
|
|
|
"""
|
2020-12-17 11:03:45 +00:00
|
|
|
hist_losses = []
|
|
|
|
hist_durations = []
|
2020-12-23 19:38:57 +00:00
|
|
|
hist_memory = []
|
2020-03-30 22:16:32 +00:00
|
|
|
|
2020-12-23 19:38:57 +00:00
|
|
|
device_type = "cuda" if torch.cuda.is_available() else "cpu"
|
2020-05-12 11:53:20 +00:00
|
|
|
torch.backends.cudnn.deterministic = True
|
2021-07-26 11:37:35 +00:00
|
|
|
for i in tqdm(range(num_runs), desc=f"{kind} with {cls_model.__name__}"):
|
2020-12-23 19:38:57 +00:00
|
|
|
gc.collect()
|
2021-07-26 11:37:35 +00:00
|
|
|
if device_type == "cuda":
|
2020-12-23 19:38:57 +00:00
|
|
|
torch.cuda.empty_cache()
|
|
|
|
torch.cuda.reset_max_memory_cached()
|
|
|
|
torch.cuda.reset_max_memory_allocated()
|
|
|
|
torch.cuda.reset_accumulated_memory_stats()
|
|
|
|
torch.cuda.reset_peak_memory_stats()
|
|
|
|
time.sleep(1)
|
2020-03-30 22:16:32 +00:00
|
|
|
|
2020-12-23 19:38:57 +00:00
|
|
|
time_start = time.perf_counter()
|
2020-03-30 22:16:32 +00:00
|
|
|
|
2020-12-23 19:38:57 +00:00
|
|
|
_loop = lightning_loop if kind == "PT Lightning" else vanilla_loop
|
|
|
|
final_loss, used_memory = _loop(cls_model, idx=i, device_type=device_type, num_epochs=num_epochs)
|
2020-03-30 22:16:32 +00:00
|
|
|
|
|
|
|
time_end = time.perf_counter()
|
|
|
|
|
2020-12-23 19:38:57 +00:00
|
|
|
hist_losses.append(final_loss)
|
|
|
|
hist_durations.append(time_end - time_start)
|
|
|
|
hist_memory.append(used_memory)
|
2020-03-30 22:16:32 +00:00
|
|
|
|
2021-07-26 11:37:35 +00:00
|
|
|
return {"losses": hist_losses, "durations": hist_durations, "memory": hist_memory}
|
2020-03-30 22:16:32 +00:00
|
|
|
|
|
|
|
|
2021-07-26 11:37:35 +00:00
|
|
|
def vanilla_loop(cls_model, idx, device_type: str = "cuda", num_epochs=10):
|
2020-12-23 19:38:57 +00:00
|
|
|
device = torch.device(device_type)
|
|
|
|
# set seed
|
|
|
|
seed_everything(idx)
|
2020-03-30 22:16:32 +00:00
|
|
|
|
2020-12-23 19:38:57 +00:00
|
|
|
# init model parts
|
|
|
|
model = cls_model()
|
|
|
|
dl = model.train_dataloader()
|
|
|
|
optimizer = model.configure_optimizers()
|
2020-03-30 22:16:32 +00:00
|
|
|
|
2020-12-23 19:38:57 +00:00
|
|
|
# model to GPU
|
|
|
|
model = model.to(device)
|
2020-03-30 22:16:32 +00:00
|
|
|
|
2020-12-23 19:38:57 +00:00
|
|
|
epoch_losses = []
|
|
|
|
# as the first run is skipped, no need to run it long
|
|
|
|
for epoch in range(num_epochs if idx > 0 else 1):
|
2020-03-30 22:16:32 +00:00
|
|
|
|
2020-12-23 19:38:57 +00:00
|
|
|
# run through full training set
|
|
|
|
for j, batch in enumerate(dl):
|
|
|
|
batch = [x.to(device) for x in batch]
|
|
|
|
loss_dict = model.training_step(batch, j)
|
2021-07-26 11:37:35 +00:00
|
|
|
loss = loss_dict["loss"]
|
2020-12-23 19:38:57 +00:00
|
|
|
loss.backward()
|
|
|
|
optimizer.step()
|
|
|
|
optimizer.zero_grad()
|
|
|
|
|
|
|
|
# track last epoch loss
|
|
|
|
epoch_losses.append(loss.item())
|
|
|
|
|
|
|
|
return epoch_losses[-1], _hook_memory()
|
|
|
|
|
|
|
|
|
2021-07-26 11:37:35 +00:00
|
|
|
def lightning_loop(cls_model, idx, device_type: str = "cuda", num_epochs=10):
|
2020-12-23 19:38:57 +00:00
|
|
|
seed_everything(idx)
|
|
|
|
|
|
|
|
model = cls_model()
|
|
|
|
# init model parts
|
|
|
|
trainer = Trainer(
|
|
|
|
# as the first run is skipped, no need to run it long
|
|
|
|
max_epochs=num_epochs if idx > 0 else 1,
|
|
|
|
progress_bar_refresh_rate=0,
|
|
|
|
weights_summary=None,
|
2021-07-26 11:37:35 +00:00
|
|
|
gpus=1 if device_type == "cuda" else 0,
|
2020-12-23 19:38:57 +00:00
|
|
|
checkpoint_callback=False,
|
|
|
|
deterministic=True,
|
|
|
|
logger=False,
|
|
|
|
replace_sampler_ddp=False,
|
|
|
|
)
|
|
|
|
trainer.fit(model)
|
|
|
|
|
2021-06-22 09:49:32 +00:00
|
|
|
return trainer.fit_loop.running_loss.last().item(), _hook_memory()
|