mirror of
https://github.com/zebrajr/pytorch.git
synced 2025-12-07 12:21:27 +01:00
Summary: Pull Request resolved: https://github.com/pytorch/pytorch/pull/9939 Pull Request resolved: https://github.com/facebookresearch/weakly-supervised-action-detection/pull/13 Pull Request resolved: https://github.com/pytorch/translate/pull/166 Pull Request resolved: https://github.com/pytorch/pytorch/pull/9125 Closes https://github.com/pytorch/pytorch/pull/9125 Use inheritance for polymorphism, and remove template parameter This is to change the templating in call sites, the core implementations will change later Before Caffe2 Tensor class was compile-time fixed to bind to a particular device/context. With this change, we're making it a runtime property (stored inside the tensor), but preserve the same semantics. For example, one has to specify device type in order to create a Tensor - there are no uninitialized tensors. More specifically the changes are: 1. We added an extra argument *DeviceType* to most of the constructors of the tensor, e.g. (Tensor(DeviceType type)), 2. Semantics of constructor Tensor(const Tensor<SrcContext>& src, ContextForCopy* context); is changed, in this constructor, the second context is passed in to enable us to call the templated Copy function, it could be in a different context as source and target previously, now we'll enforce that the context should have same device type as src, if it is provided. 3. To preserve 'get-or-construct' semantics of Blob, we added specialized getter Blob::GetMutableTensor that verifies both that Blob contains a Tensor and that it's of a correct type 4. Specifically, Tensor type is not default-constructible any more (as we don't have unknown device tensors) and thus some of the code handling STL containers needs to change Note: Some changes are postponed just to keep this diff a bit smaller. Please see `TODO`s. Reviewed By: ezyang, houseroad Differential Revision: D9024330 fbshipit-source-id: e0b8295d2dc6ebe2963383ded5af799ad17164ba
108 lines
2.7 KiB
C++
108 lines
2.7 KiB
C++
#include "caffe2/operators/relu_n_op.h"
|
|
|
|
#include <algorithm>
|
|
#include <functional>
|
|
#include <string>
|
|
|
|
#include "caffe2/utils/eigen_utils.h"
|
|
|
|
namespace caffe2 {
|
|
|
|
template <>
|
|
template <typename T>
|
|
bool ReluNFunctor<CPUContext>::
|
|
operator()(const int N, const T* X, T* Y, CPUContext* /* context */) const {
|
|
EigenVectorMap<T>(Y, N) =
|
|
ConstEigenVectorMap<T>(X, N).cwiseMax(T(0)).cwiseMin(T(n));
|
|
return true;
|
|
}
|
|
|
|
template <>
|
|
template <typename T>
|
|
bool ReluNGradientFunctor<CPUContext>::Forward(
|
|
const std::vector<int>& Y_dims,
|
|
const std::vector<int>& /* dY_dims */,
|
|
const T* Y,
|
|
const T* dY,
|
|
T* dX,
|
|
CPUContext* /* context */) const {
|
|
const int size = std::accumulate(
|
|
Y_dims.cbegin(), Y_dims.cend(), 1, std::multiplies<int>());
|
|
ConstEigenVectorArrayMap<T> Y_arr(Y, size);
|
|
EigenVectorArrayMap<T>(dX, size) =
|
|
(Y_arr > T(0) && Y_arr < T(n))
|
|
.select(ConstEigenVectorArrayMap<T>(dY, size), T(0));
|
|
return true;
|
|
}
|
|
|
|
namespace {
|
|
|
|
OpSchema::Cost CostInferenceForReluN(
|
|
const OperatorDef& def,
|
|
const vector<TensorShape>& in) {
|
|
struct OpSchema::Cost cost = PointwiseCostInference<2>(def, in);
|
|
cost.params_bytes = 0;
|
|
return cost;
|
|
}
|
|
|
|
} // namespace
|
|
|
|
REGISTER_CPU_OPERATOR(
|
|
ReluN,
|
|
UnaryElementwiseWithArgsOp<
|
|
TensorTypes<float>,
|
|
CPUContext,
|
|
ReluNFunctor<CPUContext>>);
|
|
REGISTER_CPU_OPERATOR(
|
|
ReluNGradient,
|
|
BinaryElementwiseWithArgsOp<
|
|
TensorTypes<float>,
|
|
CPUContext,
|
|
ReluNGradientFunctor<CPUContext>>);
|
|
|
|
// Input: X, output: Y
|
|
OPERATOR_SCHEMA(ReluN)
|
|
.NumInputs(1)
|
|
.NumOutputs(1)
|
|
.Arg("n", "the cap of output")
|
|
.AllowInplace({{0, 0}})
|
|
.CostInferenceFunction(CostInferenceForReluN)
|
|
.IdenticalTypeAndShape()
|
|
.SetDoc(R"DOC(
|
|
Relu takes one input data (Tensor) and produces one output data
|
|
(Tensor) where the rectified linear function, y = min(max(0, x), n),
|
|
is applied to the tensor elementwise.
|
|
)DOC")
|
|
.Input(0, "X", "1D input tensor")
|
|
.Output(0, "Y", "1D input tensor");
|
|
|
|
// Input: Y, dY, output: dX
|
|
OPERATOR_SCHEMA(ReluNGradient)
|
|
.NumInputs(2)
|
|
.NumOutputs(1)
|
|
.Arg("n", "the cap of forward op output")
|
|
.AllowInplace({{1, 0}})
|
|
.SetDoc(R"DOC(
|
|
ReluGradient takes both Y and dY and uses this to update dX according to the
|
|
chain rule and derivatives of the rectified linear function.
|
|
)DOC");
|
|
|
|
namespace {
|
|
|
|
class GetReluNGradient : public GradientMakerBase {
|
|
using GradientMakerBase::GradientMakerBase;
|
|
std::vector<OperatorDef> GetGradientDefs() override {
|
|
return SingleGradientDef(
|
|
def_.type() + "Gradient",
|
|
"",
|
|
std::vector<std::string>{O(0), GO(0)},
|
|
std::vector<std::string>{GI(0)});
|
|
}
|
|
};
|
|
|
|
} // namespace
|
|
|
|
REGISTER_GRADIENT(ReluN, GetReluNGradient);
|
|
|
|
} // namespace caffe2
|