8000 'torch.sparse.to_sparse_semi_structured' significantly worsens performance on H100 GPUs · Issue #153825 · pytorch/pytorch · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
'torch.sparse.to_sparse_semi_structured' significantly worsens performance on H100 GPUs #153825
Open
@sixt99

Description

@sixt99

🐛 Describe the bug

I am trying to get performance speedups on AI models by using 2:4 pruning. My code is based on the following tutorial:
https://docs.pytorch.org/tutorials/advanced/semi_structured_sparse.html#pruning-bert-to-be-2-4-sparse

However, I get terrible speedups. The code I am using is:

import torch
from torch.sparse import to_sparse_semi_structured, SparseSemiStructuredTensor
from torch.utils.benchmark import Timer
SparseSemiStructuredTensor._FORCE_CUTLASS = False

a, b = 3072, 10240
mask = torch.Tensor([0, 0, 1, 1]).tile((a, b // 4)).cuda().bool()
linear = torch.nn.Linear(b, a).half().cuda().eval()
linear.weight = torch.nn.Parameter(mask * linear.weight)
x = torch.rand(a, b).half().cuda()

with torch.inference_mode():
    dense_output = linear(x)
    dense_t = Timer(stmt="linear(x)", globals={"linear": linear, "x": x}).timeit(10).median * 1e3

    # accelerate via SparseSemiStructuredTensor
    linear.weight = torch.nn.Parameter(to_sparse_semi_structured(linear.weight))
    sparse_output = linear(x)
    sparse_t = Timer(stmt="linear(x)", globals={"linear": linear, "x": x}).timeit(10).median * 1e3

    assert torch.allclose(sparse_output, dense_output, atol=1e-3)
    print(f"Dense: {dense_t:.3f}ms Sparse: {sparse_t:.3f}ms | Speedup: {(dense_t / sparse_t):.3f}x")

In the tutorial, shapes are a, b = 3072, 10240 and they yield Dense: 0.870ms Sparse: 0.630ms | Speedup: 1.382x (using A100 80GB). In my case, however, I get Dense: 0.242ms Sparse: 0.657ms | Speedup: 0.368x. Note how Sparse time is very similar in both experiments. I thought that, maybe, dense computation on H100 has been so optimized that the sparse one is simply overshadowed, but I am not sure. I have tried running the experiment with different a, b shapes, in hopes that bigger computation density would expose the benefits of sparse computation. I got:

a,b=(3072, 10240) -> Dense: 0.242ms Sparse: 0.657ms | Speedup: 0.368x
a,b=(30720, 10240) -> Dense: 25.615ms Sparse: 38.073ms | Speedup: 0.673x
a,b=(3072, 102400) -> Dense: 2.274ms Sparse: 3.690ms | Speedup: 0.616x
a,b=(30720, 102400) -> Dense: 851.877ms Sparse: 402.201ms | Speedup: 2.118x
a,b=(4096, 11008) -> Dense: 0.475ms Sparse: 0.773ms | Speedup: 0.615x
a,b=(40960, 11008) -> Dense: 49.293ms Sparse: 73.263ms | Speedup: 0.673x
a,b=(4096, 110080) -> Dense: 7.767ms Sparse: 7.667ms | Speedup: 1.013x

I was planning to apply 2:4 to common AI LLMs, like Llama-7B, but, since linear layers are relatively small, I seem to not be able to take advantage of this technique at all. Is there something I am doing wrong? :(

By the way, since H100 GPUs are 9.x, I need to use cuSPARSELt by setting SparseSemiStructuredTensor._FORCE_CUTLASS = False (in the mentioned tutorial, they set this variable to True and thus use CUTLASS).

Lastly, I should mention that I have conducted a similar analysis outside the PyTorch context, specifically by following the example provided in:
https://github.com/NVIDIA/CUDALibrarySamples/blob/master/cuSPARSELt/matmul/matmul_example.cpp.
In this example, 2:4 structured sparsity is applied to a matrix, followed by a GEMM operation. When comparing the performance of this sparse GEMM to its dense counterpart, I see a speedup of approximately 1.6× in most cases. Shouldn't a similar level of improvement be reflected in the experiments discussed earlier?

Any help would be very much appreciated.

Thank you

Versions

Collecting environment information...
PyTorch version: 2.7.0+cu126
Is debug build: False
CUDA used to build PyTorch: 12.6
ROCM used to build PyTorch: N/A

OS: Red Hat Enterprise Linux 9.2 (Plow) (x86_64)
GCC version: (GCC) 11.3.1 20221121 (Red Hat 11.3.1-4)
Clang version: Could not collect
CMake version: Could not collect
Libc version: glibc-2.34

Python version: 3.9.16 (main, Sep 12 2023, 00:00:00) [GCC 11.3.1 20221121 (Red Hat 11.3.1-4)] (64-bit runtime)
Python platform: Linux-5.14.0-284.30.1.el9_2.x86_64-x86_64-with-glibc2.34
Is CUDA available: True
CUDA runtime version: Could not collect
CUDA_MODULE_LOADING set to: LAZY
GPU models and configuration:
GPU 0: NVIDIA H100
GPU 1: NVIDIA H100
GPU 2: NVIDIA H100
GPU 3: NVIDIA H100

Nvidia driver version: 535.86.10
cuDNN version: Could not collect
HIP runtime version: N/A
MIOpen runtime version: N/A
Is XNNPACK available: True

CPU:
Architecture: x86_64
CPU op-mode(s): 32-bit, 64-bit
Address sizes: 46 bits physical, 57 bits virtual
Byte Order: Little Endian
CPU(s): 160
On-line CPU(s) list: 0-159
Vendor ID: GenuineIntel
Model name: Intel(R) Xeon(R) Platinum 8460Y+
CPU family: 6
Model: 143
Thread(s) per core: 2
Core(s) per socket: 40
Socket(s): 2
Stepping: 8
CPU max MHz: 3700.0000
CPU min MHz: 800.0000
BogoMIPS: 4000.00
Flags: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc art arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc cpuid aperfmperf tsc_known_freq pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid dca sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm 3dnowprefetch cpuid_fault epb cat_l3 cat_l2 cdp_l3 invpcid_single intel_ppin cdp_l2 ssbd mba ibrs ibpb stibp ibrs_enhanced tpr_shadow vnmi flexpriority ept vpid ept_ad fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid cqm rdt_a avx512f avx512dq rdseed adx smap avx512ifma clflushopt clwb intel_pt avx512cd sha_ni avx512bw avx512vl xsaveopt xsavec xgetbv1 xsaves cqm_llc cqm_occup_llc cqm_mbm_total cqm_mbm_local split_lock_detect avx_vnni avx512_bf16 wbnoinvd dtherm ida arat pln pts hwp hwp_act_window hwp_epp hwp_pkg_req hfi avx512vbmi umip pku ospke waitpkg avx512_vbmi2 gfni vaes vpclmulqdq avx512_vnni avx512_bitalg tme avx512_vpopcntdq la57 rdpid bus_lock_detect cldemote movdiri movdir64b enqcmd fsrm md_clear serialize tsxldtrk pconfig arch_lbr ibt amx_bf16 avx512_fp16 amx_tile amx_int8 flush_l1d arch_capabilities
Virtualization: VT-x
L1d cache: 3.8 MiB (80 instances)
L1i cache: 2.5 MiB (80 instances)
L2 cache: 160 MiB (80 instances)
L3 cache: 210 MiB (2 instances)
NUMA node(s): 4
NUMA node0 CPU(s): 0-19,80-99
NUMA node1 CPU(s): 20-39,100-119
NUMA node2 CPU(s): 40-59,120-139
NUMA node3 CPU(s): 60-79,140-159
Vulnerability Itlb multihit: Not affected
Vulnerability L1tf: Not affected
Vulnerability Mds: Not affected
Vulnerability Meltdown: Not affected
Vulnerability Mmio stale data: Not affected
Vulnerability Retbleed: Not affected
Vulnerability Spec store bypass: Mitigation; Speculative Store Bypass disabled via prctl
Vulnerability Spectre v1: Mitigation; usercopy/swapgs barriers and __user pointer sanitization
Vulnerability Spectre v2: Mitigation; Enhanced IBRS, IBPB conditional, RSB filling, PBRSB-eIBRS SW sequence
Vulnerability Srbds: Not affected
Vulnerability Tsx async abort: Not affected

Versions of relevant libraries:
[pip3] mypy-extensions==1.0.0
[pip3] numpy==1.24.4
[pip3] nvidia-cublas-cu12==12.6.4.1
[pip3] nvidia-cuda-cupti-cu12==12.6.80
[pip3] nvidia-cuda-nvrtc-cu12==12.6.77
[pip3] nvidia-cuda-runtime-cu12==12.6.77
[pip3] nvidia-cudnn-cu12==9.5.1.17
[pip3] nvidia-cufft-cu12==11.3.0.4
[pip3] nvidia-curand-cu12==10.3.7.77
[pip3] nvidia-cusolver-cu12==11.7.1.2
[pip3] nvidia-cusparse-cu12==12.5.4.2
[pip3] nvidia-cusparselt-cu12==0.6.3
[pip3] nvidia-nccl-cu12==2.26.2
[pip3] nvidia-nvjitlink-cu12==12.6.85
[pip3] nvidia-nvtx-cu12==12.6.77
[pip3] > [pip3] > [pip3] pytorch-memlab==0.3.0
[pip3] torch==2.7.0
[pip3] torchvision==0.22.0
[pip3] triton==3.3.0
[conda] Could not collect

cc @msaroufim @jerryzh168 @alexsamardzic @nikitaved @pearu @cpuhrsch @amjames @bhosmer @jcaip @ptrblck @eqy

Metadata

Metadata

Assignees

Labels

module: cudaRelated to torch.cuda, and CUDA support in generalmodule: performanceIssues related to performance, either of kernel code or framework gluemodule: sparseRelated to torch.sparsetriagedThis issue has been looked at a team member, and triaged and prioritized into an appropriate module

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions

    0