tensorflow/core/tpu/graph_rewrite/tpu_embedding_software_deduplication_rewrite_pass.cc
// Constructs a NodeDef proto for the _RecvTPUEmbeddingDeduplicationData node to
// be added to the graph or function def.
Constructs a NodeDef proto for the _RecvTPUEmbeddingDeduplicationData node to
be added to the graph or function def.
Constructs a NodeDef proto for the _RecvTPUEmbeddingDeduplicationData node to be added to the graph or function def .
xla::StatusOr<NodeDef> MakeSendGradientsNodeDef(
    const NodeDef& old_gradients_node_def,
    absl::string_view deduplication_data_node_name,
    absl::string_view device_name, absl::string_view tpu_replicate_attr,
    absl::string_view tpu_embedding_config_str,
    absl::Span<const NodeDefBuilder::NodeOut> data_inputs,
    absl::Span<const std::string> control_inputs) {
  tpu::TPUEmbeddingConfiguration tpu_embedding_config;
  if (!tpu_embedding_config.ParseFromString(
          std::string(tpu_embedding_config_str))) {  
    return errors::InvalidArgument(
        "Malformed config attribute in the SendTPUEmbeddingGradients node.");
  }
  int32 num_inputs;
  TF_RETURN_IF_ERROR(
      GetNodeAttr(AttrSlice(old_gradients_node_def), "N", &num_inputs));
  TF_RETURN_IF_ERROR(CheckNumInputsOrOutputs(num_inputs, "num_inputs",
                                             "SendTPUEmbeddingGradients",
                                             tpu_embedding_config));
  int32 learning_rate_tag_count = 0;
  if (!GetNodeAttr(AttrSlice(old_gradients_node_def), "NN",
                   &learning_rate_tag_count)
           .ok()) {
    LOG(INFO)
        << "Missing the NN attribute (number of dynamic learning rate tags) in "
           "the SendTPUEmbeddingGradients node. Setting the value to 0.";
  }
  auto status_or_lr_tag_count =
      tpu::ComputeTotalTagCountForDynamicLearningRates(tpu_embedding_config);
  if (!status_or_lr_tag_count.ok()) {
    return errors::InvalidArgument(status_or_lr_tag_count.status().message());
  }
  const int32 expected_learning_rate_tag_count = status_or_lr_tag_count.value();
  if (learning_rate_tag_count != expected_learning_rate_tag_count) {
    return errors::InvalidArgument(absl::StrFormat(
        "Number of dynamic learning rate tags in the TPU embedding config: %d "
        "does not match the NN attribute: %d in the SendTPUEmbeddingGradients "
        "node.",
        expected_learning_rate_tag_count, learning_rate_tag_count));
  }
  if (data_inputs.size() !=
      static_cast<uint64>(num_inputs + learning_rate_tag_count)) {
    return errors::InvalidArgument(absl::StrFormat(
        "Mismatch in the number of inputs for SendTPUEmbeddingGradients node, "
        "expected: %d, actual: %d",
        num_inputs + learning_rate_tag_count, data_inputs.size()));
  }
  NodeDefBuilder builder(old_gradients_node_def.name(),
                         "_SendTPUEmbeddingGradients");
  if (!device_name.empty()) {
    builder.Device(device_name);
  }
  builder.Attr("NumTables", num_inputs)
      .Attr("NumLearningRateTags", learning_rate_tag_count)
      .Attr("config", tpu_embedding_config_str);
  if (!tpu_replicate_attr.empty()) {
    builder.Attr("_tpu_replicate", tpu_replicate_attr);
  }
  std::string embedding_layer;
  if (TryGetNodeAttr(AttrSlice(old_gradients_node_def), "_tpu_embedding_layer",
                     &embedding_layer)) {
    builder.Attr("_tpu_embedding_layer", embedding_layer);
  }
  builder.Input(absl::MakeConstSpan(data_inputs.data(), num_inputs))
      .Input(absl::MakeConstSpan(data_inputs.data() + num_inputs,
                                 learning_rate_tag_count))
      .Input(absl::StrCat(deduplication_data_node_name, ":output"),
             0, DT_VARIANT);
  for (const std::string& control_input : control_inputs) {
    builder.ControlInput(control_input);
  }
  NodeDef gradients_node_def;
  TF_RETURN_IF_ERROR(builder.Finalize(&gradients_node_def));
  *gradients_node_def.mutable_experimental_debug_info() =
      old_gradients_node_def.experimental_debug_info();
  VLOG(1) << "Created new _SendTPUEmbeddingGradients node def: "
          << gradients_node_def.DebugString();
  return gradients_node_def;
}
xla :: StatusOr < NodeDef > MakeSendGradientsNodeDef ( const NodeDef & old_gradients_node_def , absl :: string_view deduplication_data_node_name , absl :: string_view device_name , absl :: string_view tpu_replicate_attr , absl :: string_view tpu_embedding_config_str , absl :: Span < const NodeDefBuilder :: NodeOut > data_inputs , absl :: Span < const std :: string > control_inputs ) { tpu :: TPUEmbeddingConfiguration tpu_embedding_config ; if ( ! tpu_embedding_config . ParseFromString ( std :: string ( tpu_embedding_config_str ) ) ) { return errors :: InvalidArgument ( " " ) ; } int32 num_inputs ; TF_RETURN_IF_ERROR ( GetNodeAttr ( AttrSlice ( old_gradients_node_def ) , " " , & num_inputs ) ) ; TF_RETURN_IF_ERROR ( CheckNumInputsOrOutputs ( num_inputs , " " , " " , tpu_embedding_config ) ) ; int32 learning_rate_tag_count = 0 ; if ( ! GetNodeAttr ( AttrSlice ( old_gradients_node_def ) , " " , & learning_rate_tag_count ) . ok ( ) ) { LOG ( INFO ) << " " " " ; } auto status_or_lr_tag_count = tpu :: ComputeTotalTagCountForDynamicLearningRates ( tpu_embedding_config ) ; if ( ! status_or_lr_tag_count . ok ( ) ) { return errors :: InvalidArgument ( status_or_lr_tag_count . status ( ) . message ( ) ) ; } const int32 expected_learning_rate_tag_count = status_or_lr_tag_count . value ( ) ; if ( learning_rate_tag_count != expected_learning_rate_tag_count ) { return errors :: InvalidArgument ( absl :: StrFormat ( " " " " " " , expected_learning_rate_tag_count , learning_rate_tag_count ) ) ; } if ( data_inputs . size ( ) != static_cast < uint64 > ( num_inputs + learning_rate_tag_count ) ) { return errors :: InvalidArgument ( absl :: StrFormat ( " " " " , num_inputs + learning_rate_tag_count , data_inputs . size ( ) ) ) ; } NodeDefBuilder builder ( old_gradients_node_def . name ( ) , " " ) ; if ( ! device_name . empty ( ) ) { builder . Device ( device_name ) ; } builder . Attr ( " " , num_inputs ) . Attr ( " " , learning_rate_tag_count ) . Attr ( " " , tpu_embedding_config_str ) ; if ( ! tpu_replicate_attr . empty ( ) ) { builder . Attr ( " " , tpu_replicate_attr ) ; } std :: string embedding_layer ; if ( TryGetNodeAttr ( AttrSlice ( old_gradients_node_def ) , " " , & embedding_layer ) ) { builder . Attr ( " " , embedding_layer ) ; } builder . Input ( absl :: MakeConstSpan ( data_inputs . data ( ) , num_inputs ) ) . Input ( absl :: MakeConstSpan ( data_inputs . data ( ) + num_inputs , learning_rate_tag_count ) ) . Input ( absl :: StrCat ( deduplication_data_node_name , " " ) , 0 , DT_VARIANT ) ; for ( const std :: string & control_input : control_inputs ) { builder . ControlInput ( control_input ) ; } NodeDef gradients_node_def ; TF_RETURN_IF_ERROR ( builder . Finalize ( & gradients_node_def ) ) ; * gradients_node_def . mutable_experimental_debug_info ( ) = old_gradients_node_def . experimental_debug_info ( ) ; VLOG ( 1 ) << " " << gradients_node_def . DebugString ( ) ; return gradients_node_def ; }
Constructs a NodeDef proto for the _RecvTPUEmbeddingDeduplicationData node to
be added to the graph or function def.
Constructs a NodeDef proto for the _RecvTPUEmbeddingDeduplicationData node to be added to the graph or function def .
// NOLINT // The Numtables here can be interpreted as num features if the feature //  descriptor is present in the config. /*src_index=*/