Skip to content

Commit 92272fc

Browse files
committed
Remove DimensionBounds and EncodeBounds from Shape
These fields are only needed for DynamicReshape and DynamicBroadcastInDim operations, which are being added in a separate follow-up PR.
1 parent 2af062d commit 92272fc

3 files changed

Lines changed: 5 additions & 86 deletions

File tree

internal/shapeinference/shapeinference.go

Lines changed: 1 addition & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -210,32 +210,15 @@ func binaryOpImpl(opType optypes.OpType, lhsShape, rhsShape shapes.Shape) (outpu
210210
}
211211
output = lhsShape.Clone()
212212

213-
// Determine if we need to track bounds
214-
needsBounds := len(lhsShape.DimensionBounds) > 0 || len(rhsShape.DimensionBounds) > 0
215-
216213
for axis := range output.Rank() {
217214
lhsDim := lhsShape.Dimensions[axis]
218215
rhsDim := rhsShape.Dimensions[axis]
219216

220-
// Get bounds for each side
221-
lhsBound := 0
222-
if axis < len(lhsShape.DimensionBounds) {
223-
lhsBound = lhsShape.DimensionBounds[axis]
224-
}
225-
rhsBound := 0
226-
if axis < len(rhsShape.DimensionBounds) {
227-
rhsBound = rhsShape.DimensionBounds[axis]
228-
}
229-
230217
// Handle dynamic dimensions (DimUnknown)
231218
switch {
232219
case lhsDim == shapes.DimUnknown && rhsDim == shapes.DimUnknown:
233-
// Both dynamic - result is unknown, merge bounds
220+
// Both dynamic - result is unknown
234221
output.Dimensions[axis] = shapes.DimUnknown
235-
if needsBounds && axis < len(output.DimensionBounds) {
236-
// Use maximum of both bounds
237-
output.DimensionBounds[axis] = max(lhsBound, rhsBound)
238-
}
239222
case lhsDim >= 0 && rhsDim >= 0:
240223
// Both static (including zero-dimension tensors) - use existing max logic for broadcasting
241224
if lhsDim != 1 && rhsDim != 1 && lhsDim != rhsDim {
@@ -244,43 +227,19 @@ func binaryOpImpl(opType optypes.OpType, lhsShape, rhsShape shapes.Shape) (outpu
244227
return
245228
}
246229
output.Dimensions[axis] = max(lhsDim, rhsDim)
247-
// Clear bounds for static dimensions
248-
if len(output.DimensionBounds) > axis {
249-
output.DimensionBounds[axis] = 0
250-
}
251230
case lhsDim < 0 && rhsDim > 1:
252231
// Dynamic vs concrete > 1: use concrete (dynamic must be compatible)
253232
output.Dimensions[axis] = rhsDim
254-
// Clear bounds since we're now static
255-
if len(output.DimensionBounds) > axis {
256-
output.DimensionBounds[axis] = 0
257-
}
258233
case rhsDim < 0 && lhsDim > 1:
259234
// Concrete > 1 vs dynamic: use concrete
260235
output.Dimensions[axis] = lhsDim
261-
// Clear bounds since we're now static
262-
if len(output.DimensionBounds) > axis {
263-
output.DimensionBounds[axis] = 0
264-
}
265236
default:
266237
err = errors.Errorf("incompatible dynamic dimensions at axis %d: %d vs %d for BinaryOp (%s)",
267238
axis, lhsDim, rhsDim, opType)
268239
return
269240
}
270241
}
271242

272-
// Clean up DimensionBounds if all are zero
273-
allZero := true
274-
for _, b := range output.DimensionBounds {
275-
if b != 0 {
276-
allZero = false
277-
break
278-
}
279-
}
280-
if allZero {
281-
output.DimensionBounds = nil
282-
}
283-
284243
return
285244
}
286245

