1 /* Copyright 2016 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 #if TENSORFLOW_USE_SYCL 17 18 #include "tensorflow/core/common_runtime/sycl/sycl_device.h" 19 #include "third_party/eigen3/unsupported/Eigen/CXX11/Tensor" 20 21 #include "tensorflow/core/framework/tensor.pb_text.h" 22 #include "tensorflow/core/platform/tracing.h" 23 24 namespace tensorflow { 25 26 SYCLDevice::~SYCLDevice() {} 27 28 void SYCLDevice::Compute(OpKernel* op_kernel, OpKernelContext* context) { 29 assert(context); 30 // When ThreadScape profiling is off (which is the default), constructing the 31 // following code is simple enough that its overhead is negligible. 32 tracing::ScopedRegion region(tracing::EventCategory::kCompute, 33 op_kernel->name()); 34 35 op_kernel->Compute(context); 36 } 37 38 Allocator* SYCLDevice::GetAllocator(AllocatorAttributes attr) { 39 if (attr.on_host()) 40 return cpu_allocator_; 41 else 42 return sycl_allocator_; 43 } 44 45 Status SYCLDevice::MakeTensorFromProto(const TensorProto& tensor_proto, 46 const AllocatorAttributes alloc_attrs, 47 Tensor* tensor) { 48 AllocatorAttributes attr; 49 attr.set_on_host(true); 50 Allocator* host_alloc = GetAllocator(attr); 51 52 Tensor parsed(tensor_proto.dtype()); 53 if (!parsed.FromProto(host_alloc, tensor_proto)) { 54 return errors::InvalidArgument("Cannot parse tensor from proto: ", 55 tensor_proto.DebugString()); 56 } 57 Status status; 58 if (alloc_attrs.on_host()) { 59 *tensor = parsed; 60 } else { 61 Tensor copy(GetAllocator(alloc_attrs), parsed.dtype(), parsed.shape()); 62 63 // If the tensor is not initialized, we likely ran out of memory. 64 if (!copy.IsInitialized()) { 65 return errors::ResourceExhausted( 66 "OOM when allocating tensor of shape ", parsed.shape().DebugString(), 67 " and type ", DataTypeString(parsed.dtype())); 68 } 69 70 device_context_->CopyCPUTensorToDevice( 71 &parsed, this, ©, [&status](const Status& s) { status = s; }); 72 *tensor = copy; 73 } 74 return status; 75 } 76 77 Status SYCLDevice::FillContextMap(const Graph* graph, 78 DeviceContextMap* device_context_map) { 79 // Fill in the context map. It is OK for this map to contain 80 // duplicate DeviceContexts so long as we increment the refcount. 81 device_context_map->resize(graph->num_node_ids()); 82 for (Node* n : graph->nodes()) { 83 device_context_->Ref(); 84 (*device_context_map)[n->id()] = device_context_; 85 } 86 87 return Status::OK(); 88 } 89 90 Status SYCLDevice::Sync() { 91 sycl_allocator_->Synchronize(); 92 if (sycl_allocator_->Ok()) { 93 return Status::OK(); 94 } else { 95 return errors::Internal("Unknown error detected on device ", name()); 96 } 97 } 98 99 } // namespace tensorflow 100 101 #endif // TENSORFLOW_USE_SYCL 102