Isalia20
ad67170c8b
[MPS] sparse matmuls (#165232)
Implements matmuls for sparse tensors. With this commit most of the core sparse operations should be implemented. Fixes:
https://github.com/pytorch/pytorch/issues/156540
https://github.com/pytorch/pytorch/issues/129842
Should be merged after:
https://github.com/pytorch/pytorch/pull/165102
To compare MPS and CPU, you can use this script:
```python
import torch
import time
import matplotlib.pyplot as plt
B, I, J, K = 8, 20000, 20000, 20000
num_iterations = 500
nnz_values = [10, 50, 100, 200, 500, 1000, 2000, 5000, 10000, 20000, 100000]
speedups = []
for nnz in nnz_values:
indices = torch.stack([
torch.randint(0, B, (nnz,)),
torch.randint(0, I, (nnz,)),
torch.randint(0, J, (nnz,)),
])
values = torch.rand(nnz)
sparse = torch.sparse_coo_tensor(indices, values, size=(B, I, J), device="mps").coalesce()
dense = torch.randn(B, J, 200, device="mps")
t1 = time.time()
for _ in range(num_iterations):
result = torch.bmm(sparse, dense)
torch.mps.synchronize()
t2 = time.time()
mps_time = (t2 - t1) / num_iterations
sparse_cpu = sparse.cpu()
dense_cpu = dense.cpu()
t1 = time.time()
for _ in range(num_iterations):
result_cpu = torch.bmm(sparse_cpu, dense_cpu)
t2 = time.time()
cpu_time = (t2 - t1) / num_iterations
speedup = cpu_time / mps_time
speedups.append(speedup)
print(f"nnz={nnz}: MPS={mps_time:.6f}s, CPU={cpu_time:.6f}s, Speedup={speedup:.2f}x")
plt.figure(figsize=(10, 6))
plt.plot(nnz_values, speedups, marker='o', linewidth=2, markersize=8)
plt.xlabel('Number of Non-Zero Elements (nnz)', fontsize=12)
plt.ylabel('Speedup (CPU time / MPS time)', fontsize=12)
plt.title('MPS vs CPU Speedup for Sparse-Dense BMM', fontsize=14)
plt.grid(True, alpha=0.3)
plt.axhline(y=1, color='r', linestyle='--', alpha=0.5)
plt.xscale('log')
plt.tight_layout()
plt.show()
```
## Tested on M1 Pro
<img width="1000" height="600" alt="Figure_1" src="https://github.com/user-attachments/assets/4a2402ec-3dc4-402d-8196-a0426906ca3d" />
Pull Request resolved: https://github.com/pytorch/pytorch/pull/165232
Approved by: https://github.com/malfet
2025-10-18 09:04:42 +00:00
..
2025-10-18 07:36:18 +00:00
2024-12-13 22:13:12 +00:00
2025-07-17 12:08:33 +00:00
2025-06-21 18:33:38 +00:00
2025-10-17 18:32:39 +00:00
2025-06-24 04:53:54 +00:00
2025-10-17 18:32:39 +00:00
2025-03-29 01:39:13 +00:00
2025-07-25 02:37:30 +00:00
2025-10-18 07:36:18 +00:00
2025-10-18 07:36:18 +00:00
2025-10-18 07:36:18 +00:00
2025-10-17 17:44:45 +00:00
2025-10-01 21:32:59 +00:00
2025-10-15 17:47:23 +00:00
2025-10-17 22:35:50 +00:00
2025-10-09 15:42:51 +00:00
2025-10-18 07:36:18 +00:00
2025-10-17 07:27:11 +00:00
2025-10-17 22:35:50 +00:00
2025-10-18 07:36:18 +00:00
2025-09-10 04:39:20 +00:00
2025-01-27 18:12:39 +00:00
2025-10-18 07:36:18 +00:00
2025-03-29 01:39:13 +00:00
2025-10-11 01:03:55 +00:00
2025-10-17 07:27:11 +00:00
2025-10-18 07:36:18 +00:00
2025-10-18 07:36:18 +00:00
2025-10-18 07:36:18 +00:00
2025-08-06 02:26:10 +00:00
2025-10-18 07:36:18 +00:00
2025-10-18 07:36:18 +00:00
2025-07-29 03:26:09 +00:00
2025-02-22 03:44:53 +00:00
2025-10-17 07:27:11 +00:00
2025-09-02 16:53:55 +00:00
2025-09-19 07:37:14 +00:00
2025-10-09 15:42:51 +00:00
2025-08-03 20:53:58 +00:00
2025-10-02 22:22:04 +00:00
2024-11-04 18:30:29 +00:00
2025-10-14 19:19:03 +00:00
2025-05-12 18:30:52 +00:00
2025-10-13 11:47:32 +00:00
2025-08-08 17:41:22 +00:00
2025-09-18 16:08:13 +00:00
2025-04-26 18:10:58 +00:00
2025-10-16 23:08:27 +00:00
2025-07-30 19:30:55 +00:00
2025-10-17 07:27:11 +00:00
2025-10-13 01:48:55 +00:00
2025-06-04 14:38:13 +00:00
2025-10-08 07:27:17 +00:00
2024-11-22 20:54:55 +00:00
2025-10-08 07:27:17 +00:00
2025-09-09 15:49:21 +00:00
2025-08-04 20:37:39 +00:00
2025-09-29 15:15:10 +00:00
2025-09-29 17:50:12 +00:00
2025-04-10 21:02:14 +00:00
2025-04-25 20:15:04 +00:00
2025-01-04 14:17:20 +00:00
2025-09-09 15:49:21 +00:00
2025-10-09 03:24:50 +00:00
2025-09-23 22:15:10 +00:00
2025-01-04 10:47:51 +00:00
2024-12-18 23:02:30 +00:00
2025-10-17 23:46:02 +00:00
2025-10-14 14:18:42 +00:00
2025-10-18 07:36:18 +00:00
2025-10-18 07:36:18 +00:00
2025-10-17 07:27:11 +00:00
2025-06-14 03:37:38 +00:00
2025-10-14 17:17:11 +00:00
2025-10-18 07:36:18 +00:00
2025-08-04 20:37:39 +00:00
2025-01-23 00:31:39 +00:00
2025-09-26 15:45:02 +00:00
2024-12-18 23:02:30 +00:00
2025-10-09 18:03:12 +00:00
2025-10-06 22:42:01 +00:00
2024-12-18 23:02:30 +00:00
2025-02-08 00:55:20 +00:00
2025-01-04 10:47:51 +00:00
2025-07-09 11:02:23 +00:00
2025-07-09 11:02:23 +00:00
2025-10-15 03:18:57 +00:00
2025-07-09 11:02:23 +00:00
2025-07-10 06:34:46 +00:00
2025-10-17 07:27:11 +00:00
2025-02-28 00:47:03 +00:00
2025-06-04 14:38:13 +00:00
2025-10-18 07:36:18 +00:00
2025-10-17 07:27:11 +00:00
2025-10-02 22:22:04 +00:00
2025-10-18 07:36:18 +00:00
2025-10-02 22:22:04 +00:00
2025-10-02 22:22:04 +00:00
2024-12-18 23:02:30 +00:00
2025-10-09 03:24:50 +00:00
2025-10-18 07:36:18 +00:00
2024-12-18 23:02:30 +00:00
2025-09-26 18:26:56 +00:00
2025-07-09 11:02:23 +00:00
2025-07-09 11:02:23 +00:00
2025-10-15 22:26:47 +00:00
2025-07-09 11:02:23 +00:00
2025-07-21 21:44:49 +00:00
2025-10-18 07:36:18 +00:00
2025-09-17 20:29:12 +00:00
2024-12-18 23:02:30 +00:00
2025-01-04 10:47:51 +00:00
2025-07-17 08:57:34 +00:00
2024-12-18 23:02:30 +00:00
2025-09-10 14:19:34 +00:00
2024-12-18 23:02:30 +00:00
2025-09-12 15:02:40 +00:00
2024-12-18 23:02:30 +00:00
2025-10-18 07:36:18 +00:00
2025-10-16 19:34:10 +00:00
2025-10-13 01:48:55 +00:00
2025-09-26 17:41:00 +00:00
2025-01-04 10:47:51 +00:00
2024-12-06 21:45:18 +00:00
2025-10-13 17:59:18 +00:00
2025-10-17 07:27:11 +00:00
2025-01-04 10:47:51 +00:00
2025-10-18 07:36:18 +00:00
2025-09-26 18:26:56 +00:00
2025-10-17 07:27:11 +00:00
2025-10-14 20:21:04 +00:00
2025-10-08 09:09:16 +00:00
2024-12-18 23:02:30 +00:00
2025-03-18 16:09:39 +00:00
2025-07-09 11:02:23 +00:00
2025-09-29 09:08:04 +00:00
2025-09-16 12:07:50 +00:00
2024-12-27 07:58:44 +00:00
2025-09-13 07:52:50 +00:00
2025-07-09 11:02:23 +00:00
2025-09-19 19:41:33 +00:00
2025-10-01 21:32:59 +00:00
2025-10-08 18:42:37 +00:00
2025-10-17 07:27:11 +00:00
2025-10-07 20:51:22 +00:00
2025-10-15 17:24:50 +00:00
2025-09-05 20:15:29 +00:00
2025-07-11 03:21:47 +00:00
2025-10-18 07:36:18 +00:00
2025-07-17 01:27:44 +00:00
2025-10-17 17:52:19 +00:00
2025-09-15 06:50:00 +00:00
2025-08-10 18:35:42 +00:00
2025-10-17 07:27:11 +00:00
2025-10-18 07:36:18 +00:00
2025-02-25 03:47:40 +00:00
2025-08-14 17:06:27 +00:00
2025-10-18 07:36:18 +00:00
2025-09-23 07:52:00 +00:00
2025-10-18 09:04:42 +00:00
2025-06-04 01:58:52 +00:00
2025-07-09 11:02:23 +00:00
2025-10-18 07:36:18 +00:00
2024-12-18 23:02:30 +00:00
2025-09-29 01:42:01 +00:00
2025-10-17 07:27:11 +00:00
2025-10-18 07:36:18 +00:00
2025-10-18 07:36:18 +00:00
2025-09-29 01:42:01 +00:00
2025-01-26 03:37:20 +00:00
2025-10-18 07:36:18 +00:00
2025-10-17 07:27:11 +00:00
2025-10-09 21:58:54 +00:00
2025-10-08 07:27:17 +00:00
2025-10-08 07:27:17 +00:00
2025-10-13 01:48:55 +00:00
2025-07-09 11:02:23 +00:00
2025-09-15 05:44:15 +00:00
2025-02-05 19:40:10 +00:00
2024-12-12 01:18:34 +00:00
2025-10-04 15:25:45 +00:00
2025-10-15 19:45:55 +00:00
2025-10-18 07:36:18 +00:00
2024-12-18 23:02:30 +00:00
2025-07-09 11:02:23 +00:00
2025-10-18 07:36:18 +00:00
2025-10-12 12:11:57 +00:00