[llvm] [mlir] Add shard Dialect Python Bindings (PR #162578)
Siavash Nazari via llvm-commits
llvm-commits at lists.llvm.org
Wed Oct 8 21:54:58 PDT 2025
================
@@ -0,0 +1,73 @@
+# RUN: %PYTHON %s | FileCheck %s
+
+from mlir.ir import *
+from mlir.dialects import shard
+from mlir.dialects import func
+
+
+def constructAndPrintInModule(f):
+ print("\nTEST:", f.__name__)
+ with Context(), Location.unknown():
+ module = Module.create()
+ with InsertionPoint(module.body):
+ f()
+ print(module)
+ return f
+
+
+# CHECK-LABEL: TEST: testShardGrid
+ at constructAndPrintInModule
+def testShardGrid():
+ # Test creating shard grids with different shapes
+ grid2d = shard.GridOp("grid_2d", [2, 2])
+ grid1d = shard.GridOp("grid_1d", [4])
+ grid_dynamic = shard.GridOp("grid_dynamic", [2, -1]) # -1 for dynamic dimension
+
+
+# CHECK: shard.grid @grid_2d(shape = 2x2)
+# CHECK: shard.grid @grid_1d(shape = 4)
+# CHECK: shard.grid @grid_dynamic(shape = 2x?)
+
+
+# CHECK-LABEL: TEST: testCollectiveOperations
+ at constructAndPrintInModule
+def testCollectiveOperations():
+ # Create grid and types
+ grid = shard.GridOp("grid_2x2", [2, 2])
+ i32 = IntegerType.get_signless(32)
+ input_type = RankedTensorType.get([4, 2], i32)
+ gather_result_type = RankedTensorType.get([4, 4], i32)
+
+ # Create a function to hold the operations
+ func_type = FunctionType.get([input_type], [input_type])
+ test_func = func.FuncOp("test_collectives", func_type)
+
+ with InsertionPoint(test_func.add_entry_block()):
+ arg = test_func.entry_block.arguments[0]
+
+ # All-gather operation
+ gather_op = shard.AllGatherOp(
+ input=arg,
+ grid=FlatSymbolRefAttr.get("grid_2x2"),
+ grid_axes=ArrayAttr.get([IntegerAttr.get(i32, 1)]),
+ gather_axis=IntegerAttr.get(i32, 1),
+ result=gather_result_type
+ )
+
+ # All-reduce operation (ReductionKind might need different construction)
+ reduce_op = shard.AllReduceOp(
+ input=arg,
+ grid=FlatSymbolRefAttr.get("grid_2x2"),
+ reduction=IntegerAttr.get(IntegerType.get_signless(32), 1), # 1 = sum from enum
----------------
Svoch wrote:
Thanks for catching this! Updated to use the generated `ReductionKind`.
https://github.com/llvm/llvm-project/pull/162578
More information about the llvm-commits
mailing list