Skip to content

Commit

Permalink
Remove some headers to decrease the size of executable file
Browse files Browse the repository at this point in the history
  • Loading branch information
joey12300 committed Mar 26, 2022
1 parent 6f8decf commit 71a8d1a
Show file tree
Hide file tree
Showing 8 changed files with 237 additions and 11 deletions.
2 changes: 1 addition & 1 deletion paddle/phi/api/lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -173,5 +173,5 @@ cc_library(phi_bw_function_api SRCS ${bw_api_source_file} DEPS phi_tensor_raw ph
cc_library(sparse_api SRCS ${sparse_api_source_file} DEPS phi_tensor_raw phi kernel_dispatch api_gen_utils sparse_api_custom_impl)
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)
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)
cc_library(strings_api SRCS ${strings_api_source_file} DEPS phi_tensor_raw phi kernel_dispatch api_gen_utils phi_data_transform)
cc_library(strings_api SRCS ${strings_api_source_file} DEPS phi_tensor_raw phi kernel_dispatch api_gen_utils)
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)
10 changes: 5 additions & 5 deletions paddle/phi/api/lib/api_gen_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -160,12 +160,12 @@ phi::TensorBase* SetStringsKernelOutput(Backend backend,
Tensor* out,
TensorType type) {
if (!out->initialized()) {
auto place = phi::TransToPhiPlace(backend);
if (type == TensorType::STRING_TENSOR) {
auto strings_tensor = std::make_shared<phi::StringTensor>(
paddle::memory::AllocShared(place, 0), phi::StringTensorMeta());
out->set_impl(strings_tensor);
return strings_tensor.get();
if (out->impl() == nullptr) {
auto strings_tensor = std::make_shared<phi::StringTensor>();
out->set_impl(strings_tensor);
}
return out->impl().get();
}
}
return out->impl().get();
Expand Down
4 changes: 4 additions & 0 deletions paddle/phi/common/layout.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ inline DataLayout StringToDataLayout(const std::string& str) {
return DataLayout::SPARSE_CSR;
} else if (s == "NDHWC") {
return DataLayout::kNDHWC;
} else if (s == "PSTRING_UNION") {
return DataLayout::PSTRING_UNION;
} else if (s == "NCDHW") {
return DataLayout::kNCDHW;
} else {
Expand All @@ -102,6 +104,8 @@ inline std::string DataLayoutToString(const DataLayout& layout) {
return "NDHWC";
case DataLayout::kNCDHW:
return "NCDHW";
case DataLayout::PSTRING_UNION:
return "PSTRING_UNION";
default:
PD_THROW("Unknown Data Layout type ", static_cast<int>(layout), ".");
}
Expand Down
2 changes: 2 additions & 0 deletions paddle/phi/tests/api/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,5 @@ cc_test(test_split_api SRCS test_split_api.cc DEPS ${COMMON_API_TEST_DEPS})
cc_test(test_data_transform SRCS test_data_transform.cc DEPS ${COMMON_API_TEST_DEPS})
cc_test(test_sparse_utils_api SRCS test_sparse_utils_api.cc DEPS ${COMMON_API_TEST_DEPS})
cc_test(test_sparse_conv_api SRCS test_sparse_conv_api.cc DEPS ${COMMON_API_TEST_DEPS})
cc_test(test_strings_empty_api SRCS test_strings_empty_api.cc DEPS ${COMMON_API_TEST_DEPS})
cc_test(test_strings_lower_upper_api SRCS test_strings_lower_upper_api.cc DEPS ${COMMON_API_TEST_DEPS})
83 changes: 83 additions & 0 deletions paddle/phi/tests/api/test_strings_empty_api.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/* Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See
the License for the specific language governing permissions and
limitations under the License. */

#include <gtest/gtest.h>
#include <memory>

#include "paddle/phi/api/include/strings_api.h"
#include "paddle/phi/api/lib/utils/allocator.h"
#include "paddle/phi/common/backend.h"
#include "paddle/phi/core/dense_tensor.h"
#include "paddle/phi/core/kernel_registry.h"
#include "paddle/phi/core/string_tensor.h"

PD_DECLARE_KERNEL(strings_empty, CPU, ALL_LAYOUT);
PD_DECLARE_KERNEL(strings_empty_like, CPU, ALL_LAYOUT);

namespace paddle {
namespace tests {

using phi::CPUPlace;
using phi::StringTensor;
using phi::StringTensorMeta;

TEST(API, strings_empty) {
// 1. create tensor
auto cpu = CPUPlace();
const auto alloc =
std::make_shared<paddle::experimental::DefaultAllocator>(cpu);

auto dense_shape = std::make_shared<phi::DenseTensor>(
alloc.get(),
phi::DenseTensorMeta(
phi::DataType::INT64, phi::make_ddim({2}), phi::DataLayout::NCHW));
auto* shape_data =
dense_shape->mutable_data<int64_t>(paddle::platform::CPUPlace());
shape_data[0] = 2;
shape_data[1] = 3;

paddle::experimental::Tensor tensor_shape(dense_shape);

// 2. test API
auto empty_out = paddle::experimental::strings::empty(tensor_shape);

// 3. check result
ASSERT_EQ(empty_out.dims().size(), 2);
ASSERT_EQ(empty_out.dims()[0], 2);
ASSERT_EQ(empty_out.dims()[1], 3);
ASSERT_EQ(empty_out.numel(), 6);
}

TEST(API, strings_empty_like) {
auto cpu = CPUPlace();
const auto alloc =
std::make_shared<paddle::experimental::DefaultAllocator>(cpu);
// 1. create tensor
const phi::DDim dims({1, 2});
StringTensorMeta meta(dims);
auto cpu_strings_x = std::make_shared<phi::StringTensor>(
alloc.get(), phi::StringTensorMeta(meta));

// 2. test API
paddle::experimental::Tensor x(cpu_strings_x);
auto empty_like_out = paddle::experimental::strings::empty_like(x);

// 3. check result
ASSERT_EQ(empty_like_out.dims().size(), 2);
ASSERT_EQ(empty_like_out.dims()[0], 1);
ASSERT_EQ(empty_like_out.numel(), 2);
}

} // namespace tests
} // namespace paddle
137 changes: 137 additions & 0 deletions paddle/phi/tests/api/test_strings_lower_upper_api.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
/* Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See
the License for the specific language governing permissions and
limitations under the License. */

#include <gtest/gtest.h>
#include <memory>

#include "paddle/phi/api/include/strings_api.h"
#include "paddle/phi/api/lib/utils/allocator.h"
#include "paddle/phi/backends/all_context.h"
#include "paddle/phi/core/kernel_registry.h"
#include "paddle/phi/core/string_tensor.h"

PD_DECLARE_KERNEL(strings_lower, CPU, ALL_LAYOUT);
PD_DECLARE_KERNEL(strings_upper, CPU, ALL_LAYOUT);

namespace paddle {
namespace tests {

using phi::CPUPlace;
using phi::StringTensor;
using phi::StringTensorMeta;

TEST(API, case_convert) {
auto cpu = CPUPlace();
const auto alloc =
std::make_shared<paddle::experimental::DefaultAllocator>(cpu);
// 1. create tensor
const phi::DDim dims({1, 2});
StringTensorMeta meta(dims);
auto cpu_strings_x = std::make_shared<phi::StringTensor>(
alloc.get(), phi::StringTensorMeta(meta));
phi::DeviceContextPool& pool = phi::DeviceContextPool::Instance();
auto* dev_ctx = pool.Get(phi::CPUPlace());

pstring* cpu_strings_x_data =
dev_ctx->template Alloc<pstring>(cpu_strings_x.get());
std::string strs[] = {"A Short Pstring.",
"A Large Pstring Whose Length Is Longer Than 22."};
for (int i = 0; i < 2; ++i) {
cpu_strings_x_data[i] = strs[i];
}
// 2. get expected results
std::string expected_results[] = {strs[0], strs[0], strs[1], strs[1]};
std::transform(
strs[0].begin(), strs[0].end(), expected_results[0].begin(), ::tolower);
std::transform(
strs[0].begin(), strs[0].end(), expected_results[1].begin(), ::toupper);
std::transform(
strs[1].begin(), strs[1].end(), expected_results[2].begin(), ::tolower);
std::transform(
strs[1].begin(), strs[1].end(), expected_results[3].begin(), ::toupper);
// 3. test API, ascii encoding
paddle::experimental::Tensor x(cpu_strings_x);
auto lower_out = paddle::experimental::strings::lower(x, false);
auto upper_out = paddle::experimental::strings::upper(x, false);

auto lower_tensor =
std::dynamic_pointer_cast<phi::StringTensor>(lower_out.impl());
auto upper_tensor =
std::dynamic_pointer_cast<phi::StringTensor>(upper_out.impl());
ASSERT_EQ(lower_tensor->dims(), dims);
ASSERT_EQ(upper_tensor->dims(), dims);

auto lower_tensor_ptr = lower_tensor->data();
auto upper_tensor_ptr = upper_tensor->data();

const std::string cpu_results[] = {lower_tensor_ptr[0].data(),
upper_tensor_ptr[0].data(),
lower_tensor_ptr[1].data(),
upper_tensor_ptr[1].data()};

for (int i = 0; i < 4; ++i) {
ASSERT_EQ(cpu_results[i], expected_results[i]);
}
}

TEST(API, case_convert_utf8) {
auto cpu = CPUPlace();
const auto alloc =
std::make_shared<paddle::experimental::DefaultAllocator>(cpu);
// 1. create tensor
const phi::DDim dims({1, 2});
StringTensorMeta meta(dims);
auto cpu_strings_x = std::make_shared<phi::StringTensor>(
alloc.get(), phi::StringTensorMeta(meta));
phi::DeviceContextPool& pool = phi::DeviceContextPool::Instance();
auto* dev_ctx = pool.Get(phi::CPUPlace());

pstring* cpu_strings_x_data =
dev_ctx->template Alloc<pstring>(cpu_strings_x.get());
std::string strs[] = {"óÓsscHloëË", "óÓsscHloëËóÓsscHloëËóÓsscHloëË"};
for (int i = 0; i < 2; ++i) {
cpu_strings_x_data[i] = strs[i];
}
// 2. get expected results
std::string expected_results[] = {"óósschloëë",
"ÓÓSSCHLOËË",
"óósschloëëóósschloëëóósschloëë",
"ÓÓSSCHLOËËÓÓSSCHLOËËÓÓSSCHLOËË"};
// 3. test API, ascii encoding
paddle::experimental::Tensor x(cpu_strings_x);
auto lower_out = paddle::experimental::strings::lower(x, true);
auto upper_out = paddle::experimental::strings::upper(x, true);

auto lower_tensor =
std::dynamic_pointer_cast<phi::StringTensor>(lower_out.impl());
auto upper_tensor =
std::dynamic_pointer_cast<phi::StringTensor>(upper_out.impl());
ASSERT_EQ(lower_tensor->dims(), dims);
ASSERT_EQ(upper_tensor->dims(), dims);

auto lower_tensor_ptr = lower_tensor->data();
auto upper_tensor_ptr = upper_tensor->data();

const char* cpu_results[] = {lower_tensor_ptr[0].data(),
upper_tensor_ptr[0].data(),
lower_tensor_ptr[1].data(),
upper_tensor_ptr[1].data()};

for (int i = 0; i < 4; ++i) {
ASSERT_EQ(std::string(cpu_results[i]), expected_results[i]);
}
}

} // namespace tests
} // namespace paddle
4 changes: 2 additions & 2 deletions paddle/phi/tests/core/test_string_tensor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -171,14 +171,14 @@ TEST(pstring, func) {
CHECK_EQ(nchar_str, "AAAAAB");

// Test operator =
long_str = std::move(nchar_str);
CHECK_EQ(long_str, "AAAAAB");
long_str = short_str;
CHECK_EQ(short_str, long_str);
short_str = 'A';
CHECK_EQ(short_str, "A");
short_str = std::move(copy_nchar_str);
CHECK_EQ(short_str, "AAAAA");
long_str = std::move(nchar_str);
CHECK_EQ(long_str, "AAAAAB");
}

} // namespace tests
Expand Down
6 changes: 3 additions & 3 deletions python/paddle/utils/code_gen/strings_api_gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ def gen_string_tensor_kernel_code(self, inplace_flag=False, code_indent=""):

return f"""
// 1. Get kernel signature and kernel
auto kernel = phi::KernelFactory::Instance().SelectKernelOrThrowError(
const auto& kernel = phi::KernelFactory::Instance().SelectKernelOrThrowError(
"{self.kernel['func'][0]}", {{kernel_backend, kernel_layout, kernel_data_type}});
VLOG(6) << "{self.api} api strings kernel key: [" << kernel_backend << ", " << kernel_layout << ", "<< kernel_data_type << "]";
VLOG(6) << "{self.api} api strings kernel: " << kernel;
Expand Down Expand Up @@ -276,9 +276,9 @@ def header_include():
#include <tuple>
#include "paddle/phi/api/include/tensor.h"
#include "paddle/phi/common/backend.h"
#include "paddle/phi/common/scalar.h"
#include "paddle/phi/common/scalar_array.h"
#include "paddle/utils/optional.h"
"""


Expand All @@ -291,9 +291,9 @@ def source_include(header_file_path):
#include "paddle/phi/core/string_tensor.h"
#include "paddle/phi/infermeta/strings/nullary.h"
#include "paddle/phi/infermeta/strings/unary.h"
#include "paddle/phi/kernels/declarations.h"
#include "paddle/phi/api/lib/api_registry.h"
#include "paddle/phi/api/lib/kernel_dispatch.h"
#include "paddle/phi/core/kernel_registry.h"
"""


Expand Down

1 comment on commit 71a8d1a

@paddle-bot-old
Copy link

@paddle-bot-old paddle-bot-old bot commented on 71a8d1a Mar 26, 2022

Choose a reason for hiding this comment

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

🕵️ CI failures summary

🔍 PR: #39830 Commit ID: 71a8d1a contains failed CI.

🔹 Failed: PR-CI-APPROVAL

Unknown Failed
Unknown Failed

Please sign in to comment.