diff --git a/paddle/phi/api/lib/data_transform.cc b/paddle/phi/api/lib/data_transform.cc index 23ff797d77c01a6d39ac3a91970774f9d124c2ee..72e65ae5286ee562097d6a075fb73125c15220ac 100644 --- a/paddle/phi/api/lib/data_transform.cc +++ b/paddle/phi/api/lib/data_transform.cc @@ -307,7 +307,7 @@ paddle::optional> PrepareData( void TransDataBackend(const phi::DenseTensor* tensor, Backend target_backend, phi::DenseTensor* out) { - if (tensor) { + if (tensor && tensor->initialized()) { *out = TransDataPlace(*tensor, phi::TransToPhiPlace(target_backend)); } } diff --git a/python/paddle/fluid/tests/unittests/test_jit_layer.py b/python/paddle/fluid/tests/unittests/test_jit_layer.py index bc5658127b2da341633486a24bd52fa25b1037ac..5a03e0ac3b80ee71bfc89857269582487950932e 100644 --- a/python/paddle/fluid/tests/unittests/test_jit_layer.py +++ b/python/paddle/fluid/tests/unittests/test_jit_layer.py @@ -18,7 +18,7 @@ import unittest import tempfile import numpy as np from paddle.static import InputSpec -from paddle.fluid.framework import _enable_legacy_dygraph +from paddle.fluid.framework import _dygraph_place_guard from paddle.jit.layer import Layer from paddle.fluid.dygraph.dygraph_to_static.program_translator import ProgramTranslator @@ -51,9 +51,14 @@ class Net(paddle.nn.Layer): class TestMultiLoad(unittest.TestCase): - def test_multi_load(self): + def setUp(self): self.temp_dir = tempfile.TemporaryDirectory() + def tearDown(self): + self.temp_dir.cleanup() + + def test_multi_load(self): + x = paddle.full([2, 4], 2) model = Net() program_translator = ProgramTranslator() @@ -74,8 +79,41 @@ class TestMultiLoad(unittest.TestCase): np.testing.assert_allclose(forward_out1, forward_out2[0], rtol=1e-05) np.testing.assert_allclose(infer_out1, infer_out2[0], rtol=1e-05) + +class SaveLinear(paddle.nn.Layer): + + def __init__(self): + super().__init__() + self.linear = paddle.nn.Linear(80, 80) + + @paddle.jit.to_static( + input_spec=[InputSpec(shape=[None, 80], dtype='float32')]) + def forward(self, x): + out = self.linear(x) + return out + + +class TestMKLOutput(unittest.TestCase): + + def setUp(self): + self.temp_dir = tempfile.TemporaryDirectory() + + def tearDown(self): self.temp_dir.cleanup() + def test_mkl_output(self): + with _dygraph_place_guard(place=paddle.CPUPlace()): + net = SaveLinear() + model_path = os.path.join(self.temp_dir.name, 'save_linear') + paddle.jit.save(net, model_path, combine_params=True) + + layer = Layer() + layer.load(model_path, paddle.CPUPlace()) + x = paddle.ones([498, 80]) + out = layer.forward(x) + out = paddle.unsqueeze(out[0], 0) + np.testing.assert_equal(out.shape, [1, 498, 80]) + if __name__ == '__main__': unittest.main()