diff --git a/paddle/fluid/eager/api/utils/tensor_utils.cc b/paddle/fluid/eager/api/utils/tensor_utils.cc index bd4e9f0af941e33321099f70babaf1888daf477a..801b608b7cde76ed5d83cc194da15922c373ee02 100644 --- a/paddle/fluid/eager/api/utils/tensor_utils.cc +++ b/paddle/fluid/eager/api/utils/tensor_utils.cc @@ -43,7 +43,7 @@ paddle::experimental::Tensor CreateTensorWithValue( bool is_leaf) { paddle::experimental::Tensor out = paddle::experimental::full( paddle::framework::vectorize(ddim), paddle::experimental::Scalar(value), - dtype, pten::TransToPtenBackend(place), layout); + dtype, pten::TransToPtenBackend(place)); auto meta = EagerUtils::autograd_meta(&out); if (is_leaf) { diff --git a/paddle/fluid/operators/fill_any_like_op.h b/paddle/fluid/operators/fill_any_like_op.h index 3ebda1a074a206f4a968e6171acf767275ca4a01..1e5b43c81d3d493f99b8893093a388d5cc537f75 100644 --- a/paddle/fluid/operators/fill_any_like_op.h +++ b/paddle/fluid/operators/fill_any_like_op.h @@ -33,6 +33,7 @@ class FillAnyLikeKernel : public framework::OpKernel { float, T>::type>::type; void Compute(const framework::ExecutionContext& context) const override { + auto* x = context.Input("X"); auto* out = context.Output("Out"); out->mutable_data(context.GetPlace()); @@ -65,7 +66,7 @@ class FillAnyLikeKernel : public framework::OpKernel { pten::FullLikeKernel( static_cast::TYPE&>(dev_ctx), - value, out); + *x, value, pten::DataType::UNDEFINED, out); } }; diff --git a/paddle/fluid/operators/fill_any_like_op_xpu.cc b/paddle/fluid/operators/fill_any_like_op_xpu.cc index b4788d0445d8d67972829c1bc2e5bd652f49b6d0..fba5d8ece3e1e0724e0b62bfd3d81e5f53992f09 100644 --- a/paddle/fluid/operators/fill_any_like_op_xpu.cc +++ b/paddle/fluid/operators/fill_any_like_op_xpu.cc @@ -31,6 +31,7 @@ class FillAnyLikeXPUKernel : public framework::OpKernel { using XPUInTDType = typename XPUTypeTrait::Type; void Compute(const framework::ExecutionContext& context) const override { + auto* x = context.Input("X"); auto* out = context.Output("Out"); out->mutable_data(context.GetPlace()); @@ -63,7 +64,7 @@ class FillAnyLikeXPUKernel : public framework::OpKernel { pten::FullLikeKernel( static_cast::TYPE&>(dev_ctx), - value, out); + *x, value, pten::DataType::UNDEFINED, out); } }; diff --git a/paddle/pten/infermeta/nullary.cc b/paddle/pten/infermeta/nullary.cc index 6823c6252eaddc01db0cb9c6e53e51b88047b18e..e924914d8f88802876950f60ec552eacd9680074 100644 --- a/paddle/pten/infermeta/nullary.cc +++ b/paddle/pten/infermeta/nullary.cc @@ -28,9 +28,8 @@ void CreateInferMetaBase(const std::vector& shape, void CreateInferMeta(const ScalarArray& shape, DataType dtype, - DataLayout layout, MetaTensor* out) { - CreateInferMetaBase(shape.GetData(), dtype, layout, out); + CreateInferMetaBase(shape.GetData(), dtype, DataLayout::NCHW, out); } } // namespace pten diff --git a/paddle/pten/infermeta/nullary.h b/paddle/pten/infermeta/nullary.h index 965e240e903b67adb34b6b5b8c1249a878de2be3..a7530abcf11299e7cad9be6e90ebe1f4a3d7fd4f 100644 --- a/paddle/pten/infermeta/nullary.h +++ b/paddle/pten/infermeta/nullary.h @@ -33,9 +33,6 @@ void CreateInferMetaBase(const std::vector& shape, DataLayout layout, MetaTensor* out); -void CreateInferMeta(const ScalarArray& shape, - DataType dtype, - DataLayout layout, - MetaTensor* out); +void CreateInferMeta(const ScalarArray& shape, DataType dtype, MetaTensor* out); } // namespace pten diff --git a/paddle/pten/infermeta/unary.cc b/paddle/pten/infermeta/unary.cc index ec9ba519b95ba740f7370a8547b092ed4c9acb4f..cd35d4fef708bccd99a369fbee31324dd47d2861 100644 --- a/paddle/pten/infermeta/unary.cc +++ b/paddle/pten/infermeta/unary.cc @@ -79,13 +79,10 @@ void CastInferMeta(const MetaTensor& x, DataType out_dtype, MetaTensor* out) { out->set_layout(x.layout()); } -void CreateLikeInferMeta(const MetaTensor& x, - DataType dtype, - DataLayout layout, - MetaTensor* out) { +void CreateLikeInferMeta(const MetaTensor& x, DataType dtype, MetaTensor* out) { out->set_dims(x.dims()); out->set_dtype(dtype == DataType::UNDEFINED ? x.dtype() : dtype); - out->set_layout(layout == DataLayout::UNDEFINED ? x.layout() : layout); + out->set_layout(x.layout()); } static pten::framework::DDim ValidateShape( diff --git a/paddle/pten/infermeta/unary.h b/paddle/pten/infermeta/unary.h index 5bdf1d491c6342e302bb1f0ea39f18c65749b8d6..2bc4b53f8facb796afceb39fdbd173b12dc26102 100644 --- a/paddle/pten/infermeta/unary.h +++ b/paddle/pten/infermeta/unary.h @@ -41,10 +41,7 @@ void FlattenInferMeta(const MetaTensor& x, void CastInferMeta(const MetaTensor& x, DataType out_dtype, MetaTensor* out); -void CreateLikeInferMeta(const MetaTensor& x, - DataType dtype, - DataLayout layout, - MetaTensor* out); +void CreateLikeInferMeta(const MetaTensor& x, DataType dtype, MetaTensor* out); void InferMetaFromVecValue(const MetaTensor& x, const std::vector& shape, diff --git a/paddle/pten/kernels/cpu/full_kernel.cc b/paddle/pten/kernels/cpu/full_kernel.cc index 62e1bbf1d9d9c3cb219f2c96338a53ab20b14082..49a613f868c6a0b460e57bfbf355bac7884433b4 100644 --- a/paddle/pten/kernels/cpu/full_kernel.cc +++ b/paddle/pten/kernels/cpu/full_kernel.cc @@ -16,7 +16,62 @@ limitations under the License. */ #include "paddle/pten/backends/cpu/cpu_context.h" #include "paddle/pten/core/kernel_registry.h" -#include "paddle/pten/kernels/impl/full_kernel_impl.h" + +#include "paddle/pten/kernels/funcs/eigen/common.h" +#include "paddle/pten/kernels/funcs/eigen/eigen_function.h" + +namespace pten { + +template +void FullValue(const Context& dev_ctx, DenseTensor* tensor, VType val) { + dev_ctx.template Alloc(tensor); + auto t = pten::EigenVector::Flatten(*tensor); + t.device(*dev_ctx.eigen_device()) = t.constant(static_cast(val)); +} + +template +void FullKernel(const Context& dev_ctx, + const ScalarArray& shape, + const Scalar& val, + DataType dtype, + DenseTensor* out) { + out->ResizeAndAllocate(pten::framework::make_ddim(shape.GetData())); + FullValue(dev_ctx, out, val.to()); +} + +template +void FullLikeKernel(const Context& dev_ctx, + const DenseTensor& x, + const Scalar& val, + DataType dtype, + DenseTensor* out) { + auto value = val.to(); + using CommonType = typename std::common_type< + float, + typename std::conditional::value, + float, + T>::type>::type; + + auto common_type_value = static_cast(value); + + PADDLE_ENFORCE_EQ( + (common_type_value >= + static_cast(std::numeric_limits::lowest())) && + (common_type_value <= + static_cast(std::numeric_limits::max())), + true, + pten::errors::InvalidArgument( + "The filled value is out of range for target type, " + "current kernel type is %s, the range should between %f " + "and %f, but now value is %f.", + typeid(T).name(), + static_cast(std::numeric_limits::lowest()), + static_cast(std::numeric_limits::max()), + static_cast(value))); + FullValue(dev_ctx, out, value); +} + +} // namespace pten PT_REGISTER_KERNEL(full, CPU, diff --git a/paddle/pten/kernels/empty_kernel.cc b/paddle/pten/kernels/empty_kernel.cc index 03fe240a88b13a3bb38fab72ea6c317aaa7a5ccc..0f49a4380c9b3be345aa8bf6355934aa0a5db9c8 100644 --- a/paddle/pten/kernels/empty_kernel.cc +++ b/paddle/pten/kernels/empty_kernel.cc @@ -23,12 +23,16 @@ namespace pten { template void EmptyKernel(const Context& dev_ctx, const ScalarArray& shape, + DataType dtype, DenseTensor* out) { out->ResizeAndAllocate(pten::framework::make_ddim(shape.GetData())); } template -void EmptyLikeKernel(const Context& dev_ctx, DenseTensor* out) { +void EmptyLikeKernel(const Context& dev_ctx, + const DenseTensor& x, + DataType dtype, + DenseTensor* out) { dev_ctx.template Alloc(out); } diff --git a/paddle/pten/kernels/empty_kernel.h b/paddle/pten/kernels/empty_kernel.h index 98f7e03d771bee306030a554633f9a2355747154..a7ba51242463a6a05fe8af638dedaf67822be95d 100644 --- a/paddle/pten/kernels/empty_kernel.h +++ b/paddle/pten/kernels/empty_kernel.h @@ -25,10 +25,14 @@ namespace pten { template void EmptyKernel(const Context& dev_ctx, const ScalarArray& shape, + DataType dtype, DenseTensor* out); template -void EmptyLikeKernel(const Context& dev_ctx, DenseTensor* out); +void EmptyLikeKernel(const Context& dev_ctx, + const DenseTensor& x, + DataType dtype, + DenseTensor* out); // TODO(chenweihang): the tensor creation method need to be replaced later, // all kernel api call Empty here instead of making tensor self @@ -52,27 +56,22 @@ DenseTensor Empty(const Context& dev_ctx) { template DenseTensor Empty(const Context& dev_ctx, const ScalarArray& shape, - DataType dtype = DataType::FLOAT32, - Backend backend = Backend::CPU, // Is backend needed here? - DataLayout layout = DataLayout::NCHW) { + DataType dtype = DataType::FLOAT32) { auto dense_out = Empty(dev_ctx); MetaTensor meta_out(&dense_out); - CreateInferMeta(shape, dtype, layout, &meta_out); - EmptyKernel(dev_ctx, shape, &dense_out); + CreateInferMeta(shape, dtype, &meta_out); + EmptyKernel(dev_ctx, shape, dtype, &dense_out); return dense_out; } template -DenseTensor EmptyLike( - const Context& dev_ctx, - const DenseTensor& x, - DataType dtype = DataType::UNDEFINED, - Backend backend = Backend::UNDEFINED, // Is backend needed here? - DataLayout layout = DataLayout::UNDEFINED) { +DenseTensor EmptyLike(const Context& dev_ctx, + const DenseTensor& x, + DataType dtype = DataType::UNDEFINED) { auto dense_out = Empty(dev_ctx); MetaTensor meta_out(&dense_out); - CreateLikeInferMeta(x, dtype, layout, &meta_out); - EmptyLikeKernel(dev_ctx, &dense_out); + CreateLikeInferMeta(x, dtype, &meta_out); + EmptyLikeKernel(dev_ctx, x, dtype, &dense_out); return dense_out; } diff --git a/paddle/pten/kernels/full_kernel.h b/paddle/pten/kernels/full_kernel.h index 030eb4b1c760f688a1c2ee11cff3664e0ef03137..b8b78e311ab90877b9e5801018032f2b493d3545 100644 --- a/paddle/pten/kernels/full_kernel.h +++ b/paddle/pten/kernels/full_kernel.h @@ -27,39 +27,37 @@ template void FullKernel(const Context& dev_ctx, const ScalarArray& shape, const Scalar& val, + DataType dtype, DenseTensor* out); template void FullLikeKernel(const Context& dev_ctx, + const DenseTensor& x, const Scalar& val, + DataType dtype, DenseTensor* out); template DenseTensor Full(const Context& dev_ctx, const ScalarArray& shape, const Scalar& val, - DataType dtype = DataType::FLOAT32, - Backend backend = Backend::CPU, // Is backend needed here? - DataLayout layout = DataLayout::NCHW) { + DataType dtype = DataType::FLOAT32) { auto dense_out = Empty(dev_ctx); MetaTensor meta_out(&dense_out); - CreateInferMeta(shape, dtype, layout, &meta_out); - FullKernel(dev_ctx, shape, val, &dense_out); + CreateInferMeta(shape, dtype, &meta_out); + FullKernel(dev_ctx, shape, val, dtype, &dense_out); return dense_out; } template -DenseTensor FullLike( - const Context& dev_ctx, - const DenseTensor& x, - const Scalar& val, - DataType dtype = DataType::UNDEFINED, - Backend backend = Backend::UNDEFINED, // Is backend needed here? - DataLayout layout = DataLayout::UNDEFINED) { +DenseTensor FullLike(const Context& dev_ctx, + const DenseTensor& x, + const Scalar& val, + DataType dtype = DataType::UNDEFINED) { auto dense_out = Empty(dev_ctx); MetaTensor meta_out(&dense_out); - CreateLikeInferMeta(x, dtype, layout, &meta_out); - FullLikeKernel(dev_ctx, val, &dense_out); + CreateLikeInferMeta(x, dtype, &meta_out); + FullLikeKernel(dev_ctx, x, val, dtype, &dense_out); return dense_out; } diff --git a/paddle/pten/kernels/gpu/full_kernel.cu b/paddle/pten/kernels/gpu/full_kernel.cu index 7f600fb3134721dac12829390e95f08a3c4533ea..4ae50625e2f9a4ca5e70df7638d675fd4f349c35 100644 --- a/paddle/pten/kernels/gpu/full_kernel.cu +++ b/paddle/pten/kernels/gpu/full_kernel.cu @@ -33,10 +33,11 @@ struct FullFuctor { } }; -template -void FullKernel(const ContextT& dev_ctx, +template +void FullKernel(const Context& dev_ctx, const ScalarArray& shape, const Scalar& val, + DataType dtype, DenseTensor* out) { out->Resize(paddle::framework::make_ddim(shape.GetData())); int numel = out->numel(); @@ -53,9 +54,11 @@ void FullKernel(const ContextT& dev_ctx, } } -template -void FullLikeKernel(const ContextT& dev_ctx, +template +void FullLikeKernel(const Context& dev_ctx, + const DenseTensor& x, const Scalar& val, + DataType dtype, DenseTensor* out) { auto value = val.to(); using CommonType = typename std::common_type< diff --git a/paddle/pten/kernels/xpu/full_kernel.cc b/paddle/pten/kernels/xpu/full_kernel.cc index cf6befac02367bd0f3419202f01c34571663d73d..bd406fdb3ecceb4cafa7bb146522256a8818e3e0 100644 --- a/paddle/pten/kernels/xpu/full_kernel.cc +++ b/paddle/pten/kernels/xpu/full_kernel.cc @@ -57,6 +57,7 @@ template void FullKernel(const Context& dev_ctx, const ScalarArray& shape, const Scalar& val, + DataType dtype, DenseTensor* out) { out->ResizeAndAllocate(pten::framework::make_ddim(shape.GetData())); FullValueXPU(dev_ctx, out, val.to()); @@ -64,7 +65,9 @@ void FullKernel(const Context& dev_ctx, template void FullLikeKernel(const Context& dev_ctx, + const DenseTensor& x, const Scalar& val, + DataType dtype, DenseTensor* out) { auto value = val.to(); using XPUInTDType = typename XPUTypeTrait::Type; diff --git a/paddle/pten/ops/compat/empty_sig.cc b/paddle/pten/ops/compat/empty_sig.cc index c74f6106981a011c263bc38118b74fedf1436b21..35aa17dcf9d34e769a00e1d067dd9750e609984f 100644 --- a/paddle/pten/ops/compat/empty_sig.cc +++ b/paddle/pten/ops/compat/empty_sig.cc @@ -18,11 +18,11 @@ namespace pten { KernelSignature EmptyOpArgumentMapping(const ArgumentMappingContext& ctx) { if (ctx.HasInput("ShapeTensor")) { - return KernelSignature("empty", {}, {"ShapeTensor"}, {"Out"}); + return KernelSignature("empty", {}, {"ShapeTensor", "dtype"}, {"Out"}); } else if (ctx.InputSize("ShapeTensorList") > 0) { - return KernelSignature("empty", {}, {"ShapeTensorList"}, {"Out"}); + return KernelSignature("empty", {}, {"ShapeTensorList", "dtype"}, {"Out"}); } else { - return KernelSignature("empty", {}, {"shape"}, {"Out"}); + return KernelSignature("empty", {}, {"shape", "dtype"}, {"Out"}); } } diff --git a/paddle/pten/ops/compat/fill_any_like_sig.cc b/paddle/pten/ops/compat/fill_any_like_sig.cc index 81065d0c8aebd54a9b09b55ad68b900ae075485a..0440d3769f643240aeeaaf645c2223d5f0bb2139 100644 --- a/paddle/pten/ops/compat/fill_any_like_sig.cc +++ b/paddle/pten/ops/compat/fill_any_like_sig.cc @@ -18,7 +18,7 @@ namespace pten { KernelSignature FillAnyLikeOpArgumentMapping( const ArgumentMappingContext& ctx) { - return KernelSignature("full_like", {}, {"value"}, {"Out"}); + return KernelSignature("full_like", {"X"}, {"value", "dtype"}, {"Out"}); } } // namespace pten diff --git a/paddle/pten/ops/compat/fill_constant_sig.cc b/paddle/pten/ops/compat/fill_constant_sig.cc index 73dee270f7072ef4860da1a404c260aaa35a787b..242fefe9998fa09751d7354832f8154b2cd79680 100644 --- a/paddle/pten/ops/compat/fill_constant_sig.cc +++ b/paddle/pten/ops/compat/fill_constant_sig.cc @@ -23,42 +23,46 @@ KernelSignature FillConstantOpArgumentMapping( if (ctx.HasInput("ShapeTensor")) { if (ctx.HasInput("ValueTensor")) { return KernelSignature( - "full", {}, {"ShapeTensor", "ValueTensor"}, {"Out"}); + "full", {}, {"ShapeTensor", "ValueTensor", "dtype"}, {"Out"}); } else { const auto& str_value = paddle::any_cast(ctx.Attr("str_value")); if (str_value.empty()) { - return KernelSignature("full", {}, {"ShapeTensor", "value"}, {"Out"}); + return KernelSignature( + "full", {}, {"ShapeTensor", "value", "dtype"}, {"Out"}); } else { return KernelSignature( - "full", {}, {"ShapeTensor", "str_value"}, {"Out"}); + "full", {}, {"ShapeTensor", "str_value", "dtype"}, {"Out"}); } } } else if (ctx.InputSize("ShapeTensorList") > 0) { if (ctx.HasInput("ValueTensor")) { return KernelSignature( - "full", {}, {"ShapeTensorList", "ValueTensor"}, {"Out"}); + "full", {}, {"ShapeTensorList", "ValueTensor", "dtype"}, {"Out"}); } else { const auto& str_value = paddle::any_cast(ctx.Attr("str_value")); if (str_value.empty()) { return KernelSignature( - "full", {}, {"ShapeTensorList", "value"}, {"Out"}); + "full", {}, {"ShapeTensorList", "value", "dtype"}, {"Out"}); } else { return KernelSignature( - "full", {}, {"ShapeTensorList", "str_value"}, {"Out"}); + "full", {}, {"ShapeTensorList", "str_value", "dtype"}, {"Out"}); } } } else { if (ctx.HasInput("ValueTensor")) { - return KernelSignature("full", {}, {"shape", "ValueTensor"}, {"Out"}); + return KernelSignature( + "full", {}, {"shape", "ValueTensor", "dtype"}, {"Out"}); } else { const auto& str_value = paddle::any_cast(ctx.Attr("str_value")); if (str_value.empty()) { - return KernelSignature("full", {}, {"shape", "value"}, {"Out"}); + return KernelSignature( + "full", {}, {"shape", "value", "dtype"}, {"Out"}); } else { - return KernelSignature("full", {}, {"shape", "str_value"}, {"Out"}); + return KernelSignature( + "full", {}, {"shape", "str_value", "dtype"}, {"Out"}); } } } diff --git a/python/paddle/utils/code_gen/api.yaml b/python/paddle/utils/code_gen/api.yaml index 60e64c028430c109d6668e98109462a8be467034..390ccdd157363260435b97993b3181a8532a2d15 100644 --- a/python/paddle/utils/code_gen/api.yaml +++ b/python/paddle/utils/code_gen/api.yaml @@ -51,30 +51,28 @@ func : dot - api : empty - args : (ScalarArray shape, DataType dtype=DataType::FLOAT32, Backend place=Backend::CPU, DataLayout layout=DataLayout::NCHW) + args : (ScalarArray shape, DataType dtype=DataType::FLOAT32, Backend place=Backend::CPU) output: Tensor infer_meta : func : CreateInferMeta - param : [shape, dtype, layout] + param : [shape, dtype] kernel : func : empty - param : [shape] + param : [shape, dtype] data_type : dtype backend : place - layout : layout - api : empty_like - args : (Tensor x, DataType dtype = DataType::UNDEFINED, Backend place = Backend::UNDEFINED, DataLayout layout = DataLayout::UNDEFINED) + args : (Tensor x, DataType dtype = DataType::UNDEFINED, Backend place = Backend::UNDEFINED) output: Tensor infer_meta : func : CreateLikeInferMeta - param : [x, dtype, layout] + param : [x, dtype] kernel : func : empty_like - param : [] + param : [x, dtype] data_type : dtype > x backend : place > x - layout : layout > x - api : flatten args : (Tensor x, int start_axis, int stop_axis) @@ -85,30 +83,28 @@ func : flatten - api : full - args : (ScalarArray shape, Scalar value, DataType dtype=DataType::FLOAT32, Backend place=Backend::CPU, DataLayout layout=DataLayout::NCHW) + args : (ScalarArray shape, Scalar value, DataType dtype=DataType::FLOAT32, Backend place=Backend::CPU) output: Tensor infer_meta : func : CreateInferMeta - param : [shape, dtype, layout] + param : [shape, dtype] kernel : func : full - param : [shape, value] + param : [shape, value, dtype] data_type : dtype backend : place - layout : layout - api : full_like - args : (Tensor x, Scalar value, DataType dtype = DataType::UNDEFINED, Backend place = Backend::UNDEFINED, DataLayout layout = DataLayout::UNDEFINED) + args : (Tensor x, Scalar value, DataType dtype = DataType::UNDEFINED, Backend place = Backend::UNDEFINED) output: Tensor infer_meta : func : CreateLikeInferMeta - param : [x, dtype, layout] + param : [x, dtype] kernel : func : full_like - param : [value] + param : [x, value, dtype] data_type : dtype > x backend : place > x - layout : layout > x - api : matmul args : (Tensor x, Tensor y, bool transpose_x = false, bool transpose_y = false) @@ -136,9 +132,9 @@ func : multiply - api : ones_like - args : (Tensor x, DataType dtype=DataType::UNDEFINED, Backend place=Backend::UNDEFINED, DataLayout layout=DataLayout::UNDEFINED) + args : (Tensor x, DataType dtype=DataType::UNDEFINED, Backend place=Backend::UNDEFINED) output : Tensor - invoke : full_like(x, 1, dtype, place, layout) + invoke : full_like(x, 1, dtype, place) - api : reshape args : (Tensor x, ScalarArray shape) @@ -185,6 +181,6 @@ data_type : x - api : zeros_like - args : (Tensor x, DataType dtype=DataType::UNDEFINED, Backend place=Backend::UNDEFINED, DataLayout layout=DataLayout::UNDEFINED) + args : (Tensor x, DataType dtype=DataType::UNDEFINED, Backend place=Backend::UNDEFINED) output : Tensor - invoke : full_like(x, 0, dtype, place, layout) + invoke : full_like(x, 0, dtype, place) diff --git a/python/paddle/utils/code_gen/api_base.py b/python/paddle/utils/code_gen/api_base.py index 26abfdc031270ce29b0f9424c3394547a7c0d01c..7667a836e564e8ffad26bff4d29718599abc1d4d 100644 --- a/python/paddle/utils/code_gen/api_base.py +++ b/python/paddle/utils/code_gen/api_base.py @@ -358,8 +358,8 @@ PADDLE_API {self.outputs['return_type']} {self.get_api_func_name() + '_'}({self. """ if len(input_names) == 0: - assert attr_backend_count > 0 and attr_layout_count > 0 and attr_data_type_count > 0, \ - f"{api} api: When there is no input tensor, the args must have 'Backend', 'DataLayout' and 'DataType'." + assert attr_backend_count > 0 and attr_data_type_count > 0, \ + f"{api} api: When there is no input tensor, the args must have 'Backend' and 'DataType'." kernel_select_args = "" for input_name in input_names: diff --git a/python/paddle/utils/code_gen/wrapped_infermeta_gen.py b/python/paddle/utils/code_gen/wrapped_infermeta_gen.py index 6972b9af25a96dc02f91d6fd3c213fe8568bb298..f5cf8705735f3cca520e8ad53161cb4167ef8390 100644 --- a/python/paddle/utils/code_gen/wrapped_infermeta_gen.py +++ b/python/paddle/utils/code_gen/wrapped_infermeta_gen.py @@ -29,30 +29,36 @@ def gene_wrapped_infermeta_and_register(api): PT_REGISTER_INFER_META_FN({api.kernel['func'][0]}, pten::{api.infer_meta['func']});""" if api.infer_meta['param'] is not None: + kernel_params = api.kernel['param'] + if kernel_params is None: + kernel_params = api.inputs['names'] + api.attrs['names'] + if kernel_params == api.infer_meta['param']: + return '', '', register_code + + assert len(api.infer_meta['param']) <= len(kernel_params), \ + f"{api.api} api: Parameters error. The params of infer_meta should be a subset of kernel params." + tensor_type_map = { 'const Tensor&': 'const MetaTensor&', 'const std::vector&': 'const std::vector&', 'Tensor': 'MetaTensor*', 'std::vector': 'std::vector*', } + wrapped_infermeta_name = get_wrapped_infermeta_name(api.api) args = [] - check_args = [] for input_name in api.inputs['names']: - args.append(tensor_type_map[api.inputs['input_info'][ - input_name]] + ' ' + input_name) - check_args.append(input_name) + if input_name in kernel_params: + args.append(tensor_type_map[api.inputs['input_info'][ + input_name]] + ' ' + input_name) for attr_name in api.attrs['names']: - args.append(api.attrs['attr_info'][attr_name][0] + ' ' + - attr_name) - check_args.append(attr_name) + if attr_name in kernel_params: + args.append(api.attrs['attr_info'][attr_name][0] + ' ' + + attr_name) for i, out_type in enumerate(api.outputs['types']): args.append(tensor_type_map[out_type] + ' ' + api.outputs[ 'names'][i]) - if check_args == api.infer_meta['param']: - return '', '', register_code - invoke_param = api.infer_meta['param'] invoke_param.extend(api.outputs['names'])