File: utils.cc

package info (click to toggle)
pytorch 1.13.1%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 139,252 kB
  • sloc: cpp: 1,100,274; python: 706,454; ansic: 83,052; asm: 7,618; java: 3,273; sh: 2,841; javascript: 612; makefile: 323; xml: 269; ruby: 185; yacc: 144; objc: 68; lex: 44
file content (360 lines) | stat: -rw-r--r-- 11,299 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
#include <c10/util/accumulate.h>
#include "caffe2/core/logging.h"
#include "caffe2/utils/math/utils.h"

#include <algorithm>
#include <functional>
#include <numeric>
#include <vector>

namespace caffe2 {
namespace math {
namespace utils {

#define CAFFE2_SPECIALIZED_INCREASE_INDEX_IN_DIMS(TIndex)  \
  template <>                                              \
  C10_EXPORT void IncreaseIndexInDims<TIndex>(             \
      const int ndim, const TIndex* dims, TIndex* index) { \
    for (int i = ndim - 1; i >= 0; --i) {                  \
      ++index[i];                                          \
      if (index[i] >= dims[i]) {                           \
        index[i] -= dims[i];                               \
      } else {                                             \
        break;                                             \
      }                                                    \
    }                                                      \
  }
CAFFE2_SPECIALIZED_INCREASE_INDEX_IN_DIMS(std::int32_t)
CAFFE2_SPECIALIZED_INCREASE_INDEX_IN_DIMS(std::int64_t)
#undef CAFFE2_SPECIALIZED_INCREASE_INDEX_IN_DIMS

#define CAFFE2_SPECIALIZED_GET_INDEX_FROM_DIMS(TIndex)        \
  template <>                                                 \
  C10_EXPORT TIndex GetIndexFromDims(                         \
      const int n, const TIndex* dims, const TIndex* index) { \
    TIndex sum = 0;                                           \
    for (int i = 0; i < n; ++i) {                             \
      if (dims[i] > 1) {                                      \
        sum = sum * dims[i] + index[i];                       \
      }                                                       \
    }                                                         \
    return sum;                                               \
  }
CAFFE2_SPECIALIZED_GET_INDEX_FROM_DIMS(std::int32_t)
CAFFE2_SPECIALIZED_GET_INDEX_FROM_DIMS(std::int64_t)
#undef CAFFE2_SPECIALIZED_GET_INDEX_FROM_DIMS

bool IsIdentityPermutation(const int n, const int* perm) {
  for (int i = 0; i < n; ++i) {
    if (perm[i] != i) {
      return false;
    }
  }
  return true;
}

bool CheckReduceDims(const int ndim, const int* X_dims, const int* Y_dims) {
  for (int i = 0; i < ndim; ++i) {
    if (X_dims[i] != Y_dims[i] && Y_dims[i] != 1) {
      return false;
    }
  }
  return true;
}

bool IsRowwiseReduce(
    const int ndim,
    const int* A_dims,
    const int* B_dims,
    int* rows,
    int* cols) {
  *cols = 1;
  int pivot = ndim - 1;
  for (; pivot >= 0 && B_dims[pivot] == 1; --pivot) {
    *cols *= A_dims[pivot];
  }
  *rows = 1;
  for (int i = pivot; i >= 0; --i) {
    if (A_dims[i] != B_dims[i]) {
      return false;
    }
    *rows *= A_dims[i];
  }
  return true;
}

bool IsColwiseReduce(
    const int ndim,
    const int* A_dims,
    const int* B_dims,
    int* rows,
    int* cols) {
  *rows = 1;
  int pivot = 0;
  for (; pivot < ndim && B_dims[pivot] == 1; ++pivot) {
    *rows *= A_dims[pivot];
  }
  *cols = 1;
  for (int i = pivot; i < ndim; ++i) {
    if (A_dims[i] != B_dims[i]) {
      return false;
    }
    *cols *= A_dims[i];
  }
  return true;
}

bool IsBothEndsReduce(
    const int ndim,
    const int* A_dims,
    const int* B_dims,
    int* pre,
    int* mid,
    int* nxt) {
  *nxt = 1;
  int r = ndim - 1;
  for (; r >= 0 && B_dims[r] == 1; --r) {
    *nxt *= A_dims[r];
  }
  *pre = 1;
  int l = 0;
  for (; l <= r && B_dims[l] == 1; ++l) {
    *pre *= A_dims[l];
  }
  *mid = 1;
  for (int i = l; i <= r; ++i) {
    if (A_dims[i] != B_dims[i]) {
      return false;
    }
    *mid *= A_dims[i];
  }
  return true;
}

#define CAFFE2_SPECIALIZED_COMPUTE_BROADCAST_BINARY_OP_DIMS(TIndex)       \
  template <>                                                             \
  C10_EXPORT void ComputeBroadcastBinaryOpDims(                           \
      const int A_ndim,                                                   \
      const TIndex* A_dims,                                               \
      const int B_ndim,                                                   \
      const TIndex* B_dims,                                               \
      TIndex* A_broadcast_dims,                                           \
      TIndex* B_broadcast_dims,                                           \
      TIndex* C_broadcast_dims) {                                         \
    const int ndim = std::max(A_ndim, B_ndim);                            \
    std::fill(A_broadcast_dims, A_broadcast_dims + ndim - A_ndim, 1);     \
    std::fill(B_broadcast_dims, B_broadcast_dims + ndim - B_ndim, 1);     \
    std::copy(A_dims, A_dims + A_ndim, A_broadcast_dims + ndim - A_ndim); \
    std::copy(B_dims, B_dims + B_ndim, B_broadcast_dims + ndim - B_ndim); \
    for (int i = 0; i < ndim; ++i) {                                      \
      CAFFE_ENFORCE(                                                      \
          A_broadcast_dims[i] == B_broadcast_dims[i] ||                   \
          A_broadcast_dims[i] <= 1 || B_broadcast_dims[i] <= 1);          \
      if (A_broadcast_dims[i] == 0 || B_broadcast_dims[i] == 0) {         \
        C_broadcast_dims[i] = 0;                                          \
      } else {                                                            \
        C_broadcast_dims[i] =                                             \
            std::max(A_broadcast_dims[i], B_broadcast_dims[i]);           \
      }                                                                   \
    }                                                                     \
  }
CAFFE2_SPECIALIZED_COMPUTE_BROADCAST_BINARY_OP_DIMS(std::int32_t)
CAFFE2_SPECIALIZED_COMPUTE_BROADCAST_BINARY_OP_DIMS(std::int64_t)
#undef CAFFE2_SPECIALIZED_COMPUTE_BROADCAST_BINARY_OP_DIMS

bool IsRowwiseBroadcastBinaryOp(
    const int ndim,
    const int* A_dims,
    const int* B_dims,
    int* rows,
    int* cols,
    bool* broadcast_1st) {
  if (ndim == 0) {
    return false;
  }
  int A_pivot = 0;
  for (; A_pivot < ndim && A_dims[A_pivot] == 1; ++A_pivot)
    ;
  int B_pivot = 0;
  for (; B_pivot < ndim && B_dims[B_pivot] == 1; ++B_pivot)
    ;
  if (A_pivot == B_pivot) {
    return false;
  }
  const int pivot = std::max(A_pivot, B_pivot);
  if (A_pivot > B_pivot) {
    *rows = c10::multiply_integers(B_dims + B_pivot, B_dims + pivot);
    *broadcast_1st = true;
  } else {
    *rows = c10::multiply_integers(A_dims + A_pivot, A_dims + pivot);
    *broadcast_1st = false;
  }
  *cols = 1;
  for (int i = pivot; i < ndim; ++i) {
    if (A_dims[i] != B_dims[i]) {
      return false;
    }
    *cols *= A_dims[i];
  }
  return true;
}

bool IsColwiseBroadcastBinaryOp(
    const int ndim,
    const int* A_dims,
    const int* B_dims,
    int* rows,
    int* cols,
    bool* broadcast_1st) {
  if (ndim == 0) {
    return false;
  }
  int A_pivot = ndim - 1;
  for (; A_pivot >= 0 && A_dims[A_pivot] == 1; --A_pivot)
    ;
  int B_pivot = ndim - 1;
  for (; B_pivot >= 0 && B_dims[B_pivot] == 1; --B_pivot)
    ;
  if (A_pivot == B_pivot) {
    return false;
  }
  ++A_pivot;
  ++B_pivot;
  const int pivot = std::min(A_pivot, B_pivot);
  if (A_pivot < B_pivot) {
    *cols = c10::multiply_integers(B_dims + pivot, B_dims + B_pivot);
    *broadcast_1st = true;
  } else {
    *cols = c10::multiply_integers(A_dims + pivot, A_dims + A_pivot);
    *broadcast_1st = false;
  }
  *rows = 1;
  for (int i = 0; i < pivot; ++i) {
    if (A_dims[i] != B_dims[i]) {
      return false;
    }
    *rows *= A_dims[i];
  }
  return true;
}

bool IsBothEndsBroadcastBinaryOp(
    const int ndim,
    const int* A_dims,
    const int* B_dims,
    int* pre,
    int* mid,
    int* nxt,
    bool* broadcast_1st) {
  if (ndim == 0) {
    return false;
  }
  int A_pre = 0;
  for (; A_pre < ndim && A_dims[A_pre] == 1; ++A_pre)
    ;
  int B_pre = 0;
  for (; B_pre < ndim && B_dims[B_pre] == 1; ++B_pre)
    ;
  int A_nxt = ndim - 1;
  for (; A_nxt >= 0 && A_dims[A_nxt] == 1; --A_nxt)
    ;
  int B_nxt = ndim - 1;
  for (; B_nxt >= 0 && B_dims[B_nxt] == 1; --B_nxt)
    ;
  ++A_nxt;
  ++B_nxt;
  if (A_pre == B_pre || A_nxt == B_nxt) {
    return false;
  }
  if (A_pre > B_pre && A_nxt < B_nxt) {
    *pre = c10::multiply_integers(B_dims + B_pre, B_dims + A_pre);
    *nxt = c10::multiply_integers(B_dims + A_nxt, B_dims + B_nxt);
    *broadcast_1st = true;
  } else if (A_pre < B_pre && A_nxt > B_nxt) {
    *pre = c10::multiply_integers(A_dims + A_pre, A_dims + B_pre);
    *nxt = c10::multiply_integers(A_dims + B_nxt, A_dims + A_nxt);
    *broadcast_1st = false;
  } else {
    return false;
  }
  const int l = std::max(A_pre, B_pre);
  const int r = std::min(A_nxt, B_nxt);
  *mid = 1;
  for (int i = l; i < r; ++i) {
    if (A_dims[i] != B_dims[i]) {
      return false;
    }
    *mid *= A_dims[i];
  }
  return true;
}

bool IsBatchTranspose2D(const int ndim, const int* axes) {
  if (ndim < 2) {
    return false;
  }
  for (int i = 0; i < ndim - 2; ++i) {
    if (axes[i] != i) {
      return false;
    }
  }
  return axes[ndim - 2] == ndim - 1 && axes[ndim - 1] == ndim - 2;
}

void ComputeTransposeAxesForReduceOp(
    const int num_dims,
    const int num_reduce_axes,
    const int* reduce_axes,
    int* transpose_axes) {
  const int d = num_dims - num_reduce_axes;
  std::copy_n(reduce_axes, num_reduce_axes, transpose_axes + d);
  std::sort(transpose_axes + d, transpose_axes + num_dims);
  int p = 0;
  int q = d;
  for (int i = 0; i < num_dims; ++i) {
    if (q < num_dims && i == transpose_axes[q]) {
      ++q;
    } else {
      transpose_axes[p++] = i;
    }
  }
}

void ComputeTransposeAxesForReduceOp(
    const int ndim,
    const int* dims,
    int* axes) {
  // NOLINTNEXTLINE(cppcoreguidelines-narrowing-conversions,bugprone-narrowing-conversions)
  const int d = ndim - std::count(dims, dims + ndim, 1);
  int p = 0;
  int q = d;
  for (int i = 0; i < ndim; ++i) {
    if (dims[i] == 1) {
      axes[q++] = i;
    } else {
      axes[p++] = i;
    }
  }
}

#define CAFFE2_SPECIALIZED_COMPUTE_TRANSPOSED_STRIDES(TIndex)                 \
  template <>                                                                 \
  C10_EXPORT void ComputeTransposedStrides<TIndex>(                           \
      const int ndim, const TIndex* dims, const int* axes, TIndex* strides) { \
    std::vector<TIndex> buff(ndim);                                           \
    TIndex cur_stride = 1;                                                    \
    for (int i = ndim - 1; i >= 0; --i) {                                     \
      buff[i] = cur_stride;                                                   \
      cur_stride *= dims[i];                                                  \
    }                                                                         \
    for (int i = 0; i < ndim; ++i) {                                          \
      strides[i] = buff[axes[i]];                                             \
    }                                                                         \
  }
CAFFE2_SPECIALIZED_COMPUTE_TRANSPOSED_STRIDES(std::int32_t)
CAFFE2_SPECIALIZED_COMPUTE_TRANSPOSED_STRIDES(std::int64_t)
#undef CAFFE2_SPECIALIZED_COMPUTE_TRANSPOSED_STRIDES

} // namespace utils
} // namespace math
} // namespace caffe2