Skip to content

Commit

Permalink
Plumbing for dynamic shapes for custom calls.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 521439418
  • Loading branch information
gnecula authored and jax authors committed Apr 3, 2023
1 parent 0d32724 commit 607c7c1
Showing 1 changed file with 21 additions and 6 deletions.
27 changes: 21 additions & 6 deletions jaxlib/hlo_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
# limitations under the License.

# Helpers for building MLIR operators

from typing import Dict, Optional, Sequence, Union
import jaxlib.mlir.ir as ir
import jaxlib.mlir.dialects.stablehlo as hlo
Expand All @@ -30,6 +29,7 @@ def custom_call(
has_side_effect: bool = False,
api_version: int = 2,
operand_output_aliases: Dict[int, int] = {},
indices_of_shape_operands: Sequence[int] = (),
) -> Union[ir.Value, Sequence[ir.Value]]:
"""Less-verbose helper for building a CustomCallOp.
Expand All @@ -40,12 +40,14 @@ def custom_call(
...
operand_output_alias: a dictionary mapping input numbers -> output numbers
that must alias.
indices_of_shape_operands: in presence of dynamic shapes, must pass in the
output shapes as some of the operands. These are the indices of those
operands.
"""
i32_type = ir.IntegerType.get_signless(32)
out = hlo.CustomCallOp(
(out_types
if len(out_types) == 1 else [ir.TupleType.get_tuple(out_types)]),
operands,
results = (out_types
if len(out_types) == 1 else [ir.TupleType.get_tuple(out_types)])
attributes = dict(
call_target_name=ir.StringAttr.get(call_target_name),
has_side_effect=ir.BoolAttr.get(has_side_effect),
backend_config=ir.StringAttr.get(
Expand All @@ -68,7 +70,20 @@ def custom_call(
operand_index=input,
operand_tuple_indices=[])
for input, output in operand_output_aliases.items()
]))
])
)
if indices_of_shape_operands:
attributes["indices_of_shape_operands"] = ir.DenseIntElementsAttr.get(
np.asarray(indices_of_shape_operands, dtype=np.int64))

# TODO(necula): CustomCall constructor does not yet support
# indices_of_shape_operands, so we use the generic builder

# The generic builder is pickier about the type of the operands, and some
# of the callers did not call .result
operands = [opnd if isinstance(opnd, ir.Value) else opnd.result
for opnd in operands]
out = hlo.CustomCallOp.build_generic(results=results, operands=operands, attributes=attributes)
if len(out_types) == 1:
return out.result
else:
Expand Down

0 comments on commit 607c7c1

Please sign in to comment.