302 lines · plain
1// RUN: mlir-opt --pass-pipeline="builtin.module(func.func(sharding-propagation,cse))" %s | FileCheck %s2 3shard.grid @grid_2(shape = 2)4shard.grid @grid_1d(shape = ?)5shard.grid @grid_2d(shape = 2x4)6shard.grid @grid_3d(shape = ?x?x?)7 8// CHECK-LABEL: func.func @element_wise_empty_sharding_info9func.func @element_wise_empty_sharding_info(%arg0: tensor<8x16xf32>) -> tensor<8x16xf32> {10 // CHECK-NEXT: tosa.sigmoid11 %0 = tosa.sigmoid %arg0 : (tensor<8x16xf32>) -> tensor<8x16xf32>12 // CHECK-NEXT: return13 return %0 : tensor<8x16xf32>14}15 16// CHECK-LABEL: func.func @element_wise_on_def17// CHECK-SAME: %[[ARG:.*]]: tensor<8x16xf32>18func.func @element_wise_on_def(%arg0: tensor<8x16xf32>) -> tensor<8x16xf32> {19 // CHECK-NEXT: %[[S0:.*]] = shard.sharding @grid_2d split_axes = {{\[\[}}0], [1]] : !shard.sharding20 // CHECK-NEXT: %[[V0:.*]] = shard.shard %[[ARG]] to %[[S0]] annotate_for_users : tensor<8x16xf32>21 // CHECK-NEXT: %[[V1:.*]] = tosa.sigmoid %[[V0]]22 %0 = tosa.sigmoid %arg0 : (tensor<8x16xf32>) -> tensor<8x16xf32>23 // CHECK-NEXT: %[[V2:.*]] = shard.shard %[[V1]] to %[[S0]] : tensor<8x16xf32>24 %s1 = shard.sharding @grid_2d split_axes = [[0], [1]] : !shard.sharding25 %1 = shard.shard %0 to %s1 : tensor<8x16xf32>26 // CHECK-NEXT: return %[[V2]]27 return %1 : tensor<8x16xf32>28}29 30// CHECK-LABEL: func.func @element_wise_on_use31// CHECK-SAME: %[[ARG:.*]]: tensor<8x16xf32>32func.func @element_wise_on_use(%arg0: tensor<8x16xf32>) -> tensor<8x16xf32> {33 // CHECK-NEXT: %[[S0:.*]] = shard.sharding @grid_2d split_axes = {{\[\[}}0], [1]] : !shard.sharding34 // CHECK-NEXT: %[[V0:.*]] = shard.shard %[[ARG]] to %[[S0]] annotate_for_users : tensor<8x16xf32>35 %s0 = shard.sharding @grid_2d split_axes = [[0], [1]] : !shard.sharding36 %0 = shard.shard %arg0 to %s0 annotate_for_users : tensor<8x16xf32>37 // CHECK-NEXT: %[[V1:.*]] = tosa.sigmoid %[[V0]]38 %1 = tosa.sigmoid %0 : (tensor<8x16xf32>) -> tensor<8x16xf32>39 // CHECK-NEXT: %[[V2:.*]] = shard.shard %[[V1]] to %[[S0]] : tensor<8x16xf32>40 // CHECK-NEXT: return %[[V2]]41 return %1 : tensor<8x16xf32>42}43 44// CHECK-LABEL: func.func @element_wise_on_graph_output45// CHECK-SAME: %[[ARG:.*]]: tensor<8x16xf32>46func.func @element_wise_on_graph_output(%arg0: tensor<8x16xf32>) -> tensor<8x16xf32> {47 // CHECK-NEXT: %[[S0:.*]] = shard.sharding @grid_2d split_axes = {{\[\[}}0], [1]] : !shard.sharding48 // CHECK-NEXT: %[[V0:.*]] = shard.shard %[[ARG]] to %[[S0]] annotate_for_users : tensor<8x16xf32>49 // CHECK-NEXT: %[[V1:.*]] = tosa.sigmoid %[[V0]]50 %0 = tosa.sigmoid %arg0 : (tensor<8x16xf32>) -> tensor<8x16xf32>51 // CHECK-NEXT: %[[V2:.*]] = shard.shard %[[V1]] to %[[S0]] : tensor<8x16xf32>52 // CHECK-NEXT: %[[V3:.*]] = shard.shard %[[V2]] to %[[S0]] annotate_for_users : tensor<8x16xf32>53 %s1 = shard.sharding @grid_2d split_axes = [[0], [1]] : !shard.sharding54 %1 = shard.shard %0 to %s1 annotate_for_users : tensor<8x16xf32>55 // CHECK-NEXT: return %[[V3]]56 return %1 : tensor<8x16xf32>57}58 59// CHECK-LABEL: func.func @element_wise_on_graph_input60// CHECK-SAME: %[[ARG:.*]]: tensor<8x16xf32>61func.func @element_wise_on_graph_input(%arg0: tensor<8x16xf32>) -> tensor<8x16xf32> {62 // CHECK-NEXT: %[[S0:.*]] = shard.sharding @grid_2d split_axes = {{\[\[}}0], [1]] : !shard.sharding63 // CHECK-NEXT: %[[V0:.*]] = shard.shard %[[ARG]] to %[[S0]] : tensor<8x16xf32>64 // CHECK-NEXT: %[[V1:.*]] = shard.shard %[[V0]] to %[[S0]] annotate_for_users : tensor<8x16xf32>65 %s0 = shard.sharding @grid_2d split_axes = [[0], [1]] : !shard.sharding66 %0 = shard.shard %arg0 to %s0 : tensor<8x16xf32>67 // CHECK-NEXT: %[[V2:.*]] = tosa.sigmoid %[[V1]]68 %1 = tosa.sigmoid %0 : (tensor<8x16xf32>) -> tensor<8x16xf32>69 // CHECK-NEXT: %[[V3:.*]] = shard.shard %[[V2]] to %[[S0]] : tensor<8x16xf32>70 // CHECK-NEXT: return %[[V3]]71 return %1 : tensor<8x16xf32>72}73 74// CHECK-LABEL: func.func @arrow_structure75// CHECK-SAME: %[[ARG:.*]]: tensor<8x16xf32>76func.func @arrow_structure(%arg0: tensor<8x16xf32>, %arg1: tensor<1xf32>, %arg2: tensor<1xf32>) -> (tensor<8x16xf32>, tensor<8x16xf32>) {77 // CHECK-NEXT: %[[S1:.*]] = shard.sharding @grid_2d split_axes = {{\[\[}}0], [1]] : !shard.sharding78 // CHECK-NEXT: %[[V1:.*]] = shard.shard %[[ARG]] to %[[S1]] annotate_for_users : tensor<8x16xf32>79 // CHECK-NEXT: %[[V2:.*]] = tosa.tanh %[[V1]]80 // CHECK-NEXT: %[[V3:.*]] = shard.shard %[[V2]] to %[[S1]] : tensor<8x16xf32>81 %0 = tosa.tanh %arg0 : (tensor<8x16xf32>) -> tensor<8x16xf32>82 // CHECK-NEXT: %[[V4:.*]] = shard.shard %[[V3]] to %[[S1]] annotate_for_users : tensor<8x16xf32>83 // CHECK-NEXT: %[[V5:.*]] = tosa.abs %[[V4]]84 // CHECK-NEXT: %[[V6:.*]] = shard.shard %[[V5]] to %[[S1]] : tensor<8x16xf32>85 %1 = tosa.abs %0: (tensor<8x16xf32>) -> tensor<8x16xf32>86 // CHECK-NEXT: %[[S3:.*]] = shard.sharding @grid_2d split_axes = {{\[\[}}]] : !shard.sharding87 // CHECK-NEXT: %[[ZP1:.*]] = shard.shard %arg1 to %[[S3]] annotate_for_users : tensor<1xf32>88 // CHECK-NEXT: %[[ZP2:.*]] = shard.shard %arg2 to %[[S3]] annotate_for_users : tensor<1xf32>89 // CHECK-NEXT: %[[V7:.*]] = tosa.negate %[[V4]], %[[ZP1]], %[[ZP2]]90 // CHECK-NEXT: %[[V8:.*]] = shard.shard %[[V7]] to %[[S1]] : tensor<8x16xf32>91 %2 = tosa.negate %0, %arg1, %arg2 : (tensor<8x16xf32>, tensor<1xf32>, tensor<1xf32>) -> tensor<8x16xf32>92 %s3 = shard.sharding @grid_2d split_axes = [[0], [1]] : !shard.sharding93 %3 = shard.shard %2 to %s3 : tensor<8x16xf32>94 // CHECK-NEXT: return %[[V6]], %[[V8]]95 return %1, %3 : tensor<8x16xf32>, tensor<8x16xf32>96}97 98// CHECK-LABEL: func.func @matmul_on_def_shard_batch_and_m99// CHECK-SAME: %[[ARG0:.*]]: tensor<2x16x8xf32>, %[[ARG1:.*]]: tensor<2x8x32xf32>, %[[ARG2:.*]]: tensor<1xf32>100func.func @matmul_on_def_shard_batch_and_m(%arg0: tensor<2x16x8xf32>, %arg1: tensor<2x8x32xf32>, %arg2: tensor<1xf32>) -> tensor<2x16x32xf32> {101 // CHECK-NEXT: %[[S0:.*]] = shard.sharding @grid_2d split_axes = {{\[\[}}0], [1]] : !shard.sharding102 // CHECK-NEXT: %[[V0:.*]] = shard.shard %[[ARG0]] to %[[S0]] annotate_for_users : tensor<2x16x8xf32>103 // CHECK-NEXT: %[[S1:.*]] = shard.sharding @grid_2d split_axes = {{\[\[}}0]] : !shard.sharding104 // CHECK-NEXT: %[[V1:.*]] = shard.shard %[[ARG1]] to %[[S1]] annotate_for_users : tensor<2x8x32xf32>105 // CHECK-NEXT: %[[S2:.*]] = shard.sharding @grid_2d split_axes = {{\[\[}}]] : !shard.sharding106 // CHECK-NEXT: %[[ZP:.*]] = shard.shard %[[ARG2]] to %[[S2]] annotate_for_users : tensor<1xf32>107 // CHECK-NEXT: %[[V2:.*]] = tosa.matmul %[[V0]], %[[V1]], %[[ZP]], %[[ZP]]108 %0 = tosa.matmul %arg0, %arg1, %arg2, %arg2 : (tensor<2x16x8xf32>, tensor<2x8x32xf32>, tensor<1xf32>, tensor<1xf32>) -> tensor<2x16x32xf32>109 // CHECK-NEXT: %[[V3:.*]] = shard.shard %[[V2]] to %[[S0]] : tensor<2x16x32xf32>110 %s1 = shard.sharding @grid_2d split_axes = [[0], [1]] : !shard.sharding111 %1 = shard.shard %0 to %s1 : tensor<2x16x32xf32>112 // CHECK-NEXT: return %[[V3]]113 return %1 : tensor<2x16x32xf32>114}115 116// CHECK-LABEL: func.func @matmul_on_def_shard_m_and_n117// CHECK-SAME: [[varg0:%.*]]: tensor<2x16x8xf32>, [[varg1:%.*]]: tensor<2x8x32xf32>, [[varg2:%.*]]: tensor<1xf32>118func.func @matmul_on_def_shard_m_and_n(%arg0: tensor<2x16x8xf32>, %arg1: tensor<2x8x32xf32>, %arg2: tensor<1xf32>) -> tensor<2x16x32xf32> {119 // CHECK: [[vsharding:%.*]] = shard.sharding @grid_2d split_axes = {{\[\[}}], [0]] : !shard.sharding120 // CHECK: [[vsharded:%.*]] = shard.shard [[varg0]] to [[vsharding]] annotate_for_users : tensor<2x16x8xf32>121 // CHECK: [[vsharding_0:%.*]] = shard.sharding @grid_2d split_axes = {{\[\[}}], [], [1]] : !shard.sharding122 // CHECK: [[vsharded_1:%.*]] = shard.shard [[varg1]] to [[vsharding_0]] annotate_for_users : tensor<2x8x32xf32>123 // CHECK: [[vsharding_2:%.*]] = shard.sharding @grid_2d split_axes = {{\[\[}}]] : !shard.sharding124 // CHECK: [[vsharded_3:%.*]] = shard.shard [[varg2]] to [[vsharding_2]] annotate_for_users : tensor<1xf32>125 // CHECK: [[v0:%.*]] = tosa.matmul126 %0 = tosa.matmul %arg0, %arg1, %arg2, %arg2 : (tensor<2x16x8xf32>, tensor<2x8x32xf32>, tensor<1xf32>, tensor<1xf32>) -> tensor<2x16x32xf32>127 // CHECK: [[vsharding_4:%.*]] = shard.sharding @grid_2d split_axes = {{\[\[}}], [0], [1]] : !shard.sharding128 // CHECK: [[vsharded_5:%.*]] = shard.shard [[v0]] to [[vsharding_4]] : tensor<2x16x32xf32>129 %s1 = shard.sharding @grid_2d split_axes = [[], [0], [1]] : !shard.sharding130 %1 = shard.shard %0 to %s1 : tensor<2x16x32xf32>131 // CHECK-NEXT: return [[vsharded_5]]132 return %1 : tensor<2x16x32xf32>133}134 135// CHECK-LABEL: func.func @matmul_on_use_shard_m_and_k136// CHECK-SAME: [[varg0:%.*]]: tensor<2x16x8xf32>, [[varg1:%.*]]: tensor<2x8x32xf32>, [[varg2:%.*]]: tensor<1xf32>137func.func @matmul_on_use_shard_m_and_k(%arg0: tensor<2x16x8xf32>, %arg1: tensor<2x8x32xf32>, %arg2: tensor<1xf32>) -> tensor<2x16x32xf32> {138 // CHECK: [[vsharding:%.*]] = shard.sharding @grid_2d split_axes = {{\[\[}}], [0], [1]] : !shard.sharding139 %s0 = shard.sharding @grid_2d split_axes = [[], [0], [1]] : !shard.sharding140 // CHECK: [[vsharded:%.*]] = shard.shard [[varg0]] to [[vsharding]] : tensor<2x16x8xf32>141 %arg0_s = shard.shard %arg0 to %s0 : tensor<2x16x8xf32>142 // CHECK: [[vsharded_0:%.*]] = shard.shard [[vsharded]] to [[vsharding]] annotate_for_users : tensor<2x16x8xf32>143 // CHECK: [[vsharding_1:%.*]] = shard.sharding @grid_2d split_axes = {{\[\[}}], [1]] : !shard.sharding144 // CHECK: [[vsharded_2:%.*]] = shard.shard [[varg1]] to [[vsharding_1]] annotate_for_users : tensor<2x8x32xf32>145 // CHECK: [[vsharding_3:%.*]] = shard.sharding @grid_2d split_axes = {{\[\[}}]] : !shard.sharding146 // CHECK: [[vsharded_4:%.*]] = shard.shard [[varg2]] to [[vsharding_3]] annotate_for_users : tensor<1xf32>147 // CHECK: [[v0:%.*]] = tosa.matmul148 // CHECK: [[vsharding_5:%.*]] = shard.sharding @grid_2d split_axes = {{\[\[}}], [0]] : !shard.sharding149 // CHECK: [[vsharded_6:%.*]] = shard.shard [[v0]] to [[vsharding_5]] : tensor<2x16x32xf32>150 %0 = tosa.matmul %arg0_s, %arg1, %arg2, %arg2 : (tensor<2x16x8xf32>, tensor<2x8x32xf32>, tensor<1xf32>, tensor<1xf32>) -> tensor<2x16x32xf32>151 // CHECK: return [[vsharded_6]]152 return %0 : tensor<2x16x32xf32>153}154 155// CHECK-LABEL: func.func @matmul_on_use_shard_m_and_duplicted_k156// CHECK-SAME: %[[ARG0:.*]]: tensor<2x16x8xf32>, %[[ARG1:.*]]: tensor<2x8x32xf32>, %[[ARG2:.*]]: tensor<1xf32>157func.func @matmul_on_use_shard_m_and_duplicted_k(%arg0: tensor<2x16x8xf32>, %arg1: tensor<2x8x32xf32>, %arg2: tensor<1xf32>) -> tensor<2x16x32xf32> {158 // CHECK-NEXT: %[[S0:.*]] = shard.sharding @grid_2d split_axes = {{\[\[}}], [1], [0]] : !shard.sharding159 // CHECK-NEXT: %[[V0:.*]] = shard.shard %[[ARG0]] to %[[S0]] annotate_for_users : tensor<2x16x8xf32>160 %s0 = shard.sharding @grid_2d split_axes = [[], [1], [0]] : !shard.sharding161 %0 = shard.shard %arg0 to %s0 annotate_for_users : tensor<2x16x8xf32>162 // CHECK-NEXT: %[[S1:.*]] = shard.sharding @grid_2d split_axes = {{\[\[}}], [0]] : !shard.sharding163 // CHECK-NEXT: %[[V1:.*]] = shard.shard %[[ARG1]] to %[[S1]] annotate_for_users : tensor<2x8x32xf32>164 %s1 = shard.sharding @grid_2d split_axes = [[], [0]] : !shard.sharding165 %1 = shard.shard %arg1 to %s1 annotate_for_users : tensor<2x8x32xf32>166 // CHECK-NEXT: %[[S2:.*]] = shard.sharding @grid_2d split_axes = {{\[\[}}]] : !shard.sharding167 // CHECK-NEXT: %[[ZP:.*]] = shard.shard %[[ARG2]] to %[[S2]] annotate_for_users : tensor<1xf32>168 // CHECK-NEXT: %[[V2:.*]] = tosa.matmul %[[V0]], %[[V1]], %[[ZP]], %[[ZP]]169 %2 = tosa.matmul %0, %1, %arg2, %arg2 : (tensor<2x16x8xf32>, tensor<2x8x32xf32>, tensor<1xf32>, tensor<1xf32>) -> tensor<2x16x32xf32>170 // CHECK-NEXT: %[[S3:.*]] = shard.sharding @grid_2d split_axes = {{\[\[}}], [1]] : !shard.sharding171 // CHECK-NEXT: %[[V3:.*]] = shard.shard %[[V2]] to %[[S3]] : tensor<2x16x32xf32>172 // CHECK-NEXT: return %[[V3]]173 return %2 : tensor<2x16x32xf32>174}175 176// CHECK-LABEL: func.func @resolve_conflicting_annotations177func.func @resolve_conflicting_annotations(178 // CHECK-SAME: %[[IN1:.*]]: tensor<2x3xf32>,179 %arg0: tensor<2x3xf32>,180 // CHECK-SAME: %[[IN2:.*]]: tensor<3x2xf32>,181 %arg1: tensor<3x2xf32>,182 // CHECK-SAME: %[[OUT_DPS:.*]]: tensor<2x2xf32>183 %out_dps: tensor<2x2xf32>184// CHECK-SAME: ) -> tensor<2x2xf32> {185) -> tensor<2x2xf32> {186 // CHECK: %[[SIN1_SHARDED1:.*]] = shard.sharding @grid_2 split_axes = {{\[\[}}0]] : !shard.sharding187 // CHECK-NEXT: %[[IN1_SHARDED1:.*]] = shard.shard %[[IN1]] to %[[SIN1_SHARDED1]] : tensor<2x3xf32>188 // CHECK: %[[SIN2_SHARDED:.*]] = shard.sharding @grid_2 split_axes = {{\[\[}}]] : !shard.sharding189 // CHECK-NEXT: %[[IN1_SHARDED2:.*]] = shard.shard %[[IN1_SHARDED1]] to %[[SIN2_SHARDED]] annotate_for_users : tensor<2x3xf32>190 // CHECK-NEXT: %[[IN2_SHARDED:.*]] = shard.shard %[[IN2]] to %[[SIN2_SHARDED]] annotate_for_users : tensor<3x2xf32>191 // CHECK-NEXT: %[[OUT_DPS_SHARDED:.*]] = shard.shard %[[OUT_DPS]] to %[[SIN2_SHARDED]] annotate_for_users : tensor<2x2xf32>192 %sarg0_sharded = shard.sharding @grid_2 split_axes = [[0]] : !shard.sharding193 %arg0_sharded = shard.shard %arg0 to %sarg0_sharded : tensor<2x3xf32>194 // CHECK: %[[MATMUL:.*]] = linalg.matmul ins(%[[IN1_SHARDED2]], %[[IN2_SHARDED]] : tensor<2x3xf32>, tensor<3x2xf32>)195 // CHECK-SAME: outs(%[[OUT_DPS_SHARDED]] : tensor<2x2xf32>) -> tensor<2x2xf32>196 %res = linalg.matmul ins(%arg0_sharded, %arg1 : tensor<2x3xf32>, tensor<3x2xf32>)197 outs(%out_dps : tensor<2x2xf32>) -> tensor<2x2xf32>198 // CHECK-NEXT: %[[RES:.*]] = shard.shard %[[MATMUL]] to %[[SIN2_SHARDED]] : tensor<2x2xf32>199 %sres_sharded = shard.sharding @grid_2 split_axes = [[]] : !shard.sharding200 %res_sharded = shard.shard %res to %sres_sharded : tensor<2x2xf32>201 // CHECK: return %[[RES]] : tensor<2x2xf32>202 return %res_sharded : tensor<2x2xf32>203}204 205// https://arxiv.org/abs/2211.05102 Figure 2(a)206// The sharding propagation results in unnecessary reshards,207// an optimization pass should be able to remove them.208// CHECK-LABEL: func.func @mlp_1d_weight_stationary209// CHECK-SAME: [[varg0:%.*]]: tensor<2x4x8xf32>, [[varg1:%.*]]: tensor<2x8x32xf32>, [[varg2:%.*]]: tensor<2x32x8xf32>, [[varg3:%.*]]: tensor<1xf32>210func.func @mlp_1d_weight_stationary(%arg0: tensor<2x4x8xf32>, %arg1: tensor<2x8x32xf32>, %arg2: tensor<2x32x8xf32>, %arg3: tensor<1xf32>) -> tensor<2x4x8xf32> {211 %s0 = shard.sharding @grid_1d split_axes = [[], [], [0, 1, 2]] : !shard.sharding212 %sharded0 = shard.shard %arg0 to %s0 : tensor<2x4x8xf32>213 %sharded1 = shard.shard %arg1 to %s0 : tensor<2x8x32xf32>214 // CHECK: [[vsharding:%.*]] = shard.sharding @grid_1d split_axes = {{\[\[}}], [], [0, 1, 2]] : !shard.sharding215 // CHECK: [[vsharded:%.*]] = shard.shard [[varg0]] to [[vsharding]] : tensor<2x4x8xf32>216 // CHECK: [[vsharded_0:%.*]] = shard.shard [[varg1]] to [[vsharding]] : tensor<2x8x32xf32>217 // CHECK: [[vsharded_1:%.*]] = shard.shard [[vsharded]] to [[vsharding]] annotate_for_users : tensor<2x4x8xf32>218 // CHECK: [[vsharding_2:%.*]] = shard.sharding @grid_1d split_axes = {{\[\[}}], [0, 1, 2]] : !shard.sharding219 // CHECK: [[vsharded_3:%.*]] = shard.shard [[vsharded_0]] to [[vsharding_2]] annotate_for_users : tensor<2x8x32xf32>220 // CHECK: [[vsharding_4:%.*]] = shard.sharding @grid_1d split_axes = {{\[\[}}]] : !shard.sharding221 // CHECK: [[vsharded_5:%.*]] = shard.shard [[varg3]] to [[vsharding_4]] annotate_for_users : tensor<1xf32>222 // CHECK: [[v0:%.*]] = tosa.matmul223 %1 = tosa.matmul %sharded0, %sharded1, %arg3, %arg3 : (tensor<2x4x8xf32>, tensor<2x8x32xf32>, tensor<1xf32>, tensor<1xf32>) -> tensor<2x4x32xf32>224 // CHECK: [[vsharded_6:%.*]] = shard.shard [[v0]] to [[vsharding_4]] : tensor<2x4x32xf32>225 // CHECK: [[vsharded_7:%.*]] = shard.shard [[vsharded_6]] to [[vsharding_4]] annotate_for_users : tensor<2x4x32xf32>226 // CHECK: [[v1:%.*]] = tosa.sigmoid [[vsharded_7]] : (tensor<2x4x32xf32>) -> tensor<2x4x32xf32>227 // CHECK: [[vsharded_8:%.*]] = shard.shard [[v1]] to [[vsharding_4]] : tensor<2x4x32xf32>228 %2 = tosa.sigmoid %1 : (tensor<2x4x32xf32>) -> tensor<2x4x32xf32>229 %sharding = shard.sharding @grid_1d split_axes = [[], [0, 1, 2]] : !shard.sharding230 // CHECK: [[vsharded_9:%.*]] = shard.shard [[varg2]] to [[vsharding_2]] : tensor<2x32x8xf32>231 %sharded2 = shard.shard %arg2 to %sharding : tensor<2x32x8xf32>232 // CHECK: [[vsharded_10:%.*]] = shard.shard [[vsharded_8]] to [[vsharding_4]] annotate_for_users : tensor<2x4x32xf32>233 // CHECK: [[vsharded_11:%.*]] = shard.shard [[vsharded_9]] to [[vsharding]] annotate_for_users : tensor<2x32x8xf32>234 // CHECK: [[v2:%.*]] = tosa.matmul235 %3 = tosa.matmul %2, %sharded2, %arg3, %arg3 : (tensor<2x4x32xf32>, tensor<2x32x8xf32>, tensor<1xf32>, tensor<1xf32>) -> tensor<2x4x8xf32>236 // CHECK: [[vsharded_12:%.*]] = shard.shard [[v2]] to [[vsharding]] : tensor<2x4x8xf32>237 %s4 = shard.sharding @grid_1d split_axes = [[], [], [0, 1, 2]] : !shard.sharding238 %4 = shard.shard %3 to %s4 : tensor<2x4x8xf32>239 // CHECK: return [[vsharded_12]]240 return %4 : tensor<2x4x8xf32>241}242 243// https://arxiv.org/abs/2211.05102 Figure 2(b)244// The sharding propagation results in unnecessary reshards,245// an optimization pass should be able to remove them.246// CHECK-LABEL: func.func @mlp_2d_weight_stationary247// CHECK-SAME: [[varg0:%.*]]: tensor<2x4x8xf32>, [[varg1:%.*]]: tensor<2x8x32xf32>, [[varg2:%.*]]: tensor<2x32x8xf32>, [[varg3:%.*]]: tensor<1xf32>248func.func @mlp_2d_weight_stationary(%arg0: tensor<2x4x8xf32>, %arg1: tensor<2x8x32xf32>, %arg2: tensor<2x32x8xf32>, %arg3: tensor<1xf32>) -> tensor<2x4x8xf32> {249 // CHECK: [[vsharding:%.*]] = shard.sharding @grid_3d split_axes = {{\[\[}}], [], [0, 1, 2]] : !shard.sharding250 %s0 = shard.sharding @grid_3d split_axes = [[], [], [0, 1, 2]] : !shard.sharding251 // CHECK: [[vsharded:%.*]] = shard.shard [[varg0]] to [[vsharding]] : tensor<2x4x8xf32>252 %arg0_s = shard.shard %arg0 to %s0 : tensor<2x4x8xf32>253 // CHECK: [[vsharding_0:%.*]] = shard.sharding @grid_3d split_axes = {{\[\[}}], [0], [1, 2]] : !shard.sharding254 %s1 = shard.sharding @grid_3d split_axes = [[], [0], [1, 2]] : !shard.sharding255 // CHECK: [[vsharded_1:%.*]] = shard.shard [[varg1]] to [[vsharding_0]] : tensor<2x8x32xf32>256 %arg1_s = shard.shard %arg1 to %s1 : tensor<2x8x32xf32>257 // CHECK: [[vsharding_2:%.*]] = shard.sharding @grid_3d split_axes = {{\[\[}}]] : !shard.sharding258 // CHECK: [[vsharded_3:%.*]] = shard.shard [[vsharded]] to [[vsharding_2]] annotate_for_users : tensor<2x4x8xf32>259 // CHECK: [[vsharded_4:%.*]] = shard.shard [[vsharded_1]] to [[vsharding]] annotate_for_users : tensor<2x8x32xf32>260 // CHECK: [[vsharded_5:%.*]] = shard.shard [[varg3]] to [[vsharding_2]] annotate_for_users : tensor<1xf32>261 // CHECK: [[v0:%.*]] = tosa.matmul262 %1 = tosa.matmul %arg0_s, %arg1_s, %arg3, %arg3 : (tensor<2x4x8xf32>, tensor<2x8x32xf32>, tensor<1xf32>, tensor<1xf32>) -> tensor<2x4x32xf32>263 // CHECK: [[vsharded_6:%.*]] = shard.shard [[v0]] to [[vsharding]] : tensor<2x4x32xf32>264 %2 = shard.shard %1 to %s0 : tensor<2x4x32xf32>265 // CHECK: [[vsharded_7:%.*]] = shard.shard [[vsharded_6]] to [[vsharding]] annotate_for_users : tensor<2x4x32xf32>266 // CHECK: [[v1:%.*]] = tosa.sigmoid 267 // CHECK: [[vsharded_8:%.*]] = shard.shard [[v1]] to [[vsharding]] : tensor<2x4x32xf32>268 %3 = tosa.sigmoid %2 : (tensor<2x4x32xf32>) -> tensor<2x4x32xf32>269 // CHECK: [[vsharding_9:%.*]] = shard.sharding @grid_3d split_axes = {{\[\[}}], [1, 2], [0]] : !shard.sharding270 %s2 = shard.sharding @grid_3d split_axes = [[], [1, 2], [0]] : !shard.sharding271 // CHECK: [[vsharded_10:%.*]] = shard.shard [[varg2]] to [[vsharding_9]] : tensor<2x32x8xf32>272 %arg2_s = shard.shard %arg2 to %s2 : tensor<2x32x8xf32>273 // CHECK: [[vsharded_11:%.*]] = shard.shard [[vsharded_8]] to [[vsharding_2]] annotate_for_users : tensor<2x4x32xf32>274 // CHECK: [[vsharded_12:%.*]] = shard.shard [[vsharded_10]] to [[vsharding]] annotate_for_users : tensor<2x32x8xf32>275 // CHECK: [[v2:%.*]] = tosa.matmul276 %4 = tosa.matmul %3, %arg2_s, %arg3, %arg3 : (tensor<2x4x32xf32>, tensor<2x32x8xf32>, tensor<1xf32>, tensor<1xf32>) -> tensor<2x4x8xf32>277 // CHECK: [[vsharded_13:%.*]] = shard.shard [[v2]] to [[vsharding]] : tensor<2x4x8xf32>278 %5 = shard.shard %4 to %s0 : tensor<2x4x8xf32>279 // CHECK: [[vsharded_14:%.*]] = shard.shard [[vsharded_13]] to [[vsharding]] annotate_for_users : tensor<2x4x8xf32>280 %6 = shard.shard %5 to %s0 annotate_for_users : tensor<2x4x8xf32>281 // CHECK: return [[vsharded_14]]282 return %6 : tensor<2x4x8xf32>283}284 285// CHECK-LABEL: func.func @elementwise_duplicated_chain286// CHECK-SAME: %[[ARG:.*]]: tensor<8x16xf32>287func.func @elementwise_duplicated_chain(%arg0: tensor<8x16xf32>) -> tensor<8x16xf32> {288 // CHECK-NEXT: %[[S0:.*]] = shard.sharding @grid_2d split_axes = {{\[\[}}]] : !shard.sharding289 // CHECK-NEXT: %[[V0:.*]] = shard.shard %[[ARG]] to %[[S0]] annotate_for_users : tensor<8x16xf32>290 // CHECK-NEXT: %[[V1:.*]] = tosa.sigmoid %[[V0]]291 %0 = tosa.sigmoid %arg0 : (tensor<8x16xf32>) -> tensor<8x16xf32>292 // CHECK-NEXT: %[[V2:.*]] = shard.shard %[[V1]] to %[[S0]] : tensor<8x16xf32>293 // CHECK-NEXT: %[[V3:.*]] = shard.shard %[[V2]] to %[[S0]] annotate_for_users : tensor<8x16xf32>294 // CHECK-NEXT: %[[V4:.*]] = tosa.sigmoid %[[V3]]295 %1 = tosa.sigmoid %0 : (tensor<8x16xf32>) -> tensor<8x16xf32>296 // CHECK-NEXT: %[[V5:.*]] = shard.shard %[[V4]] to %[[S0]] : tensor<8x16xf32>297 %s0 = shard.sharding @grid_2d split_axes = [[]] : !shard.sharding298 %2 = shard.shard %1 to %s0 : tensor<8x16xf32>299 // CHECK-NEXT: return %[[V5]]300 return %2 : tensor<8x16xf32>301}302