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] Add op_callstack to Pir #62139

Merged
merged 17 commits into from
Mar 6, 2024
12 changes: 2 additions & 10 deletions paddle/cinn/hlir/framework/pir/utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -314,17 +314,9 @@ static utils::Attribute ConvertArrayAttribute(
CASE_ATTRIBUTE(float, FloatAttribute)
} else if (attr_vec[0].isa<::pir::DoubleAttribute>()) {
CASE_ATTRIBUTE(double, DoubleAttribute)
} else if (attr_vec[0].isa<::pir::StrAttribute>()) {
std::vector<std::string> dst_attr;
for (auto element : attr_vec) {
dst_attr.push_back(
element.dyn_cast<::pir::StrAttribute>().AsString());
}
dst_attr = dst_attr;
xingmingyyj marked this conversation as resolved.
Show resolved Hide resolved
} else {
LOG(FATAL)
<< "only support bool/int32/int64/float/double/string attribute in "
"ArrayAttribute";
LOG(FATAL) << "only support bool/int32/int64/float/double attribute in "
"ArrayAttribute";
}
}
} else {
Expand Down
12 changes: 6 additions & 6 deletions paddle/fluid/pir/dialect/op_generator/python_c_gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,9 @@

// Call ir static api
CallStackRecorder callstack_recoder("{api_name}");
callstack_recoder.record();
callstack_recoder.Record();
auto static_api_out = paddle::dialect::{api_name}({args});
callstack_recoder.attach_to_ops();
callstack_recoder.AttachToOps();
return ToPyObject(static_api_out);
}} catch (...) {{
ThrowExceptionToPython(std::current_exception());
Expand All @@ -98,9 +98,9 @@

// Call ir static api
CallStackRecorder callstack_recoder("{api_name}");
callstack_recoder.record();
callstack_recoder.Record();
paddle::dialect::{api_name}({args});
callstack_recoder.attach_to_ops();
callstack_recoder.AttachToOps();
return nullptr;
}} catch (...) {{
ThrowExceptionToPython(std::current_exception());
Expand Down Expand Up @@ -135,9 +135,9 @@

// Call ir static api
CallStackRecorder callstack_recoder("{api_name}");
callstack_recoder.record();
callstack_recoder.Record();
auto static_api_out = paddle::dialect::{api_name}({args_with_mutable_attrs});
callstack_recoder.attach_to_ops();
callstack_recoder.AttachToOps();
return ToPyObject(static_api_out);


Expand Down
68 changes: 34 additions & 34 deletions paddle/fluid/pybind/manual_static_op_function.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ static PyObject *static_api_parameter(PyObject *self,
std::string name = CastPyArg2String(name_obj, "name", 0);
// Call ir static api
CallStackRecorder callstack_recoder("parameter");
callstack_recoder.record();
callstack_recoder.Record();
auto static_api_out = paddle::dialect::parameter(name);
callstack_recoder.attach_to_ops();
callstack_recoder.AttachToOps();
return ToPyObject(static_api_out);
} catch (...) {
ThrowExceptionToPython(std::current_exception());
Expand All @@ -71,9 +71,9 @@ static PyObject *static_api_set_parameter(PyObject *self,
std::string name = CastPyArg2String(name_obj, "name", 1);
// Call ir static api
CallStackRecorder callstack_recoder("set_parameter");
callstack_recoder.record();
callstack_recoder.Record();
paddle::dialect::set_parameter(parameter, name);
callstack_recoder.attach_to_ops();
callstack_recoder.AttachToOps();
Py_RETURN_NONE;
} catch (...) {
ThrowExceptionToPython(std::current_exception());
Expand All @@ -97,9 +97,9 @@ static PyObject *static_api_set_persistable_value(PyObject *self,
std::string name = CastPyArg2String(name_obj, "name", 1);
// Call ir static api
CallStackRecorder callstack_recoder("shadow_output");
callstack_recoder.record();
callstack_recoder.Record();
paddle::dialect::shadow_output(persist_value, name);
callstack_recoder.attach_to_ops();
callstack_recoder.AttachToOps();
Py_RETURN_NONE;
} catch (...) {
ThrowExceptionToPython(std::current_exception());
Expand Down Expand Up @@ -127,9 +127,9 @@ PyObject *static_api_full(PyObject *self, PyObject *args, PyObject *kwargs) {
std::vector<int64_t> shape = CastPyArg2Longs(shape_obj, "full", 0);
float value = CastPyArg2Float(value_obj, "full", 1);
CallStackRecorder callstack_recoder("full");
callstack_recoder.record();
callstack_recoder.Record();
auto static_api_out = paddle::dialect::full(shape, value, dtype, place);
callstack_recoder.attach_to_ops();
callstack_recoder.AttachToOps();
return ToPyObject(static_api_out);
} else {
pir::Value shape, value;
Expand Down Expand Up @@ -157,10 +157,10 @@ PyObject *static_api_full(PyObject *self, PyObject *args, PyObject *kwargs) {
}

CallStackRecorder callstack_recoder("full_with_tensor");
callstack_recoder.record();
callstack_recoder.Record();
auto static_api_out =
paddle::dialect::full_with_tensor(shape, value, dtype);
callstack_recoder.attach_to_ops();
callstack_recoder.AttachToOps();

return ToPyObject(static_api_out);
}
Expand All @@ -184,9 +184,9 @@ static PyObject *static_api_create_array(PyObject *self,

// Call ir static api
CallStackRecorder callstack_recoder("create_array");
callstack_recoder.record();
callstack_recoder.Record();
auto static_api_out = paddle::dialect::create_array(dtype);
callstack_recoder.attach_to_ops();
callstack_recoder.AttachToOps();

return ToPyObject(static_api_out);
} catch (...) {
Expand All @@ -212,9 +212,9 @@ static PyObject *static_api_create_array_like(PyObject *self,

// Call ir static api
CallStackRecorder callstack_recoder("create_array_like");
callstack_recoder.record();
callstack_recoder.Record();
auto static_api_out = paddle::dialect::create_array_like(input, value);
callstack_recoder.attach_to_ops();
callstack_recoder.AttachToOps();
return ToPyObject(static_api_out);
} catch (...) {
ThrowExceptionToPython(std::current_exception());
Expand All @@ -235,9 +235,9 @@ static PyObject *static_api_array_length(PyObject *self,

// Call ir static api
CallStackRecorder callstack_recoder("array_length");
callstack_recoder.record();
callstack_recoder.Record();
auto static_api_out = paddle::dialect::array_length(x);
callstack_recoder.attach_to_ops();
callstack_recoder.AttachToOps();

return ToPyObject(static_api_out);
} catch (...) {
Expand Down Expand Up @@ -271,9 +271,9 @@ static PyObject *static_api_array_read(PyObject *self,

// Call ir static api
CallStackRecorder callstack_recoder("array_read");
callstack_recoder.record();
callstack_recoder.Record();
auto static_api_out = paddle::dialect::array_read(array, i);
callstack_recoder.attach_to_ops();
callstack_recoder.AttachToOps();

return ToPyObject(static_api_out);
} catch (...) {
Expand Down Expand Up @@ -308,9 +308,9 @@ static PyObject *static_api_array_write_(PyObject *self,

// Call ir static api
CallStackRecorder callstack_recoder("array_write_");
callstack_recoder.record();
callstack_recoder.Record();
auto static_api_out = paddle::dialect::array_write_(array, x, i);
callstack_recoder.attach_to_ops();
callstack_recoder.AttachToOps();

return ToPyObject(static_api_out);
} catch (...) {
Expand Down Expand Up @@ -350,9 +350,9 @@ static PyObject *static_api_array_to_tensor(PyObject *self,

// Call ir static api
CallStackRecorder callstack_recoder("array_to_tensor");
callstack_recoder.record();
callstack_recoder.Record();
auto static_api_out = paddle::dialect::array_to_tensor(x, axis, use_stack);
callstack_recoder.attach_to_ops();
callstack_recoder.AttachToOps();

return ToPyObject(static_api_out);
} catch (...) {
Expand All @@ -373,9 +373,9 @@ PyObject *static_api_add_n_array(PyObject *self,
auto inputs = CastPyArg2VectorOfValue(inputs_obj, "add_n", 0);

CallStackRecorder callstack_recoder("add_n_array");
callstack_recoder.record();
callstack_recoder.Record();
auto static_api_out = paddle::dialect::add_n_array(inputs);
callstack_recoder.attach_to_ops();
callstack_recoder.AttachToOps();

return ToPyObject(static_api_out);
} catch (...) {
Expand Down Expand Up @@ -427,9 +427,9 @@ static PyObject *static_api_slice_array(PyObject *self,

// Call ir static api
CallStackRecorder callstack_recoder("slice_array");
callstack_recoder.record();
callstack_recoder.Record();
auto static_api_out = paddle::dialect::slice_array(input, starts, ends);
callstack_recoder.attach_to_ops();
callstack_recoder.AttachToOps();

return ToPyObject(static_api_out);
} catch (...) {
Expand Down Expand Up @@ -466,9 +466,9 @@ static PyObject *static_api_slice_array_dense(PyObject *self,
}
// Call ir static api
CallStackRecorder callstack_recoder("slice_array_dense");
callstack_recoder.record();
callstack_recoder.Record();
auto static_api_out = paddle::dialect::slice_array_dense(input, starts);
callstack_recoder.attach_to_ops();
callstack_recoder.AttachToOps();

return ToPyObject(static_api_out);
} catch (...) {
Expand Down Expand Up @@ -791,7 +791,7 @@ static PyObject *static_api_run_custom_op(PyObject *self,
argument.AddOutputs(argument_outputs.begin(), argument_outputs.end());
::pir::PassStopGradientsDefaultly(argument);
CallStackRecorder callstack_recoder("run_custom_op");
callstack_recoder.record();
callstack_recoder.Record();
std::vector<pir::Value> op_results;
pir::Operation *op =
paddle::dialect::ApiBuilder::Instance().GetBuilder()->Build(
Expand All @@ -809,7 +809,7 @@ static PyObject *static_api_run_custom_op(PyObject *self,
op_results.push_back(op->result(i));
}
}
callstack_recoder.attach_to_ops();
callstack_recoder.AttachToOps();
return ToPyObject(op_results);
}

Expand Down Expand Up @@ -850,10 +850,10 @@ static PyObject *static_api_fused_gemm_epilogue(PyObject *self,
CastPyArg2String(activation_obj, "fused_gemm_epilogue", 5);
// Call ir static api
CallStackRecorder callstack_recoder("fused_gemm_epilogue");
callstack_recoder.record();
callstack_recoder.Record();
auto out = paddle::dialect::fused_gemm_epilogue(
x, y, bias, trans_x, trans_y, activation);
callstack_recoder.attach_to_ops();
callstack_recoder.AttachToOps();

return ToPyObject(out);
} catch (...) {
Expand All @@ -877,9 +877,9 @@ static PyObject *static_api_array_pop(PyObject *self,

// Call ir static api
CallStackRecorder callstack_recoder("array_pop");
callstack_recoder.record();
callstack_recoder.Record();
auto static_api_out = paddle::dialect::array_pop(input, index);
callstack_recoder.attach_to_ops();
callstack_recoder.AttachToOps();
return ToPyObject(static_api_out);
} catch (...) {
ThrowExceptionToPython(std::current_exception());
Expand Down
29 changes: 15 additions & 14 deletions paddle/fluid/pybind/op_callstack_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,17 @@
#include "paddle/fluid/platform/enforce.h"
#include "paddle/fluid/pybind/op_callstack_utils.h"

pir::Attribute CallStackRecorder::get_op_callstack_info() {
pir::Attribute CallStackRecorder::GetOpCallstackInfo() {
PyObject* traceback_str = PyUnicode_FromString("traceback");
PyObject* traceback_module = PyImport_Import(traceback_str);

if (NULL == traceback_module) {
Py_DECREF(traceback_str);
Py_DECREF(traceback_module);
PADDLE_THROW(paddle::platform::errors::PreconditionNotMet(
"Failed to import traceback module when getting callstack information "
"Failed to import traceback module while getting callstack information "
"for %s.",
api_name));
api_name_));
}
PyObject* tb = PyObject_GetAttrString(traceback_module, "extract_stack");
PyObject* stack = PyObject_CallObject(tb, NULL);
Expand All @@ -39,9 +39,10 @@ pir::Attribute CallStackRecorder::get_op_callstack_info() {
Py_DECREF(traceback_str);
Py_DECREF(traceback_module);
PADDLE_THROW(paddle::platform::errors::PreconditionNotMet(
"Failed to get callstack object when getting callstack information for "
"Failed to get callstack object while getting callstack information "
"for "
"%s.",
api_name));
api_name_));
}
Py_ssize_t stack_size = PyList_Size(stack);
std::vector<pir::Attribute> op_callstack_infos;
Expand Down Expand Up @@ -74,26 +75,26 @@ pir::Attribute CallStackRecorder::get_op_callstack_info() {
op_callstack_infos);
}

void CallStackRecorder::record() {
void CallStackRecorder::Record() {
auto before_insertion_point =
paddle::dialect::ApiBuilder::Instance().GetCurrentInsertionPoint();
before_insertion_iterator = (--before_insertion_point.second);
before_insertion_block = before_insertion_point.first;
before_insertion_iterator_ = (--before_insertion_point.second);
before_insertion_block_ = before_insertion_point.first;
}

void CallStackRecorder::attach_to_ops() {
before_insertion_iterator++;
pir::Attribute callstack_info_attr = get_op_callstack_info();
void CallStackRecorder::AttachToOps() {
before_insertion_iterator_++;
pir::Attribute callstack_info_attr = GetOpCallstackInfo();
pir::InsertionPoint after_insertion_point =
paddle::dialect::ApiBuilder::Instance().GetCurrentInsertionPoint();
PADDLE_ENFORCE_EQ(before_insertion_block,
PADDLE_ENFORCE_EQ(before_insertion_block_,
after_insertion_point.first,
paddle::platform::errors::PreconditionNotMet(
"The block obtained before and after calling the "
"static API %s is inconsistent.",
api_name));
api_name_));
auto after_insertion_iterator = after_insertion_point.second;
for (auto block_iterator = before_insertion_iterator;
for (auto block_iterator = before_insertion_iterator_;
block_iterator != after_insertion_iterator;
block_iterator++) {
block_iterator->set_attribute(paddle::framework::OpProtoAndCheckerMaker::
Expand Down
14 changes: 7 additions & 7 deletions paddle/fluid/pybind/op_callstack_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@
class CallStackRecorder {
public:
explicit CallStackRecorder(const std::string& api_name)
: api_name(api_name), before_insertion_block(nullptr) {}
pir::Attribute get_op_callstack_info();
void record();
void attach_to_ops();
: api_name_(api_name), before_insertion_block_(nullptr) {}
pir::Attribute GetOpCallstackInfo();
void Record();
void AttachToOps();

private:
const std::string api_name;
pir::Block::Iterator before_insertion_iterator;
pir::Block* before_insertion_block;
const std::string& api_name_;
pir::Block::Iterator before_insertion_iterator_;
pir::Block* before_insertion_block_;
};