• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2019 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/gpu/cholesky_thunk.h"
17 
18 #include <string>
19 
20 #include "absl/strings/str_cat.h"
21 #include "absl/strings/str_format.h"
22 #include "tensorflow/compiler/xla/service/gpu/hlo_execution_profiler.h"
23 #include "tensorflow/compiler/xla/types.h"
24 #include "tensorflow/compiler/xla/util.h"
25 #include "tensorflow/compiler/xla/xla_data.pb.h"
26 #include "tensorflow/core/platform/logging.h"
27 #include "tensorflow/core/platform/stream_executor_no_cuda.h"
28 #include "tensorflow/stream_executor/blas.h"
29 #include "tensorflow/stream_executor/device_memory.h"
30 
31 namespace xla {
32 namespace gpu {
33 
CholeskyThunk(ThunkInfo thunk_info,const CholeskyOptions & options,BufferAllocation::Slice a_buffer,BufferAllocation::Slice workspace_buffer,BufferAllocation::Slice info_buffer,PrimitiveType type,int64 batch_size,int64 n)34 CholeskyThunk::CholeskyThunk(ThunkInfo thunk_info,
35                              const CholeskyOptions& options,
36                              BufferAllocation::Slice a_buffer,
37                              BufferAllocation::Slice workspace_buffer,
38                              BufferAllocation::Slice info_buffer,
39                              PrimitiveType type, int64 batch_size, int64 n)
40     : Thunk(Kind::kCholesky, thunk_info),
41       uplo_(options.lower() ? se::blas::UpperLower::kLower
42                             : se::blas::UpperLower::kUpper),
43       a_buffer_(a_buffer),
44       workspace_buffer_(workspace_buffer),
45       info_buffer_(info_buffer),
46       type_(type),
47       batch_size_(batch_size),
48       a_batch_stride_(n * n * ShapeUtil::ByteSizeOfPrimitiveType(type)),
49       n_(n) {}
50 
ExecuteOnStream(const ExecuteParams & params)51 Status CholeskyThunk::ExecuteOnStream(const ExecuteParams& params) {
52   VLOG(3) << "type=" << PrimitiveType_Name(type_)
53           << " uplo=" << se::blas::UpperLowerString(uplo_)
54           << " batch_size=" << batch_size_ << " n=" << n_
55        << " a=" << a_buffer_.ToString()
56        << " workspace=" << workspace_buffer_.ToString()
57        << " info=" << info_buffer_.ToString();
58 
59   CusolverContext* context;
60   {
61     tensorflow::mutex_lock lock(mu_);
62     auto result = contexts_.emplace(params.stream, CusolverContext());
63     if (result.second) {
64       TF_ASSIGN_OR_RETURN(result.first->second,
65                           CusolverContext::Create(params.stream));
66     }
67     context = &result.first->second;
68   }
69 
70   char* a_base = static_cast<char*>(
71       params.buffer_allocations->GetDeviceAddress(a_buffer_).opaque());
72   int* info_base = static_cast<int*>(
73       params.buffer_allocations->GetDeviceAddress(info_buffer_).opaque());
74   se::DeviceMemoryBase workspace_data =
75       params.buffer_allocations->GetDeviceAddress(workspace_buffer_);
76   for (int64 i = 0; i < batch_size_; ++i) {
77     se::DeviceMemoryBase a_data =
78         se::DeviceMemoryBase(a_base + i * a_batch_stride_, a_batch_stride_);
79     se::DeviceMemory<int> info_data(
80         se::DeviceMemoryBase(info_base + i, sizeof(int)));
81     switch (type_) {
82       case F32: {
83         TF_RETURN_IF_ERROR(
84             context->Potrf(uplo_, n_, se::DeviceMemory<float>(a_data), n_,
85                            info_data, se::DeviceMemory<float>(workspace_data)));
86         break;
87       }
88       case F64: {
89         TF_RETURN_IF_ERROR(context->Potrf(
90             uplo_, n_, se::DeviceMemory<double>(a_data), n_, info_data,
91             se::DeviceMemory<double>(workspace_data)));
92         break;
93       }
94       case C64: {
95         TF_RETURN_IF_ERROR(context->Potrf(
96             uplo_, n_, se::DeviceMemory<std::complex<float>>(a_data), n_,
97             info_data, se::DeviceMemory<std::complex<float>>(workspace_data)));
98         break;
99       }
100       case C128: {
101         TF_RETURN_IF_ERROR(context->Potrf(
102             uplo_, n_, se::DeviceMemory<std::complex<double>>(a_data), n_,
103             info_data, se::DeviceMemory<std::complex<double>>(workspace_data)));
104         break;
105       }
106       default:
107         return InvalidArgument("Invalid type for cholesky %s",
108                                PrimitiveType_Name(type_));
109     }
110   }
111   return Status::OK();
112 }
113 
114 }  // namespace gpu
115 }  // namespace xla
116