aboutsummaryrefslogtreecommitdiffhomepage
path: root/tensorflow/compiler/xla/service/cpu/parallel_cpu_executable.cc
blob: f0af3e7b894af875c222b184873dcc4cc9e79b8f (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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
/* 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/xla/service/cpu/parallel_cpu_executable.h"

#include <stdint.h>
#include <algorithm>
#include <deque>
#include <iterator>
#include <list>
#include <unordered_set>
#include <utility>
#include <vector>

#include "external/llvm/include/llvm/ExecutionEngine/Orc/IRCompileLayer.h"
#include "tensorflow/compiler/xla/map_util.h"
#include "tensorflow/compiler/xla/service/buffer_assignment.h"
#include "tensorflow/compiler/xla/service/cpu/cpu_runtime.h"
#include "tensorflow/compiler/xla/service/cpu/shape_partition.h"
#include "tensorflow/compiler/xla/service/hlo_computation.h"
#include "tensorflow/compiler/xla/service/hlo_module.h"
#include "tensorflow/compiler/xla/service/hlo_opcode.h"
#include "tensorflow/compiler/xla/service/logical_buffer.h"
#include "tensorflow/compiler/xla/service/shaped_buffer.h"
#include "tensorflow/compiler/xla/shape_util.h"
#include "tensorflow/compiler/xla/status_macros.h"
#include "tensorflow/compiler/xla/types.h"
#include "tensorflow/compiler/xla/util.h"
#include "tensorflow/compiler/xla/xla_data.pb.h"
#include "tensorflow/core/lib/core/threadpool.h"
#include "tensorflow/core/lib/strings/str_util.h"
#include "tensorflow/core/lib/strings/strcat.h"
#include "tensorflow/core/lib/strings/stringprintf.h"
#include "tensorflow/core/platform/env.h"
#include "tensorflow/core/platform/logging.h"
#include "tensorflow/core/platform/mem.h"
#include "tensorflow/core/platform/mutex.h"
#include "tensorflow/core/platform/types.h"

namespace se = ::perftools::gputools;

namespace xla {
namespace cpu {

ParallelCpuExecutable::ParallelCpuExecutable(
    std::unique_ptr<SimpleOrcJIT> jit,
    std::unique_ptr<BufferAssignment> assignment,
    std::unique_ptr<HloModule> hlo_module,
    std::unique_ptr<std::map<HloInstruction*, string>> function_names,
    std::unordered_map<const HloInstruction*, size_t> hlo_to_profile_idx,
    std::unordered_map<const HloInstruction*, std::unique_ptr<unsigned char[]>>
        aligned_constants)
    : Executable(std::move(hlo_module), ParallelCpuExecutable::ShapeSizeBytes),
      jit_(std::move(jit)),
      assignment_(std::move(assignment)),
      functions_names_(std::move(function_names)),
      hlo_to_profile_idx_(std::move(hlo_to_profile_idx)),
      aligned_constants_(std::move(aligned_constants)) {}

// Type of the computation function we expect in the JIT.
using ComputeFunctionType = void (*)(void*, const void*, const void**, void**,
                                     int64*, uint64*);

// Given a pointer to an output buffer (following the CPU JIT calling
// conventions), mark addresses that are "live". The initial pointer itself is
// trivially live. If the shape of the buffer is a tuple, this analysis looks
// into the tuple's elements and marks them live as well (since tuples keep
// pointers to buffers) and also works recursively.
// address is an in-memory buffer address that contains some runtime XLA object.
// shape is its shape. marked_addresses is the set of live addresses to
// populate.
static void MarkLiveAddressesInOutput(
    const void* address, const Shape& shape,
    std::unordered_set<const void*>* marked_addresses) {
  marked_addresses->insert(address);
  const uintptr_t* address_buffer = static_cast<const uintptr_t*>(address);
  if (ShapeUtil::IsTuple(shape)) {
    for (int i = 0; i < ShapeUtil::TupleElementCount(shape); ++i) {
      const uintptr_t* element_address = address_buffer + i;
      const void* element = reinterpret_cast<const void*>(*element_address);
      MarkLiveAddressesInOutput(
          element, ShapeUtil::GetTupleElementShape(shape, i), marked_addresses);
    }
  }
}

namespace {

// Executor manages the concurrent execution of 'functions' for instructions
// in 'pending' on 'thread_pool' (storing resulting data in 'results').
class Executor {
 public:
  Executor(const std::map<HloInstruction*, ComputeFunctionType>& functions,
           const ServiceExecutableRunOptions* run_options,
           std::list<HloInstruction*>* pending,
           std::map<HloInstruction*, const void*>* results, void** temps_array,
           uint64* profile_counters_array, BufferAssignment* assignment)
      : functions_(functions),
        run_options_(run_options),
        pending_(pending),
        results_(results),
        temps_array_(temps_array),
        profile_counters_array_(profile_counters_array),
        thread_pool_(CHECK_NOTNULL(run_options_->xla_intra_op_thread_pool())),
        assignment_(assignment) {}

  // Executes pending list of instructions on thread pool.
  // Returns OK status on success, error status otherwise.
  Status Run();

 private:
  // Schedules a parallel invocation of compute function for 'instruction' on
  // 'thread_pool_', storing result in 'result_buffer'.
  // If 'partition_buffers' is non-null, parallel task will be invoked on
  // per-dimension partition [start, limit) values stored in
  // 'partition_buffers'.
  void Schedule(HloInstruction* instruction, int64* partition_buffers,
                void* result_buffer);

  // Returns true if 'instruction' has been assigned parallel tasks (returns
  // false otherwise).
  bool HasParallelTasks(HloInstruction* instruction);

  // Returns in 'partition_buffers' the partition [size, limit) for each
  // dimension.
  int64* GetPartitionBuffers(
      const std::vector<std::pair<int64, int64>>& partition);

  // Returns array of result buffers for all operands in 'instruction'.
  const void** GetOperandBuffers(HloInstruction* instruction);

  // Arguments passed into Executor.
  const std::map<HloInstruction*, ComputeFunctionType>& functions_;
  const ServiceExecutableRunOptions* run_options_;
  std::list<HloInstruction*>* pending_;
  std::map<HloInstruction*, const void*>* results_;
  void** temps_array_;
  uint64* profile_counters_array_;
  tensorflow::thread::ThreadPool* thread_pool_;
  BufferAssignment* assignment_;

  // Members used to manage instruction execution.
  tensorflow::mutex completion_queue_lock_;
  tensorflow::condition_variable completion_queue_cv_;
  std::deque<HloInstruction*> completion_queue_;
  int64 instructions_in_flight_ = 0;
  std::unordered_map<const HloInstruction*, int64> tasks_in_flight_;
};

Status Executor::Run() {
  while (!pending_->empty() || instructions_in_flight_ > 0) {
    auto pending_it = pending_->begin();
    while (pending_it != pending_->end()) {
      HloInstruction* instruction = *pending_it;
      // Skip pending instructions whose operands aren't ready.
      if (std::any_of(instruction->operands().begin(),
                      instruction->operands().end(),
                      [&](HloInstruction* operand) {
                        return !ContainsKey(*results_, operand);
                      })) {
        ++pending_it;
        continue;
      }

      // Get 'result_buffer' reference to result buffer for 'instruction'.
      TF_ASSIGN_OR_RETURN(const BufferAllocation::Slice result_slice,
                          assignment_->GetUniqueTopLevelSlice(instruction));
      void* result_buffer =
          static_cast<char*>(temps_array_[result_slice.index()]) +
          result_slice.offset();

      if (HasParallelTasks(instruction)) {
        // 'instruction' has been assigned parallel task partitions.
        CHECK_EQ(HloOpcode::kCall, instruction->opcode());
        HloInstruction* root = instruction->to_apply()->root_instruction();

        // Create ShapePartitionIterator to iterate through all outer dimension
        // partitions of 'instruction'.
        ShapePartitionIterator partition_iterator(
            root->shape(), root->outer_dimension_partitions());

        const int64 partition_count =
            partition_iterator.GetTotalPartitionCount();

        // Record total parallel task count for 'instruction' before dispatch.
        {
          tensorflow::mutex_lock l(completion_queue_lock_);
          tasks_in_flight_.insert(std::make_pair(instruction, partition_count));
          VLOG(2) << "Schedule PARALLEL"
                  << " instruction: " << instruction->name()
                  << " instruction.callee: "
                  << instruction->to_apply()->root_instruction()->name()
                  << " partition_count: " << partition_count;
        }

        for (int64 i = 0; i < partition_count; ++i) {
          // Get partition [start, limit) for each dimension.
          auto partition_buffers =
              GetPartitionBuffers(partition_iterator.GetPartition(i));
          Schedule(instruction, partition_buffers, result_buffer);
        }

      } else {
        // Set tasks in-flight to '1' for sequential instruction execution.
        {
          tensorflow::mutex_lock l(completion_queue_lock_);
          tasks_in_flight_.insert(std::make_pair(instruction, 1));
          VLOG(2) << "Schedule SEQUENTIAL"
                  << " instruction: " << instruction->name()
                  << " instruction.callee: "
                  << instruction->to_apply()->root_instruction()->name();
        }
        Schedule(instruction, nullptr, result_buffer);
      }

      ++instructions_in_flight_;
      pending_it = pending_->erase(pending_it);
    }
    // Wait for a completed HLO instruction to be present in the queue.  We will
    // pop it out of the queue and make the result available to its users.
    HloInstruction* instruction;
    do {
      tensorflow::mutex_lock l(completion_queue_lock_);
      if (completion_queue_.empty()) {
        completion_queue_cv_.wait(l);
      }
      if (!completion_queue_.empty()) {
        instruction = completion_queue_.front();
        completion_queue_.pop_front();
        break;
      }
    } while (1);
    TF_ASSIGN_OR_RETURN(const BufferAllocation::Slice result_slice,
                        assignment_->GetUniqueTopLevelSlice(instruction));
    void* result_buffer =
        static_cast<char*>(temps_array_[result_slice.index()]) +
        result_slice.offset();
    InsertOrDie(results_, instruction, result_buffer);
    --instructions_in_flight_;
  }
  return Status::OK();
}

void Executor::Schedule(HloInstruction* instruction, int64* partition_buffers,
                        void* result_buffer) {
  // The thread pool entry takes ownership of |operand_buffers|.
  auto operand_buffers = GetOperandBuffers(instruction);

  auto function = FindOrDie(functions_, instruction);
  const auto* exec_run_options = &run_options_->run_options();
  thread_pool_->Schedule([this, instruction, result_buffer, operand_buffers,
                          partition_buffers, exec_run_options, function]() {
    function(result_buffer, exec_run_options, operand_buffers, temps_array_,
             partition_buffers, profile_counters_array_);

    delete[] operand_buffers;
    delete[] partition_buffers;
    // Push the completed HLO instruction on the queue, the main
    // thread will pop it off and potentially launch more work which
    // uses the result.
    // TODO(b/27458679) Consider alternative task scheduling and synchronization
    // schemes. For example, we could avoid the overhead associate with the
    // condvar here if the thread just dequed the next instruction to execute
    // on completion.
    {
      tensorflow::mutex_lock l(completion_queue_lock_);
      // Decrement in-flight task count for this completion.
      if (--FindOrDie(tasks_in_flight_, instruction) == 0) {
        completion_queue_.push_back(instruction);
        completion_queue_cv_.notify_all();
        tasks_in_flight_.erase(instruction);
      }
    }
  });
}

int64* Executor::GetPartitionBuffers(
    const std::vector<std::pair<int64, int64>>& partition) {
  // Return in 'partition_buffers' partition [size, limit) for each dimension.
  auto partition_buffers = new int64[partition.size() * 2];
  for (int i = 0; i < partition.size(); ++i) {
    partition_buffers[2 * i + 0] = partition[i].first;
    partition_buffers[2 * i + 1] = partition[i].first + partition[i].second;
  }
  return partition_buffers;
}

bool Executor::HasParallelTasks(HloInstruction* instruction) {
  return instruction->opcode() == HloOpcode::kCall &&
         !instruction->to_apply()
              ->root_instruction()
              ->outer_dimension_partitions()
              .empty();
}

const void** Executor::GetOperandBuffers(HloInstruction* instruction) {
  // We cannot use a move-only RAII type like std::unique_ptr because the
  // list of operands is allocated on the main thread and transferred to the
  // worker via the lambda passed to enqueue_function.  In order for the
  // lambda to take ownership, we would need to use generalized lambda
  // capture which is a feature new to C++14.
  // TODO(b/27458679) Avoid dynamic allocations in Executor.
  auto operand_buffers = new const void*[instruction->operand_count()];
  std::transform(instruction->operands().begin(), instruction->operands().end(),
                 operand_buffers, [this](HloInstruction* operand) {
                   return FindOrDie(*results_, operand);
                 });
  return operand_buffers;
}

}  // namespace

Status ParallelCpuExecutable::AllocateBuffers(
    DeviceMemoryAllocator* memory_allocator, int device_ordinal,
    std::vector<perftools::gputools::DeviceMemoryBase>* buffers) {
  CHECK_EQ(buffers->size(), assignment_->Allocations().size());
  VLOG(3) << "Allocating " << assignment_->Allocations().size()
          << " allocations for module " << module().name();
  for (BufferAllocation::Index i = 0; i < assignment_->Allocations().size();
       ++i) {
    auto& allocation = assignment_->GetAllocation(i);

    VLOG(3) << allocation.ToString();

    if (allocation.is_entry_computation_parameter()) {
      VLOG(3) << "allocation #" << i << " is a parameter";
      continue;
    }

    if (allocation.is_thread_local()) {
      VLOG(3) << "buffer #" << i << " is thread-local";
      continue;
    }

    int64 buffer_size = allocation.size();
    if (!(*buffers)[i].is_null()) {
      VLOG(3) << "buffer #" << i
              << " is in the preallocated result ShapedBuffer";
    } else {
      TF_ASSIGN_OR_RETURN((*buffers)[i], memory_allocator->Allocate(
                                             device_ordinal, buffer_size));

      VLOG(3) << "buffer #" << i << " allocated " << buffer_size << " bytes ["
              << (*buffers)[i].opaque() << "]";
    }

    // Since the output buffer and all the temporary buffers were written into
    // by the JITed code, msan has no way of knowing their memory was
    // initialized. Mark them initialized so that msan doesn't flag loads from
    // these buffers.
    TF_ANNOTATE_MEMORY_IS_INITIALIZED((*buffers)[i].opaque(), buffer_size);
  }

  TF_ASSIGN_OR_RETURN(const BufferAllocation::Slice result_slice,
                      assignment_->GetUniqueTopLevelOutputSlice());
  VLOG(3) << "result index: " << result_slice.index();

  return Status::OK();
}

Status ParallelCpuExecutable::ExecuteComputeFunctions(
    const ServiceExecutableRunOptions* run_options,
    tensorflow::gtl::ArraySlice<const ShapedBuffer*> arguments,
    tensorflow::gtl::ArraySlice<se::DeviceMemoryBase> buffers,
    HloExecutionProfile* hlo_execution_profile) {
  std::vector<se::DeviceMemoryBase> argument_buffers(arguments.size());
  for (int i = 0; i < arguments.size(); ++i) {
    TF_RET_CHECK(!ShapeUtil::IsTuple(arguments[i]->shape()));
    argument_buffers[i] = arguments[i]->buffer(/*index=*/{});
  }
  return ExecuteComputeFunctions(run_options, argument_buffers, buffers,
                                 hlo_execution_profile);
}

