Skip to content

Commit 831c3d4

Browse files
authored
Merge branch 'master' into pr/_fake_quantize_learnable_per_tensor_affine
2 parents 7e4a863 + 60147ce commit 831c3d4

24 files changed

Lines changed: 2600 additions & 3 deletions

benchmark/test_conj_physical.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ def _input_fn(shape, dtype, device):
3434

3535

3636
class Conj_physicalBenchmark(base.GenericBenchmarkExcluse3D):
37-
# TODO(Qiming): Check if this is necessary
3837
def __init__(self, *args, **kwargs):
3938
super().__init__(*args, **kwargs)
4039

@@ -55,7 +54,14 @@ def set_more_shapes(self):
5554

5655
@pytest.mark.conj_physical
5756
def test_conj_physical():
58-
dtypes = consts.FLOAT_DTYPES + consts.INT_DTYPES + consts.COMPLEX_DTYPES
57+
if "npu" in flag_gems.device or "ascend" in flag_gems.device.lower():
58+
# Ascend NPU: kernel mode event timing is unstable, use operator mode
59+
from .conftest import Config
60+
61+
Config.mode = consts.BenchMode.OPERATOR
62+
dtypes = consts.FLOAT_DTYPES + consts.INT_DTYPES
63+
else:
64+
dtypes = consts.FLOAT_DTYPES + consts.INT_DTYPES + consts.COMPLEX_DTYPES
5965

6066
bench = Conj_physicalBenchmark(
6167
input_fn=_input_fn,

benchmark/test_layer_norm.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ def set_more_shapes(self):
3131
# 4D shapes represented as [batch_size, channels, H, W]
3232
(1, 8, 4, 4),
3333
(16, 8, 128, 128),
34+
# Medium normalized dimensions across representative row counts
35+
(256, 512),
36+
(4096, 256),
37+
(4096, 512),
38+
(2048, 1024),
39+
(1024, 2048),
3440
]
3541

3642

benchmark/test_linalg_det.py

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
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()

benchmark/test_replication_pad2d_backward.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
11
import pytest
22
import torch
33

4+
import flag_gems
5+
46
from . import base, consts
7+
from .conftest import Config
8+
9+
VENDOR = flag_gems.vendor_name
10+
11+
if VENDOR == "ascend":
12+
Config.mode = consts.BenchMode.OPERATOR
513

614
REPLICATION_PAD2D_BACKWARD_SHAPES = [
715
(1, 3, 256, 256),

conf/operators.yaml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5768,6 +5768,26 @@ ops:
57685768
- aten
57695769
kind:
57705770
- Math
5771+
- id: linalg_det
5772+
description: |
5773+
Computes the determinant of a square matrix via LU decomposition with partial pivoting.
5774+
for:
5775+
- linalg.det
5776+
labels:
5777+
- aten
5778+
kind:
5779+
- LinearAlg
5780+
stages:
5781+
- alpha: '5.4'
5782+
- id: linalg_det_out
5783+
description: |
5784+
A variant of linalg_det that writes the determinant to the provided out tensor.
5785+
for:
5786+
- linalg.det.out
5787+
labels:
5788+
- aten
5789+
kind:
5790+
- LinearAlg
57715791
stages:
57725792
- alpha: '5.4'
57735793
- id: linalg_lu_factor_ex

src/flag_gems/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -629,6 +629,8 @@ def torch_ge(v):
629629
("linalg_cholesky", linalg_cholesky),
630630
("linalg_cross", linalg_cross),
631631
("linalg_cross.out", linalg_cross_out),
632+
("linalg_det", linalg_det),
633+
("linalg_det.out", linalg_det_out),
632634
("linalg_ldl_factor", ldl_factor),
633635
("linalg_ldl_factor_ex", ldl_factor_ex),
634636
("linalg_lu_factor", linalg_lu_factor),

src/flag_gems/ops/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,7 @@
436436
from flag_gems.ops.lift_fresh_copy import lift_fresh_copy, lift_fresh_copy_out
437437
from flag_gems.ops.linalg_cholesky import linalg_cholesky
438438
from flag_gems.ops.linalg_cross import linalg_cross, linalg_cross_out
439+
from flag_gems.ops.linalg_det import linalg_det, linalg_det_out
439440
from flag_gems.ops.linalg_ldl_factor import ldl_factor
440441
from flag_gems.ops.linalg_ldl_solve import linalg_ldl_solve
441442
from flag_gems.ops.linalg_lstsq import linalg_lstsq
@@ -1309,6 +1310,8 @@
13091310
"linalg_cholesky",
13101311
"linalg_cross",
13111312
"linalg_cross_out",
1313+
"linalg_det",
1314+
"linalg_det_out",
13121315
"linalg_ldl_solve",
13131316
"linalg_lstsq",
13141317
"linalg_lu_factor",

0 commit comments

Comments
 (0)