xref: /aosp_15_r20/external/tensorflow/tensorflow/compiler/xla/service/gpu/cholesky_thunk.h (revision b6fb3261f9314811a0f4371741dbb8839866f948)
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 #ifndef TENSORFLOW_COMPILER_XLA_SERVICE_GPU_CHOLESKY_THUNK_H_
17 #define TENSORFLOW_COMPILER_XLA_SERVICE_GPU_CHOLESKY_THUNK_H_
18 
19 #include <optional>
20 
21 #include "tensorflow/compiler/xla/service/buffer_assignment.h"
22 #include "tensorflow/compiler/xla/service/gpu/buffer_allocations.h"
23 #include "tensorflow/compiler/xla/service/gpu/cusolver_context.h"
24 #include "tensorflow/compiler/xla/service/gpu/thunk.h"
25 #include "tensorflow/compiler/xla/service/hlo_instruction.h"
26 #include "tensorflow/compiler/xla/types.h"
27 #include "tensorflow/compiler/xla/xla_data.pb.h"
28 #include "tensorflow/core/lib/core/status.h"
29 #include "tensorflow/core/platform/stream_executor_no_cuda.h"
30 #include "tensorflow/stream_executor/blas.h"
31 #include "tensorflow/stream_executor/gpu/gpu_asm_opts.h"
32 
33 namespace xla {
34 namespace gpu {
35 
36 // This class stores everything that StreamExecutor needs to launch a Cholesky
37 // decomposition (LAPACK potrf). It is generated by IrEmitter.
38 //
39 // As an implementation detail, we may run potrf (potentially in a loop, if
40 // batch_size >1), or potrfBatched.
41 //
42 // Thread-compatible.
43 class CholeskyThunk : public Thunk {
44  public:
45   CholeskyThunk(ThunkInfo thunk_info, const CholeskyOptions& options,
46                 se::GpuAsmOpts asm_opts, BufferAllocation::Slice a_buffer,
47                 BufferAllocation::Slice workspace_buffer,
48                 BufferAllocation::Slice info_buffer, PrimitiveType type,
49                 int64_t batch_size, int64_t n);
50 
51   CholeskyThunk(const CholeskyThunk&) = delete;
52   CholeskyThunk& operator=(const CholeskyThunk&) = delete;
53 
54   Status ExecuteOnStream(const ExecuteParams& params) override;
55 
56  private:
57   se::GpuAsmOpts asm_opts_;
58   se::blas::UpperLower uplo_;
59 
60   const BufferAllocation::Slice a_buffer_;
61   const BufferAllocation::Slice workspace_buffer_;
62   const BufferAllocation::Slice info_buffer_;
63 
64   const PrimitiveType type_;
65   const int64_t batch_size_;
66   const int64_t n_;
67 };
68 
69 struct CholeskyParams {
70   int64_t n;
71   int64_t batch_size;
72   se::blas::UpperLower uplo;
73   se::DeviceMemoryBase a_buffer;
74   se::DeviceMemoryBase workspace_buffer;
75   se::DeviceMemoryBase info_buffer;
76 };
77 Status RunCholesky(const se::GpuAsmOpts& asm_opts, PrimitiveType type,
78                    CholeskyParams* params, se::Stream* stream);
79 
80 }  // namespace gpu
81 }  // namespace xla
82 
83 #endif  // TENSORFLOW_COMPILER_XLA_SERVICE_GPU_CHOLESKY_THUNK_H_
84