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

[PIR] change FullWithTensor input shape to mutable attribute #63332

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
Original file line number Diff line number Diff line change
Expand Up @@ -761,8 +761,8 @@ class FullWithTensorOpPattern

bool MatchAndRewrite(paddle::dialect::FullWithTensorOp op,
pir::PatternRewriter &rewriter) const override {
auto shape = op->operand_source(0);
auto value = op->operand_source(1);
auto value = op->operand_source(0);
auto shape = op->operand_source(1);

if (paddle::dialect::TransToPhiDataType(
value.type()
Expand Down
25 changes: 13 additions & 12 deletions paddle/fluid/ir_adaptor/translator/op_translator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2023,6 +2023,19 @@ struct FillConstant2FullWithTensorTranscriber : public OpTranscriber {
const OpInputInfoList& input_infos,
pir::Block* block) override {
std::vector<pir::Value> op_inputs;
if (op_desc.HasInput("ValueTensor", true) &&
op_desc.Input("ValueTensor", true).size() > 0) {
auto value_tensor_vars = op_desc.Input("ValueTensor", true);
auto defining_info = (*param_map)[value_tensor_vars[0]];
op_inputs.push_back(defining_info.value);
} else {
float value = PADDLE_GET_CONST(float, op_desc.GetAttr("value"));
pir::Attribute new_attr = pir::FloatAttribute::get(ctx, value);
auto defining_op =
InsertFullOperationForAttributeInput(ctx, block, new_attr);
op_inputs.push_back(defining_op->result(0));
}

if (op_desc.HasInput("ShapeTensor", true) &&
op_desc.Input("ShapeTensor", true).size() > 0) {
auto shape_tensor_vars = op_desc.Input("ShapeTensor", true);
Expand All @@ -2044,18 +2057,6 @@ struct FillConstant2FullWithTensorTranscriber : public OpTranscriber {
op_inputs.push_back(defining_op->result(0));
}

if (op_desc.HasInput("ValueTensor", true) &&
op_desc.Input("ValueTensor", true).size() > 0) {
auto value_tensor_vars = op_desc.Input("ValueTensor", true);
auto defining_info = (*param_map)[value_tensor_vars[0]];
op_inputs.push_back(defining_info.value);
} else {
float value = PADDLE_GET_CONST(float, op_desc.GetAttr("value"));
pir::Attribute new_attr = pir::FloatAttribute::get(ctx, value);
auto defining_op =
InsertFullOperationForAttributeInput(ctx, block, new_attr);
op_inputs.push_back(defining_op->result(0));
}
return op_inputs;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ bool ConcatOpInferSymbolicShape(

bool FullWithTensorOpInferSymbolicShape(
pir::Operation *op, pir::ShapeConstraintIRAnalysis *shape_analysis) {
pir::Value operand_source = op->operand_source(0);
pir::Value operand_source = op->operand_source(1);
const symbol::ShapeOrDataDimExprs &operand_shape_or_data =
shape_analysis->GetShapeOrDataForValue(operand_source);

Expand Down
2 changes: 1 addition & 1 deletion paddle/fluid/pir/dialect/operator/ir/ops.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -766,7 +766,7 @@
skip_transform : x

- op : full_with_tensor
args : (Tensor shape, Tensor value, DataType dtype=DataType::FLOAT32)
args : (Tensor value, IntArray shape, DataType dtype=DataType::FLOAT32)
output: Tensor(out)
infer_meta :
func : FullWithTensorInferMeta
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ Tensor full_with_tensor<LazyTensor>(const Tensor& shape,
std::static_pointer_cast<LazyTensor>(shape.impl())->value();
pir::Value value_res = paddle::dialect::full(
std::vector<int64_t>{}, value.to<float>(), dtype, place);
auto op_res = paddle::dialect::full_with_tensor(shape_res, value_res, dtype);
auto op_res = paddle::dialect::full_with_tensor(value_res, shape_res, dtype);
Tensor out(std::make_shared<LazyTensor>(op_res));
return out;
}
Expand Down
2 changes: 1 addition & 1 deletion paddle/fluid/pybind/manual_static_op_function.h
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ PyObject *static_api_full(PyObject *self, PyObject *args, PyObject *kwargs) {
CallStackRecorder callstack_recoder("full_with_tensor");
callstack_recoder.Record();
auto static_api_out =
paddle::dialect::full_with_tensor(shape, value, dtype);
paddle::dialect::full_with_tensor(value, shape, dtype);
callstack_recoder.AttachToOps();

return ToPyObject(static_api_out);
Expand Down
2 changes: 1 addition & 1 deletion paddle/phi/api/yaml/legacy_ops.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -551,7 +551,7 @@
skip_transform : x

- op : full_with_tensor
args : (Tensor shape, Tensor value, DataType dtype=DataType::FLOAT32)
args : (Tensor value, IntArray shape, DataType dtype=DataType::FLOAT32)
output: Tensor(out)
infer_meta :
func : FullWithTensorInferMeta
Expand Down
6 changes: 6 additions & 0 deletions paddle/phi/api/yaml/op_compat.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1279,6 +1279,12 @@
data_type : float
support_tensor : true

- op : full_with_tensor
int_array:
shape :
data_type : int64_t
support_tensor : true

- op : fused_adam_(fused_adam)
inputs :
{params : Params, grads : Grads, learning_rate : LearningRate, moments1 : Moments1,
Expand Down
4 changes: 2 additions & 2 deletions paddle/phi/infermeta/multiary.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4921,10 +4921,10 @@ void MaskedMultiheadAttentionInferMeta(const MetaTensor& x,
}
}

void FullWithTensorInferMeta(const MetaTensor& shape,
void FullWithTensorInferMeta(const IntArray& shape,
DataType dtype,
MetaTensor* out) {
out->set_dims(common::make_ddim(std::vector<int64_t>(shape.numel(), -1)));
out->set_dims(common::make_ddim(shape.GetData()));
out->set_dtype(dtype);
}

Expand Down
2 changes: 1 addition & 1 deletion paddle/phi/infermeta/multiary.h
Original file line number Diff line number Diff line change
Expand Up @@ -952,7 +952,7 @@ void MaskedMultiheadAttentionInferMeta(const MetaTensor& x,
MetaTensor* cache_kv_out,
MetaTensor* beam_cache_offset_out);

void FullWithTensorInferMeta(const MetaTensor& shape,
void FullWithTensorInferMeta(const IntArray& shape,
DataType dtype,
MetaTensor* out);

Expand Down
2 changes: 1 addition & 1 deletion paddle/phi/kernels/cpu/full_kernel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ limitations under the License. */
#include "paddle/phi/core/kernel_registry.h"
#include "paddle/phi/kernels/funcs/eigen/common.h"
#include "paddle/phi/kernels/funcs/eigen/eigen_function.h"
#include "paddle/phi/kernels/impl/full_whit_tensor_kernel_impl.h"
#include "paddle/phi/kernels/impl/full_with_tensor_kernel_impl.h"

namespace phi {

Expand Down
2 changes: 1 addition & 1 deletion paddle/phi/kernels/full_kernel.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ void FullKernel(const Context& dev_ctx,

template <typename T, typename Context>
void FullWithTensorKernel(const Context& dev_ctx,
const DenseTensor& shape,
const DenseTensor& value,
const IntArray& shape,
DataType dtype,
DenseTensor* out);

Expand Down
2 changes: 1 addition & 1 deletion paddle/phi/kernels/gpu/full_kernel.cu
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ limitations under the License. */
#include "paddle/phi/backends/gpu/gpu_context.h"
#include "paddle/phi/core/kernel_registry.h"
#include "paddle/phi/kernels/funcs/elementwise_base.h"
#include "paddle/phi/kernels/impl/full_whit_tensor_kernel_impl.h"
#include "paddle/phi/kernels/impl/full_with_tensor_kernel_impl.h"

namespace phi {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,11 @@ namespace phi {

template <typename T, typename Context>
void FullWithTensorKernel(const Context& dev_ctx,
const DenseTensor& shape,
const DenseTensor& value,
const IntArray& shape,
DataType dtype,
DenseTensor* out) {
auto shape_tmp = IntArray(shape);
out->Resize(common::make_ddim(shape_tmp.GetData()));
FullKernel<T, Context>(dev_ctx, shape_tmp, Scalar(value), dtype, out);
out->Resize(common::make_ddim(shape.GetData()));
FullKernel<T, Context>(dev_ctx, shape, Scalar(value), dtype, out);
}
} // namespace phi
4 changes: 2 additions & 2 deletions paddle/phi/kernels/selected_rows/full_kernel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ void FullKernel(const Context& dev_ctx,

template <typename T, typename Context>
void FullWithTensorKernel(const Context& dev_ctx,
const DenseTensor& shape,
const DenseTensor& value,
const IntArray& shape,
DataType dtype,
SelectedRows* out) {
phi::FullWithTensorKernel<T>(
dev_ctx, shape, value, dtype, out->mutable_value());
dev_ctx, value, shape, dtype, out->mutable_value());
}

} // namespace sr
Expand Down
2 changes: 1 addition & 1 deletion paddle/phi/kernels/selected_rows/full_kernel.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ void FullKernel(const Context& dev_ctx,

template <typename T, typename Context>
void FullWithTensorKernel(const Context& dev_ctx,
const DenseTensor& shape,
const DenseTensor& value,
const IntArray& shape,
DataType dtype,
SelectedRows* out);
} // namespace sr
Expand Down
2 changes: 1 addition & 1 deletion paddle/phi/kernels/xpu/full_kernel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
#include "paddle/phi/common/scalar.h"
#include "paddle/phi/core/kernel_registry.h"
#include "paddle/phi/core/visit_type.h"
#include "paddle/phi/kernels/impl/full_whit_tensor_kernel_impl.h"
#include "paddle/phi/kernels/impl/full_with_tensor_kernel_impl.h"

namespace phi {

Expand Down
10 changes: 5 additions & 5 deletions test/ir/pir/cinn/symbolic/simple_llama.config
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,24 @@
(%24) = "pd_op.slice" (%18, %22, %23) {axes:[(Int64)0],decrease_axis:[(Int64)0],infer_flags:[(Int64)1],is_persistable:[false],stop_gradient:[false]} : (builtin.tensor<2xi32>, builtin.tensor<1xi64>, builtin.tensor<1xi64>) -> builtin.tensor<i32>
(%25) = "pd_op.cast" (%24) {dtype:(pd_op.DataType)int64,is_persistable:[false],stop_gradient:[false]} : (builtin.tensor<i32>) -> builtin.tensor<i64>
(%26) = "pd_op.full_int_array" () {dtype:(pd_op.DataType)int64,place:(pd_op.Place)Place(cpu),stop_gradient:[true],value:[(Int64)1]} : () -> builtin.tensor<1xi64>
(%27) = "pd_op.full_with_tensor" (%26, %25) {dtype:(pd_op.DataType)int64,is_persistable:[false],stop_gradient:[false]} : (builtin.tensor<1xi64>, builtin.tensor<i64>) -> builtin.tensor<1xi64>
(%27) = "pd_op.full_with_tensor" (%25, %26) {dtype:(pd_op.DataType)int64,is_persistable:[false],stop_gradient:[false]} : (builtin.tensor<i64>, builtin.tensor<1xi64>) -> builtin.tensor<1xi64>
(%28) = "pd_op.shape" (%17) {is_persistable:[false],stop_gradient:[false]} : (builtin.tensor<-1x-1xi64>) -> builtin.tensor<2xi32>
(%29) = "pd_op.full_int_array" () {dtype:(pd_op.DataType)int64,place:(pd_op.Place)Place(cpu),stop_gradient:[true],value:[(Int64)1]} : () -> builtin.tensor<1xi64>
(%30) = "pd_op.full_int_array" () {dtype:(pd_op.DataType)int64,place:(pd_op.Place)Place(cpu),stop_gradient:[true],value:[(Int64)2]} : () -> builtin.tensor<1xi64>
(%31) = "pd_op.slice" (%28, %29, %30) {axes:[(Int64)0],decrease_axis:[(Int64)0],infer_flags:[(Int64)1],is_persistable:[false],stop_gradient:[false]} : (builtin.tensor<2xi32>, builtin.tensor<1xi64>, builtin.tensor<1xi64>) -> builtin.tensor<i32>
(%32) = "pd_op.cast" (%31) {dtype:(pd_op.DataType)int64,is_persistable:[false],stop_gradient:[false]} : (builtin.tensor<i32>) -> builtin.tensor<i64>
(%33) = "pd_op.full_int_array" () {dtype:(pd_op.DataType)int64,place:(pd_op.Place)Place(cpu),stop_gradient:[true],value:[(Int64)1]} : () -> builtin.tensor<1xi64>
(%34) = "pd_op.full_with_tensor" (%33, %32) {dtype:(pd_op.DataType)int64,is_persistable:[false],stop_gradient:[false]} : (builtin.tensor<1xi64>, builtin.tensor<i64>) -> builtin.tensor<1xi64>
(%34) = "pd_op.full_with_tensor" (%32, %33) {dtype:(pd_op.DataType)int64,is_persistable:[false],stop_gradient:[false]} : (builtin.tensor<i64>, builtin.tensor<1xi64>) -> builtin.tensor<1xi64>
(%35) = "pd_op.full" () {dtype:(pd_op.DataType)int32,is_persistable:[false],place:(pd_op.Place)Place(cpu),shape:(pd_op.IntArray)[],stop_gradient:[false],value:(Float)1} : () -> builtin.tensor<i32>
(%36) = "builtin.combine" (%21, %35) {} : (builtin.tensor<i32>, builtin.tensor<i32>) -> vec[builtin.tensor<i32>,builtin.tensor<i32>]
(%37) = "pd_op.stack" (%36) {axis:(Int32)0,stop_gradient:[true]} : (vec[builtin.tensor<i32>,builtin.tensor<i32>]) -> builtin.tensor<2xi32>
(%38) = "pd_op.full" () {dtype:(pd_op.DataType)float32,place:(pd_op.Place)Place(cpu),shape:(pd_op.IntArray)[1],stop_gradient:[true],value:(Float)1} : () -> builtin.tensor<1xf32>
(%39) = "pd_op.full_with_tensor" (%37, %38) {dtype:(pd_op.DataType)bool,is_persistable:[false],stop_gradient:[false]} : (builtin.tensor<2xi32>, builtin.tensor<1xf32>) -> builtin.tensor<-1x1xb>
(%39) = "pd_op.full_with_tensor" (%38, %37) {dtype:(pd_op.DataType)bool,is_persistable:[false],stop_gradient:[false]} : (builtin.tensor<1xf32>, builtin.tensor<2xi32>) -> builtin.tensor<-1x1xb>
(%40) = "pd_op.full" () {dtype:(pd_op.DataType)int32,is_persistable:[false],place:(pd_op.Place)Place(cpu),shape:(pd_op.IntArray)[],stop_gradient:[false],value:(Float)1} : () -> builtin.tensor<i32>
(%41) = "builtin.combine" (%21, %40) {} : (builtin.tensor<i32>, builtin.tensor<i32>) -> vec[builtin.tensor<i32>,builtin.tensor<i32>]
(%42) = "pd_op.stack" (%41) {axis:(Int32)0,stop_gradient:[true]} : (vec[builtin.tensor<i32>,builtin.tensor<i32>]) -> builtin.tensor<2xi32>
(%43) = "pd_op.full" () {dtype:(pd_op.DataType)float32,place:(pd_op.Place)Place(cpu),shape:(pd_op.IntArray)[1],stop_gradient:[true],value:(Float)0} : () -> builtin.tensor<1xf32>
(%44) = "pd_op.full_with_tensor" (%42, %43) {dtype:(pd_op.DataType)float16,is_persistable:[false],stop_gradient:[false]} : (builtin.tensor<2xi32>, builtin.tensor<1xf32>) -> builtin.tensor<-1x1xf16>
(%44) = "pd_op.full_with_tensor" (%43, %42) {dtype:(pd_op.DataType)float16,is_persistable:[false],stop_gradient:[false]} : (builtin.tensor<1xf32>, builtin.tensor<2xi32>) -> builtin.tensor<-1x1xf16>
(%45) = "pd_op.shape" (%17) {is_persistable:[false],stop_gradient:[false]} : (builtin.tensor<-1x-1xi64>) -> builtin.tensor<2xi32>
(%46) = "pd_op.full_int_array" () {dtype:(pd_op.DataType)int64,place:(pd_op.Place)Place(cpu),stop_gradient:[true],value:[(Int64)1]} : () -> builtin.tensor<1xi64>
(%47) = "pd_op.full_int_array" () {dtype:(pd_op.DataType)int64,place:(pd_op.Place)Place(cpu),stop_gradient:[true],value:[(Int64)2]} : () -> builtin.tensor<1xi64>
Expand Down Expand Up @@ -222,7 +222,7 @@
(%232) = "pd_op.full" () {dtype:(pd_op.DataType)int32,is_persistable:[false],place:(pd_op.Place)Place(cpu),shape:(pd_op.IntArray)[],stop_gradient:[false],value:(Float)1} : () -> builtin.tensor<i32>
(%233) = "builtin.combine" (%230, %232) {} : (builtin.tensor<i32>, builtin.tensor<i32>) -> vec[builtin.tensor<i32>,builtin.tensor<i32>]
(%234) = "pd_op.stack" (%233) {axis:(Int32)0,stop_gradient:[true]} : (vec[builtin.tensor<i32>,builtin.tensor<i32>]) -> builtin.tensor<2xi32>
(%235) = "pd_op.full_with_tensor" (%234, %231) {dtype:(pd_op.DataType)float16,is_persistable:[false],stop_gradient:[false]} : (builtin.tensor<2xi32>, builtin.tensor<1xf16>) -> builtin.tensor<-1x1xf16>
(%235) = "pd_op.full_with_tensor" (%231, %234) {dtype:(pd_op.DataType)float16,is_persistable:[false],stop_gradient:[false]} : (builtin.tensor<1xf16>, builtin.tensor<2xi32>) -> builtin.tensor<-1x1xf16>
(%236, %237) = "pd_op.top_p_sampling" (%225, %235, <<NULL VALUE>>) {is_persistable:[false,false],seed:(Int32)-1,stop_gradient:[false,false]} : (builtin.tensor<-1x32000xf16>, builtin.tensor<-1x1xf16>, <<NULL TYPE>>) -> builtin.tensor<-1x1xf16>, builtin.tensor<-1x1xi64>
(%238) = "pd_op.index_sample" (%226, %237) {is_persistable:[false],stop_gradient:[false]} : (builtin.tensor<-1x32000xf16>, builtin.tensor<-1x1xi64>) -> builtin.tensor<-1x1xf16>
(%239) = "pd_op.subtract" (%27, %34) {is_persistable:[false],stop_gradient:[false]} : (builtin.tensor<1xi64>, builtin.tensor<1xi64>) -> builtin.tensor<1xi64>
Expand Down
8 changes: 8 additions & 0 deletions test/legacy_test/test_zeros_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,5 +80,13 @@ def test_shape_errors(self):
assert error_msg.find("expected to be no less than 0") > 0


class ApiZerosWithDynamicShape(unittest.TestCase):
def test_dynamic_shape(self):
with paddle.pir_utils.IrGuard():
x = paddle.static.data("x", shape=[], dtype='int32')
out = paddle.zeros(shape=[101, x])
self.assertEqual(out.shape, [101, -1])


if __name__ == '__main__':
unittest.main()