mirror of
https://github.com/zebrajr/pytorch.git
synced 2025-12-07 12:21:27 +01:00
Summary:
This is an automatic change generated by the following script:
```
#!/usr/bin/env python3
from subprocess import check_output, check_call
import os
def get_compiled_files_list():
import json
with open("build/compile_commands.json") as f:
data = json.load(f)
files = [os.path.relpath(node['file']) for node in data]
for idx, fname in enumerate(files):
if fname.startswith('build/') and fname.endswith('.DEFAULT.cpp'):
files[idx] = fname[len('build/'):-len('.DEFAULT.cpp')]
return files
def run_clang_tidy(fname):
check_call(["python3", "tools/clang_tidy.py", "-c", "build", "-x", fname,"-s"])
changes = check_output(["git", "ls-files", "-m"])
if len(changes) == 0:
return
check_call(["git", "commit","--all", "-m", f"NOLINT stubs for {fname}"])
def main():
git_files = check_output(["git", "ls-files"]).decode("ascii").split("\n")
compiled_files = get_compiled_files_list()
for idx, fname in enumerate(git_files):
if fname not in compiled_files:
continue
if fname.startswith("caffe2/contrib/aten/"):
continue
print(f"[{idx}/{len(git_files)}] Processing {fname}")
run_clang_tidy(fname)
if __name__ == "__main__":
main()
```
Pull Request resolved: https://github.com/pytorch/pytorch/pull/56892
Reviewed By: H-Huang
Differential Revision: D27991944
Pulled By: malfet
fbshipit-source-id: 5415e1eb2c1b34319a4f03024bfaa087007d7179
122 lines
2.8 KiB
C++
122 lines
2.8 KiB
C++
#include "caffe2/operators/cosh_op.h"
|
|
|
|
#include <algorithm>
|
|
#include <functional>
|
|
|
|
namespace caffe2 {
|
|
|
|
template <>
|
|
template <typename T>
|
|
bool CoshGradientFunctor<CPUContext>::Forward(
|
|
const std::vector<int>& /* dY_dims */,
|
|
const std::vector<int>& X_dims,
|
|
const T* dY,
|
|
const T* X,
|
|
T* dX,
|
|
CPUContext* /* context */) const {
|
|
const int size = std::accumulate(
|
|
// NOLINTNEXTLINE(modernize-use-transparent-functors)
|
|
X_dims.cbegin(), X_dims.cend(), 1, std::multiplies<int>());
|
|
ConstEigenVectorArrayMap<T> dY_arr(dY, size);
|
|
ConstEigenVectorArrayMap<T> X_arr(X, size);
|
|
EigenVectorMap<T>(dX, size) = dY_arr * (X_arr.exp() - (-X_arr).exp()) / 2;
|
|
return true;
|
|
}
|
|
|
|
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
|
|
REGISTER_CPU_OPERATOR(
|
|
Cosh,
|
|
UnaryElementwiseOp<
|
|
TensorTypes<float>,
|
|
CPUContext,
|
|
CoshFunctor<CPUContext>>);
|
|
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
|
|
REGISTER_CPU_OPERATOR(
|
|
CoshGradient,
|
|
BinaryElementwiseOp<
|
|
TensorTypes<float>,
|
|
CPUContext,
|
|
CoshGradientFunctor<CPUContext>>);
|
|
|
|
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
|
|
OPERATOR_SCHEMA(Cosh)
|
|
.NumInputs(1)
|
|
.NumOutputs(1)
|
|
.IdenticalTypeAndShape()
|
|
.SetDoc(R"DOC(
|
|
Calculates the hyperbolic cosine of the given input tensor, element-wise.
|
|
|
|
Github Links:
|
|
|
|
- https://github.com/pytorch/pytorch/blob/master/caffe2/operators/cosh_op.cc
|
|
|
|
|
|
<details>
|
|
|
|
<summary> <b>Example</b> </summary>
|
|
|
|
**Code**
|
|
|
|
```
|
|
|
|
workspace.ResetWorkspace()
|
|
|
|
op = core.CreateOperator(
|
|
"Cosh",
|
|
["X"],
|
|
["Y"]
|
|
)
|
|
|
|
workspace.FeedBlob("X", np.random.rand(5).astype(np.float32))
|
|
print("X:", workspace.FetchBlob("X"))
|
|
workspace.RunOperatorOnce(op)
|
|
print("Y:", workspace.FetchBlob("Y"))
|
|
|
|
```
|
|
|
|
**Result**
|
|
|
|
```
|
|
|
|
X: [0.66423494 0.32074615 0.81523746 0.90423071 0.39275789]
|
|
Y: [1.22883528 1.05188156 1.35112322 1.43744212 1.07812598]
|
|
|
|
```
|
|
|
|
</details>
|
|
|
|
)DOC")
|
|
.Input(0, "input", "Input tensor")
|
|
.Output(
|
|
0,
|
|
"output",
|
|
"The hyperbolic cosine values of the input tensor, computed "
|
|
"element-wise")
|
|
.InheritOnnxSchema();
|
|
|
|
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
|
|
OPERATOR_SCHEMA(CoshGradient)
|
|
.NumInputs(2)
|
|
.NumOutputs(1)
|
|
.IdenticalTypeAndShapeOfInput(0);
|
|
|
|
namespace {
|
|
|
|
class GetCoshGradient : public GradientMakerBase {
|
|
using GradientMakerBase::GradientMakerBase;
|
|
std::vector<OperatorDef> GetGradientDefs() override {
|
|
return SingleGradientDef(
|
|
"CoshGradient",
|
|
"",
|
|
std::vector<std::string>{GO(0), I(0)},
|
|
std::vector<std::string>{GI(0)});
|
|
}
|
|
};
|
|
|
|
} // namespace
|
|
|
|
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
|
|
REGISTER_GRADIENT(Cosh, GetCoshGradient);
|
|
|
|
} // namespace caffe2
|