|
23 | 23 | from pytorch_lightning.core.lightning import LightningModule |
24 | 24 | from pytorch_lightning.plugins import IPUPlugin, IPUPrecisionPlugin |
25 | 25 | from pytorch_lightning.trainer.states import RunningStage |
| 26 | +from pytorch_lightning.trainer.supporters import CombinedLoader |
26 | 27 | from pytorch_lightning.utilities import _IPU_AVAILABLE |
27 | 28 | from pytorch_lightning.utilities.exceptions import MisconfigurationException |
28 | 29 | from tests.helpers.boring_model import BoringModel |
@@ -112,6 +113,19 @@ def test_accelerator_selected(tmpdir): |
112 | 113 | assert isinstance(trainer.accelerator, IPUAccelerator) |
113 | 114 |
|
114 | 115 |
|
| 116 | +@RunIf(ipu=True) |
| 117 | +def test_warning_if_ipus_not_used(tmpdir): |
| 118 | + with pytest.warns(UserWarning, match="IPU available but not used. Set the `ipus` flag in your trainer"): |
| 119 | + Trainer(default_root_dir=tmpdir) |
| 120 | + |
| 121 | + |
| 122 | +@RunIf(ipu=True) |
| 123 | +def test_no_warning_plugin(tmpdir): |
| 124 | + with pytest.warns(None) as record: |
| 125 | + Trainer(default_root_dir=tmpdir, plugins=IPUPlugin(training_opts=poptorch.Options())) |
| 126 | + assert len(record) == 0 |
| 127 | + |
| 128 | + |
115 | 129 | @RunIf(ipu=True) |
116 | 130 | @pytest.mark.parametrize('ipus', [1, 4]) |
117 | 131 | def test_all_stages(tmpdir, ipus): |
@@ -364,140 +378,72 @@ def test_manual_poptorch_opts(tmpdir): |
364 | 378 |
|
365 | 379 |
|
366 | 380 | @RunIf(ipu=True) |
367 | | -def test_manual_poptorch_opts_ipu_count(tmpdir): |
368 | | - """ |
369 | | - Ensure if the user passes manual poptorch Options |
370 | | - and the number of ipus do not match, we warn and we set it for the user. |
371 | | - """ |
372 | | - |
373 | | - manual_ipus = 1 |
374 | | - expected_ipus = 2 |
375 | | - model = IPUModel() |
376 | | - inference_opts = poptorch.Options() |
377 | | - inference_opts.replicationFactor(manual_ipus) |
378 | | - |
379 | | - training_opts = poptorch.Options() |
380 | | - training_opts.replicationFactor(manual_ipus) |
381 | | - |
382 | | - trainer = Trainer( |
383 | | - default_root_dir=tmpdir, |
384 | | - ipus=expected_ipus, |
385 | | - fast_dev_run=True, |
386 | | - plugins=IPUPlugin(inference_opts=inference_opts, training_opts=training_opts) |
387 | | - ) |
388 | | - with pytest.warns( |
389 | | - UserWarning, |
390 | | - match=f"Manual poptorch.Options set replicationFactor to {manual_ipus} " |
391 | | - f"which differs to the ipus={expected_ipus} flag passed to the Trainer. " |
392 | | - f"Setting to {expected_ipus} in the poptorch.Options." |
393 | | - ): |
394 | | - trainer.fit(model) |
395 | | - assert isinstance(trainer.accelerator.training_type_plugin, IPUPlugin) |
396 | | - assert trainer.accelerator.training_type_plugin.training_opts.replication_factor == 2 |
397 | | - assert trainer.accelerator.training_type_plugin.inference_opts.replication_factor == 2 |
398 | | - |
399 | | - |
400 | | -@RunIf(ipu=True) |
401 | | -def test_manual_poptorch_opts_inference_grad_accum(tmpdir): |
402 | | - """ |
403 | | - Ensure if the user passes manual poptorch Options |
404 | | - and grad accumulation is set greater than 1 for inference, we warn and set to 1. |
405 | | - """ |
406 | | - |
407 | | - model = IPUModel() |
408 | | - inference_opts = poptorch.Options() |
409 | | - inference_opts.Training.gradientAccumulation(4) |
410 | | - |
411 | | - training_opts = poptorch.Options() |
412 | | - training_opts.Training.gradientAccumulation(1) |
413 | | - |
414 | | - trainer = Trainer( |
415 | | - default_root_dir=tmpdir, |
416 | | - ipus=1, |
417 | | - fast_dev_run=True, |
418 | | - plugins=IPUPlugin(inference_opts=inference_opts, training_opts=training_opts) |
419 | | - ) |
420 | | - with pytest.warns( |
421 | | - UserWarning, |
422 | | - match="Inference poptorch.Options should set gradientAccumulation to 1. " |
423 | | - "Setting gradientAccumulation to 1 for inference options.", |
424 | | - ): |
425 | | - trainer.fit(model) |
426 | | - assert isinstance(trainer.accelerator.training_type_plugin, IPUPlugin) |
427 | | - assert trainer.accelerator.training_type_plugin.inference_opts.Training.gradient_accumulation == 1 |
428 | | - |
429 | | - |
430 | | -@RunIf(ipu=True) |
431 | | -def test_manual_poptorch_opts_train_grad_accum(tmpdir): |
| 381 | +def test_manual_poptorch_opts_custom(tmpdir): |
432 | 382 | """ |
433 | | - Ensure if the user passes manual poptorch Options |
434 | | - and grad accumulation differs to accumulate_grad_batches, we |
| 383 | + Ensure if the user passes manual poptorch Options with custom parameters set, |
| 384 | + we respect them in our poptorch options and the dataloaders. |
435 | 385 | """ |
436 | 386 |
|
437 | 387 | model = IPUModel() |
438 | | - inference_opts = poptorch.Options() |
439 | | - inference_opts.Training.gradientAccumulation(1) |
440 | | - |
441 | 388 | training_opts = poptorch.Options() |
| 389 | + training_opts.deviceIterations(8) |
| 390 | + training_opts.replicationFactor(2) |
442 | 391 | training_opts.Training.gradientAccumulation(2) |
443 | 392 |
|
444 | | - trainer = Trainer( |
445 | | - default_root_dir=tmpdir, |
446 | | - ipus=1, |
447 | | - fast_dev_run=True, |
448 | | - accumulate_grad_batches=1, |
449 | | - plugins=IPUPlugin(inference_opts=inference_opts, training_opts=training_opts) |
450 | | - ) |
451 | | - with pytest.warns( |
452 | | - UserWarning, |
453 | | - match=f"Training poptorch.Options set gradientAccumulation to {2}. " |
454 | | - f"This is different to accumulate_grad_batches which was set to {1}. " |
455 | | - f"To change gradientAccumulation, please set accumulate_grad_batches in the Trainer. " |
456 | | - f"Setting poptorch.Options gradientAccumulation to {1}", |
457 | | - ): |
458 | | - trainer.fit(model) |
459 | | - assert isinstance(trainer.accelerator.training_type_plugin, IPUPlugin) |
460 | | - assert trainer.accelerator.training_type_plugin.inference_opts.Training.gradient_accumulation == 1 |
461 | | - |
462 | | - |
463 | | -@RunIf(ipu=True) |
464 | | -def test_manual_poptorch_opts_custom(tmpdir): |
465 | | - """ |
466 | | - Ensure if the user passes manual poptorch Options with custom parameters set, |
467 | | - we respect them in our poptorch options. |
468 | | - """ |
469 | | - |
470 | | - model = IPUModel() |
471 | 393 | inference_opts = poptorch.Options() |
472 | 394 | inference_opts.deviceIterations(16) |
473 | | - inference_opts.replicationFactor(2) |
| 395 | + inference_opts.replicationFactor(1) |
474 | 396 | inference_opts.Training.gradientAccumulation(1) |
475 | 397 |
|
476 | | - training_opts = poptorch.Options() |
477 | | - training_opts.deviceIterations(8) |
478 | | - training_opts.replicationFactor(2) |
479 | | - training_opts.Training.gradientAccumulation(2) |
| 398 | + class TestCallback(Callback): |
480 | 399 |
|
481 | | - trainer = Trainer( |
482 | | - default_root_dir=tmpdir, |
483 | | - ipus=2, |
484 | | - fast_dev_run=True, |
485 | | - accumulate_grad_batches=2, |
486 | | - plugins=IPUPlugin(inference_opts=inference_opts, training_opts=training_opts) |
487 | | - ) |
| 400 | + def on_fit_end(self, trainer: Trainer, pl_module: LightningModule) -> None: |
| 401 | + # ensure dataloaders were correctly set up during training. |
| 402 | + plugin = trainer.accelerator.training_type_plugin |
| 403 | + assert isinstance(plugin, IPUPlugin) |
| 404 | + assert plugin.training_opts.replication_factor == 2 |
| 405 | + assert plugin.inference_opts.replication_factor == 1 |
| 406 | + |
| 407 | + val_dataloader = trainer.val_dataloaders[0] |
| 408 | + train_dataloader = trainer.train_dataloader |
| 409 | + assert isinstance(train_dataloader, CombinedLoader) |
| 410 | + train_dataloader = train_dataloader.loaders |
| 411 | + assert isinstance(val_dataloader, poptorch.DataLoader) |
| 412 | + assert isinstance(train_dataloader, poptorch.DataLoader) |
| 413 | + assert train_dataloader.options.replication_factor == 2 |
| 414 | + assert val_dataloader.options.replication_factor == 1 |
| 415 | + |
| 416 | + plugin = IPUPlugin(inference_opts=inference_opts, training_opts=training_opts) |
| 417 | + # ensure we default to the training options replication factor |
| 418 | + assert plugin.replication_factor == 2 |
| 419 | + trainer = Trainer(default_root_dir=tmpdir, fast_dev_run=True, plugins=plugin, callbacks=TestCallback()) |
488 | 420 | trainer.fit(model) |
| 421 | + |
489 | 422 | plugin = trainer.accelerator.training_type_plugin |
490 | 423 | assert isinstance(plugin, IPUPlugin) |
491 | | - inference_opts = plugin.inference_opts |
492 | | - training_opts = plugin.training_opts |
493 | | - assert inference_opts.device_iterations == 16 |
494 | | - assert inference_opts.replication_factor == 2 |
495 | | - assert inference_opts.Training.gradient_accumulation == 1 |
496 | 424 |
|
| 425 | + training_opts = plugin.training_opts |
497 | 426 | assert training_opts.device_iterations == 8 |
498 | 427 | assert training_opts.replication_factor == 2 |
499 | 428 | assert training_opts.Training.gradient_accumulation == 2 |
500 | 429 |
|
| 430 | + inference_opts = plugin.inference_opts |
| 431 | + assert inference_opts.device_iterations == 16 |
| 432 | + assert inference_opts.replication_factor == 1 |
| 433 | + assert inference_opts.Training.gradient_accumulation == 1 |
| 434 | + |
| 435 | + |
| 436 | +@RunIf(ipu=True) |
| 437 | +def test_replication_factor(tmpdir): |
| 438 | + """ |
| 439 | + Ensure if the user passes manual poptorch Options with custom parameters set, |
| 440 | + we set them correctly in the dataloaders. |
| 441 | + """ |
| 442 | + |
| 443 | + plugin = IPUPlugin() |
| 444 | + trainer = Trainer(ipus=2, default_root_dir=tmpdir, fast_dev_run=True, plugins=plugin) |
| 445 | + assert trainer.ipus == 2 |
| 446 | + |
501 | 447 |
|
502 | 448 | @RunIf(ipu=True) |
503 | 449 | def test_default_opts(tmpdir): |
|
0 commit comments