Skip to content

Commit a57c461

Browse files
committed
feat(ppsci): remove redundant codes
1 parent 220e895 commit a57c461

File tree

4 files changed

+94
-112
lines changed

4 files changed

+94
-112
lines changed

docs/zh/examples/data_efficient_nopt.md

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88

99
## 代码信息
1010

11+
| Model | Checkpoint | **R2** | **Slope** |
12+
| :---------: | :--------------------: | :----: | :-------: |
13+
| FNO_Possion | [finetune_b01_m0_n8192](https://dataset.bj.bcebos.com/PaddleScience/data_efficient_nopt/possion_data/finetune_b01_m0_n8192.pdparams) | 0.9765 | 0.9752 |
14+
1115
=== "模型训练命令"
1216

1317
``` sh
@@ -39,14 +43,6 @@
3943
train_config.pois_64_finetune_e5_15.pretrained_ckpt_path="./data/pretrain_b01_m0.pdparams"
4044
```
4145

42-
=== "模型评估命令"
43-
44-
暂无
45-
46-
=== "模型导出命令"
47-
48-
暂无
49-
5046
=== "模型推理命令"
5147

5248
``` sh
@@ -207,12 +203,6 @@ examples/data_efficient_nopt/data_efficient_nopt.py
207203

208204
总而言之,这篇论文提出了一种创新且高效的神经算子学习框架,通过无监督预训练在大量廉价的无标签物理数据上学习通用表示,并通过情境学习在推理阶段利用少量相似案例来提升OOD泛化能力。这一框架显著降低了对昂贵模拟数据的需求,并提高了模型在复杂物理问题中的适应性和泛化性,为科学机器学习的数据高效发展开辟了新途径。
209205

210-
下方展示了部分实验结果:
211-
212-
| Model | Checkpoint | **$RMSE$** | **RMSE (normalized)$** | **R2** | **Slope** |
213-
| :---------: | :--------------------: | :--------: | :--------------------: | :----: | :-------: |
214-
| FNO_Possion | finetune_b01_m0_n8192 | 0.2586 | 0.1414 | 0.9765 | 0.9752 |
215-
216206
## 6. 参考资料
217207

218208
- [Data-Efficient Operator Learning via Unsupervised Pretraining and In-Context Learning](https://arxiv.org/abs/2402.15734)

examples/data_efficient_nopt/data_efficient_nopt.py

Lines changed: 7 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -35,78 +35,21 @@
3535
from tqdm import tqdm
3636

3737
from ppsci.arch.data_efficient_nopt_model import YParams
38+
from ppsci.arch.data_efficient_nopt_model import add_weight_decay
3839
from ppsci.arch.data_efficient_nopt_model import build_fno
3940
from ppsci.arch.data_efficient_nopt_model import fno_pretrain as fno
4041
from ppsci.arch.data_efficient_nopt_model import gaussian_blur
42+
from ppsci.arch.data_efficient_nopt_model import get_cutoff
43+
from ppsci.arch.data_efficient_nopt_model import grad_norm
44+
from ppsci.arch.data_efficient_nopt_model import l2_err
45+
from ppsci.arch.data_efficient_nopt_model import param_diff
46+
from ppsci.arch.data_efficient_nopt_model import param_norm
4147
from ppsci.data.dataset.data_efficient_nopt_dataset import MixedDatasetLoader
4248
from ppsci.data.dataset.data_efficient_nopt_dataset import PoisHelmDatasetLoader
4349

4450
logger = logging.getLogger(__name__)
4551

4652

47-
def l2_err(pred, target, spatial_dim=(-1, -2, -3)):
48-
x = paddle.sum((pred - target) ** 2, axis=spatial_dim) / paddle.sum(
49-
target**2, axis=spatial_dim
50-
)
51-
x = paddle.sqrt(x)
52-
return paddle.mean(x)
53-
54-
55-
def grad_norm(parameters):
56-
with paddle.no_grad():
57-
total_norm = 0
58-
for p in parameters:
59-
if p.grad is not None:
60-
total_norm += p.grad.data.pow(2).sum().item()
61-
return total_norm**0.5
62-
63-
64-
def grad_clone(parameters):
65-
with paddle.no_grad():
66-
clones = []
67-
for p in parameters:
68-
if p.grad is not None:
69-
clones.append(p.grad.clone())
70-
else:
71-
clones.append(paddle.zeros_like(p))
72-
return clones
73-
74-
75-
def param_norm(parameters):
76-
with paddle.no_grad():
77-
total_norm = 0
78-
for p in parameters:
79-
total_norm += p.pow(2).sum().item()
80-
return total_norm**0.5
81-
82-
83-
def param_diff(params1, params2):
84-
with paddle.no_grad():
85-
total_norm = 0
86-
for p1, p2 in zip(params1, params2):
87-
total_norm += (p2 - p1).pow(2).sum().item()
88-
return total_norm**0.5
89-
90-
91-
def add_weight_decay(model, weight_decay=1e-5, inner_lr=1e-3, skip_list=()):
92-
decay = []
93-
no_decay = []
94-
for name, param in model.named_parameters():
95-
if param.stop_gradient:
96-
continue
97-
if len(param.squeeze().shape) <= 1 or name in skip_list:
98-
no_decay.append(param)
99-
else:
100-
decay.append(param)
101-
return [
102-
{
103-
"params": no_decay,
104-
"weight_decay": 0.0,
105-
},
106-
{"params": decay, "weight_decay": weight_decay},
107-
]
108-
109-
11053
class Trainer:
11154
def __init__(self, params, global_rank, local_rank, device, sweep_id=None):
11255
self.device = device
@@ -531,10 +474,7 @@ def validate_one_epoch(self, full=False):
531474
Note: need to split datasets for meaningful metrics, but TBD.
532475
"""
533476
self.model.eval()
534-
if full:
535-
cutoff = 999999999999
536-
else:
537-
cutoff = 40
477+
cutoff = get_cutoff(full=full)
538478
with paddle.no_grad():
539479
with amp.auto_cast(enable=False, dtype=self.mp_type):
540480
logs = {

ppsci/arch/data_efficient_nopt_model.py

Lines changed: 75 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,24 +18,79 @@
1818

1919
import paddle
2020
import paddle.nn as nn
21-
import paddle.nn.functional as F
2221
import paddle.tensor as Tensor
2322

23+
from ppsci.arch.activation import act_func_dict
2424

25-
def _get_act(activation):
26-
if activation == "tanh":
27-
func = F.tanh
28-
elif activation == "gelu":
29-
func = F.gelu
30-
elif activation == "relu":
31-
func = F.relu_
32-
elif activation == "elu":
33-
func = F.elu_
34-
elif activation == "leaky_relu":
35-
func = F.leaky_relu_
36-
else:
37-
raise ValueError(f"{activation} is not supported")
38-
return func
25+
FULL_MODE_CUTOFF = 999999999999
26+
NORMAL_MODE_CUTOFF = 40
27+
28+
29+
def get_cutoff(full):
30+
return FULL_MODE_CUTOFF if full else NORMAL_MODE_CUTOFF
31+
32+
33+
def l2_err(pred, target, spatial_dim=(-1, -2, -3)):
34+
x = paddle.sum((pred - target) ** 2, axis=spatial_dim) / paddle.sum(
35+
target**2, axis=spatial_dim
36+
)
37+
x = paddle.sqrt(x)
38+
return paddle.mean(x)
39+
40+
41+
def grad_norm(parameters):
42+
with paddle.no_grad():
43+
total_norm = 0
44+
for p in parameters:
45+
if p.grad is not None:
46+
total_norm += p.grad.data.pow(2).sum().item()
47+
return total_norm**0.5
48+
49+
50+
def grad_clone(parameters):
51+
with paddle.no_grad():
52+
clones = []
53+
for p in parameters:
54+
if p.grad is not None:
55+
clones.append(p.grad.clone())
56+
else:
57+
clones.append(paddle.zeros_like(p))
58+
return clones
59+
60+
61+
def param_norm(parameters):
62+
with paddle.no_grad():
63+
total_norm = 0
64+
for p in parameters:
65+
total_norm += p.pow(2).sum().item()
66+
return total_norm**0.5
67+
68+
69+
def param_diff(params1, params2):
70+
with paddle.no_grad():
71+
total_norm = 0
72+
for p1, p2 in zip(params1, params2):
73+
total_norm += (p2 - p1).pow(2).sum().item()
74+
return total_norm**0.5
75+
76+
77+
def add_weight_decay(model, weight_decay=1e-5, inner_lr=1e-3, skip_list=()):
78+
decay = []
79+
no_decay = []
80+
for name, param in model.named_parameters():
81+
if param.stop_gradient:
82+
continue
83+
if len(param.squeeze().shape) <= 1 or name in skip_list:
84+
no_decay.append(param)
85+
else:
86+
decay.append(param)
87+
return [
88+
{
89+
"params": no_decay,
90+
"weight_decay": 0.0,
91+
},
92+
{"params": decay, "weight_decay": weight_decay},
93+
]
3994

4095

4196
def compl_mul2d_v2(a: paddle.Tensor, b: paddle.Tensor) -> paddle.Tensor:
@@ -141,7 +196,7 @@ def __init__(
141196
]
142197
)
143198

144-
self.activation = _get_act(activation)
199+
self.activation = act_func_dict[activation]
145200

146201
def forward(self, x):
147202
"""
@@ -205,7 +260,7 @@ def __init__(
205260
self.dropout = nn.Dropout(p=dropout)
206261
self.fc1 = nn.Linear(layers[-1], fc_dim)
207262
self.fc2 = nn.Linear(fc_dim, out_dim)
208-
self.activation = _get_act(activation)
263+
self.activation = act_func_dict[activation]
209264
self.mean_constraint = mean_constraint
210265

211266
def forward(self, x):
@@ -387,7 +442,7 @@ def __init__(
387442
layers[-1] * (n_demos + 1) + out_dim * n_demos * self.num_heads, fc_dim
388443
)
389444
self.fc2 = nn.Linear(fc_dim, out_dim)
390-
self.activation = _get_act(activation)
445+
self.activation = act_func_dict[activation]
391446
self.mean_constraint = mean_constraint
392447
self.n_demos = n_demos
393448

@@ -504,8 +559,7 @@ def __init__(
504559
self.l_attn = l_attn
505560
self.fc1 = nn.Linear(self.C_fno, fc_dim)
506561
self.fc2 = nn.Linear(fc_dim, out_dim)
507-
#########################
508-
self.activation = _get_act(activation)
562+
self.activation = act_func_dict[activation]
509563
self.mean_constraint = mean_constraint
510564
self.n_demos = n_demos
511565

@@ -682,7 +736,7 @@ def __init__(
682736
)
683737
self.dropout = nn.Dropout(p=dropout)
684738
self.encoder_to_decoder = nn.Linear(self.C_fno, self.C_fno)
685-
self.activation = _get_act(activation)
739+
self.activation = act_func_dict[activation]
686740
self.mean_constraint = mean_constraint
687741

688742
def forward(self, x, mask=None):

ppsci/data/dataset/data_efficient_nopt_dataset.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,6 @@
1414
#
1515
# refs: https://github.com/delta-lab-ai/data_efficient_nopt
1616

17-
"""
18-
Remember to parameterize the file paths eventually
19-
"""
2017
import glob
2118
import logging
2219
import os
@@ -32,6 +29,8 @@
3229
from paddle.io import RandomSampler
3330
from paddle.io import Sampler
3431

32+
logger = logging.getLogger(__name__)
33+
3534
__all__ = [
3635
"MultisetSampler",
3736
]
@@ -167,7 +166,7 @@ def _get_directory_stats(self, path):
167166
with h5py.File(file, "r") as _f:
168167
samples, steps = self._get_specific_stats(_f)
169168
if steps - self.n_steps - (self.dt - 1) < 1:
170-
print(
169+
logger.warning(
171170
"WARNING: File {} has {} steps, but n_steps is {}. Setting file steps = max allowable.".format(
172171
file, steps, self.n_steps
173172
)
@@ -209,7 +208,7 @@ def _get_directory_stats(self, path):
209208
+ (steps - file_nsteps - (self.dt - 1)) * split_samples
210209
)
211210
except: # noqa
212-
print(
211+
logger.warning(
213212
"WARNING: Failed to open file {}. Continuing without it.".format(
214213
file
215214
)
@@ -221,13 +220,12 @@ def _get_directory_stats(self, path):
221220
self.len = self.offsets[-1]
222221
if self.split_level == "file":
223222
if self.train_val_test is None:
224-
print(
223+
logger.warning(
225224
"WARNING: No train/val/test split specified. Using all data for training."
226225
)
227226
self.split_offset = 0
228227
self.len = self.offsets[-1]
229228
else:
230-
print("Using train/val/test split: {}".format(self.train_val_test))
231229
total_samples = sum(self.file_samples)
232230
if (
233231
self.train_val_test[1] * total_samples < 1
@@ -495,9 +493,9 @@ def __iter__(self) -> Iterator[T_co]:
495493
for d in queue:
496494
yield d
497495
except Exception as err:
498-
print("ERRRR", err)
496+
logger.error("ERRRR", err)
499497
sampler_choices.pop(index_sampled)
500-
print(
498+
logger.warning(
501499
f"Note: dset {dset_sampled} fully used. Dsets remaining: {len(sampler_choices)}"
502500
)
503501
continue
@@ -938,7 +936,7 @@ def __getitem__(self, index):
938936
try:
939937
x, y = self.sub_dsets[file_idx][local_idx]
940938
except: # noqa
941-
print(
939+
logger.error(
942940
"FAILED AT ", file_idx, local_idx, index, int(os.environ.get("RANK", 0))
943941
)
944942

0 commit comments

Comments
 (0)