Skip to content

Commit

Permalink
Merge branch 'master' into serve
Browse files Browse the repository at this point in the history
  • Loading branch information
fnhirwa committed Mar 16, 2024
2 parents 19dcb9f + 5325446 commit 05378b5
Show file tree
Hide file tree
Showing 14 changed files with 42 additions and 35 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -146,7 +146,7 @@ class LitAutoEncoder(L.LightningModule):

def training_step(self, batch, batch_idx):
# training_step defines the train loop. It is independent of forward
x, y = batch
x, _ = batch
x = x.view(x.size(0), -1)
z = self.encoder(x)
x_hat = self.decoder(z)
Expand Down
2 changes: 1 addition & 1 deletion docs/source-app/code_samples/convert_pl_to_app/train.py
Expand Up @@ -25,7 +25,7 @@ def forward(self, x):
def training_step(self, batch, batch_idx):
# training_step defines the train loop.
# It is independent of forward
x, y = batch
x, _ = batch
x = x.view(x.size(0), -1)
z = self.encoder(x)
x_hat = self.decoder(z)
Expand Down
4 changes: 2 additions & 2 deletions docs/source-pytorch/common/evaluation_basic.rst
Expand Up @@ -45,7 +45,7 @@ To add a test loop, implement the **test_step** method of the LightningModule
def test_step(self, batch, batch_idx):
# this is the test loop
x, y = batch
x, _ = batch
x = x.view(x.size(0), -1)
z = self.encoder(x)
x_hat = self.decoder(z)
Expand Down Expand Up @@ -105,7 +105,7 @@ To add a validation loop, implement the **validation_step** method of the Lightn
def validation_step(self, batch, batch_idx):
# this is the validation loop
x, y = batch
x, _ = batch
x = x.view(x.size(0), -1)
z = self.encoder(x)
x_hat = self.decoder(z)
Expand Down
2 changes: 1 addition & 1 deletion docs/source-pytorch/common/notebooks.rst
Expand Up @@ -57,7 +57,7 @@ Paste the following code block into a notebook cell:
self.decoder = decoder
def training_step(self, batch, batch_idx):
x, y = batch
x, _ = batch
x = x.view(x.size(0), -1)
z = self.encoder(x)
x_hat = self.decoder(z)
Expand Down
2 changes: 1 addition & 1 deletion docs/source-pytorch/model/train_model_basic.rst
Expand Up @@ -68,7 +68,7 @@ The LightningModule is the full **recipe** that defines how your nn.Modules inte
def training_step(self, batch, batch_idx):
# training_step defines the train loop.
x, y = batch
x, _ = batch
x = x.view(x.size(0), -1)
z = self.encoder(x)
x_hat = self.decoder(z)
Expand Down
2 changes: 1 addition & 1 deletion docs/source-pytorch/starter/introduction.rst
Expand Up @@ -129,7 +129,7 @@ A LightningModule enables your PyTorch nn.Module to play together in complex way
def training_step(self, batch, batch_idx):
# training_step defines the train loop.
# it is independent of forward
x, y = batch
x, _ = batch
x = x.view(x.size(0), -1)
z = self.encoder(x)
x_hat = self.decoder(z)
Expand Down
6 changes: 3 additions & 3 deletions src/lightning/app/cli/react-ui-template/ui/yarn.lock
Expand Up @@ -918,9 +918,9 @@ find-root@^1.1.0:
integrity sha512-NKfW6bec6GfKc0SGx1e07QZY9PE99u0Bft/0rzSD5k3sO/vwkVUpDUKVm5Gpp5Ue3YfShPFTX2070tDs5kB9Ng==

follow-redirects@^1.15.0:
version "1.15.4"
resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.15.4.tgz#cdc7d308bf6493126b17ea2191ea0ccf3e535adf"
integrity sha512-Cr4D/5wlrb0z9dgERpUL3LrmPKVDsETIJhaCMeDfuFYcqa5bldGV6wBsAN6X/vxlXQtFBMrXdXxdL8CbDTGniw==
version "1.15.6"
resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.15.6.tgz#7f815c0cda4249c74ff09e95ef97c23b5fd0399b"
integrity sha512-wWN62YITEaOpSK584EZXJafH1AGpO8RVgElfkuXbTOrPX4fIfOyEpW/CsiNd8JdYrAoOvafRTOEnvsO++qCqFA==

form-data@^4.0.0:
version "4.0.0"
Expand Down
2 changes: 1 addition & 1 deletion src/pytorch_lightning/README.md
Expand Up @@ -174,7 +174,7 @@ class LitAutoEncoder(pl.LightningModule):

def training_step(self, batch, batch_idx):
# training_step defines the train loop. It is independent of forward
x, y = batch
x, _ = batch
x = x.view(x.size(0), -1)
z = self.encoder(x)
x_hat = self.decoder(z)
Expand Down
10 changes: 5 additions & 5 deletions tests/tests_pytorch/accelerators/test_cpu.py
Expand Up @@ -29,7 +29,7 @@ def test_availability():


