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 <torch/csrc/jit/codegen/cuda/executor.h>
#include <torch/csrc/jit/codegen/cuda/fusion.h>
#include <torch/csrc/jit/codegen/cuda/ir_all_nodes.h>
#include <torch/csrc/jit/codegen/cuda/ir_builder.h>
#include <torch/csrc/jit/codegen/cuda/ir_utils.h>
#include <torch/csrc/jit/codegen/cuda/lower2device.h>
#include <torch/csrc/jit/codegen/cuda/ops/all_ops.h>
#include <torch/csrc/jit/codegen/cuda/scheduler/all_schedulers.h>
#include <benchmark/benchmark.h>
#include <cuda_runtime.h>
#include <benchmarks/cpp/nvfuser/utils.h>
using namespace torch::jit::fuser::cuda;
//------------------------------------------------------------------------------
static void setupRMSNorm_BWD(Fusion* fusion, DataType dtype) {
FusionGuard fg(fusion);
TORCH_INTERNAL_ASSERT(
dtype == DataType::Float || dtype == DataType::Half ||
dtype == DataType::BFloat16);
const int kReductionAxis = 2;
Double* eps_ptr = IrBuilder::create<Double>(1e-6);
// setup fusion
auto grad_out = makeContigTensor(3, dtype);
auto input = makeContigTensor(3, dtype);
auto weight = makeContigTensor(1, dtype);
auto rstd = TensorViewBuilder()
.contiguity({false, false, false})
.shape({-1, -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) {
TORCH_INTERNAL_ASSERT(
dtype == DataType::Float || dtype == DataType::Half ||
dtype == DataType::BFloat16);
std::vector<int64_t> input_shape{8, benchmark_state.range(0), 1024};
// 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[2]}, options);
at::Tensor rstd = at::randn({input_shape[0], input_shape[1], 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_fp32,
setupRMSNorm_BWD,
NvFuserScheduler_RMSNorm_BWD,
DataType::Float);
NVFUSER_BENCHMARK_RUN(NvFuserScheduler_RMSNorm_BWD_fp32)
->RangeMultiplier(2)
->Ranges({{16, 64}})
->Unit(benchmark::kMicrosecond)
->UseManualTime();
NVFUSER_BENCHMARK_RUN(NvFuserScheduler_RMSNorm_BWD_fp32)
->RangeMultiplier(2)
->Ranges({{28, 56}})
->Unit(benchmark::kMicrosecond)
->UseManualTime();
NVFUSER_BENCHMARK_RUN(NvFuserScheduler_RMSNorm_BWD_fp32)
->RangeMultiplier(2)
->Ranges({{24, 48}})
->Unit(benchmark::kMicrosecond)
->UseManualTime();
NVFUSER_BENCHMARK_DEFINE(
NvFuserScheduler_RMSNorm_BWD_fp16,
setupRMSNorm_BWD,
NvFuserScheduler_RMSNorm_BWD,
DataType::Half);
NVFUSER_BENCHMARK_RUN(NvFuserScheduler_RMSNorm_BWD_fp16)
->RangeMultiplier(2)
->Ranges({{16, 64}})
->Unit(benchmark::kMicrosecond)
->UseManualTime();
NVFUSER_BENCHMARK_RUN(NvFuserScheduler_RMSNorm_BWD_fp16)
->RangeMultiplier(2)
->Ranges({{28, 56}})
->Unit(benchmark::kMicrosecond)
->UseManualTime();
NVFUSER_BENCHMARK_RUN(NvFuserScheduler_RMSNorm_BWD_fp16)
->RangeMultiplier(2)
->Ranges({{24, 48}})
->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();
|