Home | History | Annotate | Download | only in service
      1 /* Copyright 2017 The TensorFlow Authors. All Rights Reserved.
      2 
      3 Licensed under the Apache License, Version 2.0 (the "License");
      4 you may not use this file except in compliance with the License.
      5 You may obtain a copy of the License at
      6 
      7     http://www.apache.org/licenses/LICENSE-2.0
      8 
      9 Unless required by applicable law or agreed to in writing, software
     10 distributed under the License is distributed on an "AS IS" BASIS,
     11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     12 See the License for the specific language governing permissions and
     13 limitations under the License.
     14 ==============================================================================*/
     15 
     16 #include "tensorflow/compiler/xla/service/compile_only_service.h"
     17 
     18 #include <string>
     19 #include <utility>
     20 #include <vector>
     21 
     22 #include "tensorflow/compiler/xla/legacy_flags/debug_options_flags.h"
     23 #include "tensorflow/compiler/xla/service/backend.h"
     24 #include "tensorflow/compiler/xla/service/computation_layout.h"
     25 #include "tensorflow/compiler/xla/service/computation_tracker.h"
     26 #include "tensorflow/compiler/xla/service/platform_util.h"
     27 #include "tensorflow/compiler/xla/status_macros.h"
     28 #include "tensorflow/compiler/xla/types.h"
     29 #include "tensorflow/compiler/xla/util.h"
     30 #include "tensorflow/core/lib/gtl/cleanup.h"
     31 #include "tensorflow/core/lib/io/path.h"
     32 #include "tensorflow/core/lib/strings/strcat.h"
     33 #include "tensorflow/core/platform/host_info.h"
     34 #include "tensorflow/core/platform/logging.h"
     35 #include "tensorflow/core/platform/stream_executor_no_cuda.h"
     36 
     37 namespace xla {
     38 
     39 /* static */ StatusOr<std::unique_ptr<CompileOnlyService>>
     40 CompileOnlyService::NewService(perftools::gputools::Platform* platform) {
     41   ServiceOptions default_options;
     42   default_options.set_platform(platform);
     43   return NewService(default_options);
     44 }
     45 
     46 /* static */ StatusOr<std::unique_ptr<CompileOnlyService>>
     47 CompileOnlyService::NewService(const ServiceOptions& options) {
     48   perftools::gputools::Platform* platform = options.platform();
     49   if (platform == nullptr) {
     50     TF_ASSIGN_OR_RETURN(platform, PlatformUtil::GetDefaultPlatform());
     51   }
     52 
     53   TF_ASSIGN_OR_RETURN(auto compiler, Compiler::GetForPlatform(platform));
     54 
     55   std::unique_ptr<CompileOnlyService> service(
     56       new CompileOnlyService(options, compiler));
     57   return std::move(service);
     58 }
     59 
     60 CompileOnlyService::CompileOnlyService(const ServiceOptions& options,
     61                                        Compiler* compiler)
     62     : Service(options, /*execute_backend=*/nullptr), compiler_(compiler) {}
     63 
     64 StatusOr<std::vector<std::unique_ptr<AotCompilationResult>>>
     65 CompileOnlyService::CompileAheadOfTime(
     66     const tensorflow::gtl::ArraySlice<AotComputationInstance> computations,
     67     const AotCompilationOptions& options) {
     68   std::vector<std::unique_ptr<HloModule>> hlo_modules;
     69   for (const AotComputationInstance& instance : computations) {
     70     TF_ASSIGN_OR_RETURN(UserComputation * user_computation,
     71                         computation_tracker_.Resolve(instance.computation));
     72     VersionedComputationHandle versioned_handle =
     73         user_computation->GetVersionedHandle();
     74 
     75     // TODO(b/63773457): Track DebugOptions in AotCompilationOptions.
     76     DebugOptions debug_options = legacy_flags::GetDebugOptionsFromFlags();
     77 
     78     // Dump computation proto state if flag is set.
     79     const string& directory_path = debug_options.xla_dump_computations_to();
     80     if (!directory_path.empty()) {
     81       TF_ASSIGN_OR_RETURN(
     82           std::unique_ptr<SessionModule> session_module,
     83           computation_tracker_.SnapshotComputation(versioned_handle.handle));
     84       string filename = tensorflow::strings::StrCat(
     85           "computation_", versioned_handle.handle.handle(), "__",
     86           session_module->entry().name(), "__version_",
     87           versioned_handle.version);
     88       const string& per_host_path = tensorflow::io::JoinPath(
     89           directory_path, tensorflow::port::Hostname());
     90 
     91       TF_RETURN_IF_ERROR(Executable::DumpToDirectory(per_host_path, filename,
     92                                                      *session_module));
     93     }
     94 
     95     TF_ASSIGN_OR_RETURN(
     96         std::shared_ptr<const ProgramShape> program_shape,
     97         user_computation->ComputeProgramShape(versioned_handle.version));
     98 
     99     ExecutionOptions execution_options;
    100     *execution_options.mutable_debug_options() = debug_options;
    101     TF_ASSIGN_OR_RETURN(
    102         std::unique_ptr<HloModuleConfig> module_config,
    103         CreateModuleConfig(*program_shape, instance.argument_layouts,
    104                            &execution_options, *user_computation));
    105 
    106     TF_ASSIGN_OR_RETURN(std::unique_ptr<HloModule> hlo_module,
    107                         computation_tracker_.BuildHloModule(
    108                             versioned_handle, *module_config,
    109                             /*include_unreachable_instructions=*/true));
    110     TF_RETURN_IF_ERROR(MaybeDumpHloModule(*hlo_module));
    111     hlo_modules.push_back(std::move(hlo_module));
    112   }
    113 
    114   return compiler_->CompileAheadOfTime(std::move(hlo_modules), options);
    115 }
    116 
    117 }  // namespace xla
    118