File size: 7,198 Bytes
40b99fb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
from __future__ import annotations

import random
import sys
import time
from pathlib import Path

import numpy as np
import torch
import yaml


PROJECT_ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(PROJECT_ROOT))
sys.path.insert(0, str(Path(__file__).resolve().parent))

from data_utils import (  # noqa: E402
    build_evaluation_points,
    build_training_batch,
    exact_subdomain_values,
    load_mat_data,
)
from model.xpinn import XPINNPoisson2D  # noqa: E402


DEFAULT_CONFIG = PROJECT_ROOT / "conf" / "config.yaml"


def load_config(path: Path) -> dict:
    with path.open("r", encoding="utf-8") as stream:
        config = yaml.safe_load(stream)
    if not isinstance(config, dict) or "root" not in config:
        raise ValueError(f"config must contain a 'root' mapping: {path}")
    return config["root"]


def project_path(value: str | Path) -> Path:
    path = Path(value).expanduser()
    return path if path.is_absolute() else PROJECT_ROOT / path


def resolve_device(requested: str) -> torch.device:
    if requested == "auto":
        return torch.device("cuda" if torch.cuda.is_available() else "cpu")
    device = torch.device(requested)
    if device.type == "cuda" and not torch.cuda.is_available():
        raise RuntimeError("CUDA/DCU was requested but torch.cuda.is_available() is false")
    return device


def resolve_dtype(name: str) -> torch.dtype:
    try:
        return {"float32": torch.float32, "float64": torch.float64}[name]
    except KeyError as error:
        raise ValueError(f"unsupported dtype: {name}") from error


def seed_everything(seed: int) -> None:
    random.seed(seed)
    np.random.seed(seed)
    torch.manual_seed(seed)
    if torch.cuda.is_available():
        torch.cuda.manual_seed_all(seed)


def compute_loss(
    outputs: dict[str, torch.Tensor],
    batch: dict[str, torch.Tensor],
    weights: dict,
) -> tuple[torch.Tensor, dict[str, float]]:
    boundary_loss = torch.mean(
        (batch["ub"] - outputs["boundary_prediction"]).square()
    )
    pde_loss = sum(
        torch.mean(outputs[f"residual{domain}"].square()) for domain in (1, 2, 3)
    )
    interface_residual_loss = torch.mean(
        outputs["interface1_residual"].square()
    ) + torch.mean(outputs["interface2_residual"].square())
    interface_value_loss = (
        torch.mean(
            (outputs["interface1_domain1"] - outputs["interface1_average"]).square()
        )
        + torch.mean(
            (outputs["interface1_domain2"] - outputs["interface1_average"]).square()
        )
        + torch.mean(
            (outputs["interface2_domain1"] - outputs["interface2_average"]).square()
        )
        + torch.mean(
            (outputs["interface2_domain3"] - outputs["interface2_average"]).square()
        )
    )
    total = (
        float(weights["boundary"]) * boundary_loss
        + float(weights["pde"]) * pde_loss
        + float(weights["interface_residual"]) * interface_residual_loss
        + float(weights["interface_value"]) * interface_value_loss
    )
    return total, {
        "boundary": boundary_loss.item(),
        "pde": pde_loss.item(),
        "interface_residual": interface_residual_loss.item(),
        "interface_value": interface_value_loss.item(),
    }


def clear_coordinate_grads(batch: dict[str, torch.Tensor]) -> None:
    for value in batch.values():
        if value.requires_grad and value.grad is not None:
            value.grad = None


def evaluate(
    model: XPINNPoisson2D,
    points: dict[str, torch.Tensor],
    references: tuple[torch.Tensor, torch.Tensor, torch.Tensor],
) -> tuple[float, float, float, float]:
    with torch.no_grad():
        predictions = model.predict(points["xy1"], points["xy2"], points["xy3"])
        errors = tuple(
            (
                torch.linalg.vector_norm(prediction - reference)
                / torch.linalg.vector_norm(reference)
            ).item()
            for prediction, reference in zip(predictions, references, strict=True)
        )
        combined_prediction = torch.cat(predictions)
        combined_reference = torch.cat(references)
        combined_error = (
            torch.linalg.vector_norm(combined_prediction - combined_reference)
            / torch.linalg.vector_norm(combined_reference)
        ).item()
    return errors[0], errors[1], errors[2], combined_error


def main() -> None:
    config_path = DEFAULT_CONFIG.resolve()
    config = load_config(config_path)
    common = config["common"]
    device = resolve_device(str(common["device"]))
    dtype = resolve_dtype(str(common["dtype"]))
    seed = int(common["seed"])
    data_path = project_path(config["data"]["mat_file"])
    weight_dir = project_path(common["weight_dir"])
    checkpoint_path = weight_dir / config["training"]["checkpoint_name"]
    steps = int(config["training"]["steps"])
    if steps <= 0:
        raise ValueError("training steps must be positive")
    counts = {key: int(value) for key, value in config["data"]["samples"].items()}
    seed_everything(seed)

    print(f"Config: {config_path}")
    print(f"Data: {data_path}")
    print(f"Device: {device}")
    print(f"Samples: {counts}")
    data = load_mat_data(data_path)
    batch = build_training_batch(data, counts, seed, device, dtype)
    evaluation_points = build_evaluation_points(data, device, dtype)
    references = exact_subdomain_values(data, device, dtype)

    model = XPINNPoisson2D(config["model"], dtype=dtype).to(
        device=device, dtype=dtype
    )
    optimizer = torch.optim.Adam(
        model.parameters(), lr=float(config["training"]["lr"])
    )
    log_interval = int(config["training"]["log_interval"])
    started = time.time()
    for step in range(1, steps + 1):
        clear_coordinate_grads(batch)
        outputs = model.training_outputs(batch)
        loss, parts = compute_loss(outputs, batch, config["loss"])
        if not torch.isfinite(loss):
            raise FloatingPointError(f"XPINN loss became non-finite at step {step}")
        optimizer.zero_grad(set_to_none=True)
        loss.backward()
        optimizer.step()

        if step == 1 or step % log_interval == 0 or step == steps:
            error1, error2, error3, combined_error = evaluate(
                model, evaluation_points, references
            )
            print(
                f"step={step:4d} loss={loss.item():.3e} "
                f"boundary={parts['boundary']:.3e} pde={parts['pde']:.3e} "
                f"interface_r={parts['interface_residual']:.3e} "
                f"interface_u={parts['interface_value']:.3e} "
                f"l2=({error1:.3e}, {error2:.3e}, {error3:.3e}) "
                f"combined={combined_error:.3e}"
            )

    weight_dir.mkdir(parents=True, exist_ok=True)
    checkpoint = {
        "case": "poisson2d",
        "architecture": "xpinn_poisson2d",
        "model_state": model.state_dict(),
        "model_config": config["model"],
        "sample_counts": counts,
        "step": steps,
    }
    torch.save(checkpoint, checkpoint_path)
    print(f"Training finished in {time.time() - started:.1f}s")
    print(f"Saved checkpoint: {checkpoint_path}")


if __name__ == "__main__":
    main()