diff --git a/docs/zh/examples/ldc2d_steady.md b/docs/zh/examples/ldc2d_steady.md index 037dc50bc..8a52e02a3 100644 --- a/docs/zh/examples/ldc2d_steady.md +++ b/docs/zh/examples/ldc2d_steady.md @@ -14,6 +14,18 @@ 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 ``` +=== "模型导出命令" + + ``` sh + python ldc2d_steady_Re10.py mode=export + ``` + +=== "模型推理命令" + + ``` sh + python ldc2d_steady_Re10.py mode=infer + ``` + | 预训练模型 | 指标 | |:--| :--| | [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
MSE.momentum_x(Residual): 0.01435
MSE.continuity(Residual): 0.04072
MSE.momentum_y(Residual): 0.02471 | diff --git a/docs/zh/examples/ldc2d_unsteady.md b/docs/zh/examples/ldc2d_unsteady.md index 869836229..fe6a15726 100644 --- a/docs/zh/examples/ldc2d_unsteady.md +++ b/docs/zh/examples/ldc2d_unsteady.md @@ -14,6 +14,18 @@ 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 ``` +=== "模型导出命令" + + ``` sh + python ldc2d_unsteady_Re10.py mode=export + ``` + +=== "模型推理命令" + + ``` sh + python ldc2d_unsteady_Re10.py mode=infer + ``` + | 预训练模型 | 指标 | |:--| :--| | [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
MSE.momentum_x(Residual): 6.78030
MSE.continuity(Residual): 0.16590
MSE.momentum_y(Residual): 12.05981 | diff --git a/examples/ldc/conf/ldc2d_steady_Re10.yaml b/examples/ldc/conf/ldc2d_steady_Re10.yaml index 079d1e442..cc4c62c4e 100644 --- a/examples/ldc/conf/ldc2d_steady_Re10.yaml +++ b/examples/ldc/conf/ldc2d_steady_Re10.yaml @@ -22,6 +22,7 @@ hydra: mode: train # running mode: train/eval seed: 42 output_dir: ${hydra:run.dir} +log_freq: 20 # set working condition NU: 0.01 @@ -55,3 +56,21 @@ EVAL: pretrained_model_path: null batch_size: residual_validator: 8192 + +# inference settings +INFER: + pretrained_model_path: https://paddle-org.bj.bcebos.com/paddlescience/models/ldc2d_steady_Re10/ldc2d_steady_Re10_pretrained.pdparams + export_path: ./inference/ldc2d_steady_Re10 + pdmodel_path: ${INFER.export_path}.pdmodel + pdpiparams_path: ${INFER.export_path}.pdiparams + onnx_path: ${INFER.export_path}.onnx + device: gpu + engine: native + precision: fp32 + ir_optim: true + min_subgraph_size: 5 + gpu_mem: 2000 + gpu_id: 0 + max_batch_size: 8192 + num_cpu_threads: 10 + batch_size: 8192 diff --git a/examples/ldc/conf/ldc2d_unsteady_Re10.yaml b/examples/ldc/conf/ldc2d_unsteady_Re10.yaml index 0414836af..e03441980 100644 --- a/examples/ldc/conf/ldc2d_unsteady_Re10.yaml +++ b/examples/ldc/conf/ldc2d_unsteady_Re10.yaml @@ -22,6 +22,7 @@ hydra: mode: train # running mode: train/eval seed: 42 output_dir: ${hydra:run.dir} +log_freq: 20 # set working condition NU: 0.01 @@ -56,3 +57,21 @@ EVAL: pretrained_model_path: null batch_size: residual_validator: 8192 + +# inference settings +INFER: + pretrained_model_path: https://paddle-org.bj.bcebos.com/paddlescience/models/ldc2d_unsteady_Re10/ldc2d_unsteady_Re10_pretrained.pdparams + export_path: ./inference/ldc2d_unsteady_Re10 + pdmodel_path: ${INFER.export_path}.pdmodel + pdiparams_path: ${INFER.export_path}.pdiparams + onnx_path: ${INFER.export_path}.onnx + device: gpu + engine: native + precision: fp32 + ir_optim: true + min_subgraph_size: 5 + gpu_mem: 2000 + gpu_id: 0 + max_batch_size: 8192 + num_cpu_threads: 10 + batch_size: 8192 diff --git a/examples/ldc/ldc2d_steady_Re10.py b/examples/ldc/ldc2d_steady_Re10.py index 37cf32541..3c6507932 100644 --- a/examples/ldc/ldc2d_steady_Re10.py +++ b/examples/ldc/ldc2d_steady_Re10.py @@ -234,6 +234,57 @@ def evaluate(cfg: DictConfig): solver.visualize() +def export(cfg: DictConfig): + # set model + model = ppsci.arch.MLP(**cfg.MODEL) + + # initialize solver + solver = ppsci.solver.Solver( + model, + pretrained_model_path=cfg.INFER.pretrained_model_path, + ) + # export model + from paddle.static import InputSpec + + input_spec = [ + {key: InputSpec([None, 1], "float32", name=key) for key in model.input_keys}, + ] + solver.export(input_spec, cfg.INFER.export_path) + + +def inference(cfg: DictConfig): + from deploy.python_infer import pinn_predictor + + predictor = pinn_predictor.PINNPredictor(cfg) + + # set geometry + geom = {"rect": ppsci.geometry.Rectangle((-0.05, -0.05), (0.05, 0.05))} + # manually collate input data for inference + NPOINT_PDE = 99**2 + NPOINT_TOP = 101 + NPOINT_BOTTOM = 101 + NPOINT_LEFT = 99 + NPOINT_RIGHT = 99 + NPOINT_BC = NPOINT_TOP + NPOINT_BOTTOM + NPOINT_LEFT + NPOINT_RIGHT + input_dict = geom["rect"].sample_interior(NPOINT_PDE + NPOINT_BC, evenly=True) + output_dict = predictor.predict( + {key: input_dict[key] for key in cfg.MODEL.input_keys}, cfg.INFER.batch_size + ) + + # mapping data to cfg.INFER.output_keys + output_dict = { + store_key: output_dict[infer_key] + for store_key, infer_key in zip(cfg.MODEL.output_keys, output_dict.keys()) + } + + ppsci.visualize.save_vtu_from_dict( + "./ldc2d_steady_Re10.vtu", + {**input_dict, **output_dict}, + input_dict.keys(), + cfg.MODEL.output_keys, + ) + + @hydra.main( version_base=None, config_path="./conf", config_name="ldc2d_steady_Re10.yaml" ) @@ -242,8 +293,14 @@ def main(cfg: DictConfig): train(cfg) elif cfg.mode == "eval": evaluate(cfg) + elif cfg.mode == "export": + export(cfg) + elif cfg.mode == "infer": + inference(cfg) else: - raise ValueError(f"cfg.mode should in ['train', 'eval'], but got '{cfg.mode}'") + raise ValueError( + f"cfg.mode should in ['train', 'eval', 'export', 'infer'], but got '{cfg.mode}'" + ) if __name__ == "__main__": diff --git a/examples/ldc/ldc2d_unsteady_Re10.py b/examples/ldc/ldc2d_unsteady_Re10.py index 3ed4ba705..aeb88868c 100644 --- a/examples/ldc/ldc2d_unsteady_Re10.py +++ b/examples/ldc/ldc2d_unsteady_Re10.py @@ -308,6 +308,85 @@ def evaluate(cfg: DictConfig): solver.visualize() +def export(cfg: DictConfig): + # set model + model = ppsci.arch.MLP(**cfg.MODEL) + + # initialize solver + solver = ppsci.solver.Solver( + model, + pretrained_model_path=cfg.INFER.pretrained_model_path, + ) + # export model + from paddle.static import InputSpec + + input_spec = [ + {key: InputSpec([None, 1], "float32", name=key) for key in model.input_keys}, + ] + solver.export(input_spec, cfg.INFER.export_path) + + +def inference(cfg: DictConfig): + from deploy.python_infer import pinn_predictor + + predictor = pinn_predictor.PINNPredictor(cfg) + + # set timestamps(including initial t0) + timestamps = np.linspace(0.0, 1.5, cfg.NTIME_ALL, endpoint=True) + # set time-geometry + geom = { + "time_rect": ppsci.geometry.TimeXGeometry( + ppsci.geometry.TimeDomain(0.0, 1.5, timestamps=timestamps), + ppsci.geometry.Rectangle((-0.05, -0.05), (0.05, 0.05)), + ) + } + # manually collate input data for inference + NPOINT_PDE = 99**2 + NPOINT_TOP = 101 + NPOINT_DOWN = 101 + NPOINT_LEFT = 99 + NPOINT_RIGHT = 99 + NPOINT_IC = 99**2 + NTIME_PDE = cfg.NTIME_ALL - 1 + NPOINT_BC = NPOINT_TOP + NPOINT_DOWN + NPOINT_LEFT + NPOINT_RIGHT + input_dict = geom["time_rect"].sample_initial_interior( + (NPOINT_IC + NPOINT_BC), evenly=True + ) + input_pde_dict = geom["time_rect"].sample_interior( + (NPOINT_PDE + NPOINT_BC) * NTIME_PDE, evenly=True + ) + # (interior+boundary) x all timestamps + for t in range(NTIME_PDE): + for key in geom["time_rect"].dim_keys: + input_dict[key] = np.concatenate( + ( + input_dict[key], + input_pde_dict[key][ + t + * (NPOINT_PDE + NPOINT_BC) : (t + 1) + * (NPOINT_PDE + NPOINT_BC) + ], + ) + ) + output_dict = predictor.predict( + {key: input_dict[key] for key in cfg.MODEL.input_keys}, cfg.INFER.batch_size + ) + + # mapping data to cfg.INFER.output_keys + output_dict = { + store_key: output_dict[infer_key] + for store_key, infer_key in zip(cfg.MODEL.output_keys, output_dict.keys()) + } + + ppsci.visualize.save_vtu_from_dict( + "./ldc2d_unsteady_Re10_pred.vtu", + {**input_dict, **output_dict}, + input_dict.keys(), + cfg.MODEL.output_keys, + cfg.NTIME_ALL, + ) + + @hydra.main( version_base=None, config_path="./conf", config_name="ldc2d_unsteady_Re10.yaml" ) @@ -316,8 +395,14 @@ def main(cfg: DictConfig): train(cfg) elif cfg.mode == "eval": evaluate(cfg) + elif cfg.mode == "export": + export(cfg) + elif cfg.mode == "infer": + inference(cfg) else: - raise ValueError(f"cfg.mode should in ['train', 'eval'], but got '{cfg.mode}'") + raise ValueError( + f"cfg.mode should in ['train', 'eval', 'export', 'infer'], but got '{cfg.mode}'" + ) if __name__ == "__main__":