pytorch/caffe2/operators/cast_op.h
Shai Szulanski 0ddaaf6a92 [codemod][caffe2] Run clang-format - 5/7
Summary:
This directory is opted-in to clang-format but is not format-clean. This blocks continuous formatting from being enabled on fbcode, and causes hassle for other codemods that leave inconsistent formatting. This diff runs clang-format, which is widely used and considered safe.

If you are unhappy with the formatting of a particular block, please *accept this diff* and then in a stacked commit undo the change and wrap that code in `// clang-format off` and `// clang-format on`, or `/* clang-format off */` and `/* clang-format on */`.

drop-conflicts

Test Plan: sandcastleit

Reviewed By: jerryzh168

Differential Revision: D22311706

fbshipit-source-id: 1ca59a82e96156a4a5dfad70ba3e64d44c5e762a
2020-06-30 15:45:11 -07:00

55 lines
1.4 KiB
C++

#pragma once
#include "caffe2/core/context.h"
#include "caffe2/core/logging.h"
#include "caffe2/core/operator.h"
#include "caffe2/core/types.h"
#include "caffe2/utils/cast.h"
#include "caffe2/utils/conversions.h"
#include "caffe2/utils/math.h"
namespace caffe2 {
template <class Context>
class CastOp : public Operator<Context> {
public:
USE_OPERATOR_CONTEXT_FUNCTIONS;
explicit CastOp(const OperatorDef& operator_def, Workspace* ws)
: Operator<Context>(operator_def, ws) {
const ArgumentHelper helper(operator_def);
TensorProto_DataType to = cast::GetCastDataType(helper, "to");
TensorProto_DataType from = cast::GetCastDataType(helper, "from_type");
SetBody(to);
}
bool RunOnDevice() override {
return (this->*body_)();
}
// Allow for Context-specific implementations
void SetBody(TensorProto_DataType to);
template <typename DstType>
bool DoRunWithDstType();
template <typename DstType, typename SrcType>
bool DoRunWithType() {
auto& input = Input(0);
auto* output = Output(0);
output->ResizeLike(input);
const auto* data = input.template data<SrcType>();
auto* out = output->template mutable_data<DstType>();
auto N = input.size();
for (int64_t i = 0; i < N; ++i) {
out[i] = static_cast<DstType>(data[i]);
}
return true;
}
private:
bool (CastOp::*body_)();
};
} // namespace caffe2