diff --git a/cmake/pten.cmake b/cmake/pten.cmake index 8475c33e1e818c1aff7e0556a4c001adcd562c32..72014f2b9482ecef255a3c665b7e7b5a580de229 100644 --- a/cmake/pten.cmake +++ b/cmake/pten.cmake @@ -288,7 +288,7 @@ function(append_op_util_declare TARGET) string(REGEX MATCH "(PT_REGISTER_BASE_KERNEL_NAME|PT_REGISTER_ARG_MAPPING_FN)\\([ \t\r\n]*[a-z0-9_]*" util_registrar "${target_content}") string(REPLACE "PT_REGISTER_ARG_MAPPING_FN" "PT_DECLARE_ARG_MAPPING_FN" util_declare "${util_registrar}") string(REPLACE "PT_REGISTER_BASE_KERNEL_NAME" "PT_DECLARE_BASE_KERNEL_NAME" util_declare "${util_declare}") - string(APPEND util_declare ");") + string(APPEND util_declare ");\n") file(APPEND ${op_utils_header} "${util_declare}") endfunction() diff --git a/paddle/fluid/distributed/fleet_executor/test/compute_interceptor_run_op_test.cc b/paddle/fluid/distributed/fleet_executor/test/compute_interceptor_run_op_test.cc index 643ef52e87bdaff0d531a68922077a8877830a9f..beea1d5d00dac37848064721cb7e60afbaca3a47 100644 --- a/paddle/fluid/distributed/fleet_executor/test/compute_interceptor_run_op_test.cc +++ b/paddle/fluid/distributed/fleet_executor/test/compute_interceptor_run_op_test.cc @@ -26,7 +26,7 @@ limitations under the License. */ #include "paddle/fluid/framework/program_desc.h" USE_OP_ITSELF(elementwise_add); -USE_OP(fill_constant); +USE_OP_ITSELF(fill_constant); namespace paddle { namespace distributed { diff --git a/paddle/fluid/framework/new_executor/standalone_executor_test.cc b/paddle/fluid/framework/new_executor/standalone_executor_test.cc index a0708f28e37ee2088d82f1b73b79f1452dc0f262..c1d449d30205e2e2495a921cadef7f7742a8cedc 100644 --- a/paddle/fluid/framework/new_executor/standalone_executor_test.cc +++ b/paddle/fluid/framework/new_executor/standalone_executor_test.cc @@ -21,7 +21,7 @@ #include "paddle/fluid/framework/new_executor/standalone_executor.h" -USE_OP(fill_constant); +USE_OP_ITSELF(fill_constant); USE_OP(uniform_random); USE_OP(lookup_table); USE_OP(transpose2); diff --git a/paddle/fluid/operators/fill_constant_op.cc b/paddle/fluid/operators/fill_constant_op.cc index 04c2d027cac6a16747935e0254775f3fc50870dd..461679831b204067d1b6cdbf52ac0b6e0f994e71 100644 --- a/paddle/fluid/operators/fill_constant_op.cc +++ b/paddle/fluid/operators/fill_constant_op.cc @@ -178,16 +178,6 @@ REGISTER_OPERATOR( paddle::framework::EmptyGradOpMaker, paddle::framework::EmptyGradOpMaker); -REGISTER_OP_CPU_KERNEL( - fill_constant, ops::FillConstantKernel, - ops::FillConstantKernel, ops::FillConstantKernel, - ops::FillConstantKernel, ops::FillConstantKernel, - ops::FillConstantKernel, ops::FillConstantKernel, - ops::FillConstantKernel, - ops::FillConstantKernel, - ops::FillConstantKernel>, - ops::FillConstantKernel>); - REGISTER_OP_VERSION(fill_constant) .AddCheckpoint( R"ROC( diff --git a/paddle/fluid/operators/fill_constant_op.cu.cc b/paddle/fluid/operators/fill_constant_op.cu.cc deleted file mode 100644 index defd98f9d490042841fb8ee9317853ab9f62de20..0000000000000000000000000000000000000000 --- a/paddle/fluid/operators/fill_constant_op.cu.cc +++ /dev/null @@ -1,25 +0,0 @@ -/* Copyright (c) 2018 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 "paddle/fluid/operators/fill_constant_op.h" - -namespace ops = paddle::operators; -REGISTER_OP_CUDA_KERNEL( - fill_constant, ops::FillConstantKernel, - ops::FillConstantKernel, ops::FillConstantKernel, - ops::FillConstantKernel, ops::FillConstantKernel, - ops::FillConstantKernel, ops::FillConstantKernel, - ops::FillConstantKernel, - ops::FillConstantKernel>, - ops::FillConstantKernel>); diff --git a/paddle/fluid/operators/math/CMakeLists.txt b/paddle/fluid/operators/math/CMakeLists.txt index 52310ab9d48ebf716c43aa5f0a574c6bd5c51dc2..d5336af8f05ef7fce1d5b1a2153cb8928772e232 100644 --- a/paddle/fluid/operators/math/CMakeLists.txt +++ b/paddle/fluid/operators/math/CMakeLists.txt @@ -39,7 +39,7 @@ if (WITH_ASCEND_CL) else() math_library(beam_search DEPS math_function) endif() -math_library(fc DEPS blas) +math_library(fc DEPS blas jit_kernel_helper) math_library(matrix_bit_code) math_library(unpooling) diff --git a/paddle/pten/kernels/full_kernel.h b/paddle/pten/kernels/full_kernel.h index b8b78e311ab90877b9e5801018032f2b493d3545..a697725ade6d9eff285e5c046e71b0dcd164d33c 100644 --- a/paddle/pten/kernels/full_kernel.h +++ b/paddle/pten/kernels/full_kernel.h @@ -17,6 +17,7 @@ #include "paddle/pten/common/scalar.h" #include "paddle/pten/common/scalar_array.h" #include "paddle/pten/core/dense_tensor.h" +#include "paddle/pten/core/selected_rows.h" #include "paddle/pten/infermeta/nullary.h" #include "paddle/pten/kernels/empty_kernel.h" @@ -30,6 +31,13 @@ void FullKernel(const Context& dev_ctx, DataType dtype, DenseTensor* out); +template +void FullSR(const Context& dev_ctx, + const ScalarArray& shape, + const Scalar& val, + DataType dtype, + SelectedRows* out); + template void FullLikeKernel(const Context& dev_ctx, const DenseTensor& x, diff --git a/paddle/pten/kernels/selected_rows/full_kernel.cc b/paddle/pten/kernels/selected_rows/full_kernel.cc new file mode 100644 index 0000000000000000000000000000000000000000..4861c64925c8be3a71a4db6e1f887310c156feed --- /dev/null +++ b/paddle/pten/kernels/selected_rows/full_kernel.cc @@ -0,0 +1,70 @@ +/* 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 "paddle/pten/kernels/full_kernel.h" + +#include "paddle/pten/backends/cpu/cpu_context.h" +#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP) +#include "paddle/pten/backends/gpu/gpu_context.h" +#endif +#include "paddle/pten/core/kernel_registry.h" + +#include "paddle/pten/common/bfloat16.h" +#include "paddle/pten/common/complex.h" + +namespace pten { + +template +void FullSR(const Context& dev_ctx, + const ScalarArray& shape, + const Scalar& val, + DataType dtype, + SelectedRows* out) { + pten::FullKernel(dev_ctx, shape, val, dtype, out->mutable_value()); +} + +} // namespace pten + +PT_REGISTER_KERNEL(full_sr, + CPU, + ALL_LAYOUT, + pten::FullSR, + float, + double, + uint8_t, + int16_t, + int, + int64_t, + bool, + pten::dtype::float16, + pten::dtype::bfloat16, + pten::dtype::complex, + pten::dtype::complex) {} + +#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP) +PT_REGISTER_KERNEL(full_sr, + GPU, + ALL_LAYOUT, + pten::FullSR, + float, + double, + uint8_t, + int16_t, + int, + int64_t, + bool, + pten::dtype::float16, + pten::dtype::complex, + pten::dtype::complex) {} +#endif diff --git a/paddle/pten/ops/compat/fill_constant_sig.cc b/paddle/pten/ops/compat/fill_constant_sig.cc index 242fefe9998fa09751d7354832f8154b2cd79680..fa0b5ddceb31c4444875d8e3e35a94a62216f88d 100644 --- a/paddle/pten/ops/compat/fill_constant_sig.cc +++ b/paddle/pten/ops/compat/fill_constant_sig.cc @@ -66,6 +66,57 @@ KernelSignature FillConstantOpArgumentMapping( } } } + } else if (ctx.IsSelectedRowsOutput("Out")) { + if (ctx.HasInput("ShapeTensor")) { + if (ctx.HasInput("ValueTensor")) { + return KernelSignature( + "full_sr", {}, {"ShapeTensor", "ValueTensor", "dtype"}, {"Out"}); + } else { + const auto& str_value = + paddle::any_cast(ctx.Attr("str_value")); + if (str_value.empty()) { + return KernelSignature( + "full_sr", {}, {"ShapeTensor", "value", "dtype"}, {"Out"}); + } else { + return KernelSignature( + "full_sr", {}, {"ShapeTensor", "str_value", "dtype"}, {"Out"}); + } + } + } else if (ctx.InputSize("ShapeTensorList") > 0) { + if (ctx.HasInput("ValueTensor")) { + return KernelSignature("full_sr", + {}, + {"ShapeTensorList", "ValueTensor", "dtype"}, + {"Out"}); + } else { + const auto& str_value = + paddle::any_cast(ctx.Attr("str_value")); + if (str_value.empty()) { + return KernelSignature( + "full_sr", {}, {"ShapeTensorList", "value", "dtype"}, {"Out"}); + } else { + return KernelSignature("full_sr", + {}, + {"ShapeTensorList", "str_value", "dtype"}, + {"Out"}); + } + } + } else { + if (ctx.HasInput("ValueTensor")) { + return KernelSignature( + "full_sr", {}, {"shape", "ValueTensor", "dtype"}, {"Out"}); + } else { + const auto& str_value = + paddle::any_cast(ctx.Attr("str_value")); + if (str_value.empty()) { + return KernelSignature( + "full_sr", {}, {"shape", "value", "dtype"}, {"Out"}); + } else { + return KernelSignature( + "full_sr", {}, {"shape", "str_value", "dtype"}, {"Out"}); + } + } + } } return KernelSignature("unregistered", {}, {}, {}); } diff --git a/paddle/pten/tests/CMakeLists.txt b/paddle/pten/tests/CMakeLists.txt index 9946821581c3af86c4f645954c0b927429665b1c..ab5da613199be8d021392f9f5db6d8eed38c30b7 100644 --- a/paddle/pten/tests/CMakeLists.txt +++ b/paddle/pten/tests/CMakeLists.txt @@ -2,3 +2,4 @@ add_subdirectory(api) add_subdirectory(common) add_subdirectory(core) add_subdirectory(kernels) +add_subdirectory(ops_signature) diff --git a/paddle/pten/tests/ops_signature/CMakeLists.txt b/paddle/pten/tests/ops_signature/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..58ad3276696212f0aa5c320d2a6482c88115a661 --- /dev/null +++ b/paddle/pten/tests/ops_signature/CMakeLists.txt @@ -0,0 +1 @@ +cc_test(test_op_signature SRCS test_op_signature.cc DEPS op_utils) diff --git a/paddle/pten/tests/ops_signature/test_op_signature.cc b/paddle/pten/tests/ops_signature/test_op_signature.cc new file mode 100644 index 0000000000000000000000000000000000000000..252cef1f0c741ab2542b040e7e866b47c9883b77 --- /dev/null +++ b/paddle/pten/tests/ops_signature/test_op_signature.cc @@ -0,0 +1,118 @@ +/* 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 "paddle/pten/tests/ops_signature/test_op_signature.h" + +#include +#include +#include + +#include "paddle/pten/core/compat/op_utils.h" +#include "paddle/pten/ops/compat/signatures.h" + +namespace pten { +namespace tests { + +// The unittests in this file are just order to pass the CI-Coverageļ¼Œ +// so it isn't necessary to check the all cases. + +TEST(ARG_MAP, fill_constant) { + TestArgumentMappingContext arg_case1( + {"ShapeTensor", "ValueTensor"}, {}, {}, {}, {"Out"}); + auto signature1 = + OpUtilsMap::Instance().GetArgumentMappingFn("fill_constant")(arg_case1); + ASSERT_EQ(signature1.name, "full_sr"); + + TestArgumentMappingContext arg_case2( + {"ShapeTensor"}, + {}, + {{"str_value", paddle::any{std::string{"10"}}}}, + {}, + {"Out"}); + auto signature2 = + OpUtilsMap::Instance().GetArgumentMappingFn("fill_constant")(arg_case2); + ASSERT_EQ(signature2.name, "full_sr"); + + TestArgumentMappingContext arg_case3( + {"ShapeTensor"}, + {}, + {{"value", paddle::any{0}}, {"str_value", paddle::any{std::string{""}}}}, + {}, + {"Out"}); + auto signature3 = + OpUtilsMap::Instance().GetArgumentMappingFn("fill_constant")(arg_case3); + ASSERT_EQ(signature3.name, "full_sr"); + + TestArgumentMappingContext arg_case4( + {"ShapeTensorList", "ValueTensor"}, {}, {}, {}, {"Out"}); + auto signature4 = + OpUtilsMap::Instance().GetArgumentMappingFn("fill_constant")(arg_case4); + ASSERT_EQ(signature4.name, "full_sr"); + + TestArgumentMappingContext arg_case5( + {"ShapeTensorList"}, + {}, + {{"str_value", paddle::any{std::string{"10"}}}}, + {}, + {"Out"}); + auto signature5 = + OpUtilsMap::Instance().GetArgumentMappingFn("fill_constant")(arg_case5); + ASSERT_EQ(signature5.name, "full_sr"); + + TestArgumentMappingContext arg_case6( + {"ShapeTensorList"}, + {}, + {{"value", paddle::any{0}}, {"str_value", paddle::any{std::string{""}}}}, + {}, + {"Out"}); + auto signature6 = + OpUtilsMap::Instance().GetArgumentMappingFn("fill_constant")(arg_case6); + ASSERT_EQ(signature6.name, "full_sr"); + + TestArgumentMappingContext arg_case7( + {"ValueTensor"}, + {}, + {{"shape", paddle::any{std::vector{2, 3}}}}, + {}, + {"Out"}); + auto signature7 = + OpUtilsMap::Instance().GetArgumentMappingFn("fill_constant")(arg_case7); + ASSERT_EQ(signature7.name, "full_sr"); + + TestArgumentMappingContext arg_case8( + {}, + {}, + {{"shape", paddle::any{std::vector{2, 3}}}, + {"value", paddle::any{0}}, + {"str_value", paddle::any{std::string{""}}}}, + {}, + {"Out"}); + auto signature8 = + OpUtilsMap::Instance().GetArgumentMappingFn("fill_constant")(arg_case8); + ASSERT_EQ(signature8.name, "full_sr"); + + TestArgumentMappingContext arg_case9( + {}, + {}, + {{"shape", paddle::any{std::vector{2, 3}}}, + {"str_value", paddle::any{std::string{"10"}}}}, + {}, + {"Out"}); + auto signature9 = + OpUtilsMap::Instance().GetArgumentMappingFn("fill_constant")(arg_case9); + ASSERT_EQ(signature9.name, "full_sr"); +} + +} // namespace tests +} // namespace pten diff --git a/paddle/pten/tests/ops_signature/test_op_signature.h b/paddle/pten/tests/ops_signature/test_op_signature.h new file mode 100644 index 0000000000000000000000000000000000000000..6c849f5d726d22db2518dccebb244c6867518445 --- /dev/null +++ b/paddle/pten/tests/ops_signature/test_op_signature.h @@ -0,0 +1,92 @@ +/* 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. */ + +#pragma once + +#include +#include +#include +#include + +#include "paddle/pten/core/compat/op_utils.h" + +namespace pten { +namespace tests { + +class TestArgumentMappingContext : public pten::ArgumentMappingContext { + public: + TestArgumentMappingContext( + std::unordered_set dense_tensor_ins, + std::unordered_set sr_ins, + std::unordered_map op_attrs, + std::unordered_set dense_tensor_outs, + std::unordered_set sr_outs = {}) + : dense_tensor_inputs(dense_tensor_ins), + selected_rows_inputs(sr_ins), + attrs(op_attrs), + dense_tensor_outputs(dense_tensor_outs), + selected_rows_outputs(sr_outs) {} + + bool HasInput(const std::string& name) const override { + return dense_tensor_inputs.count(name) > 0 || + selected_rows_inputs.count(name) > 0; + } + + bool HasOutput(const std::string& name) const override { + return dense_tensor_outputs.count(name) > 0 || + selected_rows_outputs.count(name) > 0; + } + + bool HasAttr(const std::string& name) const override { + return attrs.count(name) > 0; + } + + paddle::any Attr(const std::string& name) const override { + return attrs.at(name); + } + + size_t InputSize(const std::string& name) const override { + return dense_tensor_inputs.size() + selected_rows_inputs.size(); + } + + size_t OutputSize(const std::string& name) const override { + return dense_tensor_outputs.size() + selected_rows_outputs.size(); + } + + bool IsDenseTensorInput(const std::string& name) const override { + return dense_tensor_inputs.count(name) > 0; + } + + bool IsSelectedRowsInput(const std::string& name) const override { + return selected_rows_inputs.count(name) > 0; + } + + bool IsDenseTensorOutput(const std::string& name) const override { + return dense_tensor_outputs.count(name) > 0; + } + + bool IsSelectedRowsOutput(const std::string& name) const override { + return selected_rows_outputs.count(name) > 0; + } + + private: + const std::unordered_set dense_tensor_inputs; + const std::unordered_set selected_rows_inputs; + const std::unordered_map attrs; + const std::unordered_set dense_tensor_outputs; + const std::unordered_set selected_rows_outputs; +}; + +} // namespace tests +} // namespace pten