forked from NVIDIA/Fuser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrms_norm_backward.cpp
157 lines (127 loc) · 4.64 KB
/
rms_norm_backward.cpp
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
// clang-format off
/*
* SPDX-FileCopyrightText: Copyright (c) 2023-present NVIDIA CORPORATION & AFFILIATES.
* All rights reserved.
* SPDX-License-Identifier: BSD-3-Clause
*/
// clang-format on
#include <csrc/exceptions.h>
#include <device_lower/lower2device.h>
#include <executor.h>
#include <fusion.h>
#include <ir/all_nodes.h>
#include <ir/builder.h>
#include <ir/utils.h>
#include <ops/all_ops.h>
#include <scheduler/all_schedulers.h>
#include <benchmark/benchmark.h>
#include <cuda_runtime.h>
#include <benchmark/utils.h>
#include <test/utils.h>
using namespace nvfuser;
//------------------------------------------------------------------------------
static void setupRMSNorm_BWD(Fusion* fusion, DataType dtype) {
FusionGuard fg(fusion);
NVF_ERROR(
dtype == DataType::Float || dtype == DataType::Half ||
dtype == DataType::BFloat16);
// setup fusion
auto grad_out = makeContigTensor(2, dtype);
auto input = makeContigTensor(2, dtype);
auto weight = makeContigTensor(1, dtype);
auto rstd = TensorViewBuilder()
.contiguity({false, std::nullopt})
.shape({-1, 1})
.dtype(dtype)
.build();
fusion->addInput(grad_out);
fusion->addInput(input);
fusion->addInput(weight);
fusion->addInput(rstd);
if (dtype == DataType::Half) {
grad_out = castOp(DataType::Float, grad_out);
input = castOp(DataType::Float, input);
weight = castOp(DataType::Float, weight);
rstd = castOp(DataType::Float, rstd);
}
auto rms_norm_results =
rms_norm_backward(grad_out, input, {1}, rstd, weight, {true, true, true});
if (dtype != DataType::Float) {
rms_norm_results.grad_input = castOp(dtype, rms_norm_results.grad_input);
rms_norm_results.grad_weight = castOp(dtype, rms_norm_results.grad_weight);
}
fusion->addOutput(rms_norm_results.grad_input);
fusion->addOutput(rms_norm_results.grad_weight);
}
static void NvFuserScheduler_RMSNorm_BWD(
benchmark::State& benchmark_state,
FusionExecutorCache* fusion_executor_cache,
DataType dtype) {
NVF_ERROR(
dtype == DataType::Float || dtype == DataType::Half ||
dtype == DataType::BFloat16);
std::vector<int64_t> input_shape{
benchmark_state.range(0), benchmark_state.range(1)};
// inputs
at::manual_seed(0);
auto options =
at::TensorOptions().dtype(data_type_to_aten(dtype)).device(at::kCUDA, 0);
at::Tensor grad_out = at::randn(input_shape, options);
at::Tensor input = at::randn(input_shape, options);
at::Tensor weight = at::randn({input_shape[1]}, options);
at::Tensor rstd = at::randn({input_shape[0], 1}, options);
std::vector<c10::IValue> aten_inputs({grad_out, input, weight, rstd});
runBenchmarkIterations(benchmark_state, fusion_executor_cache, aten_inputs);
benchmark_state.SetBytesProcessed(
int64_t(benchmark_state.iterations()) *
(3 * input.numel() + weight.numel() + rstd.numel()) *
int64_t(dataTypeSize(dtype)));
}
//------------------------------------------------------------------------------
NVFUSER_BENCHMARK_DEFINE(
NvFuserScheduler_RMSNorm_BWD_fp16,
setupRMSNorm_BWD,
NvFuserScheduler_RMSNorm_BWD,
DataType::Half);
NVFUSER_BENCHMARK_DEFINE(
NvFuserScheduler_RMSNorm_BWD_fp32,
setupRMSNorm_BWD,
NvFuserScheduler_RMSNorm_BWD,
DataType::Float);
NVFUSER_BENCHMARK_RUN(NvFuserScheduler_RMSNorm_BWD_fp16)
->Apply(addCasesOneWave128To32K)
->Unit(benchmark::kMicrosecond)
->UseManualTime();
NVFUSER_BENCHMARK_RUN(NvFuserScheduler_RMSNorm_BWD_fp16)
->Apply(addCases16Wave128To32K)
->Unit(benchmark::kMicrosecond)
->UseManualTime();
NVFUSER_BENCHMARK_RUN(NvFuserScheduler_RMSNorm_BWD_fp32)
->Apply(addCasesOneWave128To32K)
->Unit(benchmark::kMicrosecond)
->UseManualTime();
NVFUSER_BENCHMARK_RUN(NvFuserScheduler_RMSNorm_BWD_fp32)
->Apply(addCases16Wave128To32K)
->Unit(benchmark::kMicrosecond)
->UseManualTime();
// TODO: Automatically disable/enable if bf16 is supported
// NVFUSER_BENCHMARK_DEFINE(
// NvFuserScheduler_RMSNorm_BWD_bf16,
// setupRMSNorm_BWD,
// NvFuserScheduler_RMSNorm_BWD,
// DataType::BFloat16);
// NVFUSER_BENCHMARK_RUN(NvFuserScheduler_RMSNorm_BWD_bf16)
// ->RangeMultiplier(2)
// ->Ranges({{16, 64}})
// ->Unit(benchmark::kMicrosecond)
// ->UseManualTime();
// NVFUSER_BENCHMARK_RUN(NvFuserScheduler_RMSNorm_BWD_bf16)
// ->RangeMultiplier(2)
// ->Ranges({{28, 56}})
// ->Unit(benchmark::kMicrosecond)
// ->UseManualTime();
// NVFUSER_BENCHMARK_RUN(NvFuserScheduler_RMSNorm_BWD_bf16)
// ->RangeMultiplier(2)
// ->Ranges({{24, 48}})
// ->Unit(benchmark::kMicrosecond)
// ->UseManualTime();