Create a handler for a binary op: (a, b) -> node_cls(a, b, out). When lift_b is True, scalar b values are lifted to 0-D constant tensors via emit_lifted_constant, using a's dtype.
(node_cls: Any, op_name: str, lift_b: bool)
| 530 | |
| 531 | |
| 532 | def _make_binary_handler(node_cls: Any, op_name: str, lift_b: bool): |
| 533 | """Create a handler for a binary op: (a, b) -> node_cls(a, b, out). |
| 534 | |
| 535 | When lift_b is True, scalar b values are lifted to 0-D constant tensors |
| 536 | via emit_lifted_constant, using a's dtype. |
| 537 | """ |
| 538 | |
| 539 | def handler(P: MLXProgramBuilder, n: Node) -> Slot: |
| 540 | args = P.args(n) |
| 541 | require_args(args, 2, 2, op_name) |
| 542 | require_kwargs(P.kwargs(n), set(), op_name) |
| 543 | a, b = args[0], args[1] |
| 544 | if lift_b and (not isinstance(b, Slot) or b.id_type != IdType.Tensor): |
| 545 | input_meta = n.args[0].meta.get("val") |
| 546 | dtype = input_meta.dtype if input_meta is not None else torch.float32 |
| 547 | b = emit_lifted_constant(P, b, dtype) |
| 548 | out = P.make_or_get_slot(n) |
| 549 | P.emit(node_cls(a=P.slot_to_tid(a), b=P.slot_to_tid(b), out=P.slot_to_tid(out))) |
| 550 | return out |
| 551 | |
| 552 | handler.__name__ = f"_{op_name.replace('.', '_')}_handler" |
| 553 | handler.__doc__ = f"Handle {op_name} (table-driven binary op)." |
| 554 | return handler |
| 555 | |
| 556 | |
| 557 | for _targets, _node_cls, _op_name, _lift_b in _BINARY_OPS: |