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/interpreter/executable.h"
17 
18 #include <algorithm>
19 #include <cstring>
20 #include <string>
21 #include <utility>
22 #include <vector>
23 
24 #include "absl/memory/memory.h"
25 #include "tensorflow/compiler/xla/literal.h"
26 #include "tensorflow/compiler/xla/service/hlo_computation.h"
27 #include "tensorflow/compiler/xla/service/hlo_instruction.h"
28 #include "tensorflow/compiler/xla/service/interpreter/executor.h"
29 #include "tensorflow/compiler/xla/service/transfer_manager.h"
30 #include "tensorflow/compiler/xla/shape_util.h"
31 #include "tensorflow/compiler/xla/status_macros.h"
32 #include "tensorflow/core/lib/core/errors.h"
33 #include "tensorflow/core/platform/env.h"
34 #include "tensorflow/core/platform/stream_executor_no_cuda.h"
35 
36 namespace xla {
37 namespace interpreter {
38 
InterpreterExecutable(std::unique_ptr<HloModule> hlo_module,std::unique_ptr<HloEvaluator> evaluator)39 InterpreterExecutable::InterpreterExecutable(
40     std::unique_ptr<HloModule> hlo_module,
41     std::unique_ptr<HloEvaluator> evaluator)
42     : Executable(std::move(hlo_module), /*hlo_profile_printer=*/nullptr,
43                  /*hlo_profile_index_map=*/nullptr),
44       evaluator_(std::move(evaluator)) {}
45 
~InterpreterExecutable()46 InterpreterExecutable::~InterpreterExecutable() {}
47 
ExecuteOnStream(const ServiceExecutableRunOptions * run_options,absl::Span<const ShapedBuffer * const> arguments,HloExecutionProfile * hlo_execution_profile)48 StatusOr<ScopedShapedBuffer> InterpreterExecutable::ExecuteOnStream(
49     const ServiceExecutableRunOptions* run_options,
50     absl::Span<const ShapedBuffer* const> arguments,
51     HloExecutionProfile* hlo_execution_profile) {
52   se::Stream* stream = run_options->stream();
53   se::StreamExecutor* executor = stream->parent();
54   const se::Platform* platform = executor->platform();
55 
56   VLOG(1) << "Execute " << module().name();
57   if (VLOG_IS_ON(2)) {
58     for (const auto& a : arguments) {
59       VLOG(2) << "-- argument " << *a;
60     }
61   }
62 
63   uint64 start_micros = tensorflow::Env::Default()->NowMicros();
64 
65   const HloComputation* computation = module().entry_computation();
66   if (computation->num_parameters() != arguments.size()) {
67     return tensorflow::errors::Internal(
68         "Mismatch between argument count and graph parameter count.");
69   }
70 
71   // Check that the args have the right shape.
72   for (int64 i = 0; i < computation->num_parameters(); ++i) {
73     const auto& expected_shape = computation->parameter_instruction(i)->shape();
74     const auto& actual_shape = arguments[i]->on_device_shape();
75     if (!ShapeUtil::Equal(expected_shape, actual_shape)) {
76       return InvalidArgument(
77           "Shape mismatch on parameter %d.  Expected %s, but was %s.", i,
78           ShapeUtil::HumanString(expected_shape),
79           ShapeUtil::HumanString(actual_shape));
80     }
81   }
82 
83   TF_ASSIGN_OR_RETURN(TransferManager * transfer_manager,
84                       TransferManager::GetForPlatform(platform));
85 
86   // Transform the ShapedBuffer arguments into literals which the evaluator
87   // consumes.
88   std::vector<Literal> arg_literals;
89   for (int64 p = 0; p < computation->num_parameters(); ++p) {
90     TF_ASSIGN_OR_RETURN(Literal arg_literal,
91                         transfer_manager->TransferLiteralFromDevice(
92                             run_options->stream(), *arguments[p]));
93     arg_literals.push_back(std::move(arg_literal));
94   }
95 
96   // Execute the graph using the HloEvaluator.
97   Literal result_literal;
98   {
99     tensorflow::mutex_lock lock(evaluator_lock_);
100     evaluator_->ResetVisitStates();
101     TF_ASSIGN_OR_RETURN(result_literal,
102                         evaluator_->Evaluate(*computation, arg_literals));
103   }
104 
105   // Transform the result literal back into a ShapedBuffer.
106   TF_ASSIGN_OR_RETURN(ScopedShapedBuffer result,
107                       transfer_manager->AllocateScopedShapedBuffer(
108                           result_literal.shape(), run_options->allocator(),
109                           executor->device_ordinal()));
110   TF_RETURN_IF_ERROR(transfer_manager->TransferLiteralToDevice(
111       run_options->stream(), result_literal, result));
112 
113   uint64 end_micros = tensorflow::Env::Default()->NowMicros();
114 
115   {
116     tensorflow::mutex_lock lock(mutex_);
117     const double nanoseconds = (end_micros - start_micros) * 1000.0;
118     execution_profile_.set_compute_time_ns(std::max(nanoseconds, 1.0));
119   }
120 
121   return std::move(result);
122 }
123 
ExecuteAsyncOnStream(const ServiceExecutableRunOptions * run_options,absl::Span<const ShapedBuffer * const> arguments)124 StatusOr<ScopedShapedBuffer> InterpreterExecutable::ExecuteAsyncOnStream(
125     const ServiceExecutableRunOptions* run_options,
126     absl::Span<const ShapedBuffer* const> arguments) {
127   return tensorflow::errors::Unimplemented(
128       "ExecuteAsyncOnStream is not yet supported on Interpreter.");
129 }
130 
ShapeSizeBytes(const Shape & shape)131 /*static*/ int64 InterpreterExecutable::ShapeSizeBytes(const Shape& shape) {
132   if (shape.IsOpaque()) {
133     return sizeof(void*);
134   }
135   return ShapeUtil::ByteSizeOf(shape, sizeof(void*));
136 }
137 
138 }  // namespace interpreter
139 }  // namespace xla
140