@RunIf(psutil=True)
def test_get_device_stats(tmpdir):
def test_get_device_stats(tmp_path):
gpu_stats = CPUAccelerator().get_device_stats(Mock())
fields = ["cpu_vm_percent", "cpu_percent", "cpu_swap_percent"]

Expand All @@ -38,7 +38,7 @@ def test_get_device_stats(tmpdir):


@pytest.mark.parametrize("restore_after_pre_setup", [True, False])
def test_restore_checkpoint_after_pre_setup(tmpdir, restore_after_pre_setup):
def test_restore_checkpoint_after_pre_setup(tmp_path, restore_after_pre_setup):
"""Test to ensure that if restore_checkpoint_after_setup is True, then we only load the state after pre- dispatch
is called."""

Expand All @@ -58,10 +58,10 @@ def load_checkpoint(self, checkpoint_path: Union[str, Path]) -> Dict[str, Any]:
return super().load_checkpoint(checkpoint_path)

model = BoringModel()
trainer = Trainer(default_root_dir=tmpdir, fast_dev_run=True)
trainer = Trainer(default_root_dir=tmp_path, fast_dev_run=True)
trainer.fit(model)

checkpoint_path = os.path.join(tmpdir, "model.pt")
checkpoint_path = os.path.join(tmp_path, "model.pt")
trainer.save_checkpoint(checkpoint_path)

plugin = TestPlugin(
Expand All @@ -72,7 +72,7 @@ def load_checkpoint(self, checkpoint_path: Union[str, Path]) -> Dict[str, Any]:
)
assert plugin.restore_checkpoint_after_setup == restore_after_pre_setup

trainer = Trainer(default_root_dir=tmpdir, strategy=plugin, fast_dev_run=True)
trainer = Trainer(default_root_dir=tmp_path, strategy=plugin, fast_dev_run=True)
trainer.fit(model, ckpt_path=checkpoint_path)
for func in (trainer.test, trainer.validate, trainer.predict):
plugin.setup_called = False
Expand Down
4 changes: 2 additions & 2 deletions tests/tests_pytorch/accelerators/test_gpu.py
Expand Up @@ -45,10 +45,10 @@ def test_get_nvidia_gpu_stats():

