Skip to content

Commit a0be3ef

Browse files
wuhulalalasgjzfzzf
authored andcommitted
Fix Trident pointwise broadcasting
1 parent ea3c5ea commit a0be3ef

1 file changed

Lines changed: 23 additions & 1 deletion

File tree

src/flag_gems/utils/pointwise_dynamic.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1554,11 +1554,24 @@ def prepare_args(self, *args, _skip_tensor_check=False, **kwargs):
15541554
if tensors[0].numel() > INT32_MAX:
15551555
self.config.prefer_block_pointer = False
15561556
if self.config.enable_trident_jit:
1557-
# Trident path: use raw tensors (no StridedBuffer) so Dynamo can trace
1557+
# Trident path: use raw tensors (no StridedBuffer) so Dynamo can trace.
1558+
# Materialize broadcasting as Tensor views: expand() keeps the inputs
1559+
# zero-copy while encoding broadcast dimensions with stride 0.
15581560
shapes = [item.shape for item in in_tensors]
15591561
task_shape = broadcast_shapes(shapes)
15601562
ndim = len(task_shape)
15611563

1564+
if out_tensors:
1565+
for index, item in enumerate(out_tensors):
1566+
if tuple(item.shape) != tuple(task_shape):
1567+
raise RuntimeError(
1568+
f"out tensor at index {index} shape is invalid, should be {task_shape} but is {item.shape}!"
1569+
)
1570+
if has_internal_overlapping(item) == MemOverlap.Yes:
1571+
raise RuntimeError(
1572+
"Pointwise Input arguments should not have internal overlapping."
1573+
)
1574+
15621575
for item in tensors:
15631576
if item.shape == task_shape:
15641577
allocated_outputs = [
@@ -1576,6 +1589,15 @@ def prepare_args(self, *args, _skip_tensor_check=False, **kwargs):
15761589
for seq_id, output_id in enumerate(outputs_that_need_allocation):
15771590
kwargs[f"out{output_id}"] = allocated_outputs[seq_id]
15781591

1592+
args = tuple(
1593+
(
1594+
item.expand(task_shape)
1595+
if schema.is_tensor(i) and item.shape != task_shape
1596+
else item
1597+
)
1598+
for i, item in enumerate(args)
1599+
)
1600+
15791601
elif self.use_fast_path(tensors): # dimension collapse & use physical ordering
15801602
allocated_outputs = [
15811603
torch.empty_like(tensors[0], dtype=dtype)

0 commit comments

Comments
 (0)