* Enable DDP training with batch_size=None and add validity check for split sizes * Refactoring SolverInterfaces (#435) * Solver update + weighting * Updating PINN for 0.2 * Modify GAROM + tests * Adding more versatile loggers * Disable compilation when running on Windows * Fix tests --------- Co-authored-by: giovanni <giovanni.canali98@yahoo.it> Co-authored-by: FilippoOlivo <filippo@filippoolivo.com>
157 lines
5.1 KiB
Python
157 lines
5.1 KiB
Python
import torch
|
|
import pytest
|
|
|
|
from pina import LabelTensor, Condition
|
|
from pina.problem import SpatialProblem
|
|
from pina.solvers import CausalPINN
|
|
from pina.trainer import Trainer
|
|
from pina.model import FeedForward
|
|
from pina.problem.zoo import (
|
|
DiffusionReactionProblem,
|
|
InverseDiffusionReactionProblem
|
|
)
|
|
from pina.condition import (
|
|
InputOutputPointsCondition,
|
|
InputPointsEquationCondition,
|
|
DomainEquationCondition
|
|
)
|
|
from torch._dynamo.eval_frame import OptimizedModule
|
|
|
|
|
|
class DummySpatialProblem(SpatialProblem):
|
|
'''
|
|
A mock spatial problem for testing purposes.
|
|
'''
|
|
output_variables = ['u']
|
|
conditions = {}
|
|
spatial_domain = None
|
|
|
|
|
|
# define problems and model
|
|
problem = DiffusionReactionProblem()
|
|
problem.discretise_domain(50)
|
|
inverse_problem = InverseDiffusionReactionProblem()
|
|
inverse_problem.discretise_domain(50)
|
|
model = FeedForward(
|
|
len(problem.input_variables),
|
|
len(problem.output_variables)
|
|
)
|
|
|
|
# add input-output condition to test supervised learning
|
|
input_pts = torch.rand(50, len(problem.input_variables))
|
|
input_pts = LabelTensor(input_pts, problem.input_variables)
|
|
output_pts = torch.rand(50, len(problem.output_variables))
|
|
output_pts = LabelTensor(output_pts, problem.output_variables)
|
|
problem.conditions['data'] = Condition(
|
|
input_points=input_pts,
|
|
output_points=output_pts
|
|
)
|
|
|
|
|
|
@pytest.mark.parametrize("problem", [problem, inverse_problem])
|
|
@pytest.mark.parametrize("eps", [100, 100.1])
|
|
def test_constructor(problem, eps):
|
|
with pytest.raises(ValueError):
|
|
CausalPINN(model=model, problem=DummySpatialProblem())
|
|
solver = CausalPINN(model=model, problem=problem, eps=eps)
|
|
|
|
assert solver.accepted_conditions_types == (
|
|
InputOutputPointsCondition,
|
|
InputPointsEquationCondition,
|
|
DomainEquationCondition
|
|
)
|
|
|
|
|
|
@pytest.mark.parametrize("problem", [problem, inverse_problem])
|
|
@pytest.mark.parametrize("batch_size", [None, 1, 5, 20])
|
|
@pytest.mark.parametrize("compile", [True, False])
|
|
def test_solver_train(problem, batch_size, compile):
|
|
solver = CausalPINN(model=model, problem=problem)
|
|
trainer = Trainer(solver=solver,
|
|
max_epochs=2,
|
|
accelerator='cpu',
|
|
batch_size=batch_size,
|
|
train_size=1.,
|
|
val_size=0.,
|
|
test_size=0.,
|
|
compile=compile)
|
|
trainer.train()
|
|
if trainer.compile:
|
|
assert (isinstance(solver.model, OptimizedModule))
|
|
|
|
|
|
@pytest.mark.parametrize("problem", [problem, inverse_problem])
|
|
@pytest.mark.parametrize("batch_size", [None, 1, 5, 20])
|
|
@pytest.mark.parametrize("compile", [True, False])
|
|
def test_solver_validation(problem, batch_size, compile):
|
|
solver = CausalPINN(model=model, problem=problem)
|
|
trainer = Trainer(solver=solver,
|
|
max_epochs=2,
|
|
accelerator='cpu',
|
|
batch_size=batch_size,
|
|
train_size=0.9,
|
|
val_size=0.1,
|
|
test_size=0.,
|
|
compile=compile)
|
|
trainer.train()
|
|
if trainer.compile:
|
|
assert (isinstance(solver.model, OptimizedModule))
|
|
|
|
|
|
@pytest.mark.parametrize("problem", [problem, inverse_problem])
|
|
@pytest.mark.parametrize("batch_size", [None, 1, 5, 20])
|
|
@pytest.mark.parametrize("compile", [True, False])
|
|
def test_solver_test(problem, batch_size, compile):
|
|
solver = CausalPINN(model=model, problem=problem)
|
|
trainer = Trainer(solver=solver,
|
|
max_epochs=2,
|
|
accelerator='cpu',
|
|
batch_size=batch_size,
|
|
train_size=0.7,
|
|
val_size=0.2,
|
|
test_size=0.1,
|
|
compile=compile)
|
|
trainer.test()
|
|
if trainer.compile:
|
|
assert (isinstance(solver.model, OptimizedModule))
|
|
|
|
|
|
@pytest.mark.parametrize("problem", [problem, inverse_problem])
|
|
def test_train_load_restore(problem):
|
|
dir = "tests/test_solvers/tmp"
|
|
problem = problem
|
|
solver = CausalPINN(model=model, problem=problem)
|
|
trainer = Trainer(solver=solver,
|
|
max_epochs=5,
|
|
accelerator='cpu',
|
|
batch_size=None,
|
|
train_size=0.7,
|
|
val_size=0.2,
|
|
test_size=0.1,
|
|
default_root_dir=dir)
|
|
trainer.train()
|
|
|
|
# restore
|
|
new_trainer = Trainer(solver=solver, max_epochs=5, accelerator='cpu')
|
|
new_trainer.train(
|
|
ckpt_path=f'{dir}/lightning_logs/version_0/checkpoints/' +
|
|
'epoch=4-step=5.ckpt')
|
|
|
|
# loading
|
|
new_solver = CausalPINN.load_from_checkpoint(
|
|
f'{dir}/lightning_logs/version_0/checkpoints/epoch=4-step=5.ckpt',
|
|
problem=problem, model=model)
|
|
|
|
test_pts = LabelTensor(torch.rand(20, 2), problem.input_variables)
|
|
assert new_solver.forward(test_pts).shape == (20, 1)
|
|
assert new_solver.forward(test_pts).shape == (
|
|
solver.forward(test_pts).shape
|
|
)
|
|
torch.testing.assert_close(
|
|
new_solver.forward(test_pts),
|
|
solver.forward(test_pts))
|
|
|
|
# rm directories
|
|
import shutil
|
|
shutil.rmtree('tests/test_solvers/tmp')
|