mirror of
https://github.com/zebrajr/pytorch.git
synced 2025-12-07 00:21:07 +01:00
Syncing nvfuser devel branch to upstream master. https://github.com/csarofeen/pytorch/ Code changes includes: - codegen improvements: 1. Indexing refactor -> Remove reference tensor in predicate indexing logic 2. MMA Rfactor support for cross-warp and cross-CTA split on K dimension 3. Grouping grid allreduces across iterations 4. Swizzle op formulation for non-affine swizzles 5. Use scheduler_utils to cache inputs and outputs in schedulePointwise - scheduler refactor 1. New compute at interface - transformation propagation refactor on MaxInfoSpanningTree 1. Added sibling path that is required to generate consistent replay for some cases where `MaxInfoSpanningTree` is used with a selector. 2. Optimization to skip Transform propagator 3. SpanningTreePrinter for debugging - parser update 1. Fixes `div` 2. Added `_to_copy` 3. Broadcast in dim with expand to support expanding to concrete size 4. Dropout prob extremal patch - executor patch on caching strides for output allocation Squashed commits to WAR github API Commits that's actually in this PR from the devel branch: ``` 3b87896706fc98aa4d5b5c811af034cc4dddfbab Fix allocation of work buffers and `fused_reduction::ParallelReduce` with unswitch (#1818) 4cae1227f666b68d275144afd6e4be1fa7aa0786 schedulePointwise cleanup: - computeAt + InlinePropagator (#1815) 3df97426adfb5ecc6fe2c12c43d56d59670e5020 Use scheduler_utils to cache inputs and outputs in schedulePointwise (#1811) 03180aa8facde51237dffa29f6632ffa870cf923 improve broadcast resolution (#1792) bee6c69979d8c34d6d6ef7514f8886cf1416d64f bug fix (#1819) 4413c8f43a5a64dd0a6ddb0763523bbc7314f4b5 Support PYTORCH_NVFUSER_DUMP=transform_propagator (#1812) de6b7ca5ce755061ae0d26e006c4403653627ab5 Fix negative position in InlinePropagator (#1813) 10a996cb4dce5d514f09fd0d49ffcd3b88869a28 Remove redundant check in schedulePointwise (#1810) acd5ed4df825d4c25999e8c9041e0f8ca1a3448f Swizzle op formulation for non-affine swizzles (#1441) 3ed8330f881f429fe2df0e5af9000b91355a96da Kernel args patch to show zero_init buffer (#1809) 037a75a42048f1d8a9c30efb466f1ffbfd2894ad Dropout prob extremal patch (#1804) 282c42902bff07f759cddbbe619249cf5e7c5281 spam nvrtc options (#1783) 3ba6a5fe0a47044179cd36b5b62e628c75180da5 Broadcast in dim with expand (#1794) fd4be1236ddfeb31ca0659e1b0df36546424c979 remove dead indexing code (#1806) fa4e6a4739a9daaa0e4111fb4730704d79c91010 Check siblings in getMaxPosAll (#1805) 025c840c76d89b0d032b65a78a375719cab78d46 Grouping grid allreduces across iterations (#1755) 37c579e64f8145fc292273cdebb6519edeb9cf76 Temporarily disable test requring large shared memory. (#1802) 5f375d074524ab65cb78282eff7abe5846cc4203 More cleanup on InlinePropagator (#1800) 8d384da0cfb50a7c5082e91585c12f4c3a775e6c Indexing refactor stage 2 : Remove reference tensor in predicate indexing logic (#1784) f008140e26335584a143f71c2cb9e91fd61ec530 MMA Rfactor support for cross-warp and cross-CTA split on K dimension (#1554) 76b3cca5cc9a18a56db8107d2f6c8e94851bb85c Add parsing support for `_to_copy` to handle AMP casts. (#1756) ef04f6c4c0ee043979ac7aad4e5be6f22faeb547 Coding style cleanups (#1798) 38c7f3cf69ea58cc9480b0621506bbfd90a7c9d3 InlinePropagator please don't replay (#1797) 3f2c263ade35017be2d99fe8e4ec97fd0f14f754 validateDomain in TransformPropagator (#1796) c07708520d99ef815ce15ec367bf7e98797d602b Use TransformPropagatorWithCheck in many tests (#1795) d0d0908aee2e2b7615c28d04ee80a54b01a02bcd Some further cleanup for the new computeAt interface (#1793) 45f5203b5744cd3512d83263b3fb07c99795a271 Fix TransformReplay::getMatchedLeafPosWithoutReplay* (#1791) 28cbaf931870086cf59807dd60ce412d6dfad0fd New compute at interface (#1743) 635ebfc79bc016eea94d4cbde2c12324171b908b Add SpanningTreePrinter (#1786) 59f3c3223c48ea89549fe7d323f17cbecbebede0 Output allocate patch (#1790) fe93bf5a6485696ffb36751606a84080349967b5 Transform propagator skip replay when possible (#1782) ebf23a50f3adf3d28e824c3b3b4ed6ea6f9cf483 Fix isIntegralType error msg (#1789) 0c82ecf04d12b9fe5428af6824a7a978cf5e0ddd Disable register reuse across serial broadcast ops (#1787) 33a824d8d9ace7790a4a58d497e525a7a059579d Adding sibling path for MaxInfoSpanningTree (#1776) 86f46aad83cbb2aa06943419a7335d71a8798f2a Fix div(Val, TensorView) (#1778) d3de227ade763bdac9e9df15ba8671be78565ee9 Fix FusionMaxRootDomainInfoSpanningTreePrintTwice_CUDA (#1781) ecc7a87cdaaed66672d08bf819ad58d2980384cb Extend mma dimension and layout checking to support strided batched matmul and tensor contractions (#1761) ``` RUN_TORCHBENCH: nvfuser Differential Revision: [D38043938](https://our.internmc.facebook.com/intern/diff/D38043938) Pull Request resolved: https://github.com/pytorch/pytorch/pull/81861 Approved by: https://github.com/davidberard98
314 lines
11 KiB
C++
314 lines
11 KiB
C++
|
|
#include <torch/csrc/jit/codegen/cuda/utils.h>
|
|
|
|
#include <c10/util/string_view.h>
|
|
|
|
#include <cstdlib>
|
|
#include <iostream>
|
|
#include <unordered_map>
|
|
|
|
namespace torch {
|
|
namespace jit {
|
|
namespace fuser {
|
|
namespace cuda {
|
|
|
|
namespace {
|
|
|
|
auto parseDebugDumpOptions() {
|
|
std::unordered_map<DebugDumpOption, bool> options_map = {
|
|
{DebugDumpOption::FusionIr, false},
|
|
{DebugDumpOption::FusionIrMath, false},
|
|
{DebugDumpOption::KernelIr, false},
|
|
{DebugDumpOption::ComputeAtMap, false},
|
|
{DebugDumpOption::CudaKernel, false},
|
|
{DebugDumpOption::CudaFull, false},
|
|
{DebugDumpOption::CudaToFile, false},
|
|
{DebugDumpOption::LaunchParam, false},
|
|
{DebugDumpOption::FusionSegments, false},
|
|
{DebugDumpOption::FusionSegmenterLog, false},
|
|
{DebugDumpOption::FusionArgs, false},
|
|
{DebugDumpOption::KernelArgs, false},
|
|
{DebugDumpOption::EffectiveBandwidth, false},
|
|
{DebugDumpOption::FusionSegmentsDrawing, false},
|
|
{DebugDumpOption::PrintPtxasLog, false},
|
|
{DebugDumpOption::BufferReuseInfo, false},
|
|
{DebugDumpOption::SchedulerDebug, false},
|
|
{DebugDumpOption::ParallelDimensions, false},
|
|
{DebugDumpOption::Halo, false},
|
|
{DebugDumpOption::PerfDebugVerbose, false},
|
|
{DebugDumpOption::TransformPropagator, false}};
|
|
|
|
if (const char* dump_options = std::getenv("PYTORCH_NVFUSER_DUMP")) {
|
|
c10::string_view options_view(dump_options);
|
|
while (!options_view.empty()) {
|
|
const auto end_pos = options_view.find_first_of(',');
|
|
const auto token = options_view.substr(0, end_pos);
|
|
if (token == "fusion_ir") {
|
|
options_map[DebugDumpOption::FusionIr] = true;
|
|
} else if (token == "fusion_ir_math") {
|
|
options_map[DebugDumpOption::FusionIrMath] = true;
|
|
} else if (token == "kernel_ir") {
|
|
options_map[DebugDumpOption::KernelIr] = true;
|
|
} else if (token == "ca_map") {
|
|
options_map[DebugDumpOption::ComputeAtMap] = true;
|
|
} else if (token == "cuda_kernel") {
|
|
options_map[DebugDumpOption::CudaKernel] = true;
|
|
} else if (token == "cuda_full") {
|
|
options_map[DebugDumpOption::CudaFull] = true;
|
|
} else if (token == "cuda_to_file") {
|
|
options_map[DebugDumpOption::CudaToFile] = true;
|
|
} else if (token == "launch_param") {
|
|
options_map[DebugDumpOption::LaunchParam] = true;
|
|
} else if (token == "segmented_fusion") {
|
|
options_map[DebugDumpOption::FusionSegments] = true;
|
|
} else if (token == "segmenter_logging") {
|
|
options_map[DebugDumpOption::FusionSegmenterLog] = true;
|
|
} else if (token == "fusion_args") {
|
|
options_map[DebugDumpOption::FusionArgs] = true;
|
|
} else if (token == "kernel_args") {
|
|
options_map[DebugDumpOption::KernelArgs] = true;
|
|
} else if (token == "dump_eff_bandwidth") {
|
|
options_map[DebugDumpOption::EffectiveBandwidth] = true;
|
|
} else if (token == "draw_segmented_fusion") {
|
|
options_map[DebugDumpOption::FusionSegmentsDrawing] = true;
|
|
} else if (token == "ptxas_verbose") {
|
|
options_map[DebugDumpOption::PrintPtxasLog] = true;
|
|
} else if (token == "buffer_reuse_verbose") {
|
|
options_map[DebugDumpOption::BufferReuseInfo] = true;
|
|
} else if (token == "scheduler_params") {
|
|
options_map[DebugDumpOption::SchedulerDebug] = true;
|
|
} else if (token == "parallel_dimensions") {
|
|
options_map[DebugDumpOption::ParallelDimensions] = true;
|
|
} else if (token == "halo") {
|
|
options_map[DebugDumpOption::Halo] = true;
|
|
} else if (token == "perf_debug_verbose") {
|
|
options_map[DebugDumpOption::PerfDebugVerbose] = true;
|
|
} else if (token == "transform_propagator") {
|
|
options_map[DebugDumpOption::TransformPropagator] = true;
|
|
} else {
|
|
TORCH_CHECK(
|
|
false,
|
|
"Invalid debug dump option: '",
|
|
token,
|
|
"'\nAvailable options:\n",
|
|
"\tfusion_ir, fusion_ir_math, kernel_ir, ca_map, cuda_kernel, cuda_full,\n",
|
|
"\tcuda_to_file, launch_param, segmented_fusion, fusion_args,\n",
|
|
"\tkernel_args, dump_eff_bandwidth, draw_segmented_fusion,\n",
|
|
"\tscheduler_params, parallel_dimensions, buffer_reuse_verbose,\n",
|
|
"\tptxas_verbose, halo, segmenter_logging, perf_debug_verbose\n");
|
|
}
|
|
options_view = (end_pos != c10::string_view::npos)
|
|
? options_view.substr(end_pos + 1)
|
|
: "";
|
|
}
|
|
}
|
|
|
|
return options_map;
|
|
}
|
|
|
|
auto parseDisableOptions() {
|
|
std::unordered_map<DisableOption, bool> options_map = {
|
|
{DisableOption::ArchCheck, false},
|
|
{DisableOption::Fallback, false},
|
|
{DisableOption::Fma, false},
|
|
{DisableOption::IndexHoist, false},
|
|
{DisableOption::Nvtx, false},
|
|
{DisableOption::PredicateElimination, false},
|
|
{DisableOption::UnrollWithRng, false}};
|
|
|
|
if (const char* dump_options = std::getenv("PYTORCH_NVFUSER_DISABLE")) {
|
|
c10::string_view options_view(dump_options);
|
|
while (!options_view.empty()) {
|
|
const auto end_pos = options_view.find_first_of(',');
|
|
const auto token = options_view.substr(0, end_pos);
|
|
if (token == "arch_check") {
|
|
options_map[DisableOption::ArchCheck] = true;
|
|
} else if (token == "fallback") {
|
|
options_map[DisableOption::Fallback] = true;
|
|
} else if (token == "fma") {
|
|
TORCH_WARN(
|
|
"fmad is disabled for nvrtc, which could negatively affect performance. Try removing `fma` from env variable PYTORCH_NVFUSER_DISABLE for optimal performance.");
|
|
options_map[DisableOption::Fma] = true;
|
|
} else if (token == "index_hoist") {
|
|
options_map[DisableOption::IndexHoist] = true;
|
|
} else if (token == "nvtx") {
|
|
options_map[DisableOption::Nvtx] = true;
|
|
} else if (token == "predicate_elimination") {
|
|
options_map[DisableOption::PredicateElimination] = true;
|
|
} else if (token == "unroll_with_rng") {
|
|
options_map[DisableOption::UnrollWithRng] = true;
|
|
} else {
|
|
TORCH_CHECK(
|
|
false,
|
|
"Invalid disable option: '",
|
|
token,
|
|
"'\nAvailable options:\n",
|
|
"\tarch_check, fallback, fma, index_hoist, nvtx, predicate_elimination\n",
|
|
"unroll_with_rng");
|
|
}
|
|
options_view = (end_pos != c10::string_view::npos)
|
|
? options_view.substr(end_pos + 1)
|
|
: "";
|
|
}
|
|
}
|
|
|
|
return options_map;
|
|
}
|
|
|
|
auto parseEnableOptions() {
|
|
std::unordered_map<EnableOption, bool> options_map = {
|
|
{EnableOption::Complex, false},
|
|
{EnableOption::KernelProfile, false},
|
|
{EnableOption::LinearDecomposition, false},
|
|
{EnableOption::ConvDecomposition, false}};
|
|
|
|
if (const char* dump_options = std::getenv("PYTORCH_NVFUSER_ENABLE")) {
|
|
c10::string_view options_view(dump_options);
|
|
while (!options_view.empty()) {
|
|
const auto end_pos = options_view.find_first_of(',');
|
|
const auto token = options_view.substr(0, end_pos);
|
|
if (token == "complex") {
|
|
options_map[EnableOption::Complex] = true;
|
|
} else if (token == "kernel_profile") {
|
|
options_map[EnableOption::KernelProfile] = true;
|
|
} else if (token == "linear_decomposition") {
|
|
options_map[EnableOption::LinearDecomposition] = true;
|
|
} else if (token == "conv_decomposition") {
|
|
options_map[EnableOption::ConvDecomposition] = true;
|
|
} else {
|
|
TORCH_CHECK(
|
|
false,
|
|
"Invalid disable option: '",
|
|
token,
|
|
"'\nAvailable options:\n",
|
|
"\tcomplex, kernel_profile");
|
|
}
|
|
options_view = (end_pos != c10::string_view::npos)
|
|
? options_view.substr(end_pos + 1)
|
|
: "";
|
|
}
|
|
}
|
|
|
|
return options_map;
|
|
}
|
|
|
|
} // namespace
|
|
|
|
#pragma clang diagnostic push
|
|
#pragma clang diagnostic ignored "-Wunused-function"
|
|
void debugPrint(const c10::TensorTypePtr& type) {
|
|
std::stringstream sizes_s;
|
|
if (auto sizes = type->symbolic_sizes().sizes()) {
|
|
for (const auto& shape_symbol : *sizes) {
|
|
if (shape_symbol.is_static()) {
|
|
sizes_s << shape_symbol.static_size() << ", ";
|
|
} else {
|
|
sizes_s << "s(" << *reinterpret_cast<const int64_t*>(&shape_symbol)
|
|
<< "), ";
|
|
}
|
|
}
|
|
} else {
|
|
sizes_s << "no size available";
|
|
}
|
|
std::cout << "sizes:" << sizes_s.str() << std::endl;
|
|
if (const auto& stride_properties = type->stride_properties().sizes()) {
|
|
std::stringstream stride_s;
|
|
std::stringstream index_s;
|
|
std::stringstream contig_s;
|
|
|
|
for (const auto& stride_property : *stride_properties) {
|
|
if (stride_property.has_value() && stride_property->stride_.has_value()) {
|
|
stride_s << *stride_property->stride_ << ", ";
|
|
} else {
|
|
stride_s << "?, ";
|
|
}
|
|
if (stride_property.has_value() &&
|
|
stride_property->stride_index_.has_value()) {
|
|
index_s << *stride_property->stride_index_ << ", ";
|
|
} else {
|
|
index_s << "?, ";
|
|
}
|
|
if (stride_property.has_value() &&
|
|
stride_property->contiguous_.has_value()) {
|
|
contig_s << *stride_property->contiguous_ << ", ";
|
|
} else {
|
|
contig_s << "?, ";
|
|
}
|
|
}
|
|
std::cout << "stride: " << stride_s.str() << std::endl;
|
|
std::cout << "stride index: " << index_s.str() << std::endl;
|
|
std::cout << "contiguous: " << contig_s.str() << std::endl;
|
|
} else {
|
|
std::cout << "no stride properties available" << std::endl;
|
|
}
|
|
}
|
|
#pragma clang diagnostic pop
|
|
|
|
bool is_zero_dim_tensor(const std::shared_ptr<c10::TensorType>& tensor_type) {
|
|
return tensor_type && tensor_type->dim().has_value() &&
|
|
tensor_type->dim().value() == 0;
|
|
}
|
|
|
|
bool is_zero_sized_tensor(const std::shared_ptr<c10::TensorType>& tensor_type) {
|
|
auto opt_sizes = tensor_type->sizes().concrete_sizes();
|
|
if (opt_sizes.has_value()) {
|
|
auto sizes = opt_sizes.value();
|
|
for (const auto& size : sizes) {
|
|
if (size == 0) {
|
|
return true;
|
|
}
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
bool is_cpu_scalar(const at::Tensor& tensor) {
|
|
return tensor.device().is_cpu() && tensor.numel() == 1 && tensor.dim() == 0;
|
|
}
|
|
|
|
bool is_cpu_scalar(const c10::TensorType& tensor_type) {
|
|
auto opt_device = tensor_type.device();
|
|
auto opt_dim = tensor_type.dim();
|
|
auto opt_numel = tensor_type.numel();
|
|
return opt_device.has_value() && opt_device.value().is_cpu() &&
|
|
opt_dim.has_value() && opt_numel.has_value() && opt_dim.value() == 0 &&
|
|
opt_numel.value() == 1;
|
|
}
|
|
|
|
bool isDebugDumpEnabled(DebugDumpOption option) {
|
|
const static auto dump_options = parseDebugDumpOptions();
|
|
return dump_options.at(option);
|
|
}
|
|
|
|
bool isDisabled(DisableOption option) {
|
|
const static auto options = parseDisableOptions();
|
|
return options.at(option);
|
|
}
|
|
|
|
bool isEnabled(EnableOption option) {
|
|
const static auto options = parseEnableOptions();
|
|
return options.at(option);
|
|
}
|
|
|
|
bool useFallback() {
|
|
// Keep this env var for compatibility
|
|
const char* disable_fb_env = getenv("PYTORCH_NVFUSER_DISABLE_FALLBACK");
|
|
bool fallback_disabled = disable_fb_env ? atoi(disable_fb_env) : false;
|
|
fallback_disabled = fallback_disabled || isDisabled(DisableOption::Fallback);
|
|
|
|
return !fallback_disabled;
|
|
}
|
|
|
|
std::vector<int64_t> getTensorSizes(TensorTypePtr const& tensor_type) {
|
|
TORCH_INTERNAL_ASSERT(tensor_type != nullptr, "Input must be a Tensor.");
|
|
auto optional_sizes = tensor_type->sizes().concrete_sizes();
|
|
TORCH_INTERNAL_ASSERT(
|
|
optional_sizes.has_value(), "Missing size information for the tensor.");
|
|
return optional_sizes.value();
|
|
}
|
|
|
|
} // namespace cuda
|
|
} // namespace fuser
|
|
} // namespace jit
|
|
} // namespace torch
|