aboutsummaryrefslogtreecommitdiffhomepage
path: root/tensorflow/compiler/plugin/executor/executable.cc
blob: 38479d688d76b03326ebec2d0a13bd554089ad8b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/* 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/plugin/executor/executable.h"
#include "tensorflow/compiler/plugin/executor/executor.h"
#include "tensorflow/compiler/xla/literal_util.h"
#include "tensorflow/compiler/xla/service/hlo_evaluator.h"
#include "tensorflow/compiler/xla/shape_util.h"

namespace xla {
namespace executorplugin {

namespace se = ::perftools::gputools;
namespace sep = ::perftools::gputools::executorplugin;

ExecutorExecutable::ExecutorExecutable(std::unique_ptr<HloModule> hlo_module)
    : Executable(std::move(hlo_module), ShapeSizeBytes) {}

ExecutorExecutable::~ExecutorExecutable() {}

static se::DeviceMemoryBase AllocateSingleOutput(
    sep::ExecutorExecutor* executor, const Literal& literal) {
  int64 size(xla::ShapeUtil::ByteSizeOf(literal.shape()));
  void* buf = executor->Allocate(size);
  const void* src = literal.InternalData();
  memcpy(buf, src, size);
  return se::DeviceMemoryBase(buf, size);
}

static se::DeviceMemoryBase AllocateOutputBuffer(
    sep::ExecutorExecutor* executor, const Literal& literal) {
  const Shape& shape = literal.shape();
  if (shape.element_type() != xla::TUPLE) {
    return AllocateSingleOutput(executor, literal);
  } else {
    int64 size(xla::ShapeUtil::ByteSizeOf(shape, sizeof(void*)));
    void** buf = reinterpret_cast<void**>(executor->Allocate(size));
    for (int64 n = 0; n < xla::ShapeUtil::TupleElementCount(shape); n++) {
      se::DeviceMemoryBase out =
          AllocateSingleOutput(executor, literal.tuple_literals(n));
      *buf++ = out.opaque();
    }

    return se::DeviceMemoryBase(buf, size);
  }
}

StatusOr<se::DeviceMemoryBase> ExecutorExecutable::ExecuteOnStream(
    const ServiceExecutableRunOptions* run_options,
    tensorflow::gtl::ArraySlice<se::DeviceMemoryBase> arguments,
    HloExecutionProfile* hlo_execution_profile) {
  se::Stream* stream = run_options->stream();

  VLOG(1) << "Execute " << module().name();
  if (VLOG_IS_ON(2)) {
    for (const auto& a : arguments) {
      VLOG(2) << "-- argument " << a.opaque();
    }
  }

  uint64 start_micros = tensorflow::Env::Default()->NowMicros();

  HloComputation* computation = module().entry_computation();
  if (computation->num_parameters() != arguments.size()) {
    return tensorflow::errors::Internal(
        "Mismatch between argument count and graph parameter count.");
  }

  // Create the arguments as an vector of XLA literals
  std::vector<std::unique_ptr<Literal>> arg_literals;
  std::vector<Literal*> arg_literals_ptrs;
  for (int64 p = 0; p < computation->num_parameters(); p++) {
    // Create the input literal for the parameter
    HloInstruction* param = computation->parameter_instruction(p);
    arg_literals.emplace_back(Literal::CreateFromShape(param->shape()));
    arg_literals_ptrs.push_back(arg_literals.back().get());

    // Copy in the data from the stream_executor buffers
    void* buffer = arg_literals.back()->MutableInternalData();
    memcpy(buffer, arguments[p].opaque(),
           ShapeUtil::ByteSizeOf(param->shape()));
  }

  // Execute the graph using the evaluator
  HloEvaluator evaluator;
  TF_ASSIGN_OR_RETURN(std::unique_ptr<Literal> output,
                      evaluator.Evaluate(*computation, arg_literals_ptrs));

  // Copy the result into the return buffer
  perftools::gputools::StreamExecutor* executor(stream->parent());
  sep::ExecutorExecutor* executorExecutor(
      static_cast<sep::ExecutorExecutor*>(executor->implementation()));

  se::DeviceMemoryBase ret =
      AllocateOutputBuffer(executorExecutor, *(output.get()));

  uint64 end_micros = tensorflow::Env::Default()->NowMicros();

  {
    tensorflow::mutex_lock lock(mutex_);
    const double nanoseconds = (end_micros - start_micros) * 1000.0;
    execution_profile_.set_compute_time_ns(std::max(nanoseconds, 1.0));
  }

  return ret;
}

StatusOr<std::unique_ptr<ShapedBuffer>> ExecutorExecutable::ExecuteOnStream(
    const ServiceExecutableRunOptions* run_options,
    tensorflow::gtl::ArraySlice<const ShapedBuffer*> arguments,
    HloExecutionProfile* hlo_execution_profile) {
  return tensorflow::errors::Unimplemented(
      "ExecuteOnStream is not yet supported on Executor.");
}

StatusOr<se::DeviceMemoryBase> ExecutorExecutable::ExecuteAsyncOnStream(
    const ServiceExecutableRunOptions* run_options,
    tensorflow::gtl::ArraySlice<se::DeviceMemoryBase> arguments) {
  return tensorflow::errors::Unimplemented(
      "ExecuteAsyncOnStream is not yet supported on Executor.");
}

/*static*/ int64 ExecutorExecutable::ShapeSizeBytes(const Shape& shape) {
  if (ShapeUtil::IsOpaque(shape)) {
    return sizeof(void*);
  }
  return ShapeUtil::ByteSizeOf(shape, sizeof(void*));
}

}  // namespace executorplugin
}  // namespace xla