File: bench_signed_log1p.cpp

package info (click to toggle)
pytorch 1.13.1%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 139,252 kB
  • sloc: cpp: 1,100,274; python: 706,454; ansic: 83,052; asm: 7,618; java: 3,273; sh: 2,841; javascript: 612; makefile: 323; xml: 269; ruby: 185; yacc: 144; objc: 68; lex: 44
file content (166 lines) | stat: -rw-r--r-- 5,432 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#include <benchmark/benchmark.h>

#include <torch/csrc/jit/jit_log.h>
#include <torch/csrc/jit/tensorexpr/ir.h>
#include <torch/csrc/jit/tensorexpr/ir_simplifier.h>
#include <torch/csrc/jit/tensorexpr/llvm_codegen.h>
#include <torch/csrc/jit/tensorexpr/loopnest.h>
#include <torch/csrc/jit/tensorexpr/operators/operators.h>
#include <torch/csrc/jit/tensorexpr/tensor.h>
#include <torch/torch.h>

using namespace torch::jit::tensorexpr;

namespace {

class SignedLog1pBench : public benchmark::Fixture {
 public:
  void SetUp(const benchmark::State& state) override {
    input_size_ = {state.range(0), state.range(1)};
    input_size_int_ = {state.range(0), state.range(1)};
    input_ = torch::rand(input_size_);
    ref_ = signedLog1p(input_);
  }

  void TearDown(benchmark::State& state) override {
    TORCH_CHECK(at::allclose(ref_, output_, 1e-3, 1e-3));
    state.counters["GB/s"] = benchmark::Counter(
        uint64_t(state.iterations()) * 2 * output_.nbytes(),
        benchmark::Counter::kIsRate);
  }

  at::Tensor signedLog1p(const at::Tensor& inp) {
    auto sign = at::sign(inp);
    auto log1p = at::log1p(at::abs(inp));
    return sign * log1p;
  }

  void runATen(benchmark::State& state) {
    for (auto _ : state) {
      output_ = signedLog1p(input_);
    }
  }

  void runNNC(benchmark::State& state) {
    BufHandle input_ph(
        "input", {input_size_int_[0], input_size_int_[1]}, kFloat);
    Tensor abs_result = Compute(
        "aten_abs",
        {input_size_int_[0], input_size_int_[1]},
        [&](const VarHandle& m, const VarHandle& n) {
          return abs(input_ph.load(m, n));
        });
    Tensor log1p_result = Compute(
        "aten_log1p",
        {input_size_int_[0], input_size_int_[1]},
        [&](const VarHandle& m, const VarHandle& n) {
          return log1p(abs_result.load(m, n));
        });
    Tensor sign_result =
        computeSign({input_ph}, {input_size_int_[0], input_size_int_[1]});
    Tensor output = Compute(
        "aten_mul",
        {input_size_int_[0], input_size_int_[1]},
        [&](const VarHandle& m, const VarHandle& n) {
          return sign_result.load(m, n) * log1p_result.load(m, n);
        });
    LoopNest nest({output}, {abs_result, log1p_result, sign_result, output});
    GRAPH_DEBUG("Original Stmt: ", *nest.root_stmt());
    nest.inlineIntermediateBufs(true);
    nest.prepareForCodegen();
    nest.simplify();
    nest.vectorizeInnerLoops();
    nest.simplify();
    GRAPH_DEBUG("Final stmt: ", *nest.root_stmt());

    // StmtPtr s = IRSimplifier::simplify(nest.root_stmt());
    std::vector<CodeGen::BufferArg> buf_args;
    buf_args.emplace_back(input_ph);
    buf_args.emplace_back(output);
    LLVMCodeGen cg(nest.root_stmt(), buf_args);

    std::vector<CodeGen::CallArg> call_args;
    for (auto _ : state) {
      output_ = at::empty_like(ref_);
      call_args.clear();
      call_args.emplace_back(input_.data_ptr<float>());
      call_args.emplace_back(output_.data_ptr<float>());
      cg.call(call_args);
    }
  }

  void runNNCLogVml(benchmark::State& state) {
    BufHandle input_ph(
        "input", {input_size_int_[0], input_size_int_[1]}, kFloat);
    Tensor abs_result = Compute(
        "aten_abs",
        {input_size_int_[0], input_size_int_[1]},
        [&](const VarHandle& m, const VarHandle& n) {
          return abs(input_ph.load(m, n));
        });
    Tensor log_vml_result = Compute(
        "aten_log1p",
        {input_size_int_[0], input_size_int_[1]},
        [&](const VarHandle& m, const VarHandle& n) {
          return log_vml(abs_result.load(m, n) + ExprHandle(1));
        });
    Tensor sign_result =
        computeSign({input_ph}, {input_size_int_[0], input_size_int_[1]});
    Tensor output = Compute(
        "aten_mul",
        {input_size_int_[0], input_size_int_[1]},
        [&](const VarHandle& m, const VarHandle& n) {
          return sign_result.load(m, n) * log_vml_result.load(m, n);
        });
    LoopNest nest({output}, {abs_result, log_vml_result, sign_result, output});
    GRAPH_DEBUG("Original Stmt: ", *nest.root_stmt());
    nest.inlineIntermediateBufs(true);
    nest.prepareForCodegen();
    nest.simplify();
    nest.vectorizeInnerLoops();
    nest.simplify();
    GRAPH_DEBUG("Final stmt: ", *nest.root_stmt());

    // StmtPtr s = IRSimplifier::simplify(nest.root_stmt());
    std::vector<CodeGen::BufferArg> buf_args;
    buf_args.emplace_back(input_ph);
    buf_args.emplace_back(output);
    LLVMCodeGen cg(nest.root_stmt(), buf_args);

    std::vector<CodeGen::CallArg> call_args;
    for (auto _ : state) {
      output_ = at::empty_like(ref_);
      call_args.clear();
      call_args.emplace_back(input_.data_ptr<float>());
      call_args.emplace_back(output_.data_ptr<float>());
      cg.call(call_args);
    }
  }

 private:
  std::vector<long> input_size_;
  std::vector<int> input_size_int_;
  at::Tensor input_;
  at::Tensor output_;
  at::Tensor ref_;
};

} // namespace

BENCHMARK_DEFINE_F(SignedLog1pBench, ATen)(benchmark::State& state) {
  runATen(state);
}

BENCHMARK_DEFINE_F(SignedLog1pBench, NNC)(benchmark::State& state) {
  runNNC(state);
}

BENCHMARK_DEFINE_F(SignedLog1pBench, NNCLogVml)(benchmark::State& state) {
  runNNCLogVml(state);
}

BENCHMARK_REGISTER_F(SignedLog1pBench, ATen)->Args({10, 1467});

BENCHMARK_REGISTER_F(SignedLog1pBench, NNC)->Args({10, 1467});

BENCHMARK_REGISTER_F(SignedLog1pBench, NNCLogVml)->Args({10, 1467});