embedding_segments_sum.hpp
1 // Copyright (C) 2018-2021 Intel Corporation
2 // SPDX-License-Identifier: Apache-2.0
3 //
4 
5 #pragma once
6 
7 #include "ngraph/axis_set.hpp"
8 #include "ngraph/op/util/index_reduction.hpp"
9 
10 namespace ngraph
11 {
12  namespace op
13  {
14  namespace v3
15  {
16  /// \brief Returns embeddings for given indices
17  class NGRAPH_API EmbeddingSegmentsSum : public Op
18  {
19  public:
20  static constexpr NodeTypeInfo type_info{"EmbeddingSegmentsSum", 3};
21  const NodeTypeInfo& get_type_info() const override { return type_info; }
22  /// \brief Constructs a EmbeddingSegmentsSum operation.
23  EmbeddingSegmentsSum() = default;
24  /// \brief Constructs a EmbeddingSegmentsSum operation.
25  ///
26  /// EmbeddingSegmentsSum constructs an output tensor by replacing every index in a
27  /// given
28  /// input tensor with a row (from the weights matrix) at that index
29  ///
30  /// \param 'emb_table' tensor containing the embedding lookup table of the module of
31  /// shape [num_emb, emb_dim1, emb_dim2, ...] and of type T
32  /// \param 'indices' tensor of shape [num_indices] and of type T_IND. Required
33  /// \param `segment_ids` tensor of shape `[num_indices]` and of type *T_IND* with
34  /// indices
35  /// into the output Tensor. Values should be sorted and can be repeated. Required.
36  /// \param `num_segments` scalar of type *T_IND* indicating the number of segments.
37  /// Required.
38  /// \param 'default_index' scalar of type T_IND containing default index in
39  /// embedding
40  /// table to fill empty "bags". If not provided empty "bags"
41  /// are filled with zeros. Optional.
42  /// \param 'per_sample_weights' tensor of the same shape as indices and of type T.
43  /// Each value in this tensor are multiplied with each
44  /// value pooled from embedding table for each index. Optional.
45 
47  const Output<Node>& indices,
48  const Output<Node>& segment_ids,
49  const Output<Node>& num_segments,
50  const Output<Node>& default_index,
51  const Output<Node>& per_sample_weights);
52 
53  EmbeddingSegmentsSum(const Output<Node>& emb_table,
54  const Output<Node>& indices,
55  const Output<Node>& segment_ids,
56  const Output<Node>& num_segments,
57  const Output<Node>& default_index);
58 
59  EmbeddingSegmentsSum(const Output<Node>& emb_table,
60  const Output<Node>& indices,
61  const Output<Node>& segment_ids,
62  const Output<Node>& num_segments);
63 
64  void validate_and_infer_types() override;
65 
66  virtual std::shared_ptr<Node>
67  clone_with_new_inputs(const OutputVector& new_args) const override;
68 
69  virtual bool visit_attributes(AttributeVisitor& visitor) override { return true; }
70 
71  private:
72  static constexpr int EMB_TABLE = 0;
73  static constexpr int INDICES = 1;
74  static constexpr int SEGMENT_IDS = 2;
75  static constexpr int NUM_SEGMENTS = 3;
76  static constexpr int DEFAULT_INDEX = 4;
77  static constexpr int PER_SAMPLE_WEIGHTS = 5;
78  };
79  } // namespace v3
80  using v3::EmbeddingSegmentsSum;
81  } // namespace op
82 } // namespace ngraph
Visits the attributes of a node, primarily for serialization-like tasks.
Definition: attribute_visitor.hpp:59
A handle for one of a node's outputs.
Definition: node_output.hpp:33
Root of all actual ops.
Definition: op.hpp:17
Returns embeddings for given indices.
Definition: embedding_segments_sum.hpp:18
void validate_and_infer_types() override
Verifies that attributes and inputs are consistent and computes output shapes and element types....
EmbeddingSegmentsSum()=default
Constructs a EmbeddingSegmentsSum operation.
EmbeddingSegmentsSum(const Output< Node > &emb_table, const Output< Node > &indices, const Output< Node > &segment_ids, const Output< Node > &num_segments, const Output< Node > &default_index, const Output< Node > &per_sample_weights)
Constructs a EmbeddingSegmentsSum operation.
const NodeTypeInfo & get_type_info() const override
Definition: embedding_segments_sum.hpp:21
The Intel nGraph C++ API.
Definition: attribute_adapter.hpp:16
Definition: type.hpp:27