<commit_before>/* Copyright 2017 The TensorFlow Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/

#include "tensorflow/compiler/xla/service/computation_tracker.h"

#include <list>
#include <string>
#include <utility>
#include <vector>

#include "tensorflow/compiler/xla/ptr_util.h"
#include "tensorflow/compiler/xla/service/hlo_computation.h"
#include "tensorflow/compiler/xla/status_macros.h"
#include "tensorflow/compiler/xla/types.h"
#include "tensorflow/compiler/xla/util.h"
#include "tensorflow/core/lib/strings/strcat.h"
#include "tensorflow/core/lib/strings/stringprintf.h"
#include "tensorflow/core/platform/logging.h"

using ::tensorflow::strings::Appendf;

namespace xla {

ComputationTracker::ComputationTracker() : next_computation_(1) {}

ComputationHandle ComputationTracker::NewComputation(
    const string& computation_name) {
  tensorflow::mutex_lock lock(computation_mutex_);
  ComputationHandle computation_handle;
  int64 handle_value = next_computation_++;
  computation_handle.set_handle(handle_value);
  opaque_to_computation_[handle_value] =
      MakeUnique<UserComputation>(computation_name, computation_handle);
  return computation_handle;
}

StatusOr<ComputationHandle> ComputationTracker::LoadSessionModule(
    const SessionModule& session_module) {
  tensorflow::mutex_lock lock(computation_mutex_);

  // For each embedded computation, create a new computation based on its
  // serialized data, and place the mapping from the old computation handle to
  // the new computation handle.
  std::map<int64, ComputationHandle> old_to_new;
  for (const SessionComputation& computation :
       session_module.embedded_computations()) {
    const int64 old_handle = computation.computation_handle().handle();
    TF_ASSIGN_OR_RETURN(old_to_new[old_handle],
                        LoadSessionComputation(computation, &old_to_new));
  }

  // Finally, place the entry computation in the tracker with all of the
  // remappings populated from the above.
  const int64 old_handle = session_module.entry().computation_handle().handle();
  TF_ASSIGN_OR_RETURN(
      old_to_new[old_handle],
      LoadSessionComputation(session_module.entry(), &old_to_new));
  return old_to_new[old_handle];
}

StatusOr<std::unique_ptr<SessionModule>>
ComputationTracker::SnapshotComputation(const ComputationHandle& computation) {
  TF_ASSIGN_OR_RETURN(UserComputation * user_computation, Resolve(computation));
  const VersionedComputationHandle entry_versioned_handle =
      user_computation->GetVersionedHandle();<commit_msg>[XLA] Fix crash when loading a serialized computation with dependencies between embedded computations. Change: 150233783<commit_after>/* Copyright 2017 The TensorFlow Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/

#include "tensorflow/compiler/xla/service/computation_tracker.h"

#include <list>
#include <string>
#include <utility>
#include <vector>

#include "tensorflow/compiler/xla/ptr_util.h"
#include "tensorflow/compiler/xla/service/hlo_computation.h"
#include "tensorflow/compiler/xla/status_macros.h"
#include "tensorflow/compiler/xla/types.h"
#include "tensorflow/compiler/xla/util.h"
#include "tensorflow/core/lib/strings/strcat.h"
#include "tensorflow/core/lib/strings/stringprintf.h"
#include "tensorflow/core/platform/logging.h"

using ::tensorflow::strings::Appendf;

namespace xla {

ComputationTracker::ComputationTracker() : next_computation_(1) {}

ComputationHandle ComputationTracker::NewComputation(
    const string& computation_name) {
  tensorflow::mutex_lock lock(computation_mutex_);
  ComputationHandle computation_handle;
  int64 handle_value = next_computation_++;
  computation_handle.set_handle(handle_value);
  opaque_to_computation_[handle_value] =
      MakeUnique<UserComputation>(computation_name, computation_handle);
  return computation_handle;
}

StatusOr<ComputationHandle> ComputationTracker::LoadSessionModule(
    const SessionModule& session_module) {
  tensorflow::mutex_lock lock(computation_mutex_);

  // For each embedded computation, create a new computation based on its
  // serialized data, and place the mapping from the old computation handle to
  // the new computation handle.

  // Build a mapping from old embedded computation handles to new computation
  // handles. We build the ID mapping first since the embedded computations are
  // in no particular order and may refer to each other.
  std::map<int64, ComputationHandle> old_to_new;
  for (const SessionComputation& computation :
       session_module.embedded_computations()) {
    const int64 old_handle = computation.computation_handle().handle();
    if (!old_to_new.emplace(old_handle, AllocateHandle()).second) {
      return InvalidArgument("Duplicate embedded computation handle %lld",
                             old_handle);
    }
  }

  // Create a new computation from each serialized embedded computation.
  for (const SessionComputation& computation :
       session_module.embedded_computations()) {
    const int64 old_handle = computation.computation_handle().handle();
    const ComputationHandle& new_handle = old_to_new[old_handle];
    TF_ASSIGN_OR_RETURN(opaque_to_computation_[new_handle.handle()],
                        UserComputation::MakeWithRemapping(
                            computation, new_handle, old_to_new));
  }

  // Finally, place the entry computation in the tracker with all of the
  // remappings populated from the above.
  const int64 old_handle = session_module.entry().computation_handle().handle();
  TF_ASSIGN_OR_RETURN(
      old_to_new[old_handle],
      LoadSessionComputation(session_module.entry(), &old_to_new));
  return old_to_new[old_handle];
}

StatusOr<std::unique_ptr<SessionModule>>
ComputationTracker::SnapshotComputation(const ComputationHandle& computation) {
  TF_ASSIGN_OR_RETURN(UserComputation * user_computation, Resolve(computation));
  const VersionedComputationHandle entry_versioned_handle =
      user_computation->GetVersionedHandle();
{'fraction_non_alphanumeric': 0.10703791800755562, 'fraction_numerical': 0.006296348118091507, 'mean_word_length': 5.410762331838565, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '976168', 'n_tokens_mistral': 1996, 'n_tokens_neox': 1939, 'n_words': 528}