@RunIf(min_cuda_gpus=1)
@mock.patch("torch.cuda.set_device")
def test_set_cuda_device(set_device_mock, tmpdir):
def test_set_cuda_device(set_device_mock, tmp_path):
model = BoringModel()
trainer = Trainer(
default_root_dir=tmpdir,
default_root_dir=tmp_path,
fast_dev_run=True,
accelerator="gpu",
devices=1,
Expand Down
4 changes: 2 additions & 2 deletions tests/tests_pytorch/accelerators/test_mps.py
Expand Up @@ -54,10 +54,10 @@ def test_trainer_mps_accelerator(accelerator_value):

@RunIf(mps=True)
@pytest.mark.parametrize("devices", [1, [0], "-1"])
def test_single_gpu_model(tmpdir, devices):
def test_single_gpu_model(tmp_path, devices):
"""Make sure single GPU works."""
trainer_options = {
"default_root_dir": tmpdir,
"default_root_dir": tmp_path,
"enable_progress_bar": False,
"max_epochs": 1,
"limit_train_batches": 0.1,
Expand Down
20 changes: 10 additions & 10 deletions tests/tests_pytorch/accelerators/test_xla.py
Expand Up @@ -52,7 +52,7 @@ def forward(self, x):

@RunIf(tpu=True, standalone=True)
@mock.patch.dict(os.environ, os.environ.copy(), clear=True)
def test_resume_training_on_cpu(tmpdir):
def test_resume_training_on_cpu(tmp_path):
"""Checks if training can be resumed from a saved checkpoint on CPU."""
# Train a model on TPU
model = BoringModel()
Expand All @@ -72,16 +72,16 @@ def test_resume_training_on_cpu(tmpdir):
assert weight_tensor.device == torch.device("cpu")

# Verify that training is resumed on CPU
trainer = Trainer(max_epochs=1, default_root_dir=tmpdir)
trainer = Trainer(max_epochs=1, default_root_dir=tmp_path)
trainer.fit(model, ckpt_path=model_path)


@RunIf(tpu=True)
@mock.patch.dict(os.environ, os.environ.copy(), clear=True)
def test_if_test_works_after_train(tmpdir):
def test_if_test_works_after_train(tmp_path):
"""Ensure that .test() works after .fit()"""
model = BoringModel()
trainer = Trainer(max_epochs=1, accelerator="tpu", devices="auto", default_root_dir=tmpdir, fast_dev_run=True)
trainer = Trainer(max_epochs=1, accelerator="tpu", devices="auto", default_root_dir=tmp_path, fast_dev_run=True)
trainer.fit(model)
out = trainer.test(model)
assert len(out) == 1
Expand Down Expand Up @@ -159,13 +159,13 @@ def on_train_end(self):

@RunIf(tpu=True)
@mock.patch.dict(os.environ, os.environ.copy(), clear=True)
def test_manual_optimization_tpus(tmpdir):
def test_manual_optimization_tpus(tmp_path):
model = ManualOptimizationModel()
model_copy = deepcopy(model)

trainer = Trainer(
max_epochs=1,
default_root_dir=tmpdir,
default_root_dir=tmp_path,
limit_train_batches=3,
limit_test_batches=0,
limit_val_batches=0,
Expand Down Expand Up @@ -198,13 +198,13 @@ def test_strategy_choice_tpu_strategy():

@RunIf(tpu=True)
@mock.patch.dict(os.environ, os.environ.copy(), clear=True)
def test_auto_parameters_tying_tpus(tmpdir):
def test_auto_parameters_tying_tpus(tmp_path):
model = WeightSharingModule()
shared_params = find_shared_parameters(model)

assert shared_params[0] == ["layer_1.weight", "layer_3.weight"]

trainer = Trainer(default_root_dir=tmpdir, limit_train_batches=3, accelerator="tpu", devices="auto", max_epochs=1)
trainer = Trainer(default_root_dir=tmp_path, limit_train_batches=3, accelerator="tpu", devices="auto", max_epochs=1)
trainer.fit(model)

assert torch.equal(model.layer_1.weight, model.layer_3.weight)
Expand Down Expand Up @@ -236,9 +236,9 @@ def forward(self, x):

@RunIf(tpu=True)
@mock.patch.dict(os.environ, os.environ.copy(), clear=True)
def test_auto_parameters_tying_tpus_nested_module(tmpdir):
def test_auto_parameters_tying_tpus_nested_module(tmp_path):
model = NestedModule()
trainer = Trainer(default_root_dir=tmpdir, limit_train_batches=3, accelerator="tpu", devices="auto", max_epochs=1)
trainer = Trainer(default_root_dir=tmp_path, limit_train_batches=3, accelerator="tpu", devices="auto", max_epochs=1)
trainer.fit(model)

assert torch.all(torch.eq(model.net_a.layer.weight, model.net_b.layer.weight))
Expand Down
4 changes: 2 additions & 2 deletions tests/tests_pytorch/conftest.py
Expand Up @@ -271,8 +271,8 @@ def caplog(caplog):


@pytest.fixture()
def tmpdir_server(tmpdir):
Handler = partial(SimpleHTTPRequestHandler, directory=str(tmpdir))
def tmpdir_server(tmp_path):
Handler = partial(SimpleHTTPRequestHandler, directory=str(tmp_path))
from http.server import ThreadingHTTPServer

with ThreadingHTTPServer(("localhost", 0), Handler) as server:
Expand Down
13 changes: 10 additions & 3 deletions tests/tests_pytorch/test_cli.py
Expand Up @@ -47,6 +47,7 @@
from lightning.pytorch.utilities.exceptions import MisconfigurationException
from lightning.pytorch.utilities.imports import _TORCHVISION_AVAILABLE
from lightning_utilities import compare_version
from lightning_utilities.core.imports import RequirementCache
from lightning_utilities.test.warning import no_warning_call
from tensorboard.backend.event_processing import event_accumulator
from tensorboard.plugins.hparams.plugin_data_pb2 import HParamsPluginData
Expand Down Expand Up @@ -635,14 +636,14 @@ def test_cli_config_overwrite(cleandir):
LightningCLI(BoringModel, save_config_kwargs={"overwrite": True}, trainer_defaults=trainer_defaults)


def test_cli_config_filename(tmpdir):
def test_cli_config_filename(tmp_path):
with mock.patch("sys.argv", ["any.py", "fit"]):
LightningCLI(
BoringModel,
trainer_defaults={"default_root_dir": str(tmpdir), "logger": False, "max_steps": 1, "max_epochs": 1},
trainer_defaults={"default_root_dir": str(tmp_path), "logger": False, "max_steps": 1, "max_epochs": 1},
save_config_kwargs={"config_filename": "name.yaml"},
)
assert os.path.isfile(tmpdir / "name.yaml")
assert os.path.isfile(tmp_path / "name.yaml")


@pytest.mark.parametrize("run", [False, True])
Expand Down Expand Up @@ -1481,6 +1482,12 @@ def __init__(self, activation: torch.nn.Module = lazy_instance(torch.nn.LeakyReL
assert cli.model.activation is not model.activation


@pytest.mark.xfail(
# https://github.com/omni-us/jsonargparse/issues/473
condition=(RequirementCache("jsonargparse>=4.27.6")),
strict=False,
reason="Breaking change for `lazy_instance` in jsonargparse",
)
def test_ddpstrategy_instantiation_and_find_unused_parameters(mps_count_0):
strategy_default = lazy_instance(DDPStrategy, find_unused_parameters=True)
with mock.patch("sys.argv", ["any.py", "--trainer.strategy.process_group_backend=group"]):
Expand Down

0 comments on commit 05378b5

Please sign in to comment.