Skip to content

Commit

Permalink
[TF FE] Speed up compilation - part 4 (openvinotoolkit#21269)
Browse files Browse the repository at this point in the history
Signed-off-by: Kazantsev, Roman <roman.kazantsev@intel.com>
  • Loading branch information
rkazants authored Nov 24, 2023
1 parent 965313b commit 9f87f72
Show file tree
Hide file tree
Showing 21 changed files with 206 additions and 145 deletions.
18 changes: 10 additions & 8 deletions src/frontends/tensorflow_common/src/op/random_uniform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
// SPDX-License-Identifier: Apache-2.0
//

#include "openvino/op/random_uniform.hpp"

#include "common_op_table.hpp"
#include "openvino/opsets/opset8.hpp"
#include "openvino/op/constant.hpp"

using namespace std;
using namespace ov::opset8;
using namespace ov::op;

namespace ov {
namespace frontend {
Expand All @@ -21,9 +23,9 @@ ov::OutputVector translate_random_uniform_op(const NodeContext& node) {
auto seed2 = node.get_attribute<int64_t>("seed2", 0);
auto output_type = node.get_attribute<ov::element::Type>("dtype");

auto minval = make_shared<Constant>(output_type, Shape{}, 0);
auto maxval = make_shared<Constant>(output_type, Shape{}, 1);
auto random = std::make_shared<RandomUniform>(shape, minval, maxval, output_type, seed, seed2);
auto minval = make_shared<v0::Constant>(output_type, Shape{}, 0);
auto maxval = make_shared<v0::Constant>(output_type, Shape{}, 1);
auto random = std::make_shared<v8::RandomUniform>(shape, minval, maxval, output_type, seed, seed2);

set_node_name(node.get_name(), random);
return random->outputs();
Expand All @@ -42,10 +44,10 @@ ov::OutputVector translate_random_uniform_int_op(const NodeContext& node) {
auto output_type = minval.get_element_type();
Output<Node> random;
if (output_type.is_static()) {
random = std::make_shared<RandomUniform>(shape, minval, maxval, output_type, seed, seed2);
random = std::make_shared<v8::RandomUniform>(shape, minval, maxval, output_type, seed, seed2);
} else {
random = std::make_shared<RandomUniform>(shape, minval, maxval, element::i64, seed, seed2);
random = make_shared<ConvertLike>(random, minval);
random = std::make_shared<v8::RandomUniform>(shape, minval, maxval, element::i64, seed, seed2);
random = make_shared<v1::ConvertLike>(random, minval);
}

set_node_name(node.get_name(), random.get_node_shared_ptr());
Expand Down
12 changes: 7 additions & 5 deletions src/frontends/tensorflow_common/src/op/range.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
// SPDX-License-Identifier: Apache-2.0
//

#include "openvino/op/range.hpp"

#include "common_op_table.hpp"
#include "openvino/opsets/opset8.hpp"
#include "openvino/op/convert_like.hpp"

using namespace std;
using namespace ov::opset8;
using namespace ov::op;

namespace ov {
namespace frontend {
Expand All @@ -22,10 +24,10 @@ OutputVector translate_range_op(const NodeContext& node) {
auto start_type = start.get_element_type();
Output<Node> range;
if (start_type.is_static()) {
range = make_shared<Range>(start, limit, delta, start_type);
range = make_shared<v4::Range>(start, limit, delta, start_type);
} else {
range = make_shared<Range>(start, limit, delta, element::f32);
range = make_shared<ConvertLike>(range, start);
range = make_shared<v4::Range>(start, limit, delta, element::f32);
range = make_shared<v1::ConvertLike>(range, start);
}
set_node_name(node.get_name(), range.get_node_shared_ptr());
return {range};
Expand Down
11 changes: 6 additions & 5 deletions src/frontends/tensorflow_common/src/op/rank.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
//

#include "common_op_table.hpp"
#include "openvino/opsets/opset8.hpp"
#include "openvino/op/shape_of.hpp"
#include "openvino/op/squeeze.hpp"

using namespace std;
using namespace ov::opset8;
using namespace ov::op;

namespace ov {
namespace frontend {
Expand All @@ -16,9 +17,9 @@ namespace op {
ov::OutputVector translate_rank_op(const NodeContext& node) {
default_op_checks(node, 1, {"Rank"});
auto input = node.get_input(0);
auto input_shape = make_shared<ShapeOf>(input, ov::element::i32);
auto unsqueeze_input_rank = make_shared<ShapeOf>(input_shape, ov::element::i32);
auto input_rank = make_shared<Squeeze>(unsqueeze_input_rank);
auto input_shape = make_shared<v3::ShapeOf>(input, ov::element::i32);
auto unsqueeze_input_rank = make_shared<v3::ShapeOf>(input_shape, ov::element::i32);
auto input_rank = make_shared<v0::Squeeze>(unsqueeze_input_rank);
set_node_name(node.get_name(), input_rank);
return {input_rank};
}
Expand Down
6 changes: 3 additions & 3 deletions src/frontends/tensorflow_common/src/op/reciprocal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
//

#include "common_op_table.hpp"
#include "openvino/opsets/opset8.hpp"
#include "openvino/op/power.hpp"
#include "utils.hpp"

using namespace std;
using namespace ov::opset8;
using namespace ov::op;

namespace ov {
namespace frontend {
Expand All @@ -19,7 +19,7 @@ OutputVector translate_reciprocal_op(const NodeContext& node) {
default_op_checks(node, 1, {"Reciprocal"});
auto x = node.get_input(0);
auto minus_one_const = create_same_type_const_scalar<int32_t>(x, -1);
auto reciprocal = make_shared<Power>(x, minus_one_const);
auto reciprocal = make_shared<v1::Power>(x, minus_one_const);
set_node_name(node.get_name(), reciprocal);
return {reciprocal};
}
Expand Down
26 changes: 16 additions & 10 deletions src/frontends/tensorflow_common/src/op/reduce.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,16 @@
//

#include "common_op_table.hpp"
#include "openvino/opsets/opset8.hpp"
#include "openvino/op/reduce_l2.hpp"
#include "openvino/op/reduce_logical_and.hpp"
#include "openvino/op/reduce_logical_or.hpp"
#include "openvino/op/reduce_max.hpp"
#include "openvino/op/reduce_mean.hpp"
#include "openvino/op/reduce_min.hpp"
#include "openvino/op/reduce_sum.hpp"

using namespace std;
using namespace ov::opset8;
using namespace ov::op;

namespace ov {
namespace frontend {
Expand All @@ -24,14 +30,14 @@ OutputVector translate_direct_reduce_op(const NodeContext& node) {
return {reduce_op};
}

template OutputVector translate_direct_reduce_op<ReduceLogicalOr>(const NodeContext& node);
template OutputVector translate_direct_reduce_op<ReduceLogicalAnd>(const NodeContext& node);
template OutputVector translate_direct_reduce_op<ReduceMax>(const NodeContext& node);
template OutputVector translate_direct_reduce_op<ReduceMean>(const NodeContext& node);
template OutputVector translate_direct_reduce_op<ReduceMin>(const NodeContext& node);
template OutputVector translate_direct_reduce_op<ReduceProd>(const NodeContext& node);
template OutputVector translate_direct_reduce_op<ReduceSum>(const NodeContext& node);
template OutputVector translate_direct_reduce_op<ReduceL2>(const NodeContext& node);
template OutputVector translate_direct_reduce_op<v1::ReduceLogicalOr>(const NodeContext& node);
template OutputVector translate_direct_reduce_op<v1::ReduceLogicalAnd>(const NodeContext& node);
template OutputVector translate_direct_reduce_op<v1::ReduceMax>(const NodeContext& node);
template OutputVector translate_direct_reduce_op<v1::ReduceMean>(const NodeContext& node);
template OutputVector translate_direct_reduce_op<v1::ReduceMin>(const NodeContext& node);
template OutputVector translate_direct_reduce_op<v1::ReduceProd>(const NodeContext& node);
template OutputVector translate_direct_reduce_op<v1::ReduceSum>(const NodeContext& node);
template OutputVector translate_direct_reduce_op<v4::ReduceL2>(const NodeContext& node);
} // namespace op
} // namespace tensorflow
} // namespace frontend
Expand Down
7 changes: 4 additions & 3 deletions src/frontends/tensorflow_common/src/op/reshape.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
// SPDX-License-Identifier: Apache-2.0
//

#include "openvino/op/reshape.hpp"

#include "common_op_table.hpp"
#include "openvino/opsets/opset8.hpp"

using namespace std;
using namespace ov::opset8;
using namespace ov::op;

namespace ov {
namespace frontend {
Expand All @@ -17,7 +18,7 @@ OutputVector translate_reshape_op(const NodeContext& node) {
default_op_checks(node, 2, {"Reshape"});
auto tensor = node.get_input(0);
auto shape = node.get_input(1);
auto reshape = make_shared<Reshape>(tensor, shape, false);
auto reshape = make_shared<v1::Reshape>(tensor, shape, false);
set_node_name(node.get_name(), reshape);
return {reshape};
}
Expand Down
35 changes: 21 additions & 14 deletions src/frontends/tensorflow_common/src/op/reverse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,28 @@
//

#include "common_op_table.hpp"
#include "openvino/opsets/opset9.hpp"
#include "openvino/op/broadcast.hpp"
#include "openvino/op/constant.hpp"
#include "openvino/op/gather.hpp"
#include "openvino/op/reverse_sequence.hpp"
#include "openvino/op/squeeze.hpp"
#include "openvino/op/unsqueeze.hpp"

using namespace std;
using namespace ov;
using namespace ov::opset9;
using namespace ov::op;

namespace ov {
namespace frontend {
namespace tensorflow {
namespace op {
shared_ptr<Node> compute_sequence_lengths(const Output<Node>& input_shape, int64_t batch_axis, int64_t seq_axis) {
auto batch_axis_const = make_shared<Constant>(element::i32, Shape{1}, batch_axis);
auto seq_axis_const = make_shared<Constant>(element::i32, Shape{1}, seq_axis);
auto gather_axis = make_shared<Constant>(element::i32, Shape{}, 0);
auto batch_dim = make_shared<Gather>(input_shape, batch_axis_const, gather_axis);
auto seq_dim = make_shared<Gather>(input_shape, seq_axis_const, gather_axis);
auto seq_lengths = make_shared<Broadcast>(seq_dim, batch_dim);
auto batch_axis_const = make_shared<v0::Constant>(element::i32, Shape{1}, batch_axis);
auto seq_axis_const = make_shared<v0::Constant>(element::i32, Shape{1}, seq_axis);
auto gather_axis = make_shared<v0::Constant>(element::i32, Shape{}, 0);
auto batch_dim = make_shared<v8::Gather>(input_shape, batch_axis_const, gather_axis);
auto seq_dim = make_shared<v8::Gather>(input_shape, seq_axis_const, gather_axis);
auto seq_lengths = make_shared<v3::Broadcast>(seq_dim, batch_dim);

return seq_lengths;
}
Expand Down Expand Up @@ -56,18 +61,20 @@ OutputVector translate_reverse_base_op(const NodeContext& node,
auto batched_input = input;
if (unsqueeze_axes.size() > 0) {
// prepare input to issue auxiliary dimensions for batch
auto unsqueeze_axes_const = make_shared<Constant>(element::i32, Shape{unsqueeze_axes.size()}, unsqueeze_axes);
batched_input = make_shared<Unsqueeze>(input, unsqueeze_axes_const);
auto unsqueeze_axes_const =
make_shared<v0::Constant>(element::i32, Shape{unsqueeze_axes.size()}, unsqueeze_axes);
batched_input = make_shared<v0::Unsqueeze>(input, unsqueeze_axes_const);
}

auto input_shape = make_shared<ShapeOf>(batched_input, element::i32);
auto input_shape = make_shared<v3::ShapeOf>(batched_input, element::i32);
auto seq_lenghts = compute_sequence_lengths(input_shape, batch_axis, seq_axis);
auto reverse_sequence = make_shared<ReverseSequence>(batched_input, seq_lenghts, batch_axis, seq_axis)->output(0);
auto reverse_sequence =
make_shared<v0::ReverseSequence>(batched_input, seq_lenghts, batch_axis, seq_axis)->output(0);

if (unsqueeze_axes.size() > 0) {
// remove earlier added additional dimensions from the result
auto squeeze_axes_const = make_shared<Constant>(element::i32, Shape{unsqueeze_axes.size()}, unsqueeze_axes);
reverse_sequence = make_shared<Squeeze>(reverse_sequence, squeeze_axes_const);
auto squeeze_axes_const = make_shared<v0::Constant>(element::i32, Shape{unsqueeze_axes.size()}, unsqueeze_axes);
reverse_sequence = make_shared<v0::Squeeze>(reverse_sequence, squeeze_axes_const);
}

set_node_name(node.get_name(), reverse_sequence.get_node_shared_ptr());
Expand Down
7 changes: 4 additions & 3 deletions src/frontends/tensorflow_common/src/op/reverse_sequence.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
// SPDX-License-Identifier: Apache-2.0
//

#include "openvino/op/reverse_sequence.hpp"

#include "common_op_table.hpp"
#include "openvino/opsets/opset8.hpp"

using namespace std;
using namespace ov::opset8;
using namespace ov::op;

namespace ov {
namespace frontend {
Expand All @@ -21,7 +22,7 @@ OutputVector translate_reverse_sequence_op(const NodeContext& node) {
auto seq_dim = node.get_attribute<int64_t>("seq_dim");
auto batch_dim = node.get_attribute<int64_t>("batch_dim", 0);

auto reverse_sequence = make_shared<ReverseSequence>(input, seq_lengths, batch_dim, seq_dim);
auto reverse_sequence = make_shared<v0::ReverseSequence>(input, seq_lengths, batch_dim, seq_dim);
set_node_name(node.get_name(), reverse_sequence);
return {reverse_sequence};
}
Expand Down
9 changes: 5 additions & 4 deletions src/frontends/tensorflow_common/src/op/scatter_nd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
//

#include "common_op_table.hpp"
#include "openvino/opsets/opset8.hpp"
#include "openvino/op/broadcast.hpp"
#include "openvino/op/scatter_nd_update.hpp"
#include "utils.hpp"

using namespace std;
using namespace ov::opset8;
using namespace ov::op;

namespace ov {
namespace frontend {
Expand All @@ -20,8 +21,8 @@ OutputVector translate_scatter_nd_op(const NodeContext& node) {
auto shape = node.get_input(2);

auto input_data = create_same_type_const<int32_t>(updates, vector<int32_t>{0}, Shape{1});
auto broadcast = make_shared<opset8::Broadcast>(input_data, shape);
auto scatter_nd = make_shared<opset8::ScatterNDUpdate>(broadcast, input_indices, updates);
auto broadcast = make_shared<v3::Broadcast>(input_data, shape);
auto scatter_nd = make_shared<v3::ScatterNDUpdate>(broadcast, input_indices, updates);
set_node_name(node.get_name(), scatter_nd);
return {scatter_nd};
}
Expand Down
26 changes: 16 additions & 10 deletions src/frontends/tensorflow_common/src/op/segment_sum.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,18 @@
//

#include "common_op_table.hpp"
#include "openvino/opsets/opset9.hpp"
#include "openvino/op/add.hpp"
#include "openvino/op/constant.hpp"
#include "openvino/op/embedding_segments_sum.hpp"
#include "openvino/op/range.hpp"
#include "openvino/op/reduce_max.hpp"
#include "openvino/op/shape_of.hpp"
#include "openvino/op/squeeze.hpp"
#include "utils.hpp"

using namespace std;
using namespace ov;
using namespace ov::opset9;
using namespace ov::op;

namespace ov {
namespace frontend {
Expand All @@ -28,18 +34,18 @@ OutputVector translate_segment_sum_op(const NodeContext& node) {
auto indices_type = segment_ids.get_element_type();
// 1. compute a number of segments using segment_ids values
// do not forget that segment ids are counting from zero
auto reduction_axis = make_shared<Constant>(element::i32, Shape{1}, 0);
auto num_segments_minus1 = make_shared<ReduceMax>(segment_ids, reduction_axis, false);
auto num_segments = make_shared<Add>(num_segments_minus1, const_one);
auto reduction_axis = make_shared<v0::Constant>(element::i32, Shape{1}, 0);
auto num_segments_minus1 = make_shared<v1::ReduceMax>(segment_ids, reduction_axis, false);
auto num_segments = make_shared<v1::Add>(num_segments_minus1, const_one);

// 2. generate indices input for EmbeddingSegmentSum
// that will collect slices consequently from data for each segment
auto squeeze_axis = make_shared<Constant>(element::i32, Shape{1}, 0);
auto segment_ids_shape = make_shared<ShapeOf>(segment_ids, indices_type);
auto num_indices = make_shared<Squeeze>(segment_ids_shape, squeeze_axis);
auto indices = make_shared<Range>(const_zero, num_indices, const_one, indices_type);
auto squeeze_axis = make_shared<v0::Constant>(element::i32, Shape{1}, 0);
auto segment_ids_shape = make_shared<v3::ShapeOf>(segment_ids, indices_type);
auto num_indices = make_shared<v0::Squeeze>(segment_ids_shape, squeeze_axis);
auto indices = make_shared<v4::Range>(const_zero, num_indices, const_one, indices_type);

auto emb_segment_sum = make_shared<EmbeddingSegmentsSum>(data, indices, segment_ids, num_segments);
auto emb_segment_sum = make_shared<v3::EmbeddingSegmentsSum>(data, indices, segment_ids, num_segments);
set_node_name(node.get_name(), emb_segment_sum);
return {emb_segment_sum};
}
Expand Down
Loading

0 comments on commit 9f87f72

Please sign in to comment.