aboutsummaryrefslogtreecommitdiffhomepage
path: root/tensorflow/compiler/xla/service/transfer_manager.cc
blob: c7f6a13023d32f48d357430bb62241cd537422ca (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/xla/service/transfer_manager.h"

#include <string>
#include <utility>

#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/core/platform/logging.h"
#include "tensorflow/core/platform/macros.h"

namespace se = ::perftools::gputools;

namespace xla {

/* static */ tensorflow::mutex*
TransferManager::platform_transfer_manager_mutex() {
  static tensorflow::mutex* m = new tensorflow::mutex;
  return m;
}

/* static */ std::map<perftools::gputools::Platform::Id,
                      TransferManager::State>*
TransferManager::GetPlatformTransferManagers() {
  static auto* r =
      new std::map<perftools::gputools::Platform::Id, TransferManager::State>;
  return r;
}

/* static */ void TransferManager::RegisterTransferManager(
    se::Platform::Id platform_id,
    TransferManagerCreationFunction creation_function) {
  tensorflow::mutex_lock lock(
      *TransferManager::platform_transfer_manager_mutex());
  auto* managers = GetPlatformTransferManagers();
  CHECK(managers->find(platform_id) == managers->end());
  (*managers)[platform_id].creation_function = creation_function;
}

/* static */ StatusOr<TransferManager*> TransferManager::GetForPlatform(
    const se::Platform* platform) {
  tensorflow::mutex_lock lock(
      *TransferManager::platform_transfer_manager_mutex());
  auto* managers = GetPlatformTransferManagers();

  auto it = managers->find(platform->id());
  if (it == managers->end()) {
    return NotFound(
        "could not find registered transfer manager for platform %s -- check "
        "target linkage",
        platform->Name().c_str());
  }

  if (it->second.manager == nullptr) {
    // Lazily create the transfer manager the first time it is needed
    it->second.manager = (*it->second.creation_function)();
  }

  return it->second.manager;
}

Status TransferManager::TransferBufferFromDevice(
    se::StreamExecutor* executor, const se::DeviceMemoryBase& source,
    int64 size, void* destination) {
  if (source.size() < size) {
    return FailedPrecondition(
        "Source allocation on device not large enough for data tranfer: "
        "%lld < %lld",
        source.size(), size);
  }
  auto copy_status = executor->SynchronousMemcpyD2H(source, size, destination);
  if (!copy_status.ok()) {
    return AddStatus(
        Status(static_cast<tensorflow::error::Code>(copy_status.code()),
               copy_status.error_message()),
        "failed transfer from device to buffer");
  }
  return Status::OK();
}

Status TransferManager::TransferBufferToDevice(
    se::StreamExecutor* executor, int64 size, const void* source,
    se::DeviceMemoryBase* destination) {
  if (destination->size() < size) {
    return FailedPrecondition(
        "Destination allocation on device not large enough for data tranfer: "
        "%lld < %lld",
        destination->size(), size);
  }
  auto copy_status = executor->SynchronousMemcpyH2D(source, size, destination);
  if (!copy_status.ok()) {
    return AddStatus(
        Status(static_cast<tensorflow::error::Code>(copy_status.code()),
               copy_status.error_message()),
        "failed transfer of buffer to device");
  }
  return Status::OK();
}

StatusOr<std::set<se::DeviceMemoryBase>>
TransferManager::GatherBufferPointersFromTuple(
    se::StreamExecutor* executor, const se::DeviceMemoryBase& source,
    const Shape& shape) {
  TF_RET_CHECK(ShapeUtil::IsTuple(shape));

  std::set<se::DeviceMemoryBase> buffer_pointers;
  buffer_pointers.insert(source);

  TF_ASSIGN_OR_RETURN(std::vector<se::DeviceMemoryBase> tuple_elements,
                      ShallowCopyTupleFromDevice(executor, source, shape));
  for (auto i = 0; i < tuple_elements.size(); ++i) {
    const Shape& element_shape = shape.tuple_shapes(i);
    if (ShapeUtil::IsTuple(element_shape)) {
      TF_ASSIGN_OR_RETURN(
          std::set<se::DeviceMemoryBase> buffer_pointers_in_element,
          GatherBufferPointersFromTuple(executor, tuple_elements[i],
                                        element_shape));
      buffer_pointers.insert(buffer_pointers_in_element.begin(),
                             buffer_pointers_in_element.end());
    } else {
      buffer_pointers.insert(tuple_elements[i]);
    }
  }
  return std::move(buffer_pointers);
}

}  // namespace xla