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
358 lines
11 KiB
C++
358 lines
11 KiB
C++
#include <torch/csrc/jit/codegen/cuda/arith.h>
|
|
#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_utils.h>
|
|
#include <torch/csrc/jit/codegen/cuda/ops/all_ops.h>
|
|
#include <torch/csrc/jit/codegen/cuda/scheduler/all_schedulers.h>
|
|
#include <torch/csrc/jit/codegen/cuda/scheduler/matmul.h>
|
|
|
|
#include <benchmark/benchmark.h>
|
|
|
|
#include <cuda_runtime.h>
|
|
|
|
#include <benchmarks/cpp/nvfuser/utils.h>
|
|
|
|
using namespace torch::jit::fuser::cuda;
|
|
|
|
bool cudaArchGuardShouldSkip(int required_major, int required_minor) {
|
|
int capability_major = at::cuda::getCurrentDeviceProperties()->major;
|
|
int capability_minor = at::cuda::getCurrentDeviceProperties()->minor;
|
|
|
|
if (capability_major < required_major ||
|
|
(capability_major == required_major &&
|
|
capability_minor < required_minor)) {
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
bool hasRequiredSmemSize(size_t required_size) {
|
|
// Only checking device 0
|
|
return at::cuda::getDeviceProperties(0)->sharedMemPerBlockOptin >=
|
|
required_size;
|
|
}
|
|
|
|
#define NVFUSER_BENCHMARK_ARCH_SMEM_GUARD( \
|
|
REQUIRED_MAJOR, REQUIRED_MINOR, SMEM_SIZE, STATE) \
|
|
if (cudaArchGuardShouldSkip(REQUIRED_MAJOR, REQUIRED_MINOR) || \
|
|
!hasRequiredSmemSize(SMEM_SIZE)) { \
|
|
STATE.SkipWithError("Unsupported arch or not enough smem!"); \
|
|
return; \
|
|
}
|
|
|
|
// util to track support matmul operand layout.
|
|
using MatmulLayout = MmaOptions::MmaInputLayout;
|
|
|
|
static constexpr std::array<MatmulLayout, 3> kAllSupportedLayout = {
|
|
MatmulLayout::TT,
|
|
MatmulLayout::NT,
|
|
MatmulLayout::TN};
|
|
|
|
// Generic interface to get matmul op with the given layout.
|
|
TensorView* matmul(TensorView* a, TensorView* b, MatmulLayout layout) {
|
|
TORCH_CHECK(
|
|
a->nDims() == 2 && b->nDims() == 2, "only pure matmuls for these tests");
|
|
TensorView *tv2 = nullptr, *tv0b = nullptr, *tv1b = nullptr;
|
|
switch (layout) {
|
|
case MatmulLayout::TT:
|
|
tv0b = broadcast(a, {false, false, true});
|
|
tv1b = broadcast(b, {true, false, false});
|
|
tv2 = fusedMultiplySum(tv0b, tv1b, {1});
|
|
break;
|
|
case MatmulLayout::TN:
|
|
tv0b = broadcast(a, {false, true, false});
|
|
tv1b = broadcast(b, {true, false, false});
|
|
tv2 = fusedMultiplySum(tv0b, tv1b, {2});
|
|
break;
|
|
case MatmulLayout::NT:
|
|
tv0b = broadcast(a, {false, false, true});
|
|
tv1b = broadcast(b, {false, true, false});
|
|
tv2 = fusedMultiplySum(tv0b, tv1b, {0});
|
|
break;
|
|
default:
|
|
TORCH_CHECK(false, "unsupported data layout.");
|
|
}
|
|
return tv2;
|
|
}
|
|
|
|
// Utility to generate matmul input tensors based on given layout
|
|
at::Tensor atMatmul(at::Tensor a, at::Tensor b, MatmulLayout layout) {
|
|
switch (layout) {
|
|
case MatmulLayout::TT:
|
|
return a.matmul(b);
|
|
case MatmulLayout::TN:
|
|
return a.matmul(b.t());
|
|
case MatmulLayout::NT:
|
|
return a.t().matmul(b);
|
|
default:
|
|
TORCH_CHECK(false, "unsupported data layout.");
|
|
}
|
|
return at::Tensor();
|
|
}
|
|
|
|
// Utility to generate reference results based on given layout
|
|
std::pair<at::Tensor, at::Tensor> fp16MatmulAtInput(
|
|
int M,
|
|
int N,
|
|
int K,
|
|
MatmulLayout layout) {
|
|
auto options = at::TensorOptions().dtype(at::kHalf).device(at::kCUDA, 0);
|
|
|
|
switch (layout) {
|
|
case MatmulLayout::TT:
|
|
return std::make_pair(
|
|
at::randn({M, K}, options), at::randn({K, N}, options));
|
|
case MatmulLayout::TN:
|
|
return std::make_pair(
|
|
at::randn({M, K}, options), at::randn({N, K}, options));
|
|
case MatmulLayout::NT:
|
|
return std::make_pair(
|
|
at::randn({K, M}, options), at::randn({K, N}, options));
|
|
default:
|
|
TORCH_CHECK(false, "unsupported data layout.");
|
|
}
|
|
return std::make_pair(at::Tensor(), at::Tensor());
|
|
}
|
|
|
|
// TODO: separate compute and schedule definition once the can schedule
|
|
// logic and pattern matching is ready.
|
|
void setupMatmul(Fusion* fusion, MatmulLayout layout, MatmulParam params) {
|
|
// Only hgemm on the initial setup
|
|
auto a = makeContigTensor(2, DataType::Half);
|
|
auto b = makeContigTensor(2, DataType::Half);
|
|
|
|
auto c = matmul(a, b, layout);
|
|
|
|
fusion->addInput(a);
|
|
fusion->addInput(b);
|
|
fusion->addOutput(c);
|
|
|
|
scheduleMatmul(c, a, b, params);
|
|
}
|
|
|
|
static void SingleMatmulBase(
|
|
benchmark::State& benchmark_state,
|
|
MatmulLayout layout,
|
|
MatmulParam params) {
|
|
std::vector<int64_t> input_mnk{
|
|
benchmark_state.range(0),
|
|
benchmark_state.range(1),
|
|
benchmark_state.range(2)};
|
|
|
|
auto fusion_ptr = std::make_unique<Fusion>();
|
|
auto fusion = fusion_ptr.get();
|
|
FusionGuard fg(fusion);
|
|
|
|
// Define fusion graph
|
|
setupMatmul(fusion, layout, params);
|
|
|
|
// inputs
|
|
at::manual_seed(0);
|
|
|
|
// Tensor inputs
|
|
auto inputs = fp16MatmulAtInput(
|
|
input_mnk.at(0), input_mnk.at(1), input_mnk.at(2), layout);
|
|
|
|
KernelArgumentHolder args = KernelArgumentHolder::createKernelArgumentHolder(
|
|
{inputs.first, inputs.second});
|
|
|
|
// Always use 32b indexing mode for now.
|
|
TORCH_INTERNAL_ASSERT(args.getIndexMode() == KernelIndexMode::INT32);
|
|
|
|
// Compile kernel
|
|
FusionExecutor fe;
|
|
fe.compileFusion(fusion, args, LaunchParams());
|
|
|
|
// Warm up run
|
|
auto outputs = fe.runFusion({inputs.first, inputs.second});
|
|
fe.setMeasureKernelTimeFlag(true);
|
|
|
|
// Sync everything up before we start
|
|
for (auto _ : benchmark_state) {
|
|
clearL2Cache();
|
|
auto outputs = fe.runFusion({inputs.first, inputs.second});
|
|
benchmark_state.SetIterationTime(fe.kernelTimeMs() / 1000.0);
|
|
}
|
|
// Sync everything up before we're finished, don't want to run ahead on the
|
|
// cpu while benchmarking.
|
|
cudaDeviceSynchronize();
|
|
|
|
// TODO: FLOPS calculation
|
|
}
|
|
|
|
static void EagerModeMatmul(
|
|
benchmark::State& benchmark_state,
|
|
MatmulLayout layout) {
|
|
std::vector<int64_t> input_mnk{
|
|
benchmark_state.range(0),
|
|
benchmark_state.range(1),
|
|
benchmark_state.range(2)};
|
|
|
|
at::manual_seed(0);
|
|
|
|
auto inputs = fp16MatmulAtInput(
|
|
input_mnk.at(0), input_mnk.at(1), input_mnk.at(2), layout);
|
|
|
|
// warm up run
|
|
auto outputs = atMatmul(inputs.first, inputs.second, layout);
|
|
|
|
for (auto _ : benchmark_state) {
|
|
clearL2Cache();
|
|
CudaKernelTimer timer;
|
|
outputs = atMatmul(inputs.first, inputs.second, layout);
|
|
benchmark_state.SetIterationTime(timer.elapsed() / 1000.0);
|
|
}
|
|
// Sync everything up before we're finished, don't want to run ahead on the
|
|
// cpu while benchmarking.
|
|
cudaDeviceSynchronize();
|
|
}
|
|
|
|
// Actual benchmarking
|
|
// -----------------------------------------------------------------
|
|
|
|
size_t getSmemSize(GemmTile cta_tile, int stage_number) {
|
|
return ((cta_tile.m * cta_tile.k) + (cta_tile.n * cta_tile.k)) *
|
|
dataTypeSize(DataType::Half) * stage_number;
|
|
}
|
|
|
|
// TODO: this part eventually will be automated by heuristics
|
|
MatmulParam getMatmulParams(
|
|
GemmTile cta_tile,
|
|
int stage_number,
|
|
MatmulLayout layout) {
|
|
MatMulTileOptions gemm_tile;
|
|
gemm_tile.cta_tile = cta_tile;
|
|
// TODO: pipe through split K
|
|
gemm_tile.warp_tile = GemmTile(64, 64, cta_tile.k);
|
|
gemm_tile.instruction_tile = GemmTile(16, 16, 16);
|
|
|
|
// Collect mma swizzle info
|
|
auto mma_builder =
|
|
MmaBuilder(MmaOptions::MacroType::Ampere_16_16_16, gemm_tile)
|
|
.layout(layout);
|
|
|
|
MatmulParam params(mma_builder);
|
|
params.tile_sizes = gemm_tile;
|
|
params.async_gmem_load_operands = true;
|
|
params.double_buffer_options.double_buffer_smem_write = true;
|
|
params.double_buffer_options.double_buffer_smem_read = true;
|
|
params.double_buffer_options.smem_double_buffer_stage = stage_number;
|
|
|
|
return params;
|
|
}
|
|
|
|
static void Nvfuser_Matmul_4warp3stage(
|
|
benchmark::State& benchmark_state,
|
|
MatmulLayout layout) {
|
|
auto cta_tile = GemmTile(128, 128, 32);
|
|
int number_of_stage = 3;
|
|
|
|
auto params = getMatmulParams(cta_tile, number_of_stage, layout);
|
|
|
|
NVFUSER_BENCHMARK_ARCH_SMEM_GUARD(
|
|
8, 0, getSmemSize(cta_tile, number_of_stage), benchmark_state);
|
|
|
|
// Run benchmark:
|
|
SingleMatmulBase(benchmark_state, layout, params);
|
|
}
|
|
|
|
static void Nvfuser_Matmul_8warp3stage(
|
|
benchmark::State& benchmark_state,
|
|
MatmulLayout layout) {
|
|
auto cta_tile = GemmTile(256, 128, 32);
|
|
int number_of_stage = 3;
|
|
|
|
auto params = getMatmulParams(cta_tile, number_of_stage, layout);
|
|
|
|
NVFUSER_BENCHMARK_ARCH_SMEM_GUARD(
|
|
8, 0, getSmemSize(cta_tile, number_of_stage), benchmark_state);
|
|
|
|
// Run benchmark:
|
|
SingleMatmulBase(benchmark_state, layout, params);
|
|
}
|
|
|
|
static void Nvfuser_Matmul_4warp4stage(
|
|
benchmark::State& benchmark_state,
|
|
MatmulLayout layout) {
|
|
auto cta_tile = GemmTile(128, 128, 32);
|
|
int number_of_stage = 4;
|
|
|
|
auto params = getMatmulParams(cta_tile, number_of_stage, layout);
|
|
|
|
NVFUSER_BENCHMARK_ARCH_SMEM_GUARD(
|
|
8, 0, getSmemSize(cta_tile, number_of_stage), benchmark_state);
|
|
|
|
// Run benchmark:
|
|
SingleMatmulBase(benchmark_state, layout, params);
|
|
}
|
|
|
|
static void Nvfuser_Matmul_8warp4stage(
|
|
benchmark::State& benchmark_state,
|
|
MatmulLayout layout) {
|
|
auto cta_tile = GemmTile(256, 128, 32);
|
|
int number_of_stage = 4;
|
|
|
|
auto params = getMatmulParams(cta_tile, number_of_stage, layout);
|
|
|
|
NVFUSER_BENCHMARK_ARCH_SMEM_GUARD(
|
|
8, 0, getSmemSize(cta_tile, number_of_stage), benchmark_state);
|
|
|
|
// Run benchmark:
|
|
SingleMatmulBase(benchmark_state, layout, params);
|
|
}
|
|
|
|
// ----------------------------- Benchmark Instantiation-------
|
|
|
|
// Common utils:
|
|
#define NO_TILE_QUANTIZATION_ARGS \
|
|
ArgsProduct( \
|
|
{{2048}, {3456}, benchmark::CreateDenseRange(512, 4096, /*step=*/512)}) \
|
|
->Unit(benchmark::kMicrosecond) \
|
|
->UseManualTime();
|
|
|
|
#define ForAllLayouts(run) \
|
|
run(TT, MatmulLayout::TT); \
|
|
run(TN, MatmulLayout::TN); \
|
|
run(NT, MatmulLayout::NT)
|
|
|
|
// Instantiations:
|
|
#define Nvfuser_4warp3stage_test(layout_label, layout) \
|
|
BENCHMARK_CAPTURE( \
|
|
Nvfuser_Matmul_4warp3stage, \
|
|
no_quant_nvfuser_4warp_##layout_label, \
|
|
layout) \
|
|
->NO_TILE_QUANTIZATION_ARGS
|
|
|
|
#define Nvfuser_8warp3stage_test(layout_label, layout) \
|
|
BENCHMARK_CAPTURE( \
|
|
Nvfuser_Matmul_8warp3stage, \
|
|
no_quant_nvfuser_8warp_##layout_label, \
|
|
layout) \
|
|
->NO_TILE_QUANTIZATION_ARGS
|
|
|
|
#define Nvfuser_4warp4stage_test(layout_label, layout) \
|
|
BENCHMARK_CAPTURE( \
|
|
Nvfuser_Matmul_4warp4stage, \
|
|
no_quant_nvfuser_4warp_##layout_label, \
|
|
layout) \
|
|
->NO_TILE_QUANTIZATION_ARGS
|
|
|
|
#define Nvfuser_8warp4stage_test(layout_label, layout) \
|
|
BENCHMARK_CAPTURE( \
|
|
Nvfuser_Matmul_8warp4stage, \
|
|
no_quant_nvfuser_8warp_##layout_label, \
|
|
layout) \
|
|
->NO_TILE_QUANTIZATION_ARGS
|
|
|
|
#define Eagermode_test(layout_label, layout) \
|
|
BENCHMARK_CAPTURE( \
|
|
EagerModeMatmul, no_quant_eagermode_##layout_label, layout) \
|
|
->NO_TILE_QUANTIZATION_ARGS
|
|
|
|
ForAllLayouts(Nvfuser_4warp3stage_test);
|
|
ForAllLayouts(Nvfuser_4warp4stage_test);
|
|
ForAllLayouts(Nvfuser_8warp3stage_test);
|
|
ForAllLayouts(Nvfuser_8warp4stage_test);
|
|
ForAllLayouts(Eagermode_test);
|