Skip to content

Commit dbd9234

Browse files
authored
【PPSCI Export&Infer No.15-16】 (#875)
* fix doc bugs * fix codestyle bugs * 【PPSCI Export&Infer No.15-16】 * fix codestyle bug for PPSCI Export&Infer No.15-16】 * fix codestyle bugs for 【PPSCI Export&Infer No.15-16】 * fix codestyle bugs for 【PPSCI Export&Infer No.15-16】 * fix codestyle bugs for 【PPSCI Export&Infer No.15-16】 * fix bugs for 【PPSCI Export&Infer No.15-16】 * fix codestyle bugs
1 parent 41914da commit dbd9234

File tree

6 files changed

+206
-2
lines changed

6 files changed

+206
-2
lines changed

docs/zh/examples/ldc2d_steady.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,18 @@
1414
python ldc2d_steady_Re10.py mode=eval EVAL.pretrained_model_path=https://paddle-org.bj.bcebos.com/paddlescience/models/ldc2d_steady_Re10/ldc2d_steady_Re10_pretrained.pdparams
1515
```
1616

17+
=== "模型导出命令"
18+
19+
``` sh
20+
python ldc2d_steady_Re10.py mode=export
21+
```
22+
23+
=== "模型推理命令"
24+
25+
``` sh
26+
python ldc2d_steady_Re10.py mode=infer
27+
```
28+
1729
| 预训练模型 | 指标 |
1830
|:--| :--|
1931
| [ldc2d_steady_Re10_pretrained.pdparams](https://paddle-org.bj.bcebos.com/paddlescience/models/ldc2d_steady_Re10/ldc2d_steady_Re10_pretrained.pdparams) | loss(Residual): 365.36164<br>MSE.momentum_x(Residual): 0.01435<br>MSE.continuity(Residual): 0.04072<br>MSE.momentum_y(Residual): 0.02471 |

docs/zh/examples/ldc2d_unsteady.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,18 @@
1414
python ldc2d_unsteady_Re10.py mode=eval EVAL.pretrained_model_path=https://paddle-org.bj.bcebos.com/paddlescience/models/ldc2d_unsteady_Re10/ldc2d_unsteady_Re10_pretrained.pdparams
1515
```
1616

17+
=== "模型导出命令"
18+
19+
``` sh
20+
python ldc2d_unsteady_Re10.py mode=export
21+
```
22+
23+
=== "模型推理命令"
24+
25+
``` sh
26+
python ldc2d_unsteady_Re10.py mode=infer
27+
```
28+
1729
| 预训练模型 | 指标 |
1830
|:--| :--|
1931
| [ldc2d_unsteady_Re10_pretrained.pdparams](https://paddle-org.bj.bcebos.com/paddlescience/models/ldc2d_unsteady_Re10/ldc2d_unsteady_Re10_pretrained.pdparams) | loss(Residual): 155652.67530<br>MSE.momentum_x(Residual): 6.78030<br>MSE.continuity(Residual): 0.16590<br>MSE.momentum_y(Residual): 12.05981 |

examples/ldc/conf/ldc2d_steady_Re10.yaml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ hydra:
2222
mode: train # running mode: train/eval
2323
seed: 42
2424
output_dir: ${hydra:run.dir}
25+
log_freq: 20
2526

2627
# set working condition
2728
NU: 0.01
@@ -55,3 +56,21 @@ EVAL:
5556
pretrained_model_path: null
5657
batch_size:
5758
residual_validator: 8192
59+
60+
# inference settings
61+
INFER:
62+
pretrained_model_path: https://paddle-org.bj.bcebos.com/paddlescience/models/ldc2d_steady_Re10/ldc2d_steady_Re10_pretrained.pdparams
63+
export_path: ./inference/ldc2d_steady_Re10
64+
pdmodel_path: ${INFER.export_path}.pdmodel
65+
pdpiparams_path: ${INFER.export_path}.pdiparams
66+
onnx_path: ${INFER.export_path}.onnx
67+
device: gpu
68+
engine: native
69+
precision: fp32
70+
ir_optim: true
71+
min_subgraph_size: 5
72+
gpu_mem: 2000
73+
gpu_id: 0
74+
max_batch_size: 8192
75+
num_cpu_threads: 10
76+
batch_size: 8192

examples/ldc/conf/ldc2d_unsteady_Re10.yaml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ hydra:
2222
mode: train # running mode: train/eval
2323
seed: 42
2424
output_dir: ${hydra:run.dir}
25+
log_freq: 20
2526

2627
# set working condition
2728
NU: 0.01
@@ -56,3 +57,21 @@ EVAL:
5657
pretrained_model_path: null
5758
batch_size:
5859
residual_validator: 8192
60+
61+
# inference settings
62+
INFER:
63+
pretrained_model_path: https://paddle-org.bj.bcebos.com/paddlescience/models/ldc2d_unsteady_Re10/ldc2d_unsteady_Re10_pretrained.pdparams
64+
export_path: ./inference/ldc2d_unsteady_Re10
65+
pdmodel_path: ${INFER.export_path}.pdmodel
66+
pdiparams_path: ${INFER.export_path}.pdiparams
67+
onnx_path: ${INFER.export_path}.onnx
68+
device: gpu
69+
engine: native
70+
precision: fp32
71+
ir_optim: true
72+
min_subgraph_size: 5
73+
gpu_mem: 2000
74+
gpu_id: 0
75+
max_batch_size: 8192
76+
num_cpu_threads: 10
77+
batch_size: 8192

examples/ldc/ldc2d_steady_Re10.py

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,57 @@ def evaluate(cfg: DictConfig):
234234
solver.visualize()
235235

236236

237+
def export(cfg: DictConfig):
238+
# set model
239+
model = ppsci.arch.MLP(**cfg.MODEL)
240+
241+
# initialize solver
242+
solver = ppsci.solver.Solver(
243+
model,
244+
pretrained_model_path=cfg.INFER.pretrained_model_path,
245+
)
246+
# export model
247+
from paddle.static import InputSpec
248+
249+
input_spec = [
250+
{key: InputSpec([None, 1], "float32", name=key) for key in model.input_keys},
251+
]
252+
solver.export(input_spec, cfg.INFER.export_path)
253+
254+
255+
def inference(cfg: DictConfig):
256+
from deploy.python_infer import pinn_predictor
257+
258+
predictor = pinn_predictor.PINNPredictor(cfg)
259+
260+
# set geometry
261+
geom = {"rect": ppsci.geometry.Rectangle((-0.05, -0.05), (0.05, 0.05))}
262+
# manually collate input data for inference
263+
NPOINT_PDE = 99**2
264+
NPOINT_TOP = 101
265+
NPOINT_BOTTOM = 101
266+
NPOINT_LEFT = 99
267+
NPOINT_RIGHT = 99
268+
NPOINT_BC = NPOINT_TOP + NPOINT_BOTTOM + NPOINT_LEFT + NPOINT_RIGHT
269+
input_dict = geom["rect"].sample_interior(NPOINT_PDE + NPOINT_BC, evenly=True)
270+
output_dict = predictor.predict(
271+
{key: input_dict[key] for key in cfg.MODEL.input_keys}, cfg.INFER.batch_size
272+
)
273+
274+
# mapping data to cfg.INFER.output_keys
275+
output_dict = {
276+
store_key: output_dict[infer_key]
277+
for store_key, infer_key in zip(cfg.MODEL.output_keys, output_dict.keys())
278+
}
279+
280+
ppsci.visualize.save_vtu_from_dict(
281+
"./ldc2d_steady_Re10.vtu",
282+
{**input_dict, **output_dict},
283+
input_dict.keys(),
284+
cfg.MODEL.output_keys,
285+
)
286+
287+
237288
@hydra.main(
238289
version_base=None, config_path="./conf", config_name="ldc2d_steady_Re10.yaml"
239290
)
@@ -242,8 +293,14 @@ def main(cfg: DictConfig):
242293
train(cfg)
243294
elif cfg.mode == "eval":
244295
evaluate(cfg)
296+
elif cfg.mode == "export":
297+
export(cfg)
298+
elif cfg.mode == "infer":
299+
inference(cfg)
245300
else:
246-
raise ValueError(f"cfg.mode should in ['train', 'eval'], but got '{cfg.mode}'")
301+
raise ValueError(
302+
f"cfg.mode should in ['train', 'eval', 'export', 'infer'], but got '{cfg.mode}'"
303+
)
247304

248305

249306
if __name__ == "__main__":

examples/ldc/ldc2d_unsteady_Re10.py

Lines changed: 86 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,85 @@ def evaluate(cfg: DictConfig):
308308
solver.visualize()
309309

310310

311+
def export(cfg: DictConfig):
312+
# set model
313+
model = ppsci.arch.MLP(**cfg.MODEL)
314+
315+
# initialize solver
316+
solver = ppsci.solver.Solver(
317+
model,
318+
pretrained_model_path=cfg.INFER.pretrained_model_path,
319+
)
320+
# export model
321+
from paddle.static import InputSpec
322+
323+
input_spec = [
324+
{key: InputSpec([None, 1], "float32", name=key) for key in model.input_keys},
325+
]
326+
solver.export(input_spec, cfg.INFER.export_path)
327+
328+
329+
def inference(cfg: DictConfig):
330+
from deploy.python_infer import pinn_predictor
331+
332+
predictor = pinn_predictor.PINNPredictor(cfg)
333+
334+
# set timestamps(including initial t0)
335+
timestamps = np.linspace(0.0, 1.5, cfg.NTIME_ALL, endpoint=True)
336+
# set time-geometry
337+
geom = {
338+
"time_rect": ppsci.geometry.TimeXGeometry(
339+
ppsci.geometry.TimeDomain(0.0, 1.5, timestamps=timestamps),
340+
ppsci.geometry.Rectangle((-0.05, -0.05), (0.05, 0.05)),
341+
)
342+
}
343+
# manually collate input data for inference
344+
NPOINT_PDE = 99**2
345+
NPOINT_TOP = 101
346+
NPOINT_DOWN = 101
347+
NPOINT_LEFT = 99
348+
NPOINT_RIGHT = 99
349+
NPOINT_IC = 99**2
350+
NTIME_PDE = cfg.NTIME_ALL - 1
351+
NPOINT_BC = NPOINT_TOP + NPOINT_DOWN + NPOINT_LEFT + NPOINT_RIGHT
352+
input_dict = geom["time_rect"].sample_initial_interior(
353+
(NPOINT_IC + NPOINT_BC), evenly=True
354+
)
355+
input_pde_dict = geom["time_rect"].sample_interior(
356+
(NPOINT_PDE + NPOINT_BC) * NTIME_PDE, evenly=True
357+
)
358+
# (interior+boundary) x all timestamps
359+
for t in range(NTIME_PDE):
360+
for key in geom["time_rect"].dim_keys:
361+
input_dict[key] = np.concatenate(
362+
(
363+
input_dict[key],
364+
input_pde_dict[key][
365+
t
366+
* (NPOINT_PDE + NPOINT_BC) : (t + 1)
367+
* (NPOINT_PDE + NPOINT_BC)
368+
],
369+
)
370+
)
371+
output_dict = predictor.predict(
372+
{key: input_dict[key] for key in cfg.MODEL.input_keys}, cfg.INFER.batch_size
373+
)
374+
375+
# mapping data to cfg.INFER.output_keys
376+
output_dict = {
377+
store_key: output_dict[infer_key]
378+
for store_key, infer_key in zip(cfg.MODEL.output_keys, output_dict.keys())
379+
}
380+
381+
ppsci.visualize.save_vtu_from_dict(
382+
"./ldc2d_unsteady_Re10_pred.vtu",
383+
{**input_dict, **output_dict},
384+
input_dict.keys(),
385+
cfg.MODEL.output_keys,
386+
cfg.NTIME_ALL,
387+
)
388+
389+
311390
@hydra.main(
312391
version_base=None, config_path="./conf", config_name="ldc2d_unsteady_Re10.yaml"
313392
)
@@ -316,8 +395,14 @@ def main(cfg: DictConfig):
316395
train(cfg)
317396
elif cfg.mode == "eval":
318397
evaluate(cfg)
398+
elif cfg.mode == "export":
399+
export(cfg)
400+
elif cfg.mode == "infer":
401+
inference(cfg)
319402
else:
320-
raise ValueError(f"cfg.mode should in ['train', 'eval'], but got '{cfg.mode}'")
403+
raise ValueError(
404+
f"cfg.mode should in ['train', 'eval', 'export', 'infer'], but got '{cfg.mode}'"
405+
)
321406

322407

323408
if __name__ == "__main__":

0 commit comments

Comments
 (0)