From f15bed11bd2be6cb02cf0af2dfc7aac0fa76e690 Mon Sep 17 00:00:00 2001 From: liym27 <33742067+liym27@users.noreply.github.com> Date: Tue, 19 Jan 2021 21:06:03 +0800 Subject: [PATCH] [Cherry-Pick] Fix bug: GetAttrValue should deal with attr with attrType vector (#30564) cherry-pick #30536 --- paddle/fluid/framework/attribute.cc | 9 +++++++ .../unittests/dygraph_to_static/test_slice.py | 24 +++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/paddle/fluid/framework/attribute.cc b/paddle/fluid/framework/attribute.cc index 7460686c1a..63934d17f9 100644 --- a/paddle/fluid/framework/attribute.cc +++ b/paddle/fluid/framework/attribute.cc @@ -69,6 +69,15 @@ Attribute GetAttrValue(const proto::OpDesc::Attr& attr_desc) { } return val; } + + case proto::AttrType::FLOAT64S: { + std::vector val(attr_desc.float64s_size()); + for (int i = 0; i < attr_desc.float64s_size(); ++i) { + val[i] = attr_desc.float64s(i); + } + return val; + } + default: PADDLE_THROW(platform::errors::Unavailable("Unsupport attribute type %d.", attr_desc.type())); diff --git a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_slice.py b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_slice.py index bf74299806..0d856f26c7 100644 --- a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_slice.py +++ b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_slice.py @@ -92,6 +92,18 @@ def test_set_value(x): return x +class LayerWithSetValue(paddle.nn.Layer): + def __init__(self, input_dim, hidden): + super(LayerWithSetValue, self).__init__() + self.linear = paddle.nn.Linear(input_dim, hidden) + + @paddle.jit.to_static + def forward(self, x): + x = self.linear(x) + x[0] = 1 + return x + + class TestSliceWithoutControlFlow(unittest.TestCase): def setUp(self): self.init_input() @@ -149,5 +161,17 @@ class TestSetValue(TestSliceWithoutControlFlow): self.dygraph_func = test_set_value +class TestSetValueWithLayerAndSave(unittest.TestCase): + def test_set_value_with_save(self): + prog_trans.enable(True) + model = LayerWithSetValue(input_dim=10, hidden=1) + x = paddle.full(shape=[5, 10], fill_value=5.0, dtype="float32") + paddle.jit.save( + layer=model, + path="./layer_use_set_value", + input_spec=[x], + output_spec=None) + + if __name__ == '__main__': unittest.main() -- GitLab