xref: /aosp_15_r20/external/tensorflow/tensorflow/core/kernels/sparse/kernels.cc (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 #include "tensorflow/core/kernels/sparse/kernels.h"
17 
18 #include <numeric>
19 
20 #include "third_party/eigen3/unsupported/Eigen/CXX11/Tensor"
21 #include "tensorflow/core/framework/op_kernel.h"
22 #include "tensorflow/core/framework/tensor_types.h"
23 #include "tensorflow/core/lib/core/errors.h"
24 #include "tensorflow/core/lib/core/status.h"
25 #include "tensorflow/core/platform/errors.h"
26 
27 namespace tensorflow {
28 namespace functor {
29 
operator ()(const int64_t batch_size,const int num_rows,TTypes<int64_t>::ConstMatrix indices,TTypes<int32>::Vec batch_ptr,TTypes<int32>::Vec csr_row_ptr,TTypes<int32>::Vec csr_col_ind)30 Status SparseTensorToCSRSparseMatrixCPUFunctor::operator()(
31     const int64_t batch_size, const int num_rows,
32     TTypes<int64_t>::ConstMatrix indices, TTypes<int32>::Vec batch_ptr,
33     TTypes<int32>::Vec csr_row_ptr, TTypes<int32>::Vec csr_col_ind) {
34   // Validate inputs.
35   if (batch_ptr.size() != batch_size + 1) {
36     return errors::InvalidArgument(
37         "Expected batch_ptr.size() == batch_size + 1. Got: ", batch_ptr.size(),
38         " vs. ", batch_size + 1);
39   }
40   if (csr_row_ptr.size() != batch_size * (num_rows + 1)) {
41     return errors::InvalidArgument(
42         "Expected csr_row_ptr.size() == batch_size * (num_rows + 1). Got: ",
43         csr_row_ptr.size(), " vs. ", batch_size * (num_rows + 1));
44   }
45 
46   const int64_t total_nnz = indices.dimension(0);
47   const int rank = indices.dimension(1);
48   if (rank == 2 && batch_size != 1) {
49     return errors::InvalidArgument(
50         "Expected batch_size == 1 when rank is 2. Got batch_size: ",
51         batch_size);
52   }
53   if (csr_col_ind.size() != total_nnz) {
54     return errors::InvalidArgument(
55         "Expected csr_col_ind.size() == total_nnz. Got: ", csr_col_ind.size(),
56         " vs. ", total_nnz);
57   }
58 
59   int prev_batch = -1;
60   if (rank == 2) {
61     // For a single batch, the batch_ptrs are {0, total_nnz}.
62     batch_ptr(0) = 0;
63     ++prev_batch;
64 
65     for (int64_t i = 0; i < total_nnz; ++i) {
66       // For now, the rows pointers store the corresponding row counts.
67       int64_t ix = indices(i, 0) + 1;
68       if (ix >= csr_row_ptr.size()) {
69         return errors::InvalidArgument("Got an index ", ix,
70                                        " that is outside of csr_row_ptr");
71       }
72       csr_row_ptr(indices(i, 0) + 1) += 1;
73       csr_col_ind(i) = indices(i, 1);
74     }
75   } else {  // rank == 3
76     for (int64_t i = 0; i < total_nnz; ++i) {
77       const int cur_batch = indices(i, 0);
78       // For now, the rows pointers store the corresponding row counts.
79       csr_row_ptr(cur_batch * (num_rows + 1) + indices(i, 1) + 1) += 1;
80       csr_col_ind(i) = indices(i, 2);
81 
82       // We're at a new batch and might have skipped over empty batches.
83       while (prev_batch < cur_batch) {
84         // The previous batch ends at position i.
85         batch_ptr(prev_batch + 1) = i;
86         ++prev_batch;
87       }
88     }
89   }
90   // Set the last element of batch_ptr and account for trailing empty batches.
91   while (prev_batch < batch_size) {
92     batch_ptr(prev_batch + 1) = total_nnz;
93     ++prev_batch;
94   }
95 
96   // Compute the cumulative row counts for each batch.
97   for (int batch_idx = 0; batch_idx < batch_size; ++batch_idx) {
98     auto* row_ptr_batch = csr_row_ptr.data() + batch_idx * (num_rows + 1);
99     std::partial_sum(row_ptr_batch, row_ptr_batch + num_rows + 1,
100                      row_ptr_batch);
101   }
102   return OkStatus();
103 }
104 
105 }  // namespace functor
106 }  // namespace tensorflow
107