Skip to content

Commit 0780c19

Browse files
update example code for PINNPredictor (#918)
1 parent 2fdee45 commit 0780c19

File tree

3 files changed

+69
-7
lines changed

3 files changed

+69
-7
lines changed

deploy/python_infer/__init__.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,13 @@
1515
from deploy.python_infer.base import Predictor
1616
from deploy.python_infer.pinn_predictor import PINNPredictor
1717

18+
1819
# alias as PINNPredictor can be used in most cases
19-
GeneralPredictor = PINNPredictor
20+
class GeneralPredictor(PINNPredictor):
21+
"""Use PINNPredictor as GeneralPredictor."""
22+
23+
pass
24+
2025

2126
__all__ = [
2227
"Predictor",

deploy/python_infer/pinn_predictor.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,57 @@ class PINNPredictor(base.Predictor):
3030
3131
Args:
3232
cfg (DictConfig): Running configuration.
33+
34+
Examples:
35+
>>> import numpy as np
36+
>>> import paddle
37+
>>> from omegaconf import DictConfig
38+
>>> from paddle.static import InputSpec
39+
>>> import ppsci
40+
>>> from deploy.python_infer import pinn_predictor
41+
>>> model = ppsci.arch.MLP(("x", "y"), ("u", "v", "p"), 3, 16)
42+
>>> static_model = paddle.jit.to_static(
43+
... model,
44+
... input_spec=[
45+
... {
46+
... key: InputSpec([None, 1], "float32", name=key)
47+
... for key in model.input_keys
48+
... },
49+
... ],
50+
... )
51+
>>> paddle.jit.save(static_model, "./inference")
52+
>>> cfg = DictConfig(
53+
... {
54+
... "log_freq": 10,
55+
... "INFER": {
56+
... "pdmodel_path": "./inference.pdmodel",
57+
... "pdiparams_path": "./inference.pdiparams",
58+
... "device": "cpu",
59+
... "engine": "native",
60+
... "precision": "fp32",
61+
... "onnx_path": None,
62+
... "ir_optim": True,
63+
... "min_subgraph_size": 15,
64+
... "gpu_mem": 500,
65+
... "gpu_id": 0,
66+
... "max_batch_size": 10,
67+
... "num_cpu_threads": 10,
68+
... }
69+
... }
70+
... )
71+
>>> predictor = pinn_predictor.PINNPredictor(cfg) # doctest: +SKIP
72+
>>> pred = predictor.predict(
73+
... {
74+
... "x": np.random.randn(4, 1).astype("float32"),
75+
... "y": np.random.randn(4, 1).astype("float32"),
76+
... },
77+
... batch_size=2,
78+
... ) # doctest: +SKIP
79+
>>> for k, v in pred.items(): # doctest: +SKIP
80+
... print(k, v.shape) # doctest: +SKIP
81+
save_infer_model/scale_0.tmp_0 (4, 1)
82+
save_infer_model/scale_1.tmp_0 (4, 1)
83+
save_infer_model/scale_2.tmp_0 (4, 1)
3384
"""
3485

3586
def __init__(

ppsci/geometry/mesh.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
import numpy as np
2525
import paddle
26+
from typing_extensions import Literal
2627

2728
from ppsci.geometry import geometry
2829
from ppsci.geometry import geometry_3d
@@ -351,7 +352,7 @@ def inflated_random_points(self, n, distance, random="pseudo", criteria=None):
351352

352353
def _approximate_area(
353354
self,
354-
random: str = "pseudo",
355+
random: Literal["pseudo"] = "pseudo",
355356
criteria: Optional[Callable] = None,
356357
n_appr: int = 10000,
357358
) -> float:
@@ -444,7 +445,12 @@ def random_boundary_points(self, n, random="pseudo"):
444445
return points, normal, areas
445446

446447
def sample_boundary(
447-
self, n, random="pseudo", criteria=None, evenly=False, inflation_dist=None
448+
self,
449+
n: int,
450+
random: Literal["pseudo"] = "pseudo",
451+
criteria: Optional[Callable[..., np.ndarray]] = None,
452+
evenly: bool = False,
453+
inflation_dist: Union[float, Tuple[float, ...]] = None,
448454
) -> Dict[str, np.ndarray]:
449455
# TODO(sensen): support for time-dependent points(repeat data in time)
450456
if inflation_dist is not None:
@@ -561,10 +567,10 @@ def random_points(self, n, random="pseudo", criteria=None):
561567

562568
def sample_interior(
563569
self,
564-
n,
565-
random="pseudo",
566-
criteria=None,
567-
evenly=False,
570+
n: int,
571+
random: Literal["pseudo"] = "pseudo",
572+
criteria: Optional[Callable[..., np.ndarray]] = None,
573+
evenly: bool = False,
568574
compute_sdf_derivatives: bool = False,
569575
):
570576
"""Sample random points in the geometry and return those meet criteria."""

0 commit comments

Comments
 (0)