|
| 1 | +import math |
| 2 | + |
| 3 | +import pytest |
| 4 | +import torch |
| 5 | + |
| 6 | +import flag_gems |
| 7 | +from flag_gems import linalg_det, linalg_det_out |
| 8 | + |
| 9 | +from . import base, consts |
| 10 | +from .conftest import Config |
| 11 | + |
| 12 | +VENDOR = flag_gems.vendor_name |
| 13 | + |
| 14 | + |
| 15 | +if VENDOR == "ascend": |
| 16 | + Config.mode = consts.BenchMode.OPERATOR |
| 17 | + |
| 18 | + |
| 19 | +def _small_ops_det(A): |
| 20 | + n = A.shape[-1] |
| 21 | + batch_shape = A.shape[:-2] |
| 22 | + B = math.prod(batch_shape) if batch_shape else 1 |
| 23 | + LU = A.clone().reshape(B, n, n) |
| 24 | + sign = torch.ones(B, dtype=A.dtype, device=A.device) |
| 25 | + bidx = torch.arange(B, device=A.device) |
| 26 | + for k in range(n): |
| 27 | + p = LU[:, k:, k].abs().argmax(dim=-1) + k |
| 28 | + swap = p != k |
| 29 | + sign = torch.where(swap, -sign, sign) |
| 30 | + row_k = LU[:, k, :].clone() |
| 31 | + row_p = LU[bidx, p, :].clone() |
| 32 | + LU[:, k, :] = row_p |
| 33 | + LU[bidx[swap], p[swap], :] = row_k[swap] |
| 34 | + pivot = LU[:, k, k] |
| 35 | + safe_pivot = torch.where(pivot == 0, torch.ones_like(pivot), pivot) |
| 36 | + col = LU[:, k + 1 :, k] |
| 37 | + mult = torch.where( |
| 38 | + (pivot == 0).unsqueeze(-1), |
| 39 | + torch.zeros_like(col), |
| 40 | + col / safe_pivot.unsqueeze(-1), |
| 41 | + ) |
| 42 | + LU[:, k + 1 :, k] = mult |
| 43 | + LU[:, k + 1 :, k + 1 :] -= mult.unsqueeze(-1) * LU[:, k : k + 1, k + 1 :] |
| 44 | + det = LU.diagonal(dim1=-2, dim2=-1).prod(dim=-1) * sign |
| 45 | + return det.reshape(batch_shape) |
| 46 | + |
| 47 | + |
| 48 | +def _torch_det(A): |
| 49 | + if A.device.type == "npu": |
| 50 | + return _small_ops_det(A) |
| 51 | + return torch.linalg.det(A) |
| 52 | + |
| 53 | + |
| 54 | +def _torch_det_out(A, *, out): |
| 55 | + if A.device.type == "npu": |
| 56 | + out.copy_(_torch_det(A)) |
| 57 | + return out |
| 58 | + return torch.linalg.det(A, out=out) |
| 59 | + |
| 60 | + |
| 61 | +DET_SHAPES = [ |
| 62 | + (16, 16), |
| 63 | + (32, 32), |
| 64 | + (64, 64), |
| 65 | + (128, 128), |
| 66 | + (256, 256), |
| 67 | + (4096, 4, 4), |
| 68 | + (1024, 8, 8), |
| 69 | + (1024, 16, 16), |
| 70 | + (128, 16, 16), |
| 71 | + (4, 32, 32), |
| 72 | + (512, 32, 32), |
| 73 | + (256, 64, 64), |
| 74 | + (32, 128, 128), |
| 75 | + (8, 256, 256), |
| 76 | +] |
| 77 | + |
| 78 | +DET_DTYPES = [torch.float32] + ( |
| 79 | + [torch.float64] if flag_gems.runtime.device.support_fp64 else [] |
| 80 | +) |
| 81 | + |
| 82 | + |
| 83 | +class DetBenchmark(base.Benchmark): |
| 84 | + def set_shapes(self, shape_file_path=None): |
| 85 | + self.shapes = DET_SHAPES |
| 86 | + |
| 87 | + def get_input_iter(self, cur_dtype): |
| 88 | + for shape in self.shapes: |
| 89 | + A = torch.randn(shape, dtype=cur_dtype, device=self.device) |
| 90 | + yield (A,) |
| 91 | + |
| 92 | + |
| 93 | +@pytest.mark.linalg_det |
| 94 | +def test_linalg_det(): |
| 95 | + bench = DetBenchmark( |
| 96 | + op_name="linalg_det", |
| 97 | + torch_op=_torch_det, |
| 98 | + dtypes=DET_DTYPES, |
| 99 | + ) |
| 100 | + bench.set_gems(linalg_det) |
| 101 | + bench.run() |
| 102 | + |
| 103 | + |
| 104 | +class DetOutBenchmark(base.Benchmark): |
| 105 | + def set_shapes(self, shape_file_path=None): |
| 106 | + self.shapes = DET_SHAPES |
| 107 | + |
| 108 | + def get_input_iter(self, cur_dtype): |
| 109 | + for shape in self.shapes: |
| 110 | + A = torch.randn(shape, dtype=cur_dtype, device=self.device) |
| 111 | + out = torch.empty(shape[:-2], dtype=cur_dtype, device=self.device) |
| 112 | + yield (A, {"out": out}) |
| 113 | + |
| 114 | + |
| 115 | +@pytest.mark.linalg_det_out |
| 116 | +def test_linalg_det_out(): |
| 117 | + bench = DetOutBenchmark( |
| 118 | + op_name="linalg_det_out", |
| 119 | + torch_op=_torch_det_out, |
| 120 | + dtypes=DET_DTYPES, |
| 121 | + ) |
| 122 | + bench.set_gems(linalg_det_out) |
| 123 | + bench.run() |
0 commit comments