mirror of
https://github.com/pytorch/pytorch.git
synced 2026-11-20 09:04:25 +08:00
0e582fbfcc
Syncing nvfuser devel branch to upstream master. https://github.com/csarofeen/pytorch/ Codegen changes include: - codegen improvement: i. improved view support on pointwise and transpose scheduler ii. grouped grid welford added for better outer-norm grid persistence in normalization - misc: i. new composite ops added: variance_mean , arange, ii. fixes misaligned address for transpose scheduler iii. refactor on separation of compilation API from execution API to prepare us for async compilation iv. double type support on expression evaluator v. PYTORCH_NVFUSER_DUMP refactor to save PTX and CUBIN Commits that's in this PR from the devel branch: ```89330aa23aTensor factories must set the output shape as its input (#1939)b2fd01ea93arange support (#1933)56c00fd392Double support on all expression evaluators (#1937)371f28223eImprove trivial reduction merge support (#1931)1d0c26790eTest `rand` in a fusion with zero tensor input (#1932)0dab160fb2Fix softmax bwd sizes. (#1890)ef98f360f6Fix a bug (#1936)63132a0c56Propagate permissive mapping information into indexing pass (#1929)b4ac2c88d7Map IterationDomains through view operations. (#1919)c0a187a761do not use deprecated functions (#1935)88de85e758Upstream cherry pick fixes 0811 (#1934)b247dcf7c5Separate kernel compilation API from kernel execution API (#1914)b34e3b93eeFix `ir_utils::hasBlockSync` + misc fixes in transpose scheduler (#1924)14a53e6707Nullary RNGOp (#1892)3c3c89e638Misc fixes/tuning for transpose scheduler (#1912)20cf109c8bGrouped grid welford (#1921)6cf7eb024cTranspose scheduler small dim sizes better support (#1910)9341ea9a5bDisabled ViewPersistentShmoo sizes that results in NAN (#1922)057237f66dFix CUDA driver error: misaligned address for transpose scheduler (#1918)3fb3d80339Add variance_mean function using Welford (#1907)98febf6aa3Remove DisableOption::UnrollWithRng (#1913)ee8ef33a55Minor fix for the debug interface of using PTX directly (#1917)6e8f953351Add PYTORCH_NVFUSER_DUMP options to save PTX and CUBIN (#1916)5eefa9a723dopt is only available since nvrtc 11.7 (#1915)2ec8fc711eKill computeAtBetween (#1911)d0d106a1d9Improve view support on pointwise and transpose scheduler (#1906)e71e1ecefeFix name clash of RNG with shared memory (#1904)3381793a25Fix mutator and sameAs for expanded IterDomain (#1902) ``` RUN_TORCHBENCH: nvfuser Differential Revision: [D39324552](https://our.internmc.facebook.com/intern/diff/D39324552) Pull Request resolved: https://github.com/pytorch/pytorch/pull/84626 Approved by: https://github.com/malfet
212 lines
6.7 KiB
C++
212 lines
6.7 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 auto getLayerBackwardNormRuntime(
|
|
std::unique_ptr<Fusion> fusion_ptr,
|
|
std::unique_ptr<FusionExecutorCache>& fec,
|
|
std::vector<at::IValue>& aten_inputs,
|
|
std::vector<int64_t>& shape,
|
|
std::vector<int64_t>& norm_shape) {
|
|
Fusion& fusion = *fusion_ptr.get();
|
|
|
|
const size_t kM = shape.size();
|
|
const size_t kN = norm_shape.size();
|
|
const size_t kOuterNumDims = kM - kN;
|
|
|
|
std::vector<int64_t> outer_shape;
|
|
for (size_t idx = 0; idx < kOuterNumDims; ++idx) {
|
|
outer_shape.push_back(shape[idx]);
|
|
}
|
|
for (size_t idx = kOuterNumDims; idx < kM; ++idx) {
|
|
outer_shape.push_back(1);
|
|
}
|
|
|
|
auto grad_out = makeSymbolicTensor(shape.size());
|
|
auto input = makeSymbolicTensor(shape.size());
|
|
auto mean = makeConcreteTensor(outer_shape);
|
|
auto rstd = makeConcreteTensor(outer_shape);
|
|
auto weight = makeSymbolicTensor(norm_shape.size());
|
|
auto bias = makeSymbolicTensor(norm_shape.size());
|
|
fusion.addInput(grad_out);
|
|
fusion.addInput(input);
|
|
fusion.addInput(mean);
|
|
fusion.addInput(rstd);
|
|
fusion.addInput(weight);
|
|
fusion.addInput(bias);
|
|
|
|
auto grads = layer_norm_backward(
|
|
grad_out,
|
|
input,
|
|
norm_shape,
|
|
mean,
|
|
rstd,
|
|
weight,
|
|
bias,
|
|
{true, true, true});
|
|
|
|
fusion.addOutput(grads.grad_input);
|
|
fusion.addOutput(grads.grad_weight);
|
|
fusion.addOutput(grads.grad_bias);
|
|
|
|
auto options = at::TensorOptions().dtype(at::kFloat).device(at::kCUDA, 0);
|
|
at::Tensor aten_grad_out = at::randn(shape, options);
|
|
at::Tensor aten_input = at::randn(shape, options);
|
|
at::Tensor aten_weight = at::randn(norm_shape, options);
|
|
at::Tensor aten_bias = at::randn(norm_shape, options);
|
|
auto at_weight = c10::optional<at::Tensor>(aten_weight);
|
|
auto at_bias = c10::optional<at::Tensor>(aten_bias);
|
|
|
|
const float kEps = 1e-5;
|
|
auto aten_results =
|
|
at::native_layer_norm(aten_input, norm_shape, at_weight, at_bias, kEps);
|
|
auto aten_output = std::get<0>(aten_results);
|
|
auto aten_mean = std::get<1>(aten_results);
|
|
auto aten_rstd = std::get<2>(aten_results);
|
|
|
|
fec = std::make_unique<FusionExecutorCache>(std::move(fusion_ptr));
|
|
aten_inputs = {
|
|
aten_grad_out, aten_input, aten_mean, aten_rstd, aten_weight, aten_bias};
|
|
auto cg_outputs = fec->runFusionWithInputs(aten_inputs);
|
|
|
|
return fec->getMostRecentKernelRuntime();
|
|
}
|
|
|
|
void LayerNormBackward_ShapeInference_Base(
|
|
benchmark::State& benchmark_state,
|
|
bool disable_launch_parameter_cache) {
|
|
std::unique_ptr<Fusion> fusion_ptr = std::make_unique<Fusion>();
|
|
FusionGuard fg(fusion_ptr.get());
|
|
|
|
// PreAllocate
|
|
std::unique_ptr<FusionExecutorCache> fec;
|
|
std::vector<at::IValue> aten_inputs;
|
|
|
|
std::vector<int64_t> shape{20, 100, 35, 67};
|
|
std::vector<int64_t> norm_shape{67};
|
|
|
|
auto runtime = getLayerBackwardNormRuntime(
|
|
std::move(fusion_ptr), fec, aten_inputs, shape, norm_shape);
|
|
|
|
KernelArgumentHolder args = KernelArgumentHolder::createKernelArgumentHolder(aten_inputs);
|
|
|
|
TORCH_INTERNAL_ASSERT(
|
|
runtime->getMaybeHeuristicsFor(args).has_value());
|
|
|
|
fec->profile(true);
|
|
fec->disableKernelLaunch();
|
|
fec->runFusionWithInputs(aten_inputs);
|
|
if (disable_launch_parameter_cache) {
|
|
fec->disableLaunchParamCache();
|
|
}
|
|
|
|
for (auto _ : benchmark_state) {
|
|
// Setup (not included in the measurement)
|
|
fec->runFusionWithInputs(aten_inputs);
|
|
}
|
|
}
|
|
|
|
static void LayerNormBackward_ShapeInference(
|
|
benchmark::State& benchmark_state) {
|
|
LayerNormBackward_ShapeInference_Base(benchmark_state, true);
|
|
}
|
|
|
|
static void LayerNormBackward_NoShapeInferenceCachedBaseline(
|
|
benchmark::State& benchmark_state) {
|
|
LayerNormBackward_ShapeInference_Base(benchmark_state, false);
|
|
}
|
|
|
|
static auto getLayerForwardNormRuntime(
|
|
std::unique_ptr<Fusion> fusion_ptr,
|
|
std::unique_ptr<FusionExecutorCache>& fec,
|
|
std::vector<at::IValue>& aten_inputs,
|
|
std::vector<int64_t>& shape,
|
|
std::vector<int64_t>& norm_shape) {
|
|
Fusion& fusion = *fusion_ptr.get();
|
|
|
|
const float kEps = 1e-5;
|
|
Double* eps_ptr = IrBuilder::create<Double>(kEps);
|
|
|
|
auto input = makeSymbolicTensor(shape.size());
|
|
fusion.addInput(input);
|
|
|
|
auto result = layer_norm(input, norm_shape, nullptr, nullptr, eps_ptr);
|
|
|
|
fusion.addOutput(result.output);
|
|
fusion.addOutput(result.mean);
|
|
fusion.addOutput(result.invstd);
|
|
|
|
auto options = at::TensorOptions().dtype(at::kFloat).device(at::kCUDA, 0);
|
|
at::Tensor aten_input = at::randn(shape, options);
|
|
|
|
fec = std::make_unique<FusionExecutorCache>(std::move(fusion_ptr));
|
|
aten_inputs = {aten_input};
|
|
auto cg_outputs = fec->runFusionWithInputs(aten_inputs);
|
|
|
|
return fec->getMostRecentKernelRuntime();
|
|
}
|
|
|
|
void LayerNormForward_ShapeInferenceBase(
|
|
benchmark::State& benchmark_state,
|
|
bool disable_launch_param_cache) {
|
|
std::unique_ptr<Fusion> fusion_ptr = std::make_unique<Fusion>();
|
|
FusionGuard fg(fusion_ptr.get());
|
|
|
|
// PreAllocate
|
|
std::unique_ptr<FusionExecutorCache> fec;
|
|
std::vector<at::IValue> aten_inputs;
|
|
|
|
std::vector<int64_t> shape{20, 100, 35, 67};
|
|
std::vector<int64_t> norm_shape{67};
|
|
|
|
auto runtime = getLayerForwardNormRuntime(
|
|
std::move(fusion_ptr), fec, aten_inputs, shape, norm_shape);
|
|
|
|
KernelArgumentHolder args = KernelArgumentHolder::createKernelArgumentHolder(aten_inputs);
|
|
|
|
TORCH_INTERNAL_ASSERT(
|
|
runtime->getMaybeHeuristicsFor(args).has_value());
|
|
|
|
fec->profile(true);
|
|
fec->disableKernelLaunch();
|
|
fec->runFusionWithInputs(aten_inputs);
|
|
|
|
if (disable_launch_param_cache) {
|
|
fec->disableLaunchParamCache();
|
|
}
|
|
|
|
for (auto _ : benchmark_state) {
|
|
// Setup (not included in the measurement)
|
|
fec->runFusionWithInputs(aten_inputs);
|
|
}
|
|
}
|
|
|
|
static void LayerNormForward_NoShapeInferenceCachedBaseline(
|
|
benchmark::State& benchmark_state) {
|
|
LayerNormForward_ShapeInferenceBase(benchmark_state, false);
|
|
}
|
|
|
|
static void LayerNormForward_ShapeInference(benchmark::State& benchmark_state) {
|
|
LayerNormForward_ShapeInferenceBase(benchmark_state, true);
|
|
}
|
|
|
|
BENCHMARK(LayerNormBackward_ShapeInference)->Unit(benchmark::kMicrosecond);
|
|
BENCHMARK(LayerNormForward_ShapeInference)->Unit(benchmark::kMicrosecond);
|
|
BENCHMARK(LayerNormBackward_NoShapeInferenceCachedBaseline)
|
|
->Unit(benchmark::kMicrosecond);
|
|
BENCHMARK(LayerNormForward_NoShapeInferenceCachedBaseline)
|
|
->Unit(benchmark::kMicrosecond);
|