Status ParallelCpuExecutable::ExecuteComputeFunctions(
    const ServiceExecutableRunOptions* run_options,
    tensorflow::gtl::ArraySlice<se::DeviceMemoryBase> arguments,
    tensorflow::gtl::ArraySlice<se::DeviceMemoryBase> buffers,
    HloExecutionProfile* hlo_execution_profile) {
  // Allocate profiling counters for each hlo instruction that we would like to
  // profile.  Allocate an additional profile counter for the entire
  // computation.
  std::vector<uint64> profile_counters(hlo_to_profile_idx_.size() + 1);

  std::vector<void*> buffer_pointers;
  buffer_pointers.reserve(buffers.size());
  for (auto device_allocation : buffers) {
    buffer_pointers.push_back(device_allocation.opaque());
  }

  // Resolve functions for all the HLO instructions ahead of time.
  std::map<HloInstruction*, ComputeFunctionType> functions;
  for (auto& entry : *functions_names_) {
    tensorflow::mutex_lock lock(jit_mutex_);
    HloInstruction* instruction = entry.first;
    llvm::JITSymbol sym = jit_->FindSymbol(entry.second);
    TF_RET_CHECK(sym);
    InsertOrDie(
        &functions, instruction,
        reinterpret_cast<ComputeFunctionType>(cantFail(sym.getAddress())));
  }

  // Map containing pointers to result buffers for each instruction.
  std::map<HloInstruction*, const void*> results;

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

  std::list<HloInstruction*> pending;

  // Call the function for each HLO instruction in topological order.
  const HloComputation& entry_computation = *module().entry_computation();
  for (auto* instruction : entry_computation.MakeInstructionPostOrder()) {
    // Parameters and constants have no functions associated with them. Instead
    // just copy the existing buffer into the map containing instruction
    // results..
    if (instruction->opcode() == HloOpcode::kParameter) {
      InsertOrDie(&results, instruction,
                  arguments[instruction->parameter_number()].opaque());
    } else if (instruction->opcode() == HloOpcode::kConstant) {
      unsigned char* aligned_data =
          FindOrDie(aligned_constants_, instruction).get();
      InsertOrDie(&results, instruction, aligned_data);
    } else {
      TF_RET_CHECK(instruction->opcode() == HloOpcode::kCall);
      pending.push_back(instruction);
    }
  }

  // TODO(b/27458679) Manage scheduling based on in-flight concurrency limits.
  // For example, if we expect a library conv/matmul call to run at max
  // concurrency, we should not dispatch runnable instructions until the
  // library call is finished (to avoid expensive cache invalidation).
  Executor executor(functions, run_options, &pending, &results,
                    buffer_pointers.data(), profile_counters.data(),
                    assignment_.get());

  TF_RETURN_IF_ERROR(executor.Run());

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

  {
    tensorflow::mutex_lock lock(mutex_);
    double nanoseconds = (end_micros - start_micros) * 1000.0;
    execution_profile_.set_compute_time_ns(std::max(nanoseconds, 1.0));
    // The last profile counter is used for the computation as a whole.
    execution_profile_.set_compute_cycle_count(profile_counters.back());
  }
  if (hlo_execution_profile != nullptr) {
    hlo_execution_profile->set_total_cycles_executed(entry_computation,
                                                     profile_counters.back());

    for (auto hlo_prof_idx : hlo_to_profile_idx_) {
      const HloInstruction* hlo = hlo_prof_idx.first;
      uint64 cycles_taken = profile_counters[hlo_prof_idx.second];
      hlo_execution_profile->AddProfileResult(hlo, cycles_taken);
    }
  }

  return Status::OK();
}

