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 #ifndef TENSORFLOW_COMPILER_XLA_SERVICE_INTERPRETER_EXECUTABLE_H_
17 #define TENSORFLOW_COMPILER_XLA_SERVICE_INTERPRETER_EXECUTABLE_H_
18 
19 #include <memory>
20 
21 #include "absl/types/span.h"
22 #include "tensorflow/compiler/xla/service/executable.h"
23 #include "tensorflow/compiler/xla/service/hlo_cost_analysis.h"
24 #include "tensorflow/compiler/xla/service/hlo_evaluator.h"
25 #include "tensorflow/compiler/xla/service/hlo_execution_profile.h"
26 #include "tensorflow/compiler/xla/service/hlo_module.h"
27 #include "tensorflow/compiler/xla/service/hlo_module_config.h"
28 #include "tensorflow/compiler/xla/service/service_executable_run_options.h"
29 #include "tensorflow/compiler/xla/service/shaped_buffer.h"
30 #include "tensorflow/compiler/xla/statusor.h"
31 #include "tensorflow/compiler/xla/types.h"
32 #include "tensorflow/compiler/xla/xla_data.pb.h"
33 #include "tensorflow/core/platform/macros.h"
34 #include "tensorflow/core/platform/mutex.h"
35 #include "tensorflow/core/platform/stream_executor_no_cuda.h"
36 #include "tensorflow/core/platform/types.h"
37 
38 namespace xla {
39 namespace interpreter {
40 
41 // Responsible for running a HLO graph through the HloEvaluator and output
42 // buffer allocation. Refer to interpreter/README.md for more.
43 class InterpreterExecutable : public Executable {
44  public:
45   InterpreterExecutable(std::unique_ptr<HloModule> hlo_module,
46                         std::unique_ptr<HloEvaluator> evaluator);
47   ~InterpreterExecutable() override;
48 
49   StatusOr<ScopedShapedBuffer> ExecuteOnStream(
50       const ServiceExecutableRunOptions* run_options,
51       absl::Span<const ShapedBuffer* const> arguments,
52       HloExecutionProfile* hlo_execution_profile) override
53       LOCKS_EXCLUDED(evaluator_lock_);
54 
55   StatusOr<ScopedShapedBuffer> ExecuteAsyncOnStream(
56       const ServiceExecutableRunOptions* run_options,
57       absl::Span<const ShapedBuffer* const> arguments) override;
58 
59   static int64 ShapeSizeBytes(const Shape& shape);
60 
61  protected:
62   // The interpreter interprets executables with an HloEvaluator.
63   std::unique_ptr<HloEvaluator> evaluator_ PT_GUARDED_BY(evaluator_lock_);
64   mutable tensorflow::mutex evaluator_lock_;
65 
66  private:
67   TF_DISALLOW_COPY_AND_ASSIGN(InterpreterExecutable);
68 };
69 
70 }  // namespace interpreter
71 }  // namespace xla
72 
73 #endif  // TENSORFLOW_COMPILER_XLA_SERVICE_INTERPRETER_EXECUTABLE_H_
74