No Op Optimizer (#12390)

Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/12390

Introduce a no op optimizer for when we don't want updates to happen, but don't want to affect downstream processes.

Reviewed By: mlappelbaum

Differential Revision: D10209812

fbshipit-source-id: 2af4ebc0fb42e78ea851c3a9f4860f3d224037b6
This commit is contained in:
Dong Shi 2018-10-10 18:06:58 -07:00 committed by Facebook Github Bot
parent 8399778049
commit da3dd9af12
4 changed files with 75 additions and 0 deletions

View File

@ -0,0 +1,17 @@
#include "caffe2/operators/data_couple.h"
namespace caffe2 {
REGISTER_CPU_OPERATOR(DataCouple, DataCoupleOp<CPUContext>);
OPERATOR_SCHEMA(DataCouple)
.EnforceOneToOneInplace()
.SetDoc(R"DOC(
A one to one operator that takes an arbitrary number of input and output blobs
such that each input blob is inplace with it's matching output blob. It then proceedes
to do nothing with each of these operators. This serves two purposes. It can make it
appear as if a blob has been written to, as well as can tie together different blobs
in a data dependency
)DOC");
} // namespace caffe2

View File

@ -0,0 +1,22 @@
#ifndef CAFFE2_OPERATORS_NO_OP_OPTIMIZER_OP_H_
#define CAFFE2_OPERATORS_NO_OP_OPTIMIZER_OP_H_
#include "caffe2/core/context.h"
#include "caffe2/core/operator.h"
namespace caffe2 {
template <class Context>
class DataCoupleOp : public Operator<Context> {
public:
USE_SIMPLE_CTOR_DTOR(DataCoupleOp)
bool RunOnDevice() override {
// Actually does nothing...
return true;
}
};
} // namespace caffe2
#endif // CAFFE2_OPERATORS_NO_OP_OPTIMIZER_OP_H_

View File

@ -0,0 +1,6 @@
#include "caffe2/core/context_gpu.h"
#include "caffe2/operators/data_couple.h"
namespace caffe2 {
REGISTER_CUDA_OPERATOR(DataCouple, DataCoupleOp<CUDAContext>);
}

View File

@ -0,0 +1,30 @@
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
from caffe2.python import core, workspace
from caffe2.python.test_util import TestCase
import numpy as np
class TestDataCoupleOp(TestCase):
def test_data_couple_op(self):
param_array = np.random.rand(10, 10)
gradient_array = np.random.rand(10, 10)
extra_array = np.random.rand(10, 10)
workspace.FeedBlob("param", param_array)
workspace.FeedBlob("gradient", gradient_array)
workspace.FeedBlob("extraBlob", extra_array)
workspace.RunOperatorOnce(core.CreateOperator(
"DataCouple",
["param", "gradient", "extraBlob"],
["param", "gradient"]))
result1 = workspace.FetchBlob('param')
result2 = workspace.FetchBlob('gradient')
self.assertFalse((result1 - param_array).any())
self.assertFalse((result2 - gradient_array).any())