forked from openvinotoolkit/openvino
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[PT FE] Add support for PackedSequence for aten::lstm (openvinotoolki…
…t#22586) * [PT FE] Add support for PackedSequence for aten::lstm * Apply suggestions from code review
- Loading branch information
Showing
8 changed files
with
374 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// Copyright (C) 2018-2024 Intel Corporation | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
||
#pragma once | ||
|
||
#include "internal_op.hpp" | ||
#include "utils.hpp" | ||
|
||
namespace ov { | ||
namespace frontend { | ||
namespace pytorch { | ||
|
||
class PackPadded : public InternalOperation { | ||
public: | ||
OPENVINO_OP("PackPadded", "util", ov::op::util::FrameworkNode); | ||
PackPadded(const Output<Node>& input, const Output<Node>& lengths) | ||
: InternalOperation("prim::PackPadded", {input, lengths}, 2, "This is PackedSequence pack operation.") { | ||
validate_and_infer_types(); | ||
} | ||
|
||
void validate_and_infer_types() override { | ||
set_output_type(0, get_input_element_type(0), PartialShape({-1, -1, -1})); | ||
set_output_type(1, get_input_element_type(1), PartialShape::dynamic()); | ||
} | ||
}; | ||
|
||
class PadPacked : public InternalOperation { | ||
public: | ||
OPENVINO_OP("PadPacked", "util", ov::op::util::FrameworkNode); | ||
PadPacked(const Output<Node>& input, const Output<Node>& lengths) | ||
: InternalOperation("prim::PadPacked", {input, lengths}, 2, "This is PackedSequence unpack operation.") { | ||
validate_and_infer_types(); | ||
} | ||
|
||
void validate_and_infer_types() override { | ||
set_output_type(0, get_input_element_type(0), PartialShape({-1, -1, -1})); | ||
set_output_type(1, get_input_element_type(1), get_input_partial_shape(1)); | ||
} | ||
}; | ||
} // namespace pytorch | ||
} // namespace frontend | ||
} // namespace ov |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// Copyright (C) 2018-2024 Intel Corporation | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
||
#include "helper_ops/packed_sequence.hpp" | ||
#include "openvino/frontend/pytorch/node_context.hpp" | ||
#include "openvino/op/constant.hpp" | ||
#include "openvino/op/convert.hpp" | ||
#include "openvino/op/transpose.hpp" | ||
#include "utils.hpp" | ||
|
||
namespace ov { | ||
namespace frontend { | ||
namespace pytorch { | ||
namespace op { | ||
|
||
using namespace ov::op; | ||
|
||
OutputVector translate_pack_padded_sequence(const NodeContext& context) { | ||
num_inputs_check(context, 3, 3); | ||
auto seq = context.get_input(0); | ||
auto lengths = context.get_input(1); | ||
const auto batch_first = context.const_input<bool>(2); | ||
|
||
const auto order_102 = v0::Constant::create(element::i32, Shape{3}, {1, 0, 2}); | ||
if (batch_first) | ||
seq = context.mark_node(std::make_shared<v1::Transpose>(seq, order_102)); | ||
if (lengths.get_element_type() != element::i32) | ||
lengths = context.mark_node(std::make_shared<v0::Convert>(lengths, element::i32)); | ||
return context.mark_node(std::make_shared<PackPadded>(seq, lengths))->outputs(); | ||
}; | ||
|
||
OutputVector translate_pad_packed_sequence(const NodeContext& context) { | ||
// aten::_pad_packed_sequence with schema aten::_pad_packed_sequence(Tensor data, Tensor batch_sizes, bool | ||
// batch_first, Scalar padding_value, int total_length) -> (Tensor, Tensor) | ||
num_inputs_check(context, 3, 5); | ||
auto seq = context.get_input(0); | ||
auto lengths = context.get_input(1); | ||
const auto batch_first = context.const_input<bool>(2); | ||
auto pad_packed = context.mark_node(std::make_shared<PadPacked>(seq, lengths)); | ||
seq = pad_packed->output(0); | ||
lengths = pad_packed->output(1); | ||
const auto order_102 = v0::Constant::create(element::i32, Shape{3}, {1, 0, 2}); | ||
if (batch_first) | ||
seq = context.mark_node(std::make_shared<v1::Transpose>(seq, order_102)); | ||
return {seq, lengths}; | ||
}; | ||
|
||
} // namespace op | ||
} // namespace pytorch | ||
} // namespace frontend | ||
} // namespace ov |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
135 changes: 135 additions & 0 deletions
135
src/frontends/pytorch/src/transforms/remove_packing_ops.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
// Copyright (C) 2018-2024 Intel Corporation | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
||
#include "remove_packing_ops.hpp" | ||
|
||
#include "helper_ops/packed_sequence.hpp" | ||
#include "openvino/op/gather.hpp" | ||
#include "openvino/op/gru_sequence.hpp" | ||
#include "openvino/op/lstm_sequence.hpp" | ||
#include "openvino/op/reshape.hpp" | ||
#include "openvino/op/rnn_sequence.hpp" | ||
#include "openvino/op/shape_of.hpp" | ||
#include "openvino/op/squeeze.hpp" | ||
#include "openvino/op/transpose.hpp" | ||
#include "openvino/pass/pattern/matcher.hpp" | ||
#include "openvino/pass/pattern/op/wrap_type.hpp" | ||
#include "utils.hpp" | ||
|
||
namespace ov { | ||
namespace frontend { | ||
namespace pytorch { | ||
namespace pass { | ||
|
||
using namespace ov::pass; | ||
using namespace ov::op; | ||
|
||
namespace { | ||
bool is_rnn(std::shared_ptr<Node> node) { | ||
if (as_type_ptr<v5::LSTMSequence>(node) || as_type_ptr<v5::RNNSequence>(node) || | ||
as_type_ptr<v5::GRUSequence>(node)) { | ||
return true; | ||
} | ||
return false; | ||
} | ||
} // namespace | ||
|
||
MovePackThroughLstm::MovePackThroughLstm() { | ||
auto pack_op = pattern::wrap_type<PackPadded>(); | ||
|
||
ov::matcher_pass_callback callback = [=](pattern::Matcher& m) { | ||
auto pack = m.get_match_root(); | ||
|
||
auto targets = pack->output(0).get_target_inputs(); | ||
if (targets.size() != 1) | ||
return false; | ||
auto rnn = targets.begin()->get_node()->shared_from_this(); | ||
// Input to rnn may be transposed, skipping Transpose | ||
if (as_type_ptr<v1::Transpose>(rnn)) | ||
rnn = rnn->output(0).get_target_inputs().begin()->get_node()->shared_from_this(); | ||
if (!is_rnn(rnn)) | ||
return false; | ||
targets = rnn->output(0).get_target_inputs(); | ||
if (targets.size() != 1) | ||
return false; | ||
|
||
// The rnn is followed by a transpose and a reshape (if bidirectional), or by a squeeze (if unidirectional). | ||
auto next = targets.begin()->get_node()->shared_from_this(); | ||
if (as_type_ptr<v1::Transpose>(next)) { | ||
next = next->output(0).get_target_inputs().begin()->get_node()->shared_from_this(); | ||
if (!as_type_ptr<v1::Reshape>(next)) { | ||
return false; | ||
} | ||
} else if (!as_type_ptr<v0::Squeeze>(next)) { | ||
return false; | ||
} | ||
|
||
// remove PackPadded from in front of the RNN | ||
pack->output(0).replace(pack->input_value(0)); | ||
|
||
auto batch_sizes = pack->output(1); | ||
for (auto node_input : batch_sizes.get_target_inputs()) { | ||
auto user = node_input.get_node()->shared_from_this(); | ||
// Make calculation of max_batch_size not depend on batch_sizes. | ||
// This looks for a pattern generated by code such as | ||
// https://github.com/pytorch/pytorch/blob/febff45/torch/nn/modules/rnn.py#L815-L815. | ||
// | ||
// Replace Gather[axis=0](batch_sizes, 0) | ||
// with Gather[axis=0](ShapeOf(rnn_input), 0) | ||
if (const auto gather = as_type_ptr<v8::Gather>(user)) { | ||
if (gather->get_axis() != 0) | ||
continue; | ||
auto rnn_shape = std::make_shared<v3::ShapeOf>(rnn->input_value(0), element::i32); | ||
auto indx_1 = v0::Constant::create(element::i32, Shape{}, {0}); | ||
auto new_gather = std::make_shared<v8::Gather>(rnn_shape, indx_1, gather->input_value(2)); | ||
copy_runtime_info_and_name(gather, {new_gather, rnn_shape, indx_1}); | ||
replace_node(gather, new_gather); | ||
} else if (user == rnn) { | ||
node_input.replace_source_output(pack->input_value(1)); | ||
} | ||
} | ||
// and insert new PackPadded after the RNN | ||
auto next_target_inputs = next->output(0).get_target_inputs(); | ||
auto newPackPadded = std::make_shared<PackPadded>(next->output(0), pack->input_value(1)); | ||
register_new_node(newPackPadded); | ||
|
||
// make things consume from the new PackPadded | ||
for (auto& input : next_target_inputs) | ||
input.replace_source_output(newPackPadded->output(0)); | ||
pack->output(1).replace(newPackPadded->output(1)); | ||
|
||
return true; | ||
}; | ||
|
||
auto m = std::make_shared<ov::pass::pattern::Matcher>(pack_op, "ov::frontend::pytorch::pass::MovePackThroughLstm"); | ||
this->register_matcher(m, callback); | ||
}; | ||
|
||
RemovePackingOps::RemovePackingOps() { | ||
auto unpack_op = pattern::wrap_type<PadPacked>(); | ||
|
||
ov::matcher_pass_callback callback = [](pattern::Matcher& m) { | ||
const auto& unpack = m.get_match_root(); | ||
auto pack_node = unpack->input_value(0).get_node_shared_ptr(); | ||
if (as_type_ptr<v1::Transpose>(pack_node)) | ||
pack_node = std::dynamic_pointer_cast<PackPadded>(pack_node->input_value(0).get_node_shared_ptr()); | ||
if (!pack_node) | ||
return false; | ||
|
||
pack_node->output(0).replace(pack_node->input_value(0)); | ||
pack_node->output(1).replace(pack_node->input_value(1)); | ||
unpack->output(0).replace(unpack->input_value(0)); | ||
unpack->output(1).replace(unpack->input_value(1)); | ||
|
||
return true; | ||
}; | ||
|
||
auto m = std::make_shared<ov::pass::pattern::Matcher>(unpack_op, "ov::frontend::pytorch::pass::RemovePackingOps"); | ||
this->register_matcher(m, callback); | ||
}; | ||
|
||
} // namespace pass | ||
} // namespace pytorch | ||
} // namespace frontend | ||
} // namespace ov |
36 changes: 36 additions & 0 deletions
36
src/frontends/pytorch/src/transforms/remove_packing_ops.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// Copyright (C) 2018-2024 Intel Corporation | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
||
#pragma once | ||
|
||
#include "openvino/pass/graph_rewrite.hpp" | ||
#include "openvino/pass/pass.hpp" | ||
|
||
namespace ov { | ||
namespace frontend { | ||
namespace pytorch { | ||
namespace pass { | ||
|
||
/** | ||
* Move PackPadded through RNN ops, because RNN(PackPadded(x)) == PackPadded(RNN(x)). | ||
*/ | ||
class MovePackThroughLstm : public ov::pass::MatcherPass { | ||
public: | ||
OPENVINO_RTTI("ov::frontend::pytorch::pass::MovePackThroughLstm"); | ||
MovePackThroughLstm(); | ||
}; | ||
|
||
/** | ||
* Remove PackPadded -> PadPacked ops. | ||
*/ | ||
class RemovePackingOps : public ov::pass::MatcherPass { | ||
public: | ||
OPENVINO_RTTI("ov::frontend::pytorch::pass::RemovePackingOps"); | ||
RemovePackingOps(); | ||
}; | ||
|
||
} // namespace pass | ||
} // namespace pytorch | ||
} // namespace frontend | ||
} // namespace ov |
Oops, something went wrong.