Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

【PPSCI Export&Infer No.5】lorenz #801

Merged
merged 2 commits into from
Mar 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions docs/zh/examples/lorenz.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,24 @@
python train_transformer.py mode=eval EVAL.pretrained_model_path=https://paddle-org.bj.bcebos.com/paddlescience/models/lorenz/lorenz_transformer_pretrained.pdparams EMBEDDING_MODEL_PATH=https://paddle-org.bj.bcebos.com/paddlescience/models/lorenz/lorenz_pretrained.pdparams
```

=== "模型导出命令"

``` sh
python train_enn.py mode=export
```

=== "模型推理命令"

``` sh
# linux
wget -nc https://paddle-org.bj.bcebos.com/paddlescience/datasets/transformer_physx/lorenz_training_rk.hdf5 -P ./datasets/
wget -nc https://paddle-org.bj.bcebos.com/paddlescience/datasets/transformer_physx/lorenz_valid_rk.hdf5 -P ./datasets/
# windows
# curl https://paddle-org.bj.bcebos.com/paddlescience/datasets/transformer_physx/lorenz_training_rk.hdf5 --output ./datasets/lorenz_training_rk.hdf5
# curl https://paddle-org.bj.bcebos.com/paddlescience/datasets/transformer_physx/lorenz_valid_rk.hdf5 --output ./datasets/lorenz_valid_rk.hdf5
python train_transformer.py mode=infer EMBEDDING_MODEL_PATH=https://paddle-org.bj.bcebos.com/paddlescience/models/lorenz/lorenz_pretrained.pdparams
```

| 模型 | MSE |
| :-- | :-- |
| [lorenz_transformer_pretrained.pdparams](https://paddle-org.bj.bcebos.com/paddlescience/models/lorenz/lorenz_transformer_pretrained.pdparams) | 0.054 |
Expand Down
19 changes: 19 additions & 0 deletions examples/lorenz/conf/transformer.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ hydra:
mode: train # running mode: train/eval
seed: 42
output_dir: ${hydra:run.dir}
log_freq: 20
TRAIN_BLOCK_SIZE: 64
VALID_BLOCK_SIZE: 256
TRAIN_FILE_PATH: ./datasets/lorenz_training_rk.hdf5
Expand Down Expand Up @@ -63,3 +64,21 @@ TRAIN:
EVAL:
batch_size: 16
pretrained_model_path: null

# inference settings
INFER:
pretrained_model_path: https://paddle-org.bj.bcebos.com/paddlescience/models/lorenz/lorenz_transformer_pretrained.pdparams
export_path: ./inference/lorenz_transformer
pdmodel_path: ${INFER.export_path}.pdmodel
pdpiparams_path: ${INFER.export_path}.pdiparams
device: gpu
engine: native
precision: fp32
onnx_path: ${INFER.export_path}.onnx
ir_optim: false
min_subgraph_size: 10
gpu_mem: 4000
gpu_id: 0
max_batch_size: 64
num_cpu_threads: 4
batch_size: 16
80 changes: 79 additions & 1 deletion examples/lorenz/train_transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,14 +245,92 @@ def evaluate(cfg: DictConfig):
solver.visualize()


def export(cfg: DictConfig):
# set model
model = ppsci.arch.PhysformerGPT2(**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, 256, 32], "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)

embedding_model = build_embedding_model(cfg.EMBEDDING_MODEL_PATH)
output_transform = OutputTransform(embedding_model)
dataset_cfg = {
"name": "LorenzDataset",
"file_path": cfg.VALID_FILE_PATH,
"input_keys": cfg.MODEL.input_keys,
"label_keys": cfg.MODEL.output_keys,
"block_size": cfg.VALID_BLOCK_SIZE,
"stride": 1024,
"embedding_model": embedding_model,
}

dataset = ppsci.data.dataset.build_dataset(dataset_cfg)

input_dict = {
"embeds": dataset.embedding_data[: cfg.VIS_DATA_NUMS, :-1, :],
}

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: paddle.to_tensor(output_dict[infer_key])
for store_key, infer_key in zip(cfg.MODEL.output_keys, output_dict.keys())
}

input_dict = {
"states": dataset.data[: cfg.VIS_DATA_NUMS, 1:, :],
}

output_dict = {
"pred_states": output_transform(output_dict).numpy(),
}

data_dict = {**input_dict, **output_dict}
for i in range(cfg.VIS_DATA_NUMS):
ppsci.visualize.save_plot_from_3d_dict(
f"./lorenz_transformer_pred_{i}",
{key: value[i] for key, value in data_dict.items()},
("states", "pred_states"),
)


@hydra.main(version_base=None, config_path="./conf", config_name="transformer.yaml")
def main(cfg: DictConfig):
if cfg.mode == "train":
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__":
Expand Down