pytorch/caffe2/operators/rowmul_op.h
Richard Barnes 1622546050 use irange for loops (#70248)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/70248

Modified loops in files under fbsource/fbcode/caffe2/ from the format
```
for(TYPE var=x0;var<x_max;x++)
```
to the format
```
for(const auto var: irange(xmax))
```

This was achieved by running r-barnes's loop upgrader script (D28874212) with some modification to exclude all files under /torch/jit and a number of reversions or unused variable suppression warnings added by hand.

Test Plan: Sandcastle

Reviewed By: malfet

Differential Revision: D32813863

fbshipit-source-id: 527244b4a2b220fdfe7f17dee3599603f492a2ca
2022-01-06 23:14:29 -08:00

78 lines
2.0 KiB
C++

#ifndef CAFFE2_OPERATORS_ROW_MUL_H_
#define CAFFE2_OPERATORS_ROW_MUL_H_
#include "caffe2/core/context.h"
#include "caffe2/core/logging.h"
#include "caffe2/core/operator.h"
#include "caffe2/utils/math.h"
#include "c10/util/irange.h"
namespace caffe2 {
// A hacky version of Mul with broadcast
// RowMul([mat, w], [output])
template <typename T, class Context>
class RowMulOp : public Operator<Context> {
public:
USE_OPERATOR_CONTEXT_FUNCTIONS;
USE_SIMPLE_CTOR_DTOR(RowMulOp);
bool RunOnDevice() override {
auto& mat = Input(0);
auto& w = Input(1);
auto* output = Output(0, mat.sizes(), at::dtype<T>());
T* output_data = output->template mutable_data<T>();
const T* mat_data = mat.template data<T>();
const T* w_data = w.template data<T>();
// Dimension checking
CAFFE_ENFORCE_EQ(
w.numel(),
mat.dim32(0),
"Length of w should be equal to the first dim of mat");
auto block_size = mat.size_from_dim(1);
for (const auto i : c10::irange(w.numel())) {
size_t offset = i * block_size;
for (const auto j : c10::irange(block_size)) {
output_data[offset + j] = mat_data[offset + j] * w_data[i];
}
}
return true;
}
};
// A hacky version
template <typename T, class Context>
class ReduceTailSumOp : public Operator<Context> {
public:
USE_OPERATOR_CONTEXT_FUNCTIONS;
USE_SIMPLE_CTOR_DTOR(ReduceTailSumOp);
bool RunOnDevice() override {
auto& mat = Input(0);
int N = mat.dim32(0);
int block_size = mat.size_from_dim(1);
auto* output = Output(0, {N}, at::dtype<T>());
T* output_data = output->template mutable_data<T>();
const T* mat_data = mat.template data<T>();
for (const auto i : c10::irange(N)) {
output_data[i] = 0;
size_t offset = i * block_size;
for (const auto j : c10::irange(block_size)) {
output_data[i] += mat_data[offset + j];
}
}
return true;
}
};
} // namespace caffe2
#endif // CAFFE2_OPERATORS_ROW_MUL_H_