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

[jit] jit.save support property serialization #44581

Merged
merged 6 commits into from
Jul 27, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions python/paddle/fluid/dygraph/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
INFER_MODEL_SUFFIX = ".pdmodel"
INFER_PARAMS_SUFFIX = ".pdiparams"
INFER_PARAMS_INFO_SUFFIX = ".pdiparams.info"
INFER_PROPERTY_SUFFIX = '.meta'
zh794390558 marked this conversation as resolved.
Show resolved Hide resolved

LOADED_VAR_SUFFIX = "load"
PARAMETER_NAME_PREFIX = "param"
Expand Down
34 changes: 32 additions & 2 deletions python/paddle/fluid/dygraph/jit.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
from paddle.fluid.dygraph.dygraph_to_static.convert_call_func import ConversionOptions, CONVERSION_OPTIONS
from paddle.fluid.dygraph.dygraph_to_static.logging_utils import set_code_level, set_verbosity
from paddle.fluid.dygraph.dygraph_to_static.program_translator import ProgramTranslator, StaticFunction, unwrap_decorators
from paddle.fluid.dygraph.io import TranslatedLayer, INFER_MODEL_SUFFIX, INFER_PARAMS_SUFFIX, INFER_PARAMS_INFO_SUFFIX
from paddle.fluid.dygraph.io import TranslatedLayer, INFER_MODEL_SUFFIX, INFER_PARAMS_SUFFIX, INFER_PARAMS_INFO_SUFFIX, INFER_PROPERTY_SUFFIX
from paddle.fluid.dygraph.layers import Layer
from paddle.fluid.executor import Executor, scope_guard
from paddle.fluid.framework import Block, ParamBase, Program, Variable, Parameter, EagerParamBase
Expand Down Expand Up @@ -1034,7 +1034,37 @@ def fun(inputs):
filter(paddle.fluid.io.is_persistable,
all_vars)),
filename=params_filename)
# TODO: save property
# save property
property_filename = file_prefix + INFER_PROPERTY_SUFFIX

def set_property(meta, key, val):
if isinstance(val, float):
meta.set_float(key, val)
elif isinstance(val, int):
meta.set_int(key, val)
elif isinstance(val, str):
meta.set_string(key, val)
elif isinstance(val, paddle.Tensor):
meta.set_tensor(key, val)
elif isinstance(val, (tuple, list)):
if isinstance(val[0], float):
meta.set_floats(key, val)
elif isinstance(val[0], int):
meta.set_ints(key, val)
elif isinstance(val[0], str):
meta.set_strings(key, val)
elif isinstance(val[0], paddle.Tensor):
meta.set_tensors(key, val)
else:
raise ValueError(f"Note support val type: {type(val)}")
return

with open(property_filename, 'wb') as f:
meta = paddle.framework.core.Property()
for item in property_vals:
val, key = item[0], item[1]
set_property(meta, key, val)
f.write(meta.serialize_to_string())
zh794390558 marked this conversation as resolved.
Show resolved Hide resolved

# NOTE(chenweihang): [ Save extra variable info ]
# save_inference_model will lose some important variable information, including:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,10 @@ def test_set_float_wo_name(self):
"""
a = paddle.framework.core.Property()
a.set_float(10.0)
self.assertEqual(a.get_float(0), 10.0)
self.assertAlmostEqual(a.get_float(0), 10.0)

a.set_float("ftest", 1.4)
self.assertAlmostEqual(a.get_float('ftest'), 1.4)

def test_set(self):
"""test propety set.
Expand Down
28 changes: 22 additions & 6 deletions python/paddle/fluid/tests/unittests/test_jit_save_load.py
Original file line number Diff line number Diff line change
Expand Up @@ -1156,7 +1156,7 @@ def forward(self, x):
class Net(paddle.nn.Layer):

def __init__(self):
super(Net, self).__init__()
super().__init__()
self.fc1 = paddle.nn.Linear(4, 4)
self.fc2 = paddle.nn.Linear(4, 4)
self.bias = 0.4
Expand Down Expand Up @@ -1185,13 +1185,28 @@ def infer(self, input):
def fbias(self):
return self.bias + 1

# For extra Tensor
@paddle.jit.to_static(property=True)
def fflag(self):
return self.flag
def down_sampling(self):
return 4

@paddle.jit.to_static(property=True)
def fstr(self):
return "save str property"

@paddle.jit.to_static(property=True)
def ints(self):
return [10, 20]

@paddle.jit.to_static(property=True)
def floats(self):
return [1.1, 2.2]

class TestJitSaveCombine(unittest.TestCase):
@paddle.jit.to_static(property=True)
def strs(self):
return ["hello", "world"]


class TestJitSaveCombineProperty(unittest.TestCase):

def setUp(self):
# enable dygraph mode
Expand All @@ -1201,13 +1216,14 @@ def setUp(self):
def tearDown(self):
self.temp_dir.cleanup()

def test_save_load_finetune_load(self):
def test_jit_save_combine_property(self):
model_path = os.path.join(self.temp_dir.name,
"test_jit_save_combine/model")

# Use new namespace
with unique_name.guard():
net = Net()

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里的单测只是测试导出是成功的,虽然语音这边暂时没有Python端load的需求,但是否需要在单测里加一下「反序列化」读取的测试逻辑?

Copy link
Contributor Author

@zh794390558 zh794390558 Jul 26, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Property反序列化的测试在python/paddle/fluid/tests/unittests/dygraph_to_static/test_jit_property_save.py 中。

#save
paddle.jit.save(net, model_path, combine_params=True)

Expand Down