mirror of
https://github.com/pytorch/pytorch.git
synced 2026-11-20 09:04:25 +08:00
7b419e8513
Syncing nvfuser devel branch to upstream master. https://github.com/csarofeen/pytorch/ Codegen changes include: * codegen improvement: i. allow non-root trivial reductions, allow empty/no-op fusion ii. fixes vectorization checks and size calculation iii. bank conflict handle improvement iv. enables transpose scheduler * misc: i. CI tests failure fixes ii. cpp tests file clean up iii. trivial forwarding supports added in codegen runtime iv. added factory methods support in codegen Commits that's in this PR from the devel branch: ```7117a7e37epatching nvfuser conv cudnn test numerics mismatch (#2048)65af1a4e70Inserting sync for redundant parallel types is already done at the (#2023)6ac74d1816Fix sync map (#2047)f5bca33335Bank conflict checker improvements (#2032)d2ca7e3fd2Minor update on cp.async code generation. (#1901)d36cf61f55Test file cleanup (#2040)0b8e83f49cAllow non-root trivial reductions (#2037)a2dfe40b27Fix vectorize size calculation (#2035)e040676a31Use withPredicate to replace setPredicate to maintain Exprs immutable (#2025)197221b847removing ci workflow (#2034)40e2703d00Reduction rand like patch (#2031)bc772661cbAdd utility for checking bank conflict of shared memory (#2029)ddd1cf7695Add back FusionReductionWithTrivialReduction_CUDA (#2030)fbd97e5ef1Revert "Cleanup trivial reduction workarounds (#2006)" (#2024)bca20c1dfbCleanup trivial reduction workarounds (#2006)e4b65850eeTrivial forwarding (#1995)1a0e355b50Fix contiguity analysis of predicates to match updated contiguity. (#1991)a4effa6a5fEnable output allocation cache (#2010)35440b7953Patching bn inference (#2016)0f9f0b4060Add matmul benchmark (#2007)45045cd05eEnable tests previously disabled due to an aliasing bug (#2005)967aa77d2cContiguous indexing for View operations (#1990)a43cb20f48Make inlining even more modular (#2004)dc458358c0Test util cleanup (#2003)3ca21ebe4dMore strict validation (#2000)a7a7d57331Fix build problem (#1999)fc235b064eJust fixes comments (#1998)482386c050cleanup (#1997)4cbe0db655Improve divisible split detection (#1970)42ccc52bdcMinor build fix. (#1996)fcf8c091f7Cleanup of lower_utils.cpp: Isolate out GpuLower usage (#1989)15f2f6dba8Move ConcretizedBroadcastDomains to shared_ptr in GpuLower. (#1988)8f1c7f5267Minor cleanup lower_unroll.cpp (#1994)1d9858c803Minor cleanup (#1992)f262d9cab5Add support for uniform RNG (#1986)eb1dad10c7Remove non-const functions, remove GpuLower instance on build, pass in ca_map. (#1987)634820c5e3Add support for some empty fusion (#1981)eabe8d844aSegment self mapping fusions (#1954)e96aacfd9cEnable Transpose operation (#1882)425dce2777Add a null scheduler that helps segmenting away no-op schedules (#1835)306d4a68f1Fix canScheduleCompileTime check of transpose scheduler (#1969)b1bd32cc1bMinor fix (#1967)bd93578143Enable transpose scheduler (#1927)b7a206e93bMove scheduler vectorize utilities into their own file (#1959)d9420e4ca0View scheduling (#1928)c668e13aeaUpstream push ci fixes (#1965)c40202bb40Fix dump effective bandwidth (#1962)93505bcbb9WAR on index mapping when exact and permissive maps differ (#1960)45e95fd1d3Allow splitting inner-most ID to create virtual innermost ID in transpose scheduler (#1930)a3ecb33944Improve the comments at the beginning of index_compute.h (#1946)f7bc3417ccRemove unused variables (#1955)df3393adbbSome cleanup (#1957)7d1d7c8724TVDomainGuard factory (#1953)357ba224c0Fill allocation with nan on tests (#1956)8eafc54685Fix detection of unmappable root domains (#1952)90a51f2826Some indexing cleanups, Add eye support (#1940)ddc01e4e16Exclude unsupported data types (#1951)992e17c068test the groups the same order as they are merged (#1949)208262b75dMove detection of self mapping IDs to IterDomainGraph from (#1941)ac4de38c6eMerge pull request #1945 from csarofeen/master_merge_0828631094891aAdd full, full_like, zeros, zeros_like, ones, ones_like (#1943)aab10bce45Merge remote-tracking branch 'upstream/viable/strict' into HEAD4c254c063bFix arange when step is negative (#1942)89330aa23aTensor factories must set the output shape as its input (#1939) ``` RUN_TORCHBENCH: nvfuser Differential Revision: [D40869846](https://our.internmc.facebook.com/intern/diff/D40869846) Pull Request resolved: https://github.com/pytorch/pytorch/pull/87779 Approved by: https://github.com/davidberard98
275 lines
9.2 KiB
C++
275 lines
9.2 KiB
C++
#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 setupLayerNorm_BWD(Fusion* fusion, DataType dtype) {
|
|
FusionGuard fg(fusion);
|
|
|
|
TORCH_INTERNAL_ASSERT(dtype == DataType::Float || dtype == DataType::Half);
|
|
|
|
// setup fusion
|
|
auto grad_out = makeContigTensor(2, dtype);
|
|
auto input = makeContigTensor(2, dtype);
|
|
auto weight = makeContigTensor(1, dtype);
|
|
auto bias = makeContigTensor(1, dtype);
|
|
|
|
auto mean = TensorViewBuilder()
|
|
.contiguity({false, false})
|
|
.shape({-1, 1})
|
|
.dtype(DataType::Float)
|
|
.build();
|
|
auto rstd = TensorViewBuilder()
|
|
.contiguity({false, false})
|
|
.shape({-1, 1})
|
|
.dtype(DataType::Float)
|
|
.build();
|
|
|
|
fusion->addInput(grad_out);
|
|
fusion->addInput(input);
|
|
fusion->addInput(weight);
|
|
fusion->addInput(bias);
|
|
fusion->addInput(mean);
|
|
fusion->addInput(rstd);
|
|
|
|
if (dtype == DataType::Half) {
|
|
grad_out = castOp(DataType::Float, grad_out);
|
|
input = castOp(DataType::Float, input);
|
|
weight = castOp(DataType::Float, weight);
|
|
bias = castOp(DataType::Float, bias);
|
|
}
|
|
|
|
auto layer_norm_results = layer_norm_backward(
|
|
grad_out, input, {1}, mean, rstd, weight, bias, {true, true, true});
|
|
|
|
if (dtype != DataType::Float) {
|
|
layer_norm_results.grad_input =
|
|
castOp(dtype, layer_norm_results.grad_input);
|
|
layer_norm_results.grad_bias = castOp(dtype, layer_norm_results.grad_bias);
|
|
layer_norm_results.grad_weight =
|
|
castOp(dtype, layer_norm_results.grad_weight);
|
|
}
|
|
|
|
fusion->addOutput(layer_norm_results.grad_input);
|
|
fusion->addOutput(layer_norm_results.grad_bias);
|
|
fusion->addOutput(layer_norm_results.grad_weight);
|
|
}
|
|
|
|
static void NvFuserScheduler_LayerNorm_BWD(
|
|
benchmark::State& benchmark_state,
|
|
FusionExecutorCache* fusion_executor_cache,
|
|
DataType dtype) {
|
|
TORCH_INTERNAL_ASSERT(dtype == DataType::Float || dtype == DataType::Half);
|
|
|
|
std::vector<int64_t> input_shape{
|
|
benchmark_state.range(0), benchmark_state.range(1)};
|
|
|
|
// inputs
|
|
at::manual_seed(0);
|
|
auto maybe_fp16_options =
|
|
at::TensorOptions().dtype(data_type_to_aten(dtype)).device(at::kCUDA, 0);
|
|
auto fp32_options =
|
|
at::TensorOptions().dtype(at::kFloat).device(at::kCUDA, 0);
|
|
at::Tensor grad_out = at::randn(input_shape, maybe_fp16_options);
|
|
at::Tensor input = at::randn(input_shape, maybe_fp16_options);
|
|
at::Tensor weight = at::randn({input_shape[1]}, maybe_fp16_options);
|
|
at::Tensor bias = at::randn({input_shape[1]}, maybe_fp16_options);
|
|
at::Tensor mean = at::randn({input_shape[0], 1}, fp32_options);
|
|
at::Tensor rstd = at::randn({input_shape[0], 1}, fp32_options);
|
|
|
|
std::vector<c10::IValue> aten_inputs(
|
|
{grad_out, input, weight, bias, mean, rstd});
|
|
|
|
runBenchmarkIterations(benchmark_state, fusion_executor_cache, aten_inputs);
|
|
|
|
benchmark_state.SetBytesProcessed(
|
|
int64_t(benchmark_state.iterations()) *
|
|
(3 * input.numel() + weight.numel() + bias.numel() + mean.numel() +
|
|
rstd.numel()) *
|
|
int64_t(dataTypeSize(dtype)));
|
|
}
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
static void Baseline_LayerNorm_BWD(
|
|
benchmark::State& benchmark_state,
|
|
DataType dtype) {
|
|
TORCH_INTERNAL_ASSERT(dtype == DataType::Float || dtype == DataType::Half);
|
|
|
|
std::vector<int64_t> input_shape{
|
|
benchmark_state.range(0), benchmark_state.range(1)};
|
|
const size_t kReductionAxis = 1;
|
|
std::vector<int64_t> norm_shape;
|
|
for (auto idx = kReductionAxis; idx < input_shape.size(); ++idx) {
|
|
norm_shape.push_back(input_shape[idx]);
|
|
}
|
|
|
|
// inputs
|
|
at::manual_seed(0);
|
|
auto maybe_fp16_options =
|
|
at::TensorOptions().dtype(data_type_to_aten(dtype)).device(at::kCUDA, 0);
|
|
auto fp32_options =
|
|
at::TensorOptions().dtype(at::kFloat).device(at::kCUDA, 0);
|
|
at::Tensor grad_out = at::randn(input_shape, maybe_fp16_options);
|
|
at::Tensor input = at::randn(input_shape, maybe_fp16_options);
|
|
at::Tensor weight = at::randn({input_shape[1]}, maybe_fp16_options);
|
|
at::Tensor bias = at::randn({input_shape[1]}, maybe_fp16_options);
|
|
at::Tensor mean = at::randn({input_shape[0], 1}, fp32_options);
|
|
at::Tensor rstd = at::randn({input_shape[0], 1}, fp32_options);
|
|
std::array<bool, 3> output_mask = {true, true, true};
|
|
|
|
clearL2Cache();
|
|
C10_CUDA_CHECK(cudaDeviceSynchronize());
|
|
for (auto _ : benchmark_state) {
|
|
CudaKernelTimer timer;
|
|
at::native_layer_norm_backward(
|
|
grad_out, input, norm_shape, mean, rstd, weight, bias, output_mask);
|
|
|
|
auto output = at::layer_norm(input, norm_shape, weight, bias);
|
|
benchmark_state.SetIterationTime(timer.elapsed() / 1000.0);
|
|
C10_CUDA_CHECK(cudaDeviceSynchronize());
|
|
clearL2Cache();
|
|
C10_CUDA_CHECK(cudaDeviceSynchronize());
|
|
}
|
|
|
|
benchmark_state.SetBytesProcessed(
|
|
int64_t(benchmark_state.iterations()) *
|
|
(3 * input.numel() + weight.numel() + bias.numel() + mean.numel() +
|
|
rstd.numel()) *
|
|
int64_t(dataTypeSize(dtype)));
|
|
}
|
|
|
|
static void Baseline_LayerNorm_BWD_fp32(benchmark::State& benchmark_state) {
|
|
Baseline_LayerNorm_BWD(benchmark_state, DataType::Float);
|
|
}
|
|
|
|
static void Baseline_LayerNorm_BWD_fp16(benchmark::State& benchmark_state) {
|
|
Baseline_LayerNorm_BWD(benchmark_state, DataType::Half);
|
|
}
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
NVFUSER_BENCHMARK_DEFINE(
|
|
NvFuserScheduler_LayerNorm_BWD_fp32,
|
|
setupLayerNorm_BWD,
|
|
NvFuserScheduler_LayerNorm_BWD,
|
|
DataType::Float);
|
|
|
|
NVFUSER_BENCHMARK_RUN(NvFuserScheduler_LayerNorm_BWD_fp32)
|
|
// ->RangeMultiplier(2)
|
|
->Ranges({{160, 320}, {2, 1024 * 1024}})
|
|
->Unit(benchmark::kMicrosecond)
|
|
->UseManualTime();
|
|
|
|
NVFUSER_BENCHMARK_RUN(NvFuserScheduler_LayerNorm_BWD_fp32)
|
|
// ->RangeMultiplier(2)
|
|
->Ranges({{2, 16}, {32768, 16 * 1024 * 1024}})
|
|
->Unit(benchmark::kMicrosecond)
|
|
->UseManualTime();
|
|
|
|
NVFUSER_BENCHMARK_RUN(NvFuserScheduler_LayerNorm_BWD_fp32)
|
|
// ->RangeMultiplier(2)
|
|
->Ranges({{32768, 16 * 1024 * 1024}, {2, 16}})
|
|
->Unit(benchmark::kMicrosecond)
|
|
->UseManualTime();
|
|
|
|
NVFUSER_BENCHMARK_RUN(NvFuserScheduler_LayerNorm_BWD_fp32)
|
|
// ->RangeMultiplier(2)
|
|
->Ranges({{128, 1024 * 16}, {128, 1024 * 16}})
|
|
->Unit(benchmark::kMicrosecond)
|
|
->UseManualTime();
|
|
|
|
NVFUSER_BENCHMARK_DEFINE(
|
|
NvFuserScheduler_LayerNorm_BWD_fp16,
|
|
setupLayerNorm_BWD,
|
|
NvFuserScheduler_LayerNorm_BWD,
|
|
DataType::Half);
|
|
|
|
NVFUSER_BENCHMARK_RUN(NvFuserScheduler_LayerNorm_BWD_fp16)
|
|
// ->RangeMultiplier(2)
|
|
->Ranges({{160, 320}, {2, 1024 * 1024}})
|
|
->Unit(benchmark::kMicrosecond)
|
|
->UseManualTime();
|
|
|
|
NVFUSER_BENCHMARK_RUN(NvFuserScheduler_LayerNorm_BWD_fp16)
|
|
// ->RangeMultiplier(2)
|
|
->Ranges({{2, 16}, {32768, 32 * 1024 * 1024}})
|
|
->Unit(benchmark::kMicrosecond)
|
|
->UseManualTime();
|
|
|
|
NVFUSER_BENCHMARK_RUN(NvFuserScheduler_LayerNorm_BWD_fp16)
|
|
// ->RangeMultiplier(2)
|
|
->Ranges({{32768, 32 * 1024 * 1024}, {2, 16}})
|
|
->Unit(benchmark::kMicrosecond)
|
|
->UseManualTime();
|
|
|
|
NVFUSER_BENCHMARK_RUN(NvFuserScheduler_LayerNorm_BWD_fp16)
|
|
// ->RangeMultiplier(2)
|
|
->Ranges({{128, 1024 * 16}, {128, 1024 * 16}})
|
|
->Unit(benchmark::kMicrosecond)
|
|
->UseManualTime();
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
BENCHMARK(Baseline_LayerNorm_BWD_fp32)
|
|
// ->RangeMultiplier(2)
|
|
->Ranges({{160, 320}, {2, 1024 * 1024}})
|
|
->Unit(benchmark::kMicrosecond)
|
|
->UseManualTime();
|
|
|
|
BENCHMARK(Baseline_LayerNorm_BWD_fp32)
|
|
// ->RangeMultiplier(2)
|
|
->Ranges({{2, 16}, {32768, 16 * 1024 * 1024}})
|
|
->Unit(benchmark::kMicrosecond)
|
|
->UseManualTime();
|
|
|
|
BENCHMARK(Baseline_LayerNorm_BWD_fp32)
|
|
// ->RangeMultiplier(2)
|
|
->Ranges({{32768, 16 * 1024 * 1024}, {2, 16}})
|
|
->Unit(benchmark::kMicrosecond)
|
|
->UseManualTime();
|
|
|
|
BENCHMARK(Baseline_LayerNorm_BWD_fp32)
|
|
// ->RangeMultiplier(2)
|
|
->Ranges({{128, 1024 * 16}, {128, 1024 * 16}})
|
|
->Unit(benchmark::kMicrosecond)
|
|
->UseManualTime();
|
|
|
|
BENCHMARK(Baseline_LayerNorm_BWD_fp16)
|
|
// ->RangeMultiplier(2)
|
|
->Ranges({{160, 320}, {2, 1024 * 1024}})
|
|
->Unit(benchmark::kMicrosecond)
|
|
->UseManualTime();
|
|
|
|
BENCHMARK(Baseline_LayerNorm_BWD_fp16)
|
|
// ->RangeMultiplier(2)
|
|
->Ranges({{2, 16}, {32768, 32 * 1024 * 1024}})
|
|
->Unit(benchmark::kMicrosecond)
|
|
->UseManualTime();
|
|
|
|
BENCHMARK(Baseline_LayerNorm_BWD_fp16)
|
|
// ->RangeMultiplier(2)
|
|
->Ranges({{32768, 32 * 1024 * 1024}, {2, 16}})
|
|
->Unit(benchmark::kMicrosecond)
|
|
->UseManualTime();
|
|
|
|
BENCHMARK(Baseline_LayerNorm_BWD_fp16)
|
|
// ->RangeMultiplier(2)
|
|
->Ranges({{128, 1024 * 16}, {128, 1024 * 16}})
|
|
->Unit(benchmark::kMicrosecond)
|
|
->UseManualTime();
|