Skip to content

Commit 71a8d1a

Browse files
committed
Remove some headers to decrease the size of executable file
1 parent 6f8decf commit 71a8d1a

File tree

8 files changed

+237
-11
lines changed

8 files changed

+237
-11
lines changed

paddle/phi/api/lib/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,5 +173,5 @@ cc_library(phi_bw_function_api SRCS ${bw_api_source_file} DEPS phi_tensor_raw ph
173173
cc_library(sparse_api SRCS ${sparse_api_source_file} DEPS phi_tensor_raw phi kernel_dispatch api_gen_utils sparse_api_custom_impl)
174174
cc_library(sparse_bw_api SRCS ${sparse_bw_api_source_file} DEPS phi_tensor_raw phi kernel_dispatch api_gen_utils sparse_api sparse_api_custom_impl)
175175
cc_library(phi_dygraph_api SRCS ${dygraph_api_source_file} DEPS phi_tensor_raw phi kernel_dispatch api_gen_utils phi_data_transform phi_function_api sparse_api)
176-
cc_library(strings_api SRCS ${strings_api_source_file} DEPS phi_tensor_raw phi kernel_dispatch api_gen_utils phi_data_transform)
176+
cc_library(strings_api SRCS ${strings_api_source_file} DEPS phi_tensor_raw phi kernel_dispatch api_gen_utils)
177177
cc_library(phi_tensor SRCS tensor_method.cc DEPS phi_tensor_raw phi_function_api api_gen_utils kernel_dispatch infermeta sparse_api strings_api)

paddle/phi/api/lib/api_gen_utils.cc

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -160,12 +160,12 @@ phi::TensorBase* SetStringsKernelOutput(Backend backend,
160160
Tensor* out,
161161
TensorType type) {
162162
if (!out->initialized()) {
163-
auto place = phi::TransToPhiPlace(backend);
164163
if (type == TensorType::STRING_TENSOR) {
165-
auto strings_tensor = std::make_shared<phi::StringTensor>(
166-
paddle::memory::AllocShared(place, 0), phi::StringTensorMeta());
167-
out->set_impl(strings_tensor);
168-
return strings_tensor.get();
164+
if (out->impl() == nullptr) {
165+
auto strings_tensor = std::make_shared<phi::StringTensor>();
166+
out->set_impl(strings_tensor);
167+
}
168+
return out->impl().get();
169169
}
170170
}
171171
return out->impl().get();

paddle/phi/common/layout.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ inline DataLayout StringToDataLayout(const std::string& str) {
7777
return DataLayout::SPARSE_CSR;
7878
} else if (s == "NDHWC") {
7979
return DataLayout::kNDHWC;
80+
} else if (s == "PSTRING_UNION") {
81+
return DataLayout::PSTRING_UNION;
8082
} else if (s == "NCDHW") {
8183
return DataLayout::kNCDHW;
8284
} else {
@@ -102,6 +104,8 @@ inline std::string DataLayoutToString(const DataLayout& layout) {
102104
return "NDHWC";
103105
case DataLayout::kNCDHW:
104106
return "NCDHW";
107+
case DataLayout::PSTRING_UNION:
108+
return "PSTRING_UNION";
105109
default:
106110
PD_THROW("Unknown Data Layout type ", static_cast<int>(layout), ".");
107111
}

paddle/phi/tests/api/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,5 @@ cc_test(test_split_api SRCS test_split_api.cc DEPS ${COMMON_API_TEST_DEPS})
2727
cc_test(test_data_transform SRCS test_data_transform.cc DEPS ${COMMON_API_TEST_DEPS})
2828
cc_test(test_sparse_utils_api SRCS test_sparse_utils_api.cc DEPS ${COMMON_API_TEST_DEPS})
2929
cc_test(test_sparse_conv_api SRCS test_sparse_conv_api.cc DEPS ${COMMON_API_TEST_DEPS})
30+
cc_test(test_strings_empty_api SRCS test_strings_empty_api.cc DEPS ${COMMON_API_TEST_DEPS})
31+
cc_test(test_strings_lower_upper_api SRCS test_strings_lower_upper_api.cc DEPS ${COMMON_API_TEST_DEPS})
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/* Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See
12+
the License for the specific language governing permissions and
13+
limitations under the License. */
14+
15+
#include <gtest/gtest.h>
16+
#include <memory>
17+
18+
#include "paddle/phi/api/include/strings_api.h"
19+
#include "paddle/phi/api/lib/utils/allocator.h"
20+
#include "paddle/phi/common/backend.h"
21+
#include "paddle/phi/core/dense_tensor.h"
22+
#include "paddle/phi/core/kernel_registry.h"
23+
#include "paddle/phi/core/string_tensor.h"
24+
25+
PD_DECLARE_KERNEL(strings_empty, CPU, ALL_LAYOUT);
26+
PD_DECLARE_KERNEL(strings_empty_like, CPU, ALL_LAYOUT);
27+
28+
namespace paddle {
29+
namespace tests {
30+
31+
using phi::CPUPlace;
32+
using phi::StringTensor;
33+
using phi::StringTensorMeta;
34+
35+
TEST(API, strings_empty) {
36+
// 1. create tensor
37+
auto cpu = CPUPlace();
38+
const auto alloc =
39+
std::make_shared<paddle::experimental::DefaultAllocator>(cpu);
40+
41+
auto dense_shape = std::make_shared<phi::DenseTensor>(
42+
alloc.get(),
43+
phi::DenseTensorMeta(
44+
phi::DataType::INT64, phi::make_ddim({2}), phi::DataLayout::NCHW));
45+
auto* shape_data =
46+
dense_shape->mutable_data<int64_t>(paddle::platform::CPUPlace());
47+
shape_data[0] = 2;
48+
shape_data[1] = 3;
49+
50+
paddle::experimental::Tensor tensor_shape(dense_shape);
51+
52+
// 2. test API
53+
auto empty_out = paddle::experimental::strings::empty(tensor_shape);
54+
55+
// 3. check result
56+
ASSERT_EQ(empty_out.dims().size(), 2);
57+
ASSERT_EQ(empty_out.dims()[0], 2);
58+
ASSERT_EQ(empty_out.dims()[1], 3);
59+
ASSERT_EQ(empty_out.numel(), 6);
60+
}
61+
62+
TEST(API, strings_empty_like) {
63+
auto cpu = CPUPlace();
64+
const auto alloc =
65+
std::make_shared<paddle::experimental::DefaultAllocator>(cpu);
66+
// 1. create tensor
67+
const phi::DDim dims({1, 2});
68+
StringTensorMeta meta(dims);
69+
auto cpu_strings_x = std::make_shared<phi::StringTensor>(
70+
alloc.get(), phi::StringTensorMeta(meta));
71+
72+
// 2. test API
73+
paddle::experimental::Tensor x(cpu_strings_x);
74+
auto empty_like_out = paddle::experimental::strings::empty_like(x);
75+
76+
// 3. check result
77+
ASSERT_EQ(empty_like_out.dims().size(), 2);
78+
ASSERT_EQ(empty_like_out.dims()[0], 1);
79+
ASSERT_EQ(empty_like_out.numel(), 2);
80+
}
81+
82+
} // namespace tests
83+
} // namespace paddle
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
/* Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See
12+
the License for the specific language governing permissions and
13+
limitations under the License. */
14+
15+
#include <gtest/gtest.h>
16+
#include <memory>
17+
18+
#include "paddle/phi/api/include/strings_api.h"
19+
#include "paddle/phi/api/lib/utils/allocator.h"
20+
#include "paddle/phi/backends/all_context.h"
21+
#include "paddle/phi/core/kernel_registry.h"
22+
#include "paddle/phi/core/string_tensor.h"
23+
24+
PD_DECLARE_KERNEL(strings_lower, CPU, ALL_LAYOUT);
25+
PD_DECLARE_KERNEL(strings_upper, CPU, ALL_LAYOUT);
26+
27+
namespace paddle {
28+
namespace tests {
29+
30+
using phi::CPUPlace;
31+
using phi::StringTensor;
32+
using phi::StringTensorMeta;
33+
34+
TEST(API, case_convert) {
35+
auto cpu = CPUPlace();
36+
const auto alloc =
37+
std::make_shared<paddle::experimental::DefaultAllocator>(cpu);
38+
// 1. create tensor
39+
const phi::DDim dims({1, 2});
40+
StringTensorMeta meta(dims);
41+
auto cpu_strings_x = std::make_shared<phi::StringTensor>(
42+
alloc.get(), phi::StringTensorMeta(meta));
43+
phi::DeviceContextPool& pool = phi::DeviceContextPool::Instance();
44+
auto* dev_ctx = pool.Get(phi::CPUPlace());
45+
46+
pstring* cpu_strings_x_data =
47+
dev_ctx->template Alloc<pstring>(cpu_strings_x.get());
48+
std::string strs[] = {"A Short Pstring.",
49+
"A Large Pstring Whose Length Is Longer Than 22."};
50+
for (int i = 0; i < 2; ++i) {
51+
cpu_strings_x_data[i] = strs[i];
52+
}
53+
// 2. get expected results
54+
std::string expected_results[] = {strs[0], strs[0], strs[1], strs[1]};
55+
std::transform(
56+
strs[0].begin(), strs[0].end(), expected_results[0].begin(), ::tolower);
57+
std::transform(
58+
strs[0].begin(), strs[0].end(), expected_results[1].begin(), ::toupper);
59+
std::transform(
60+
strs[1].begin(), strs[1].end(), expected_results[2].begin(), ::tolower);
61+
std::transform(
62+
strs[1].begin(), strs[1].end(), expected_results[3].begin(), ::toupper);
63+
// 3. test API, ascii encoding
64+
paddle::experimental::Tensor x(cpu_strings_x);
65+
auto lower_out = paddle::experimental::strings::lower(x, false);
66+
auto upper_out = paddle::experimental::strings::upper(x, false);
67+
68+
auto lower_tensor =
69+
std::dynamic_pointer_cast<phi::StringTensor>(lower_out.impl());
70+
auto upper_tensor =
71+
std::dynamic_pointer_cast<phi::StringTensor>(upper_out.impl());
72+
ASSERT_EQ(lower_tensor->dims(), dims);
73+
ASSERT_EQ(upper_tensor->dims(), dims);
74+
75+
auto lower_tensor_ptr = lower_tensor->data();
76+
auto upper_tensor_ptr = upper_tensor->data();
77+
78+
const std::string cpu_results[] = {lower_tensor_ptr[0].data(),
79+
upper_tensor_ptr[0].data(),
80+
lower_tensor_ptr[1].data(),
81+
upper_tensor_ptr[1].data()};
82+
83+
for (int i = 0; i < 4; ++i) {
84+
ASSERT_EQ(cpu_results[i], expected_results[i]);
85+
}
86+
}
87+
88+
TEST(API, case_convert_utf8) {
89+
auto cpu = CPUPlace();
90+
const auto alloc =
91+
std::make_shared<paddle::experimental::DefaultAllocator>(cpu);
92+
// 1. create tensor
93+
const phi::DDim dims({1, 2});
94+
StringTensorMeta meta(dims);
95+
auto cpu_strings_x = std::make_shared<phi::StringTensor>(
96+
alloc.get(), phi::StringTensorMeta(meta));
97+
phi::DeviceContextPool& pool = phi::DeviceContextPool::Instance();
98+
auto* dev_ctx = pool.Get(phi::CPUPlace());
99+
100+
pstring* cpu_strings_x_data =
101+
dev_ctx->template Alloc<pstring>(cpu_strings_x.get());
102+
std::string strs[] = {"óÓsscHloëË", "óÓsscHloëËóÓsscHloëËóÓsscHloëË"};
103+
for (int i = 0; i < 2; ++i) {
104+
cpu_strings_x_data[i] = strs[i];
105+
}
106+
// 2. get expected results
107+
std::string expected_results[] = {"óósschloëë",
108+
"ÓÓSSCHLOËË",
109+
"óósschloëëóósschloëëóósschloëë",
110+
"ÓÓSSCHLOËËÓÓSSCHLOËËÓÓSSCHLOËË"};
111+
// 3. test API, ascii encoding
112+
paddle::experimental::Tensor x(cpu_strings_x);
113+
auto lower_out = paddle::experimental::strings::lower(x, true);
114+
auto upper_out = paddle::experimental::strings::upper(x, true);
115+
116+
auto lower_tensor =
117+
std::dynamic_pointer_cast<phi::StringTensor>(lower_out.impl());
118+
auto upper_tensor =
119+
std::dynamic_pointer_cast<phi::StringTensor>(upper_out.impl());
120+
ASSERT_EQ(lower_tensor->dims(), dims);
121+
ASSERT_EQ(upper_tensor->dims(), dims);
122+
123+
auto lower_tensor_ptr = lower_tensor->data();
124+
auto upper_tensor_ptr = upper_tensor->data();
125+
126+
const char* cpu_results[] = {lower_tensor_ptr[0].data(),
127+
upper_tensor_ptr[0].data(),
128+
lower_tensor_ptr[1].data(),
129+
upper_tensor_ptr[1].data()};
130+
131+
for (int i = 0; i < 4; ++i) {
132+
ASSERT_EQ(std::string(cpu_results[i]), expected_results[i]);
133+
}
134+
}
135+
136+
} // namespace tests
137+
} // namespace paddle

paddle/phi/tests/core/test_string_tensor.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,14 +171,14 @@ TEST(pstring, func) {
171171
CHECK_EQ(nchar_str, "AAAAAB");
172172

173173
// Test operator =
174+
long_str = std::move(nchar_str);
175+
CHECK_EQ(long_str, "AAAAAB");
174176
long_str = short_str;
175177
CHECK_EQ(short_str, long_str);
176178
short_str = 'A';
177179
CHECK_EQ(short_str, "A");
178180
short_str = std::move(copy_nchar_str);
179181
CHECK_EQ(short_str, "AAAAA");
180-
long_str = std::move(nchar_str);
181-
CHECK_EQ(long_str, "AAAAAB");
182182
}
183183

184184
} // namespace tests

python/paddle/utils/code_gen/strings_api_gen.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ def gen_string_tensor_kernel_code(self, inplace_flag=False, code_indent=""):
175175

176176
return f"""
177177
// 1. Get kernel signature and kernel
178-
auto kernel = phi::KernelFactory::Instance().SelectKernelOrThrowError(
178+
const auto& kernel = phi::KernelFactory::Instance().SelectKernelOrThrowError(
179179
"{self.kernel['func'][0]}", {{kernel_backend, kernel_layout, kernel_data_type}});
180180
VLOG(6) << "{self.api} api strings kernel key: [" << kernel_backend << ", " << kernel_layout << ", "<< kernel_data_type << "]";
181181
VLOG(6) << "{self.api} api strings kernel: " << kernel;
@@ -276,9 +276,9 @@ def header_include():
276276
#include <tuple>
277277
278278
#include "paddle/phi/api/include/tensor.h"
279-
#include "paddle/phi/common/backend.h"
280279
#include "paddle/phi/common/scalar.h"
281280
#include "paddle/phi/common/scalar_array.h"
281+
#include "paddle/utils/optional.h"
282282
"""
283283

284284

@@ -291,9 +291,9 @@ def source_include(header_file_path):
291291
#include "paddle/phi/core/string_tensor.h"
292292
#include "paddle/phi/infermeta/strings/nullary.h"
293293
#include "paddle/phi/infermeta/strings/unary.h"
294-
#include "paddle/phi/kernels/declarations.h"
295294
#include "paddle/phi/api/lib/api_registry.h"
296295
#include "paddle/phi/api/lib/kernel_dispatch.h"
296+
#include "paddle/phi/core/kernel_registry.h"
297297
"""
298298

299299

0 commit comments

Comments
 (0)