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

[Dy2St] Optimize range_block_do performance #69834

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
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
35 changes: 16 additions & 19 deletions paddle/fluid/pybind/pir.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1656,30 +1656,27 @@ void BindInsertionPoint(pybind11::module *m) {
return_value_policy::reference);
}

std::list<Operation *>::const_iterator list_offset(const Block *block,
int start_idx) {
auto it = block->begin();
while (it != block->end() && start_idx--) ++it;
return it;
}

template <typename F, typename S>
void range_block_do(const Block *block,
std::vector<int> range,
std::pair<size_t, size_t> range,
F fn,
S skip_fn) {
for (auto it = list_offset(block, range[0]);
it != list_offset(block, range[1]);
++it) {
if (skip_fn(*it)) {
auto [start, end] = range;
if (start >= end) {
return;
}
auto it = block->begin();
std::advance(it, start);
for (size_t i = start; i < end && it != block->end(); ++i, ++it) {
if (skip_fn(it)) {
continue;
}
fn(*it);
fn(it);
}
}

template <typename F>
void range_block_do(const Block *block, std::vector<int> range, F fn) {
void range_block_do(const Block *block, std::pair<size_t, size_t> range, F fn) {
range_block_do(block, range, fn, [](Operation *op) { return false; });
}

Expand Down Expand Up @@ -1720,8 +1717,8 @@ std::pair<std::vector<pir::Value>, std::unordered_set<pir::Value>>
AnalysisMiddleVariable(const Program &program,
const std::vector<pir::Value> &forward_inputs,
const std::vector<pir::Value> &backward_outputs,
const std::vector<int> &forward_range,
const std::vector<int> &backward_range) {
const std::pair<size_t, size_t> &forward_range,
const std::pair<size_t, size_t> &backward_range) {
std::vector<pir::Value> middle_values;

std::unordered_set<pir::Value> backward_used_values;
Expand Down Expand Up @@ -1777,7 +1774,7 @@ using SplitedAttribute = std::map<std::string, std::vector<pir::Value>>;
using SplitedResult = std::pair<SplitedProgram, SplitedAttribute>;

static auto GetNoNeedBufferValue(const ::pir::Block *whole_block,
std::vector<int> range) {
std::pair<size_t, size_t> range) {
// filter no need buffer values.
std::unordered_set<::pir::Value> need_buffer_values;
std::unordered_set<::pir::Value> no_need_buffer_values;
Expand Down Expand Up @@ -1892,8 +1889,8 @@ SplitedResult SplitForwardBackward(
const std::vector<pir::Value> &forward_inputs_grads,
const std::vector<pir::Value> &forward_params_grads,
const std::vector<pir::Value> &forward_outputs_grads,
const std::vector<int> &forward_range,
const std::vector<int> &backward_range) {
const std::pair<size_t, size_t> &forward_range,
const std::pair<size_t, size_t> &backward_range) {
std::vector<pir::Value> forward_in_out_values;
for (auto &v :
std::vector({&forward_inputs, &forward_outputs, &forward_params})) {
Expand Down
4 changes: 2 additions & 2 deletions python/paddle/jit/dy2static/pir_partial_program.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,8 +260,8 @@ def split_forward_backward(self):
self.x_grad_values,
self.param_grad_values,
self.out_grad_values,
list(self.forward_range),
list(self.backward_range),
self.forward_range,
self.backward_range,
)
return [fwd_prog, bwd_prog], prog_attr

Expand Down