pkg/types/shapes/shape.go

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -87,12 +87,10 @@ const DimUnknown = -1
8787
//
8888
// Use Make to create a new shape. See example in package shapes documentation.
8989
type Shape struct {
90-
DType dtypes.DType
91-
Dimensions []int
92-
DimensionBounds []int // Upper bounds for dynamic dimensions (when Dimensions[i] == DimUnknown). nil or len(DimensionBounds)==len(Dimensions). 0 means no bound.
93-
TupleShapes []Shape // Shapes of the tuple, if this is a tuple.
94-
Quantization *Quantization // Quantization metadata for quantized types.
95-
EncodeBounds bool // Whether to encode DimensionBounds in StableHLO output. Set to true for ops that require bounded dynamism (e.g., dynamic_reshape).
90+
DType dtypes.DType
91+
Dimensions []int
92+
TupleShapes []Shape // Shapes of the tuple, if this is a tuple.
93+
Quantization *Quantization // Quantization metadata for quantized types.
9694
}
9795

9896
// Make returns a Shape structure filled with the values given.
@@ -136,20 +134,6 @@ func (s Shape) IsDynamic() bool {
136134
return slices.Contains(s.Dimensions, DimUnknown)
137135
}
138136

139-
// hasBoundedDynamism returns true if the shape has at least one dynamic dimension
140-
// with a known upper bound. This is used for StableHLO bounds encoding.
141-
func (s Shape) hasBoundedDynamism() bool {
142-
if len(s.DimensionBounds) == 0 {
143-
return false
144-
}
145-
for i, dim := range s.Dimensions {
146-
if dim == DimUnknown && i < len(s.DimensionBounds) && s.DimensionBounds[i] > 0 {
147-
return true
148-
}
149-
}
150-
return false
151-
}
152-
153137
// Dim returns the dimension of the given axis. axis can take negative numbers, in which
154138
// case it counts as starting from the end -- so axis=-1 refers to the last axis.
155139
// Like with a slice indexing, it panics for an out-of-bound axis.
@@ -284,15 +268,13 @@ func (s Shape) EqualDimensions(s2 Shape) bool {
284268
func (s Shape) Clone() (s2 Shape) {
285269
s2.DType = s.DType
286270
s2.Dimensions = slices.Clone(s.Dimensions)
287-
s2.DimensionBounds = slices.Clone(s.DimensionBounds)
288271
if s.TupleSize() > 0 {
289272
s2.TupleShapes = make([]Shape, 0, len(s.TupleShapes))
290273
for _, subShape := range s.TupleShapes {
291274
s2.TupleShapes = append(s2.TupleShapes, subShape.Clone())
292275
}
293276
}
294277
s2.Quantization = s.Quantization.Clone()
295-
s2.EncodeBounds = s.EncodeBounds
296278
return
297279
}
298280

pkg/types/shapes/stablehlo.go

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -62,28 +62,6 @@ func (s Shape) WriteStableHLO(writer io.Writer) error {
6262
w("%s", s.DType.ToStableHLO())
6363
}
6464

65-
// Encode bounds only if explicitly requested via EncodeBounds flag.
66-
// XLA requires bounded dynamic dimensions for compilation.
67-
if s.EncodeBounds && s.hasBoundedDynamism() {
68-
w(", #stablehlo.bounds<")
69-
for i, dim := range s.Dimensions {
70-
if i > 0 {
71-
w(", ")
72-
}
73-
if dim == DimUnknown {
74-
// Dynamic dimension - check if we have a bound
75-
if i < len(s.DimensionBounds) && s.DimensionBounds[i] > 0 {
76-
w("%d", s.DimensionBounds[i])
77-
} else {
78-
w("?")
79-
}
80-
} else {
81-
// Static dimension - use ? (no bound needed, the dimension is already known)
82-
w("?")
83-
}
84-
}
85-
w(">")
86-
}
8765
w(">")
8866
return err
8967
}

0 commit comments

Comments
 (0)