• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2018 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 #include "tensorflow/core/common_runtime/collective_util.h"
16 
17 #include <memory>
18 #include <vector>
19 
20 #include "tensorflow/core/common_runtime/device.h"
21 #include "tensorflow/core/common_runtime/device_mgr.h"
22 #include "tensorflow/core/framework/collective.h"
23 #include "tensorflow/core/framework/device_attributes.pb.h"
24 #include "tensorflow/core/lib/core/errors.h"
25 #include "tensorflow/core/lib/strings/strcat.h"
26 #include "tensorflow/core/platform/types.h"
27 
28 namespace tensorflow {
29 namespace collective_util {
30 
31 /*static*/
InitializeDeviceAndLocality(const DeviceMgr * dev_mgr,const string & device_name,Device ** device,DeviceLocality * device_locality)32 Status InitializeDeviceAndLocality(const DeviceMgr* dev_mgr,
33                                    const string& device_name, Device** device,
34                                    DeviceLocality* device_locality) {
35   if (!dev_mgr) {
36     return errors::Internal("Required non-null dev_mgr ", dev_mgr,
37                             " for InitializeDeviceAndLocality");
38   }
39 
40   Status status = dev_mgr->LookupDevice(device_name, device);
41   if (status.ok()) {
42     CHECK(*device);
43     *device_locality = (*device)->attributes().locality();
44   } else {
45     LOG(ERROR) << "Failed to find device " << device_name;
46     for (auto d : dev_mgr->ListDevices()) {
47       LOG(ERROR) << "Available devices " << d->name();
48     }
49   }
50   return status;
51 }
52 
53 /*static*/
SubdivPermDebugString(const CollectiveParams & col_params)54 string SubdivPermDebugString(const CollectiveParams& col_params) {
55   const auto& subdiv_perms =
56       col_params.instance.impl_details.subdiv_permutations;
57   string buf;
58   for (int sdi = 0; sdi < subdiv_perms.size(); ++sdi) {
59     strings::StrAppend(&buf, "Subdiv ", sdi, " device order:\n");
60     for (int di = 0; di < subdiv_perms[sdi].size(); ++di) {
61       int idx = subdiv_perms[sdi][di];
62       if (idx >= 0) {
63         CHECK_GT(col_params.instance.device_names.size(), idx);
64         strings::StrAppend(&buf, col_params.instance.device_names[idx], "\n");
65       }
66     }
67     strings::StrAppend(&buf, " subdiv_offsets: ");
68     for (auto o : col_params.instance.impl_details.subdiv_offsets)
69       strings::StrAppend(&buf, o, " ");
70     strings::StrAppend(&buf, " SubdivRank: ");
71     for (auto d : col_params.subdiv_rank) strings::StrAppend(&buf, d, " ");
72     if (col_params.instance.type == BROADCAST_COLLECTIVE) {
73       strings::StrAppend(&buf, " subdiv_source_rank: ");
74       for (auto src : col_params.instance.impl_details.subdiv_source_rank)
75         strings::StrAppend(&buf, src, " ");
76     }
77     strings::StrAppend(&buf, "\n");
78   }
79   return buf;
80 }
81 
SubContext(OpKernelContext * ctx,OpKernelContext::Params * params,OpKernel * op,Tensor * output,Tensor * input)82 SubContext::SubContext(OpKernelContext* ctx, OpKernelContext::Params* params,
83                        OpKernel* op, Tensor* output, Tensor* input)
84     : sub_params_(*params),
85       sub_inputs_({output, input}),
86       sub_input_attr_({ctx->input_alloc_attr(0), ctx->input_alloc_attr(0)}),
87       sub_input_dc_(
88           {ctx->input_device_context(0), ctx->input_device_context(0)}) {
89   sub_params_.op_kernel = op;
90   sub_params_.inputs = &sub_inputs_;
91   sub_params_.input_alloc_attrs = &sub_input_attr_;
92   sub_params_.input_device_contexts = &sub_input_dc_;
93   sub_params_.eigen_gpu_device = nullptr;
94   sub_params_.ensure_eigen_gpu_device();
95   sub_params_.forward_from_array = &forward_from_;
96   sub_ctx_.reset(new OpKernelContext(&sub_params_, 1));
97 }
98 
ComputeBinOp(OpKernelContext * op_ctx,OpKernelContext::Params * params,Device * device,OpKernel * op,Tensor * output,Tensor * input)99 Status ComputeBinOp(OpKernelContext* op_ctx, OpKernelContext::Params* params,
100                     Device* device, OpKernel* op, Tensor* output,
101                     Tensor* input) {
102   // Prepare an OpKernelContext that is identical to that of the original Op
103   // (i.e. the collective), except for the input output sizes and identities and
104   // the Op itself.
105   // TODO(ayushd, tucker): Is it possible to cache and reuse these objects?
106   // They're mostly identical inside one device execution.
107   std::unique_ptr<SubContext> sub_ctx(
108       new SubContext(op_ctx, params, op, output, input));
109   device->Compute(op, sub_ctx->sub_ctx_.get());
110   return sub_ctx->sub_ctx_->status();
111 }
112 
113 }  // namespace collective_util
114 }  // namespace tensorflow
115