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

[Cherry-Pick]Fix TransDataBackend Error when call unsqueeze using MKL Tensor (#46094) #46186

Merged
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
2 changes: 1 addition & 1 deletion paddle/phi/api/lib/data_transform.cc
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ paddle::optional<std::vector<phi::DenseTensor>> 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));
}
}
Expand Down
42 changes: 40 additions & 2 deletions python/paddle/fluid/tests/unittests/test_jit_layer.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
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

Expand Down Expand Up @@ -51,9 +51,14 @@ def infer(self, input):

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()
Expand All @@ -74,8 +79,41 @@ def test_multi_load(self):
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()