pytorch/caffe2/operators/ensure_cpu_output_op.h
Junjie Bai 246f5c412e Revert "Tensor construction codemod(raw_mutable_data) (#16373)" (#18680)
Summary:
This reverts commit d73c830e23.

We have observed significant perf drop when training ResNext101 with multiple amd GPUs:

Before:
https://ci.pytorch.org/jenkins/job/caffe2-builds/job/py2-clang7-rocmdeb-ubuntu16.04-bench/1636/console
2 GPUs ResNext training got 150\~160 imgs/sec
4 GPUs ResNext training got 270\~280 imgs/sec

After:
https://ci.pytorch.org/jenkins/job/caffe2-builds/job/py2-clang7-rocmdeb-ubuntu16.04-bench/1637/console
Both 2 and 4 GPUs ResNext training drop to 110\~120 imgs/sec

Similar perf drop are seen on ResNet50 training jobs as well.
Pull Request resolved: https://github.com/pytorch/pytorch/pull/18680

Differential Revision: D14702941

Pulled By: bddppq

fbshipit-source-id: 828141805afc23f25c08d4a2eb6d4b99f817c128
2019-04-01 14:39:13 -07:00

52 lines
1.4 KiB
C++

#ifndef CAFFE2_OPERATORS_ENSURE_CPU_OUTPUT_OP_H_
#define CAFFE2_OPERATORS_ENSURE_CPU_OUTPUT_OP_H_
#include "caffe2/core/context.h"
#include "caffe2/core/operator.h"
#include "caffe2/utils/math.h"
namespace caffe2 {
template <class Context>
class EnsureCPUOutputOp : public Operator<Context> {
public:
USE_OPERATOR_CONTEXT_FUNCTIONS;
template <class... Args>
explicit EnsureCPUOutputOp(Args&&... args)
: Operator<Context>(std::forward<Args>(args)...) {}
bool RunOnDevice() override {
if (this->InputIsTensorType(0, CPU)) {
return CopyWithContext<CPUContext>();
} else if (this->InputIsTensorType(0, Context::GetDeviceType())) {
// CUDA Context will go this branch
return CopyWithContext<Context>();
} else {
CAFFE_THROW(
"Unexpected Input Blob: ",
OperatorBase::Inputs().at(0)->meta().name());
}
return true;
}
private:
template <class InputContext>
bool CopyWithContext() {
// Output is always on CPU
auto* output = this->template Output<Tensor>(0, CPU);
auto& input = this->template Input<Tensor>(0, InputContext::GetDeviceType());
output->ResizeLike(input);
context_.CopyItemsToCPU(
input.dtype(),
input.numel(),
input.raw_data(),
output->raw_mutable_data(input.dtype()));
context_.FinishDeviceComputation();
return true;
}
};
} // namespace caffe2
#endif // CAFFE2_OPERATORS_ENSURE_CPU_OUTPUT_OP_H_