Files
pytorch/benchmarks/cpp/nvfuser/batch_norm_channels_first.cpp
T
jjsjann123 7b419e8513 [NVFuser] Upstream push 1026 (#87779)
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:

```
7117a7e37e patching nvfuser conv cudnn test numerics mismatch (#2048)
65af1a4e70 Inserting sync for redundant parallel types is already done at the (#2023)
6ac74d1816 Fix sync map (#2047)
f5bca33335 Bank conflict checker improvements (#2032)
d2ca7e3fd2 Minor update on cp.async code generation. (#1901)
d36cf61f55 Test file cleanup (#2040)
0b8e83f49c Allow non-root trivial reductions (#2037)
a2dfe40b27 Fix vectorize size calculation (#2035)
e040676a31 Use withPredicate to replace setPredicate to maintain Exprs immutable (#2025)
197221b847 removing ci workflow (#2034)
40e2703d00 Reduction rand like patch (#2031)
bc772661cb Add utility for checking bank conflict of shared memory (#2029)
ddd1cf7695 Add back FusionReductionWithTrivialReduction_CUDA (#2030)
fbd97e5ef1 Revert "Cleanup trivial reduction workarounds (#2006)" (#2024)
bca20c1dfb Cleanup trivial reduction workarounds (#2006)
e4b65850ee Trivial forwarding (#1995)
1a0e355b50 Fix contiguity analysis of predicates to match updated contiguity. (#1991)
a4effa6a5f Enable output allocation cache (#2010)
35440b7953 Patching bn inference (#2016)
0f9f0b4060 Add matmul benchmark (#2007)
45045cd05e Enable tests previously disabled due to an aliasing bug (#2005)
967aa77d2c Contiguous indexing for View operations (#1990)
a43cb20f48 Make inlining even more modular (#2004)
dc458358c0 Test util cleanup (#2003)
3ca21ebe4d More strict validation (#2000)
a7a7d57331 Fix build problem (#1999)
fc235b064e Just fixes comments (#1998)
482386c050 cleanup (#1997)
4cbe0db655 Improve divisible split detection (#1970)
42ccc52bdc Minor build fix. (#1996)
fcf8c091f7 Cleanup of lower_utils.cpp: Isolate out GpuLower usage (#1989)
15f2f6dba8 Move ConcretizedBroadcastDomains to shared_ptr in GpuLower. (#1988)
8f1c7f5267 Minor cleanup lower_unroll.cpp (#1994)
1d9858c803 Minor cleanup (#1992)
f262d9cab5 Add support for uniform RNG (#1986)
eb1dad10c7 Remove non-const functions, remove GpuLower instance on build, pass in ca_map. (#1987)
634820c5e3 Add support for some empty fusion (#1981)
eabe8d844a Segment self mapping fusions (#1954)
e96aacfd9c Enable Transpose operation (#1882)
425dce2777 Add a null scheduler that helps segmenting away no-op schedules (#1835)
306d4a68f1 Fix canScheduleCompileTime check of transpose scheduler (#1969)
b1bd32cc1b Minor fix (#1967)
bd93578143 Enable transpose scheduler (#1927)
b7a206e93b Move scheduler vectorize utilities into their own file (#1959)
d9420e4ca0 View scheduling (#1928)
c668e13aea Upstream push ci fixes (#1965)
c40202bb40 Fix dump effective bandwidth (#1962)
93505bcbb9 WAR on index mapping when exact and permissive maps differ (#1960)
45e95fd1d3 Allow splitting inner-most ID to create virtual innermost ID in transpose scheduler (#1930)
a3ecb33944 Improve the comments at the beginning of index_compute.h (#1946)
f7bc3417cc Remove unused variables (#1955)
df3393adbb Some cleanup (#1957)
7d1d7c8724 TVDomainGuard factory (#1953)
357ba224c0 Fill allocation with nan on tests (#1956)
8eafc54685 Fix detection of unmappable root domains (#1952)
90a51f2826 Some indexing cleanups, Add eye support (#1940)
ddc01e4e16 Exclude unsupported data types (#1951)
992e17c068 test the groups the same order as they are merged (#1949)
208262b75d Move detection of self mapping IDs to IterDomainGraph from (#1941)
ac4de38c6e Merge pull request #1945 from csarofeen/master_merge_0828
631094891a Add full, full_like, zeros, zeros_like, ones, ones_like (#1943)
aab10bce45 Merge remote-tracking branch 'upstream/viable/strict' into HEAD
4c254c063b Fix arange when step is negative (#1942)
89330aa23a Tensor 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
2022-11-04 20:04:34 +00:00

336 lines
10 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 setupBatchNorm(Fusion* fusion, DataType dtype) {
TORCH_INTERNAL_ASSERT(dtype == DataType::Float || dtype == DataType::Half);
FusionGuard fg(fusion);
const bool kTraining = true;
const float kMomentum = 0.1;
const float kEps = 1e-5;
// setup fusion
auto input = makeContigTensor(4, dtype);
auto weight = makeContigTensor(1, dtype);
auto bias = makeContigTensor(1, dtype);
auto running_mean = makeContigTensor(1, DataType::Float);
auto running_var = makeContigTensor(1, DataType::Float);
fusion->addInput(input);
fusion->addInput(weight);
fusion->addInput(bias);
fusion->addInput(running_mean);
fusion->addInput(running_var);
if (dtype == DataType::Half) {
input = castOp(DataType::Float, input);
weight = castOp(DataType::Float, weight);
bias = castOp(DataType::Float, bias);
}
auto momentum_ptr = IrBuilder::create<Double>(kMomentum);
auto eps_ptr = IrBuilder::create<Double>(kEps);
auto result = batch_norm(
input,
weight,
bias,
running_mean,
running_var,
kTraining,
momentum_ptr,
eps_ptr);
auto output = result.output;
if (dtype == DataType::Half) {
output = castOp(DataType::Half, output);
}
fusion->addOutput(output);
}
static void NvFuserScheduler_BatchNorm(
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),
benchmark_state.range(2),
benchmark_state.range(2)};
// inputs
at::manual_seed(0);
auto 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 at_x = at::randn(input_shape, options);
at::Tensor at_weight = at::ones({input_shape[1]}, options);
at::Tensor at_bias = at::zeros({input_shape[1]}, options);
at::Tensor at_run_mean = at::zeros({input_shape[1]}, fp32_options);
at::Tensor at_run_var = at::ones({input_shape[1]}, fp32_options);
std::vector<c10::IValue> aten_inputs(
{at_x, at_weight, at_bias, at_run_mean, at_run_var});
runBenchmarkIterations(benchmark_state, fusion_executor_cache, aten_inputs);
benchmark_state.SetBytesProcessed(
int64_t(benchmark_state.iterations()) *
((2 * (at_x.numel() + at_weight.numel() + at_bias.numel())) *
int64_t(dataTypeSize(dtype)) +
(2 * (at_run_mean.numel() + at_run_var.numel()) *
int64_t(dataTypeSize(DataType::Float)))));
}
//------------------------------------------------------------------------------
static void Baseline_BatchNorm(
benchmark::State& benchmark_state,
DataType dtype) {
TORCH_INTERNAL_ASSERT(dtype == DataType::Float || dtype == DataType::Half);
const float kMomentum = 0.1;
const float kEps = 1e-5;
std::vector<int64_t> input_shape{
benchmark_state.range(0),
benchmark_state.range(1),
benchmark_state.range(2),
benchmark_state.range(2)};
// inputs
at::manual_seed(0);
auto 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 at_x = at::randn(input_shape, options);
at::Tensor at_weight = at::ones({input_shape[1]}, options);
at::Tensor at_bias = at::zeros({input_shape[1]}, options);
at::Tensor at_run_mean = at::zeros({input_shape[1]}, fp32_options);
at::Tensor at_run_var = at::ones({input_shape[1]}, fp32_options);
auto ato_weight = c10::optional<at::Tensor>(at_weight);
auto ato_bias = c10::optional<at::Tensor>(at_bias);
auto ato_run_mean = c10::optional<at::Tensor>(at_run_mean);
auto ato_run_var = c10::optional<at::Tensor>(at_run_var);
auto output = at::batch_norm(
at_x,
ato_weight,
ato_bias,
ato_run_mean,
ato_run_var,
true,
kMomentum,
kEps,
true);
clearL2Cache();
cudaDeviceSynchronize();
for (auto _ : benchmark_state) {
CudaKernelTimer timer;
auto output = at::batch_norm(
at_x,
ato_weight,
ato_bias,
ato_run_mean,
ato_run_var,
true,
kMomentum,
kEps,
true);
benchmark_state.SetIterationTime(timer.elapsed() / 1000.0);
cudaDeviceSynchronize();
clearL2Cache();
cudaDeviceSynchronize();
}
benchmark_state.SetBytesProcessed(
int64_t(benchmark_state.iterations()) *
((2 * (at_x.numel() + at_weight.numel() + at_bias.numel())) *
int64_t(dataTypeSize(dtype)) +
(2 * (at_run_mean.numel() + at_run_var.numel()) *
int64_t(dataTypeSize(DataType::Float)))));
}
//------------------------------------------------------------------------------
static void Baseline_BatchNorm_cuDNN_fp32(benchmark::State& benchmark_state) {
Baseline_BatchNorm(benchmark_state, DataType::Float);
}
static void Baseline_BatchNorm_cuDNN_fp16(benchmark::State& benchmark_state) {
Baseline_BatchNorm(benchmark_state, DataType::Half);
}
// Simple aliases just for names in the printed output
static void Baseline_ResNet_BatchNorm_cuDNN_fp16(benchmark::State& benchmark_state) {
Baseline_BatchNorm(benchmark_state, DataType::Half);
}
static void Baseline_ResNext_BatchNorm_cuDNN_fp16(benchmark::State& benchmark_state) {
Baseline_BatchNorm(benchmark_state, DataType::Half);
}
//------------------------------------------------------------------------------
NVFUSER_BENCHMARK_DEFINE(
NvFuserScheduler_BatchNorm_fp32,
setupBatchNorm,
NvFuserScheduler_BatchNorm,
DataType::Float);
NVFUSER_BENCHMARK_RUN(NvFuserScheduler_BatchNorm_fp32)
// ->RangeMultiplier(2)
->Ranges({{64, 512}, {32, 128}, {2, 64}})
->Unit(benchmark::kMicrosecond)
->UseManualTime();
NVFUSER_BENCHMARK_RUN(NvFuserScheduler_BatchNorm_fp32)
// ->RangeMultiplier(2)
->Ranges({{2, 64}, {2, 32}, {2, 256}})
->Unit(benchmark::kMicrosecond)
->UseManualTime();
NVFUSER_BENCHMARK_DEFINE(
NvFuserScheduler_BatchNorm_fp16,
setupBatchNorm,
NvFuserScheduler_BatchNorm,
DataType::Half);
NVFUSER_BENCHMARK_RUN(NvFuserScheduler_BatchNorm_fp16)
// ->RangeMultiplier(2)
->Ranges({{64, 512}, {32, 128}, {2, 128}})
->Unit(benchmark::kMicrosecond)
->UseManualTime();
NVFUSER_BENCHMARK_RUN(NvFuserScheduler_BatchNorm_fp16)
// ->RangeMultiplier(2)
->Ranges({{2, 64}, {2, 32}, {2, 256}})
->Unit(benchmark::kMicrosecond)
->UseManualTime();
//------------------------------------------------------------------------------
BENCHMARK(Baseline_BatchNorm_cuDNN_fp32)
// ->RangeMultiplier(2)
// cuDNN didn't make it to 1024
->Ranges({{64, 512}, {32, 128}, {2, 64}})
->Unit(benchmark::kMicrosecond)
->UseManualTime();
BENCHMARK(Baseline_BatchNorm_cuDNN_fp32)
// ->RangeMultiplier(2)
->Ranges({{2, 64}, {2, 32}, {2, 256}})
->Unit(benchmark::kMicrosecond)
->UseManualTime();
BENCHMARK(Baseline_BatchNorm_cuDNN_fp16)
// ->RangeMultiplier(2)
->Ranges({{64, 512}, {32, 128}, {2, 128}})
->Unit(benchmark::kMicrosecond)
->UseManualTime();
BENCHMARK(Baseline_BatchNorm_cuDNN_fp16)
// ->RangeMultiplier(2)
->Ranges({{2, 64}, {2, 32}, {2, 256}})
->Unit(benchmark::kMicrosecond)
->UseManualTime();
//------------------------------------------------------------------------------
// RESNET and REXNEXT benchmarks
NVFUSER_BENCHMARK_DEFINE(
NvFuserScheduler_ResNet_BatchNorm_fp16,
setupBatchNorm,
NvFuserScheduler_BatchNorm,
DataType::Half);
NVFUSER_BENCHMARK_RUN(NvFuserScheduler_ResNet_BatchNorm_fp16)
->Args({256, 64, 112})
->Args({256, 64, 56})
->Args({256, 256, 56})
->Args({256, 128, 56})
->Args({256, 128, 28})
->Args({256, 512, 28})
->Args({256, 256, 28})
->Args({256, 256, 14})
->Args({256, 1024, 14})
->Args({256, 512, 14})
->Args({256, 512, 7})
->Args({256, 2048, 7})
->Unit(benchmark::kMicrosecond)
->UseManualTime();
NVFUSER_BENCHMARK_DEFINE(
NvFuserScheduler_ResNext_BatchNorm_fp16,
setupBatchNorm,
NvFuserScheduler_BatchNorm,
DataType::Half);
NVFUSER_BENCHMARK_RUN(NvFuserScheduler_ResNext_BatchNorm_fp16)
->Args({128, 64, 112})
->Args({128, 128, 56})
->Args({128, 256, 56})
->Args({128, 128, 56})
->Args({128, 256, 28})
->Args({128, 512, 28})
->Args({128, 512, 14})
->Args({128, 1024, 14})
->Args({128, 1024, 7})
->Args({128, 2048, 7})
->Unit(benchmark::kMicrosecond)
->UseManualTime();
//------------------------------------------------------------------------------
BENCHMARK(Baseline_ResNet_BatchNorm_cuDNN_fp16)
->Args({256, 64, 112})
->Args({256, 64, 56})
->Args({256, 256, 56})
->Args({256, 128, 56})
->Args({256, 128, 28})
->Args({256, 512, 28})
->Args({256, 256, 28})
->Args({256, 256, 14})
->Args({256, 1024, 14})
->Args({256, 512, 14})
->Args({256, 512, 7})
->Args({256, 2048, 7})
->Unit(benchmark::kMicrosecond)
->UseManualTime();
BENCHMARK(Baseline_ResNext_BatchNorm_cuDNN_fp16)
->Args({128, 64, 112})
->Args({128, 128, 56})
->Args({128, 256, 56})
->Args({128, 128, 56})
->Args({128, 256, 28})
->Args({128, 512, 28})
->Args({128, 512, 14})
->Args({128, 1024, 14})
->Args({128, 1024, 7})
->Args({128, 2048, 7})
->Unit(benchmark::kMicrosecond)
->UseManualTime();