forked from lijialin03/PaddleScience
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request PaddlePaddle#150 from Liu-xiandong/model_standardi…
…zation Model standardization.
- Loading branch information
Showing
7 changed files
with
320 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,5 +29,6 @@ | |
from . import solver | ||
from . import visu | ||
from . import data | ||
from . import utils | ||
|
||
config.try_enable_cinn() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,167 @@ | ||
# Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserve. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
import os | ||
import yaml | ||
import argparse | ||
from .config import enable_visualdl, enable_static, enable_prim | ||
|
||
__all__ = ['parse_args'] | ||
|
||
|
||
class AttrDict(dict): | ||
def __getattr__(self, key): | ||
try: | ||
return self[key] | ||
except KeyError: | ||
raise AttributeError(key) | ||
|
||
def __setattr__(self, key, value): | ||
if key in self.__dict__: | ||
self.__dict__[key] = value | ||
else: | ||
self[key] = value | ||
|
||
|
||
def create_attr_dict(yaml_config): | ||
from ast import literal_eval | ||
for key, value in yaml_config.items(): | ||
if type(value) is dict: | ||
yaml_config[key] = value = AttrDict(value) | ||
if isinstance(value, str): | ||
try: | ||
value = literal_eval(value) | ||
except BaseException: | ||
pass | ||
if isinstance(value, AttrDict): | ||
create_attr_dict(yaml_config[key]) | ||
else: | ||
yaml_config[key] = value | ||
|
||
|
||
def parse_config(cfg_file, config_index): | ||
"""Load a config file into AttrDict""" | ||
with open(cfg_file, 'r') as fopen: | ||
yaml_config = yaml.load(fopen, Loader=yaml.SafeLoader) | ||
create_attr_dict(yaml_config[config_index]) | ||
return yaml_config[config_index] | ||
|
||
|
||
def override(dl, ks, v): | ||
""" | ||
Recursively replace dict of list | ||
Args: | ||
dl(dict or list): dict or list to be replaced | ||
ks(list): list of keys | ||
v(str): value to be replaced | ||
""" | ||
|
||
def str2num(v): | ||
try: | ||
return eval(v) | ||
except Exception: | ||
return v | ||
|
||
assert isinstance(dl, (list, dict)), ("{} should be a list or a dict") | ||
assert len(ks) > 0, ('lenght of keys should larger than 0') | ||
if isinstance(dl, list): | ||
k = str2num(ks[0]) | ||
if len(ks) == 1: | ||
assert k < len(dl), ('index({}) out of range({})'.format(k, dl)) | ||
dl[k] = str2num(v) | ||
else: | ||
override(dl[k], ks[1:], v) | ||
else: | ||
if len(ks) == 1: | ||
assert ks[0] in dl, ('{} is not exist in {}'.format(ks[0], dl)) | ||
dl[ks[0]] = str2num(v) | ||
else: | ||
override(dl[ks[0]], ks[1:], v) | ||
|
||
|
||
def override_config(config, options=None): | ||
""" | ||
Recursively override the config | ||
Args: | ||
config(dict): dict to be replaced | ||
options(list): list of pairs(key0.key1.idx.key2=value) | ||
such as: [ | ||
'topk=2', | ||
'VALID.transforms.1.ResizeImage.resize_short=300' | ||
] | ||
Returns: | ||
config(dict): replaced config | ||
""" | ||
if options is not None: | ||
for opt in options: | ||
assert isinstance(opt, | ||
str), ("option({}) should be a str".format(opt)) | ||
assert "=" in opt, ( | ||
"option({}) should contain a =" | ||
"to distinguish between key and value".format(opt)) | ||
pair = opt.split('=') | ||
assert len(pair) == 2, ("there can be only a = in the option") | ||
key, value = pair | ||
keys = key.split('.') | ||
override(config, keys, value) | ||
|
||
return config | ||
|
||
|
||
def get_config(fname, config_index=0, overrides=None): | ||
""" | ||
Read config from file | ||
""" | ||
if fname is None and overrides is None: | ||
return None | ||
assert os.path.exists(fname), ( | ||
'config file({}) is not exist'.format(fname)) | ||
config = parse_config(fname, config_index) | ||
override_config(config, overrides) | ||
|
||
return config | ||
|
||
|
||
def parse_args(): | ||
parser = argparse.ArgumentParser(description='PaddleScience') | ||
parser.add_argument( | ||
'-c', '--config-file', metavar="FILE", help='config file path') | ||
|
||
parser.add_argument( | ||
'-i', | ||
'--config-index', | ||
type=int, | ||
default=0, | ||
help='run validation every interval') | ||
|
||
# config options | ||
parser.add_argument( | ||
"-o", "--opt", nargs='+', help="set configuration options") | ||
|
||
args = parser.parse_args() | ||
|
||
# Get config | ||
cfg = get_config(args.config_file, args.config_index, args.opt) | ||
|
||
if cfg is not None: | ||
# Enable related flags | ||
if cfg['Global']['use_visualdl'] == True: | ||
enable_visualdl() | ||
if cfg['Global']['static_enable'] == True: | ||
enable_static() | ||
if cfg['Global']['prim_enable'] == True: | ||
enable_prim() | ||
else: | ||
pass | ||
|
||
return cfg |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
- input: 0 | ||
Global: | ||
use_visualdl: False | ||
static_enable: False | ||
prim_enable: False | ||
epochs: 20000 | ||
Geometry: | ||
npoints: 512 | ||
seed: 1 | ||
sampler_method: 'sampling' | ||
Model: | ||
num_layers: 5 | ||
hidden_size: 20 | ||
activation: 'tanh' | ||
Optimizer: | ||
lr: | ||
learning_rate: 0.001 | ||
Post-processing: | ||
solution_filename: 'output_laplace2d' | ||
vtk_filename: 'output_laplace2d' | ||
checkpoint_path: 'checkpoints' | ||
- input: 1 | ||
Global: | ||
use_visualdl: False | ||
static_enable: False | ||
prim_enable: False | ||
epochs: 20000 | ||
Geometry: | ||
npoints: 258 | ||
seed: 1 | ||
sampler_method: 'sampling' | ||
Model: | ||
num_layers: 5 | ||
hidden_size: 20 | ||
activation: 'tanh' | ||
Optimizer: | ||
lr: | ||
learning_rate: 0.001 | ||
Post-processing: | ||
solution_filename: 'output_laplace2d' | ||
vtk_filename: 'output_laplace2d' | ||
checkpoint_path: 'checkpoints' |