Commit 0b5b4a2
authored
Add StableHLO shape assertion check pass and related tests (#2869)
This PR adds the CheckShapeAssertion from XLA into StableHLO:
https://github.com/openxla/xla/blob/949bb09eb7d0bb563f5b564fd7d0782e0687796e/xla/python/refine_polymorphic_shapes.cc#L72-L98
This idea was discussed in the discord channel:
https://discord.com/channels/999073994483433573/999074539138990131/1409946189721374803
This allows taking programs exported from JAX and using just the
satblehlo binary to produce a program that can be compile and executed
with PJRT plugins.
----
A usage example is for instance, export a program such as:
```
import jax
from jax import export
import jax.numpy as jnp
import numpy as np
def f1(x, y): # x: f32[a, 1], y : f32[a, 4]
return x + y
# Assuming you have some actual args with concrete shapes
x = np.ones((3, 1), dtype=np.int32)
y = np.ones((3, 4), dtype=np.int32)
args_specs = export.symbolic_args_specs((x, y), 'a, ...')
exp = export.export(jax.jit(f1))(* args_specs)
code = exp.mlir_module()
print(code)
```
This gives us stableHLO like:
```
#loc = loc(unknown)
#loc2 = loc("x")
#loc3 = loc("y")
module @jit_f1 attributes {jax.uses_shape_polymorphism = true, mhlo.num_partitions = 1 : i32, mhlo.num_replicas = 1 : i32} {
func.func public @main(%arg0: tensor<?x1xi32> loc(unknown), %arg1: tensor<?x4xi32> loc(unknown)) -> (tensor<?x4xi32> {jax.result_info = "result"}) {
%c = stablehlo.constant dense<1> : tensor<i32> loc(#loc)
%0 = stablehlo.get_dimension_size %arg0, dim = 0 : (tensor<?x1xi32>) -> tensor<i32> loc(#loc7)
%1 = stablehlo.get_dimension_size %arg1, dim = 0 : (tensor<?x4xi32>) -> tensor<i32> loc(#loc7)
%2 = stablehlo.compare GE, %0, %c, SIGNED : (tensor<i32>, tensor<i32>) -> tensor<i1> loc(#loc8)
stablehlo.custom_call @shape_assertion(%2, %0) {api_version = 2 : i32, error_message = "Input shapes do not match the polymorphic shapes specification. Expected value >= 1 for dimension variable 'a'. Using the following polymorphic shapes specifications: args[0].shape = (a, 1),args[1].shape = (a, 4). Obtained dimension variables: 'a' = {0} from specification 'a' for dimension args[0].shape[0] (= {0}), . Please see https://docs.jax.dev/en/latest/export/shape_poly.html#shape-assertion-errors for more details.", has_side_effect = true} : (tensor<i1>, tensor<i32>) -> () loc(#loc9)
%3 = stablehlo.compare EQ, %1, %0, SIGNED : (tensor<i32>, tensor<i32>) -> tensor<i1> loc(#loc10)
stablehlo.custom_call @shape_assertion(%3, %1, %0) {api_version = 2 : i32, error_message = "Input shapes do not match the polymorphic shapes specification. Found inconsistency between dimension size args[1].shape[0] (= {0}) and the specification 'a' (= {1}). Using the following polymorphic shapes specifications: args[0].shape = (a, 1),args[1].shape = (a, 4). Obtained dimension variables: 'a' = {1} from specification 'a' for dimension args[0].shape[0] (= {1}), . Please see https://docs.jax.dev/en/latest/export/shape_poly.html#shape-assertion-errors for more details.", has_side_effect = true} : (tensor<i1>, tensor<i32>, tensor<i32>) -> () loc(#loc9)
%4 = call @_wrapped_jax_export_main(%0, %arg0, %arg1) : (tensor<i32>, tensor<?x1xi32>, tensor<?x4xi32>) -> tensor<?x4xi32> loc(#loc)
return %4 : tensor<?x4xi32> loc(#loc)
} loc(#loc)
func.func private @_wrapped_jax_export_main(%arg0: tensor<i32> {jax.global_constant = "a"} loc(unknown), %arg1: tensor<?x1xi32> loc("x"), %arg2: tensor<?x4xi32> loc("y")) -> (tensor<?x4xi32> {jax.result_info = "result"}) {
%c = stablehlo.constant dense<4> : tensor<1xi32> loc(#loc12)
%0 = stablehlo.reshape %arg0 : (tensor<i32>) -> tensor<1xi32> loc(#loc12)
%1 = stablehlo.concatenate %0, %c, dim = 0 : (tensor<1xi32>, tensor<1xi32>) -> tensor<2xi32> loc(#loc12)
%2 = stablehlo.dynamic_broadcast_in_dim %arg1, %1, dims = [0, 1] : (tensor<?x1xi32>, tensor<2xi32>) -> tensor<?x4xi32> loc(#loc12)
%3 = stablehlo.add %2, %arg2 : tensor<?x4xi32> loc(#loc12)
return %3 : tensor<?x4xi32> loc(#loc)
} loc(#loc)
} loc(#loc)
#loc1 = loc("<string>":13:6 to :46)
#loc4 = loc("<string>":7:8 to :13)
#loc5 = loc("<module>"(#loc1))
#loc6 = loc("f1"(#loc4))
#loc7 = loc("dimension_size"(#loc5))
#loc8 = loc("ge"(#loc5))
#loc9 = loc("shape_assertion"(#loc5))
#loc10 = loc("eq"(#loc5))
#loc11 = loc(callsite(#loc6 at #loc5))
#loc12 = loc("jit(f1)/add"(#loc11))
```
Before executing with PJRT one needs to refine shapes and remove the
shape assertions, with eg:
```
stablehlo-opt hello.mlir -stablehlo-refine-arguments="types='tensor<10x1xi32>, tensor<10x4xi32>'" -stablehlo-refine-shapes --stablehlo-check-shape-assertions
```
Which gives us:
```
module @jit_f1 attributes {jax.uses_shape_polymorphism = true, mhlo.num_partitions = 1 : i32, mhlo.num_replicas = 1 : i32} {
func.func public @main(%arg0: tensor<10x1xi32>, %arg1: tensor<10x4xi32>) -> (tensor<10x4xi32> {jax.result_info = "result"}) {
%c = stablehlo.constant dense<10> : tensor<i32>
%c_0 = stablehlo.constant dense<true> : tensor<i1>
%0 = call @_wrapped_jax_export_main(%arg0, %arg1) : (tensor<10x1xi32>, tensor<10x4xi32>) -> tensor<10x4xi32>
return %0 : tensor<10x4xi32>
}
func.func private @_wrapped_jax_export_main(%arg0: tensor<10x1xi32>, %arg1: tensor<10x4xi32>) -> (tensor<10x4xi32> {jax.result_info = "result"}) {
%c = stablehlo.constant dense<[10, 4]> : tensor<2xi32>
%0 = stablehlo.dynamic_broadcast_in_dim %arg0, %c, dims = [0, 1] : (tensor<10x1xi32>, tensor<2xi32>) -> tensor<10x4xi32>
%1 = stablehlo.add %0, %arg1 : tensor<10x4xi32>
return %1 : tensor<10x4xi32>
}
}
```1 parent 4c0d484 commit 0b5b4a2
File tree
7 files changed
+308
-0
lines changed- docs
- generated
- stablehlo
- tests/transforms
- transforms
7 files changed
+308
-0
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1135 | 1135 | | |
1136 | 1136 | | |
1137 | 1137 | | |
| 1138 | + | |
1138 | 1139 | | |
1139 | 1140 | | |
1140 | 1141 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
105 | 105 | | |
106 | 106 | | |
107 | 107 | | |
| 108 | + | |
| 109 | + | |
108 | 110 | | |
109 | 111 | | |
110 | 112 | | |
111 | 113 | | |
112 | 114 | | |
113 | 115 | | |
114 | 116 | | |
| 117 | + | |
115 | 118 | | |
116 | 119 | | |
117 | 120 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
32 | 32 | | |
33 | 33 | | |
34 | 34 | | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
35 | 61 | | |
36 | 62 | | |
37 | 63 | | |
| |||
Lines changed: 44 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
61 | 61 | | |
62 | 62 | | |
63 | 63 | | |
| 64 | + | |
64 | 65 | | |
65 | 66 | | |
66 | 67 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
55 | 55 | | |
56 | 56 | | |
57 | 57 | | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
58 | 87 | | |
59 | 88 | | |
60 | 89 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
0 commit comments