Files
pytorch/caffe2/operators/string_ops.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_STRING_OPS_H_
#define CAFFE2_OPERATORS_STRING_OPS_H_
#include "caffe2/core/operator.h"
#include "caffe2/operators/elementwise_ops.h"
namespace caffe2 {
/**
* ForEach is a unary functor that forwards each element of the input array
* into the elementwise Functor provided, and gathers the results of each
* call into the resulting array. Use it as an adaptor if you want to create
* a UnaryElementwiseOp that acts on each element of the tensor per function
* call -- this is reasonable for complex types where vectorization wouldn't
* be much of a gain, performance-wise.
*/
template <typename Functor>
struct ForEach {
explicit ForEach(OperatorBase& op) : functor(op) {}
template <typename In, typename Out, typename Context>
bool operator()(int n, const In* in, Out* out, Context* /*c*/) {
for (const auto i : c10::irange(n)) {
out[i] = functor(in[i]);
}
return true;
}
Functor functor;
};
template <typename ScalarFunctor, typename TypeMap = FixedType<std::string>>
using StringElementwiseOp = UnaryElementwiseWithArgsOp<
TensorTypes<std::string>,
CPUContext,
ForEach<ScalarFunctor>,
TypeMap>;
template <class Context>
class StringJoinOp final : public Operator<Context> {
public:
USE_OPERATOR_CONTEXT_FUNCTIONS;
template <class... Args>
explicit StringJoinOp(Args&&... args)
: Operator<Context>(std::forward<Args>(args)...),
delimiter_(
this->template GetSingleArgument<std::string>("delimiter", ",")),
axis_(this->template GetSingleArgument<int>("axis", 0)) {
CAFFE_ENFORCE(axis_ == 0 || axis_ == 1);
}
bool RunOnDevice() override {
return DispatchHelper<TensorTypes<
float,
double,
int8_t,
uint8_t,
int16_t,
uint16_t,
int32_t,
int64_t,
std::string,
bool>>::call(this, Input(0));
}
template <typename T>
bool DoRunWithType();
protected:
std::string delimiter_;
int axis_;
};
} // namespace caffe2
#endif // CAFFE2_OPERATORS_STRING_OPS_H_