StatusOr<perftools::gputools::DeviceMemoryBase>
ParallelCpuExecutable::ExecuteOnStream(
    const ServiceExecutableRunOptions* run_options,
    tensorflow::gtl::ArraySlice<se::DeviceMemoryBase> arguments,
    HloExecutionProfile* hlo_execution_profile) {
  se::Stream* stream = run_options->stream();
  DeviceMemoryAllocator* memory_allocator = run_options->allocator();
  VLOG(3) << "ExecuteOnStream arg size: " << arguments.size();
  if (!arguments.empty()) {
    VLOG(3) << "ExecuteOnStream arg[0]: " << arguments.at(0).opaque();
  }

  // Allocate the temporary buffers required for the computation.
  se::StreamExecutor* stream_executor = stream->parent();
  int device_ordinal = stream_executor->device_ordinal();
  int64 buffer_count = assignment_->Allocations().size();
  VLOG(3) << "temp buffer count: " << buffer_count;

  std::vector<se::DeviceMemoryBase> device_allocations(
      assignment_->Allocations().size());
  TF_RETURN_IF_ERROR(AllocateBuffers(memory_allocator,
                                     stream->parent()->device_ordinal(),
                                     &device_allocations));

  TF_ASSIGN_OR_RETURN(const BufferAllocation::Slice result_slice,
                      assignment_->GetUniqueTopLevelOutputSlice());
  const BufferAllocation::Index result_index = result_slice.index();
  VLOG(3) << "result index: " << result_index;

  TF_RETURN_IF_ERROR(ExecuteComputeFunctions(
      run_options, arguments, device_allocations, hlo_execution_profile));

  // Mark the buffers that are actually live (used in the output) when the
  // computation finishes executing.
  std::unordered_set<const void*> marked_addresses;
  MarkLiveAddressesInOutput(device_allocations[result_index].opaque(),
                            result_shape(), &marked_addresses);

  VLOG(3) << "Live addresses in output marking found "
          << marked_addresses.size() << " addresses:\n"
          << tensorflow::str_util::Join(
                 marked_addresses, ", ", [](string* out, const void* address) {
                   tensorflow::strings::StrAppend(
                       out, tensorflow::strings::Printf("%p", address));
                 });

  // Computation is done - deallocate temp buffers. Keep those marked
  // live because they are referenced by the output of the computation
  // and are needed by the service. They will be deallocated by the
  // service.
  for (size_t i = 0; i < device_allocations.size(); ++i) {
    auto alloc = device_allocations[i];
    if (marked_addresses.count(alloc.opaque()) == 0 &&
        alloc.opaque() != nullptr) {
      VLOG(3) << "ParallelCpuExecutable deallocating buffer #" << i << " ["
              << alloc.opaque() << "]";
      TF_RETURN_IF_ERROR(memory_allocator->Deallocate(device_ordinal, &alloc));
    }
  }

  return device_allocations[result_index];
}

