• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2020 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/tpu_computation_placer.h"
17 
18 #include "tensorflow/core/tpu/tpu_api.h"
19 #include "tensorflow/stream_executor/tpu/status_helper.h"
20 #include "tensorflow/stream_executor/tpu/tpu_platform.h"
21 #include "tensorflow/stream_executor/tpu/tpu_platform_id.h"
22 
23 namespace tensorflow {
24 namespace tpu {
25 
26 template <typename T>
27 using StatusOr = TpuComputationPlacer::StatusOr<T>;
28 
TpuComputationPlacer()29 TpuComputationPlacer::TpuComputationPlacer() {
30   placer_ = tensorflow::tpu::ExecutorApiFn()->TpuComputationPlacer_NewFn();
31 }
32 
~TpuComputationPlacer()33 TpuComputationPlacer::~TpuComputationPlacer() {
34   tensorflow::tpu::ExecutorApiFn()->TpuComputationPlacer_FreeFn(placer_);
35 }
36 
DeviceId(int replica,int computation,int replica_count,int computation_count)37 StatusOr<int> TpuComputationPlacer::DeviceId(int replica, int computation,
38                                              int replica_count,
39                                              int computation_count) {
40   LOG(FATAL) << "Unimplemented.";
41 }
42 
AssignDevices(int replica_count,int computation_count)43 StatusOr<xla::DeviceAssignment> TpuComputationPlacer::AssignDevices(
44     int replica_count, int computation_count) {
45   StatusHelper status;
46   xla::DeviceAssignment result(replica_count, computation_count);
47   tensorflow::tpu::ExecutorApiFn()->TpuComputationPlacer_AssignDevicesFn(
48       placer_, replica_count, computation_count, result.data(),
49       status.c_status);
50   if (!status.ok()) {
51     return status.status();
52   }
53   return result;
54 }
55 
56 /*static*/ StatusOr<xla::DeviceAssignment>
AssignLocalDevices(TpuHostLocationExternal host_location,int replica_count,int computation_count)57 TpuComputationPlacer::AssignLocalDevices(TpuHostLocationExternal host_location,
58                                          int replica_count,
59                                          int computation_count) {
60   StatusHelper status;
61   xla::DeviceAssignment result(replica_count, computation_count);
62   tensorflow::tpu::ExecutorApiFn()->TpuComputationPlacer_AssignLocalDevicesFn(
63       host_location.impl(), replica_count, computation_count, result.data(),
64       status.c_status);
65   if (!status.ok()) {
66     return status.status();
67   }
68   return result;
69 }
70 
CreateTpuComputationPlacer()71 static std::unique_ptr<xla::ComputationPlacer> CreateTpuComputationPlacer() {
72   return std::make_unique<TpuComputationPlacer>();
73 }
74 
InitModule()75 static bool InitModule() {
76   xla::ComputationPlacer::RegisterComputationPlacer(GetTpuPlatformId(),
77                                                     CreateTpuComputationPlacer);
78   return true;
79 }
80 static bool module_initialized = InitModule();
81 
82 }  // namespace tpu
83 }  // namespace tensorflow
84