• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <memory>
21 #include <string>
22 #include <utility>
23 #include <vector>
24 
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/executable_base.h"
29 #include "tensorflow/compiler/xla/service/interpreter/executor.h"
30 #include "tensorflow/compiler/xla/service/maybe_owning_device_memory.h"
31 #include "tensorflow/compiler/xla/service/transfer_manager.h"
32 #include "tensorflow/compiler/xla/shape_util.h"
33 #include "tensorflow/compiler/xla/status_macros.h"
34 #include "tensorflow/core/lib/core/errors.h"
35 #include "tensorflow/core/platform/env.h"
36 #include "tensorflow/core/platform/stream_executor_no_cuda.h"
37 
38 namespace xla {
39 namespace interpreter {
40 
InterpreterExecutable(std::unique_ptr<HloModule> hlo_module,std::unique_ptr<HloEvaluator> evaluator,std::optional<DynamicDimensionInference> dynamic_dymension_inference)41 InterpreterExecutable::InterpreterExecutable(
42     std::unique_ptr<HloModule> hlo_module,
43     std::unique_ptr<HloEvaluator> evaluator,
44     std::optional<DynamicDimensionInference> dynamic_dymension_inference)
45     : InterpreterExecutableBase(std::move(hlo_module)),
46       evaluator_(std::move(evaluator)),
47       dynamic_dimension_inference_(std::move(dynamic_dymension_inference)) {
48   if (dynamic_dimension_inference_.has_value()) {
49     evaluator_->set_dynamic_dimension_inference(
50         &dynamic_dimension_inference_.value());
51   }
52 }
53 
Evaluate(const ServiceExecutableRunOptions * run_options,const HloComputation & computation,absl::Span<const Literal> arg_literals)54 StatusOr<Literal> InterpreterExecutable::Evaluate(
55     const ServiceExecutableRunOptions* run_options,
56     const HloComputation& computation, absl::Span<const Literal> arg_literals) {
57   // Execute the graph using the HloEvaluator.
58   absl::MutexLock lock(&evaluator_lock_);
59   evaluator_->ResetVisitStates();
60   return evaluator_->Evaluate(computation, arg_literals);
61 }
62 
ShapeSizeBytes(const Shape & shape)63 /*static*/ int64_t InterpreterExecutable::ShapeSizeBytes(const Shape& shape) {
64   if (shape.IsOpaque()) {
65     return sizeof(void*);
66   }
67   return ShapeUtil::ByteSizeOf(shape, sizeof(void*));
68 }
69 
70 }  // namespace interpreter
71 }  // namespace xla
72