StatusOr<std::unique_ptr<ShapedBuffer>> ParallelCpuExecutable::ExecuteOnStream(
    const ServiceExecutableRunOptions* run_options,
    tensorflow::gtl::ArraySlice<const ShapedBuffer*> arguments,
    HloExecutionProfile* hlo_execution_profile) {
  if (GetRootPointsToSet().IsAmbiguous()) {
    return Unimplemented("Points-to set of root instruction is ambiguous");
  }

  se::Stream* stream = run_options->stream();
  DeviceMemoryAllocator* memory_allocator = run_options->allocator();
  std::vector<se::DeviceMemoryBase> buffers(assignment_->Allocations().size());

  TF_ASSIGN_OR_RETURN(std::unique_ptr<ShapedBuffer> result_buffer,
                      ShapedBuffer::MakeShapedBuffer(
                          result_shape(), stream->parent()->platform(),
                          stream->parent()->device_ordinal()));

  TF_RETURN_IF_ERROR(AllocateBuffers(
      memory_allocator, stream->parent()->device_ordinal(), &buffers));

  TF_RETURN_IF_ERROR(ExecuteComputeFunctions(run_options, arguments, buffers,
                                             hlo_execution_profile));

  // Copy DeviceMemoryBase values which contain the array(s) of the result into
  // the respective location in ShapedBuffer which is returned to the caller.
  std::vector<bool> buffers_in_result(assignment_->Allocations().size(), false);
  TF_RETURN_IF_ERROR(
      result_buffer->mutable_shape_index_to_buffer_entry()
          ->ForEachMutableElementWithStatus(
              [&buffers, &buffers_in_result, &result_buffer, this](
                  const ShapeIndex& index, size_t* buffer_entry) {
                if (ShapeUtil::IsLeafIndex(result_buffer->shape(), index)) {
                  const std::vector<const LogicalBuffer*>& sources =
                      this->GetRootPointsToSet().element(index);
                  // The points to set is unambiguous so the set should be a
                  // singleton.
                  CHECK_EQ(1, sources.size());
                  const LogicalBuffer* buffer_source = sources[0];
                  HloInstruction* src = buffer_source->instruction();

                  // The source for this result buffer can be a nested buffer
                  // such as a tuple element.

                  // The source instruction should have a non-parameter buffer
                  // assigned.
                  TF_ASSIGN_OR_RETURN(const BufferAllocation::Slice slice,
                                      this->assignment_->GetUniqueSlice(
                                          src, buffer_source->index()));
                  CHECK(!slice.allocation()->is_entry_computation_parameter());

                  const BufferAllocation::Index buffer_index = slice.index();
                  const se::DeviceMemoryBase& buffer = buffers[buffer_index];
                  CHECK(!buffer.is_null() || buffer.size() == 0);
                  *buffer_entry = result_buffer->mutable_buffers()->size();
                  result_buffer->mutable_buffers()->push_back(buffer);
                  buffers_in_result[buffer_index] = true;
                }
                return Status::OK();
              }));

  // Free all buffers not in the result.
  for (size_t i = 0; i < buffers.size(); ++i) {
    se::DeviceMemoryBase alloc = buffers[i];
    if (!buffers_in_result[i] && !alloc.is_null()) {
      VLOG(3) << "CpuExecutable deallocating buffer #" << i << " ["
              << alloc.opaque() << "]";
      TF_RETURN_IF_ERROR(memory_allocator->Deallocate(
          stream->parent()->device_ordinal(), &alloc));
    }
  }

  return std::move(result_buffer);
}

StatusOr<perftools::gputools::DeviceMemoryBase>
ParallelCpuExecutable::ExecuteAsyncOnStream(
    const ServiceExecutableRunOptions* run_options,
    tensorflow::gtl::ArraySlice<se::DeviceMemoryBase> arguments) {
  // TODO(b/30671675): Implement asynchronous execution mode.
  return Unimplemented(
      "Asynchronous execution on stream is not yet supported on CPU.");
}

const PointsToSet& ParallelCpuExecutable::GetRootPointsToSet() const {
  return assignment_->points_to_analysis().GetPointsToSet(
      module().entry_computation()->root_instruction());
}

}  // namespace cpu
}  // namespace xla