package neural_nets_lib
A from-scratch Deep Learning framework with an optimizing compiler, shape inference, concise syntax
Install
Dune Dependency
Authors
Maintainers
Sources
0.3.3.3.tar.gz
md5=9170d4d98422350c9a73a95adfb795dc
sha512=c1b024a69b1d0338af6e34508dbf6dccf3c2b6cc156e7628c3d7853c7040e225bdfc0a8731bb4db5a97edba90e26439987bfa505154d23af46f119c07ad809ed
doc/src/ppx_ocannl/ppx_cd.ml.html
Source file ppx_cd.ml
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 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868
open Base open Ppxlib open Ppx_arrayjit.Ppx_helper open Ppx_shared let ndarray_op ~ident_label ?axis_labels ?label expr = let loc = expr.pexp_loc in let values, batch_dims, output_dims, input_dims = ndarray_constant expr in let edims dims = Ast_builder.Default.elist ~loc dims in let op = match (axis_labels, label) with | None, None -> [%expr NTDSL.ndarray] | Some axis_labels, None -> [%expr NTDSL.ndarray ~axis_labels:[%e axis_labels]] | None, Some label -> [%expr NTDSL.ndarray ~label:[%e label]] | Some axis_labels, Some label -> [%expr NTDSL.ndarray ~label:[%e opt_pat2string_list ~loc ident_label] ~axis_labels:[%e axis_labels] ~label:[%e label]] in [%expr [%e op] ~label:[%e opt_pat2string_list ~loc ident_label] ~batch_dims:[%e edims batch_dims] ~input_dims:[%e edims input_dims] ~output_dims:[%e edims output_dims] [%e values]] type expr_type = Code | Array | Grad_of_tensor of expression | Tensor | Unknown let is_unknown = function Unknown -> true | _ -> false type projections_slot = LHS | RHS1 | RHS2 | Nonslot | Undet [@@deriving equal, sexp] let assignment_op expr = (* This should stay in sync with Arrayjit.Ops.assign_op_cd_syntax. *) let loc = expr.pexp_loc in match expr with | [%expr ( =: )] -> (false, [%expr Arrayjit.Ops.Arg2]) | [%expr ( =+ )] -> (false, [%expr Arrayjit.Ops.Add]) | [%expr ( =- )] -> (false, [%expr Arrayjit.Ops.Sub]) | [%expr ( =* )] -> (false, [%expr Arrayjit.Ops.Mul]) | [%expr ( =/ )] -> (false, [%expr Arrayjit.Ops.Div]) | [%expr ( =** )] -> (false, [%expr Arrayjit.Ops.ToPowOf]) | [%expr ( =?/ )] -> (false, [%expr Arrayjit.Ops.Relu_gate]) | [%expr ( =:+ )] -> (true, [%expr Arrayjit.Ops.Add]) | [%expr ( =:- )] -> (true, [%expr Arrayjit.Ops.Sub]) | [%expr ( =:* )] -> (true, [%expr Arrayjit.Ops.Mul]) | [%expr ( =:/ )] -> (true, [%expr Arrayjit.Ops.Div]) | [%expr ( =:** )] -> (true, [%expr Arrayjit.Ops.ToPowOf]) | [%expr ( =:?/ )] -> (true, [%expr Arrayjit.Ops.Relu_gate]) | _ -> ( false, Ast_builder.Default.pexp_extension ~loc @@ Location.error_extensionf ~loc "ppx_ocannl %%cd: expected an assignment operator, one of: %s %s" "=+ (Add), =- (Sub), =* (Mul), =/ (Div), =** (ToPowOf), =?/ (Relu_gate), =: (Arg2), =:+, =:-," " =:*, =:/, =:**, =:?/ (same with initializing the tensor to the neutral value before the start \ of the calculation)" ) let binary_op expr = (* This and is_binary_op should stay in sync with Arrayjit.Ops.binop_cd_syntax. *) let loc = expr.pexp_loc in match expr with | [%expr ( + )] -> ([%expr Shape.Pointwise_bin], [%expr Arrayjit.Ops.Add]) | [%expr ( - )] -> ([%expr Shape.Pointwise_bin], [%expr Arrayjit.Ops.Sub]) | [%expr ( * )] -> ( Ast_builder.Default.pexp_extension ~loc @@ Location.error_extensionf ~loc "No default compose type for binary `*`, try e.g. ~logic:\".\" for pointwise, %s" "~logic:\"@\" for matrix multiplication", [%expr Arrayjit.Ops.Mul] ) | [%expr ( / )] -> ( Ast_builder.Default.pexp_extension ~loc @@ Location.error_extensionf ~loc "For clarity, no default compose type for binary `/`, use ~logic:\".\" for pointwise division", [%expr Arrayjit.Ops.Div] ) | [%expr ( ** )] -> ([%expr Shape.Pointwise_bin], [%expr Arrayjit.Ops.ToPowOf]) | [%expr ( -?/ )] -> ([%expr Shape.Pointwise_bin], [%expr Arrayjit.Ops.Relu_gate]) | [%expr ( -/> )] -> ([%expr Shape.Pointwise_bin], [%expr Arrayjit.Ops.Arg2]) | [%expr ( -@> )] -> ([%expr Shape.Pointwise_bin], [%expr Arrayjit.Ops.Arg1]) | _ -> ( [%expr Shape.Pointwise_bin], Ast_builder.Default.pexp_extension ~loc @@ Location.error_extensionf ~loc "ppx_ocannl %%cd: expected a binary operator, one of: %s" "+ (Add), - (Sub), * (Mul), / (Div), ** (ToPowOf), -?/ (Relu_gate), -/> (Arg2)" ) let is_binary_op ident = List.mem [ "+"; "-"; "*"; "/"; "**"; "-?/"; "-/>"; "-@>" ] ident ~equal:String.equal let unary_op expr = (* This and is_unary_op should stay in sync with Arrayjit.Ops.unop_cd_syntax. *) let loc = expr.pexp_loc in match expr with | [%expr ( ~= )] -> ([%expr Shape.Pointwise_un], [%expr Arrayjit.Ops.Identity]) | [%expr ( ?/ )] -> ([%expr Shape.Pointwise_un], [%expr Arrayjit.Ops.Relu]) | _ -> ( [%expr Shape.Pointwise_un], Ast_builder.Default.pexp_extension ~loc @@ Location.error_extensionf ~loc "ppx_ocannl %%cd: expected a unary operator, one of: = (Identity), ?/ (Relu)" ) let is_unary_op ident = List.mem [ "~="; "?/" ] ident ~equal:String.equal let rec array_of_code c = let loc = c.pexp_loc in [%expr match [%e c] with | Arrayjit.Assignments.Accum_binop { lhs; _ } | Accum_unop { lhs; _ } -> lhs | Fetch { array; _ } -> array | Seq (_, subexpr) | Block_comment (_, subexpr) -> [%e array_of_code [%expr subexpr]] | Noop -> Location.error_extensionf ~loc "ppx_ocannl %%cd: Noop code does not refer to any data"] type binding_setup = { var : pattern; lazy_bind_to : expression; fwd_code_or_noop : expression } let with_forward_args setups body = let loc = body.pexp_loc in let bindings = List.map setups ~f:(fun { var; lazy_bind_to; _ } -> Ast_helper.Vb.mk ~loc var [%expr Lazy.force [%e lazy_bind_to]]) in let forward_args = List.map setups ~f:(fun { fwd_code_or_noop; _ } -> fwd_code_or_noop) |> List.reduce ~f:(fun code fwd -> [%expr Arrayjit.Assignments.Seq ([%e code], [%e fwd])]) in ( Code, Nonslot, match forward_args with | None -> body | Some fwd -> [%expr (* FIXME: we do not want to force the computation unnecessarily, but we want the bindings? *) (*if Arrayjit.Assignments.is_noop [%e body] then Arrayjit.Assignments.Noop else*) [%e Ast_helper.Exp.let_ ~loc Nonrecursive bindings [%expr Arrayjit.Assignments.Seq ([%e fwd], [%e body])]]] ) let project_p_slot debug loc slot = match slot with | LHS -> [%expr p.project_lhs] | RHS1 -> [%expr p.project_rhs.(0)] | RHS2 -> [%expr p.project_rhs.(1)] | Nonslot -> Ast_builder.Default.pexp_extension ~loc @@ Location.error_extensionf ~loc "ppx_ocannl %%cd: not a valid accumulation/assignment slot filler at %s" debug | Undet -> Ast_builder.Default.pexp_extension ~loc @@ Location.error_extensionf ~loc "ppx_ocannl %%cd: insufficient slot filler information at %s %s" debug "(incorporate one of: v, v1, v2, g, g1, g2, lhs, rhs, rhs1, rhs2)" let project_p_dims debug loc slot = match slot with | LHS -> [%expr p.lhs_dims] | RHS1 -> [%expr p.rhs_dims.(0)] | RHS2 -> [%expr p.rhs_dims.(1)] | Nonslot -> Ast_builder.Default.pexp_extension ~loc @@ Location.error_extensionf ~loc "ppx_ocannl %%cd: not a valid accumulation/assignment slot filler at %s" debug | Undet -> Ast_builder.Default.pexp_extension ~loc @@ Location.error_extensionf ~loc "ppx_ocannl %%cd: insufficient slot filler information at %s %s" debug "(incorporate one of: v, v1, v2, g, g1, g2, lhs, rhs, rhs1, rhs2)" type array_setup = { slot : projections_slot; filler_typ : expr_type; binding : binding_setup option; array_opt : expression; tensor : expression option; } let setup_array filler_pat (filler_typ, slot, filler) = let loc = filler.pexp_loc in match filler_typ with | Tensor | Unknown -> let t = pat2expr filler_pat in let fwd_code_or_noop = [%expr if Tensor.is_fwd_root [%e t] then ( Tensor.remove_fwd_root [%e t]; [%e t].Tensor.forward) else Arrayjit.Assignments.Noop] in { binding = Some { var = filler_pat; lazy_bind_to = [%expr lazy [%e filler]]; fwd_code_or_noop }; filler_typ; slot; array_opt = [%expr Some [%e t].value]; tensor = Some t; } | Code -> { binding = Some { var = filler_pat; lazy_bind_to = [%expr lazy [%e filler]]; fwd_code_or_noop = pat2expr filler_pat; }; filler_typ; slot; array_opt = [%expr Some [%e array_of_code filler]]; tensor = None; } | Array -> { binding = None; filler_typ; slot; array_opt = [%expr Some [%e filler]]; tensor = None } | Grad_of_tensor t -> { binding = None; filler_typ; slot; array_opt = filler; tensor = Some t } let rec translate ?ident_label ~proj_in_scope (expr : expression) : expr_type * projections_slot * expression = let loc = expr.pexp_loc in match expr with | { pexp_desc = Pexp_constant (Pconst_float _); _ } -> (Tensor, Undet, [%expr NTDSL.number ~label:[%e opt_pat2string_list ~loc ident_label] [%e expr]]) | { pexp_desc = Pexp_constant (Pconst_integer _); _ } -> ( Tensor, Undet, [%expr NTDSL.number ~label:[%e opt_pat2string_list ~loc ident_label] (Float.of_int [%e expr])] ) | [%expr [%e? { pexp_desc = Pexp_constant (Pconst_char ch); pexp_loc; _ }] [%e? { pexp_desc = Pexp_constant (Pconst_float _); _ } as f]] -> let axis = Ast_helper.Exp.constant ~loc:pexp_loc (Pconst_string (String.of_char ch, pexp_loc, None)) in ( Tensor, Undet, [%expr NTDSL.number ~label:[%e opt_pat2string_list ~loc ident_label] ~axis_label:[%e axis] [%e f]] ) | [%expr [%e? { pexp_desc = Pexp_constant (Pconst_char ch); pexp_loc; _ }] [%e? { pexp_desc = Pexp_constant (Pconst_integer _); _ } as i]] -> let axis = Ast_helper.Exp.constant ~loc:pexp_loc (Pconst_string (String.of_char ch, pexp_loc, None)) in ( Tensor, Undet, [%expr NTDSL.number ~label:[%e opt_pat2string_list ~loc ident_label] ~axis_label:[%e axis] (Float.of_int [%e i])] ) | { pexp_desc = Pexp_array _; _ } | { pexp_desc = Pexp_construct ({ txt = Lident "::"; _ }, _); _ } -> (Tensor, Undet, ndarray_op ~ident_label expr) | { pexp_desc = Pexp_ident { txt = Lident ("v" | "lhs"); _ }; _ } -> (Array, LHS, expr) | { pexp_desc = Pexp_ident { txt = Lident "g"; _ }; _ } -> (Array, LHS, expr) | { pexp_desc = Pexp_ident { txt = Lident "rhs1"; _ }; _ } -> (Array, RHS1, expr) | { pexp_desc = Pexp_ident { txt = Lident "t1"; _ }; _ } -> (Tensor, RHS1, expr) | { pexp_desc = Pexp_ident { txt = Lident "v1"; _ }; _ } -> (Array, RHS1, [%expr t1.Tensor.value]) | { pexp_desc = Pexp_ident { txt = Lident "g1"; _ }; _ } -> (Grad_of_tensor [%expr t1], RHS1, [%expr Option.map t1.Tensor.diff ~f:(fun d -> d.Tensor.grad)]) | { pexp_desc = Pexp_ident { txt = Lident "rhs2"; _ }; _ } -> (Array, RHS2, expr) | { pexp_desc = Pexp_ident { txt = Lident "t2"; _ }; _ } -> (Tensor, RHS2, expr) | { pexp_desc = Pexp_ident { txt = Lident "v2"; _ }; _ } -> (Array, RHS2, [%expr t2.Tensor.value]) | { pexp_desc = Pexp_ident { txt = Lident "g2"; _ }; _ } -> (Grad_of_tensor [%expr t2], RHS2, [%expr Option.map t2.Tensor.diff ~f:(fun d -> d.Tensor.grad)]) | { pexp_desc = Pexp_ident { txt = Lident op_ident; _ }; _ } when is_operator op_ident -> (Tensor, Undet, expr) | [%expr [%e? expr1] **. [%e? { pexp_desc = Pexp_constant (Pconst_integer _); _ } as i]] -> (* FIXME: `**.` should take a tensor and require that it's a literal. *) (* We need to hardcode these two patterns to prevent the numbers from being converted to tensors. *) let _typ1, slot1, e1 = translate ~proj_in_scope expr1 in ( Tensor, slot1, [%expr NTDSL.O.( **. ) ~label:[%e opt_pat2string_list ~loc ident_label] [%e e1] (Float.of_int [%e i])] ) | [%expr [%e? expr1] **. [%e? expr2]] -> let _typ1, slot1, e1 = translate ~proj_in_scope expr1 in ( Tensor, slot1, [%expr NTDSL.O.( **. ) ~label:[%e opt_pat2string_list ~loc ident_label] [%e e1] [%e expr2]] ) | [%expr [%e? expr1] *+ [%e? { pexp_desc = Pexp_constant (Pconst_string (spec_str, _, _)); _ } as spec] [%e? expr2]] when String.contains spec_str '>' -> let _typ1, slot1, expr1 = translate ~proj_in_scope expr1 in let _typ2, slot2, expr2 = translate ~proj_in_scope expr2 in let slot = Option.value ~default:Undet @@ List.find ~f:(function Undet -> false | _ -> true) [ slot1; slot2 ] in ( Tensor, slot, [%expr NTDSL.einsum ~label:[%e opt_pat2string_list ~loc ident_label] [%e spec] [%e expr1] [%e expr2]] ) | [%expr [%e? expr1] ++ [%e? { pexp_desc = Pexp_constant (Pconst_string (spec_str, _, _)); _ } as spec]] when String.contains spec_str '>' -> let _typ1, slot1, expr1 = translate ~proj_in_scope expr1 in ( Tensor, slot1, [%expr NTDSL.einsum1 ~label:[%e opt_pat2string_list ~loc ident_label] [%e spec] [%e expr1]] ) | [%expr [%e? expr1].grad] -> ( let typ1, slot1, expr1 = translate ?ident_label ~proj_in_scope expr1 in match typ1 with | Unknown | Tensor -> (Grad_of_tensor expr1, slot1, [%expr Option.map [%e expr1].Tensor.diff ~f:(fun d -> d.Tensor.grad)]) | Code | Array | Grad_of_tensor _ -> ( Array, slot1, Ast_builder.Default.pexp_extension ~loc @@ Location.error_extensionf ~loc "ppx_ocannl %%cd: only tensors have a gradient" )) | [%expr [%e? expr1].value] -> ( let ((typ1, slot1, expr1) as result) = translate ?ident_label ~proj_in_scope expr1 in (* TODO: maybe this is too permissive? E.g. [t1.grad.value] is accepted. *) match typ1 with | Unknown | Tensor -> (Array, slot1, [%expr [%e expr1].Tensor.value]) | Code -> (Array, slot1, array_of_code expr1) | Array | Grad_of_tensor _ -> result) | [%expr [%e? accu_op] [%e? lhs] ([%e? bin_op] [%e? rhs1] ([%e? rhs2] ~projections:[%e? projections]))] -> let initialize_neutral, accu_op = assignment_op accu_op in let setup_l = setup_array [%pat? nondiff___lhs] @@ translate ?ident_label ~proj_in_scope:true lhs in let _, bin_op = binary_op bin_op in let setup_r1 = setup_array [%pat? nondiff___rhs1] @@ translate ~proj_in_scope:true rhs1 in let setup_r2 = setup_array [%pat? nondiff___rhs2] @@ translate ~proj_in_scope:true rhs2 in let initialize_neutral = if initialize_neutral then [%expr true] else [%expr false] in (* TODO: might be better to treat missing [rhs1, rhs2] as zeros rather than eliding the code. *) let body = [%expr Option.value ~default:Arrayjit.Assignments.Noop @@ Option.map3 [%e setup_l.array_opt] [%e setup_r1.array_opt] [%e setup_r2.array_opt] ~f:(fun lhs rhs1 rhs2 -> Arrayjit.Assignments.Accum_binop { initialize_neutral = [%e initialize_neutral]; accum = [%e accu_op]; lhs; op = [%e bin_op]; rhs1; rhs2; projections = [%e projections]; })] in let setups = List.filter_map ~f:(fun setup -> setup.binding) [ setup_l; setup_r1; setup_r2 ] in with_forward_args setups body | [%expr [%e? accu_op] [%e? lhs] (([%e? un_op] [%e? rhs]) ~projections:[%e? projections])] | [%expr [%e? accu_op] [%e? lhs] ([%e? un_op] ([%e? rhs] ~projections:[%e? projections]))] -> (* Handle both un_op priority levels -- where application binds tighter and less tight. *) let initialize_neutral, accu_op = assignment_op accu_op in let setup_l = setup_array [%pat? nondiff___lhs] @@ translate ?ident_label ~proj_in_scope:true lhs in let _, un_op = unary_op un_op in let setup_r = setup_array [%pat? nondiff___rhs] @@ translate ~proj_in_scope:true rhs in let initialize_neutral = if initialize_neutral then [%expr true] else [%expr false] in (* TODO: might be better to treat missing [rhs] as zeros rather than eliding the code. *) let body = [%expr Option.value ~default:Arrayjit.Assignments.Noop @@ Option.map2 [%e setup_l.array_opt] [%e setup_r.array_opt] ~f:(fun lhs rhs -> Arrayjit.Assignments.Accum_unop { initialize_neutral = [%e initialize_neutral]; accum = [%e accu_op]; lhs; op = [%e un_op]; rhs; projections = [%e projections]; })] in let setups = List.filter_map ~f:(fun setup -> setup.binding) [ setup_l; setup_r ] in with_forward_args setups body | [%expr [%e? accu_op] [%e? lhs] ([%e? rhs] ~projections:[%e? projections])] -> let initialize_neutral, accu_op = assignment_op accu_op in let setup_l = setup_array [%pat? nondiff___lhs] @@ translate ?ident_label ~proj_in_scope:true lhs in let setup_r = setup_array [%pat? nondiff___rhs] @@ translate ~proj_in_scope:true rhs in let initialize_neutral = if initialize_neutral then [%expr true] else [%expr false] in let body = [%expr Option.value ~default:Arrayjit.Assignments.Noop @@ Option.map2 [%e setup_l.array_opt] [%e setup_r.array_opt] ~f:(fun lhs rhs -> Arrayjit.Assignments.Accum_unop { initialize_neutral = [%e initialize_neutral]; accum = [%e accu_op]; lhs; op = Arrayjit.Ops.Identity; rhs; projections = [%e projections]; })] in let setups = List.filter_map ~f:(fun setup -> setup.binding) [ setup_l; setup_r ] in with_forward_args setups body | [%expr [%e? accu_op] [%e? lhs] ([%e? bin_op] [%e? rhs1] ([%e? rhs2] ~logic:[%e? { pexp_desc = Pexp_constant (Pconst_string (spec, s_loc, _)); _ } as logic]))] -> let logic = let loc = s_loc in if String.equal spec "." then [%expr Shape.Pointwise_bin] else if String.equal spec "@" then [%expr Shape.Compose] else [%expr Shape.Einsum [%e logic]] in let initialize_neutral, accu_op = assignment_op accu_op in let setup_l = setup_array [%pat? nondiff___lhs] @@ translate ?ident_label ~proj_in_scope lhs in let _, bin_op = binary_op bin_op in let setup_r1 = setup_array [%pat? nondiff___rhs1] @@ translate ~proj_in_scope rhs1 in let setup_r2 = setup_array [%pat? nondiff___rhs2] @@ translate ~proj_in_scope rhs2 in let initialize_neutral = if initialize_neutral then [%expr true] else [%expr false] in let args_for = function | { filler_typ = Grad_of_tensor _; tensor = Some t; _ } -> (t, [%expr true]) | { filler_typ = _; tensor = Some t; _ } -> (t, [%expr false]) | _ -> ( Ast_builder.Default.pexp_extension ~loc @@ Location.error_extensionf ~loc "ppx_ocannl %%cd: cannot use `~logic` (infer shapes) for arrays, use tensors or `.grad` \ notation", [%expr false] ) in let t_expr, lhs_is_grad = args_for setup_l in let t1_expr, rhs1_is_grad = args_for setup_r1 in let t2_expr, rhs2_is_grad = args_for setup_r2 in let body = [%expr Tensor.raw_binop ~initialize_neutral:[%e initialize_neutral] ~accum:[%e accu_op] ~t:[%e t_expr] ~lhs_is_grad:[%e lhs_is_grad] ~op:[%e bin_op] ~t1:[%e t1_expr] ~rhs1_is_grad:[%e rhs1_is_grad] ~t2:[%e t2_expr] ~rhs2_is_grad:[%e rhs2_is_grad] ~logic:[%e logic]] in let setups = List.filter_map ~f:(fun setup -> setup.binding) [ setup_l; setup_r1; setup_r2 ] in with_forward_args setups body | [%expr [%e? accu_op] [%e? lhs] (([%e? un_op] [%e? rhs]) ~logic:[%e? { pexp_desc = Pexp_constant (Pconst_string (spec, s_loc, _)); _ } as logic])] | [%expr [%e? accu_op] [%e? lhs] ([%e? un_op] ([%e? rhs] ~logic:[%e? { pexp_desc = Pexp_constant (Pconst_string (spec, s_loc, _)); _ } as logic]))] -> (* Handle both un_op priority levels -- where application binds tighter and less tight. *) let logic = let loc = s_loc in if String.equal spec "." then [%expr Shape.Pointwise_un] else if String.equal spec "T" then [%expr Shape.Transpose] else [%expr Shape.Permute [%e logic]] in let initialize_neutral, accu_op = assignment_op accu_op in let setup_l = setup_array [%pat? nondiff___lhs] @@ translate ?ident_label ~proj_in_scope lhs in let _, un_op = unary_op un_op in let setup_r = setup_array [%pat? nondiff___rhs] @@ translate ~proj_in_scope rhs in let initialize_neutral = if initialize_neutral then [%expr true] else [%expr false] in let args_for = function | { filler_typ = Grad_of_tensor _; tensor = Some t; _ } -> (t, [%expr true]) | { filler_typ = _; tensor = Some t; _ } -> (t, [%expr false]) | _ -> ( Ast_builder.Default.pexp_extension ~loc @@ Location.error_extensionf ~loc "ppx_ocannl %%cd: cannot use `~logic` (infer shapes) for arrays, use tensors or `.grad` \ notation", [%expr false] ) in let t_expr, lhs_is_grad = args_for setup_l in let t1_expr, rhs_is_grad = args_for setup_r in let body = [%expr Tensor.raw_unop ~initialize_neutral:[%e initialize_neutral] ~accum:[%e accu_op] ~t:[%e t_expr] ~lhs_is_grad:[%e lhs_is_grad] ~op:[%e un_op] ~t1:[%e t1_expr] ~rhs_is_grad:[%e rhs_is_grad] ~logic:[%e logic]] in let setups = List.filter_map ~f:(fun setup -> setup.binding) [ setup_l; setup_r ] in with_forward_args setups body | [%expr [%e? { pexp_desc = Pexp_ident { txt = Lident accu_ident; loc = accu_loc }; _ } as accu_op] [%e? lhs] ([%e? { pexp_desc = Pexp_ident { txt = Lident binop_ident; loc = op_loc }; _ } as bin_op] [%e? rhs1] [%e? rhs2])] when is_assignment accu_ident && is_binary_op binop_ident && proj_in_scope -> let initialize_neutral, accu_op = assignment_op accu_op in let setup_l = setup_array [%pat? nondiff___lhs] @@ translate ?ident_label ~proj_in_scope lhs in let _, bin_op = binary_op bin_op in let setup_r1 = setup_array [%pat? nondiff___rhs1] @@ translate ~proj_in_scope rhs1 in let setup_r2 = setup_array [%pat? nondiff___rhs2] @@ translate ~proj_in_scope rhs2 in let initialize_neutral = if initialize_neutral then [%expr true] else [%expr false] in let projections = let lhs_dims = project_p_dims "LHS" lhs.pexp_loc setup_l.slot in let rhs1_dims = project_p_dims "RHS1" lhs.pexp_loc setup_r1.slot in let rhs2_dims = project_p_dims "RHS2" lhs.pexp_loc setup_r2.slot in let project_lhs = project_p_slot "LHS" lhs.pexp_loc setup_l.slot in let project_rhs1 = project_p_slot "RHS1" rhs1.pexp_loc setup_r1.slot in let project_rhs2 = project_p_slot "RHS2" rhs2.pexp_loc setup_r2.slot in [%expr lazy (let p = Lazy.force projections in Arrayjit.Indexing. { product_space = p.product_space; product_iterators = p.product_iterators; lhs_dims = [%e lhs_dims]; rhs_dims = [| [%e rhs1_dims]; [%e rhs2_dims] |]; project_lhs = [%e project_lhs]; project_rhs = [| [%e project_rhs1]; [%e project_rhs2] |]; debug_info = { p.debug_info with trace = ( "ppx_cd " ^ [%e string_expr ~loc:accu_loc accu_ident] ^ " " ^ [%e string_expr ~loc:op_loc binop_ident], Arrayjit.Indexing.unique_debug_id () ) :: p.debug_info.trace; }; })] in let body = [%expr Option.value ~default:Arrayjit.Assignments.Noop @@ Option.map3 [%e setup_l.array_opt] [%e setup_r1.array_opt] [%e setup_r2.array_opt] ~f:(fun lhs rhs1 rhs2 -> Arrayjit.Assignments.Accum_binop { initialize_neutral = [%e initialize_neutral]; accum = [%e accu_op]; lhs; op = [%e bin_op]; rhs1; rhs2; projections = [%e projections]; })] in let setups = List.filter_map ~f:(fun setup -> setup.binding) [ setup_l; setup_r1; setup_r2 ] in with_forward_args setups body | [%expr [%e? { pexp_desc = Pexp_ident { txt = Lident accu_ident; loc = accu_loc }; _ } as accu_op] [%e? lhs] ([%e? { pexp_desc = Pexp_ident { txt = Lident unop_ident; loc = op_loc }; _ } as un_op] [%e? rhs])] when is_assignment accu_ident && is_unary_op unop_ident && proj_in_scope -> let initialize_neutral, accu_op = assignment_op accu_op in let setup_l = setup_array [%pat? nondiff___lhs] @@ translate ?ident_label ~proj_in_scope lhs in let _, un_op = unary_op un_op in let setup_r1 = setup_array [%pat? nondiff___rhs1] @@ translate ~proj_in_scope rhs in let initialize_neutral = if initialize_neutral then [%expr true] else [%expr false] in let projections = let lhs_dims = project_p_dims "LHS" lhs.pexp_loc setup_l.slot in let rhs1_dims = project_p_dims "RHS1" lhs.pexp_loc setup_r1.slot in let project_lhs = project_p_slot "LHS" lhs.pexp_loc setup_l.slot in let project_rhs1 = project_p_slot "RHS1" rhs.pexp_loc setup_r1.slot in [%expr lazy (let p = Lazy.force projections in Arrayjit.Indexing. { product_space = p.product_space; product_iterators = p.product_iterators; lhs_dims = [%e lhs_dims]; rhs_dims = [| [%e rhs1_dims] |]; project_lhs = [%e project_lhs]; project_rhs = [| [%e project_rhs1] |]; debug_info = { p.debug_info with trace = ( "ppx_cd " ^ [%e string_expr ~loc:accu_loc accu_ident] ^ " " ^ [%e string_expr ~loc:op_loc unop_ident], Arrayjit.Indexing.unique_debug_id () ) :: p.debug_info.trace; }; })] in let body = [%expr Option.value ~default:Arrayjit.Assignments.Noop @@ Option.map2 [%e setup_l.array_opt] [%e setup_r1.array_opt] ~f:(fun lhs rhs -> Arrayjit.Assignments.Accum_unop { initialize_neutral = [%e initialize_neutral]; accum = [%e accu_op]; lhs; op = [%e un_op]; rhs; projections = [%e projections]; })] in let setups = List.filter_map ~f:(fun setup -> setup.binding) [ setup_l; setup_r1 ] in with_forward_args setups body | [%expr [%e? { pexp_desc = Pexp_ident { txt = Lident op_ident; loc = op_loc }; _ } as accu_op] [%e? lhs] [%e? rhs]] when is_assignment op_ident && proj_in_scope -> let initialize_neutral, accu_op = assignment_op accu_op in let setup_l = setup_array [%pat? nondiff___lhs] @@ translate ?ident_label ~proj_in_scope lhs in let setup_r1 = setup_array [%pat? nondiff___rhs1] @@ translate ~proj_in_scope rhs in let initialize_neutral = if initialize_neutral then [%expr true] else [%expr false] in let projections = let lhs_dims = project_p_dims "LHS" lhs.pexp_loc setup_l.slot in let rhs1_dims = project_p_dims "RHS1" lhs.pexp_loc setup_r1.slot in let project_lhs = project_p_slot "LHS" lhs.pexp_loc setup_l.slot in let project_rhs1 = project_p_slot "RHS1" rhs.pexp_loc setup_r1.slot in [%expr lazy (let p = Lazy.force projections in Arrayjit.Indexing. { product_space = p.product_space; product_iterators = p.product_iterators; lhs_dims = [%e lhs_dims]; rhs_dims = [| [%e rhs1_dims] |]; project_lhs = [%e project_lhs]; project_rhs = [| [%e project_rhs1] |]; debug_info = { p.debug_info with trace = ( "ppx_cd " ^ [%e string_expr ~loc:op_loc op_ident], Arrayjit.Indexing.unique_debug_id () ) :: p.debug_info.trace; }; })] in let body = [%expr Option.value ~default:Arrayjit.Assignments.Noop @@ Option.map2 [%e setup_l.array_opt] [%e setup_r1.array_opt] ~f:(fun lhs rhs -> Arrayjit.Assignments.Accum_unop { initialize_neutral = [%e initialize_neutral]; accum = [%e accu_op]; lhs; op = Arrayjit.Ops.Identity; rhs; projections = [%e projections]; })] in let setups = List.filter_map ~f:(fun setup -> setup.binding) [ setup_l; setup_r1 ] in with_forward_args setups body | [%expr [%e? { pexp_desc = Pexp_ident { txt = Lident accu_ident; _ }; _ } as accu_op] [%e? lhs] ([%e? { pexp_desc = Pexp_ident { txt = Lident binop_ident; _ }; _ } as bin_op] [%e? rhs1] [%e? rhs2])] when is_assignment accu_ident && is_binary_op binop_ident -> let initialize_neutral, accu_op = assignment_op accu_op in let setup_l = setup_array [%pat? nondiff___lhs] @@ translate ?ident_label ~proj_in_scope lhs in let logic, bin_op = binary_op bin_op in let setup_r1 = setup_array [%pat? nondiff___rhs1] @@ translate ~proj_in_scope rhs1 in let setup_r2 = setup_array [%pat? nondiff___rhs2] @@ translate ~proj_in_scope rhs2 in let initialize_neutral = if initialize_neutral then [%expr true] else [%expr false] in let args_for = function | { filler_typ = Grad_of_tensor _; tensor = Some t; _ } -> (t, [%expr true]) | { filler_typ = _; tensor = Some t; _ } -> (t, [%expr false]) | _ -> ( Ast_builder.Default.pexp_extension ~loc @@ Location.error_extensionf ~loc "ppx_ocannl %%cd: cannot use `~logic` (infer shapes) for arrays, use tensors or `.grad` \ notation", [%expr false] ) in let t_expr, lhs_is_grad = args_for setup_l in let t1_expr, rhs1_is_grad = args_for setup_r1 in let t2_expr, rhs2_is_grad = args_for setup_r2 in let body = [%expr Tensor.raw_binop ~initialize_neutral:[%e initialize_neutral] ~accum:[%e accu_op] ~t:[%e t_expr] ~lhs_is_grad:[%e lhs_is_grad] ~op:[%e bin_op] ~t1:[%e t1_expr] ~rhs1_is_grad:[%e rhs1_is_grad] ~t2:[%e t2_expr] ~rhs2_is_grad:[%e rhs2_is_grad] ~logic:[%e logic]] in let setups = List.filter_map ~f:(fun setup -> setup.binding) [ setup_l; setup_r1; setup_r2 ] in with_forward_args setups body | [%expr [%e? { pexp_desc = Pexp_ident { txt = Lident accu_ident; _ }; _ } as accu_op] [%e? lhs] ([%e? { pexp_desc = Pexp_ident { txt = Lident unop_ident; _ }; _ } as un_op] [%e? rhs])] when is_assignment accu_ident && is_unary_op unop_ident -> let initialize_neutral, accu_op = assignment_op accu_op in let setup_l = setup_array [%pat? nondiff___lhs] @@ translate ?ident_label ~proj_in_scope lhs in let logic, un_op = unary_op un_op in let setup_r = setup_array [%pat? nondiff___rhs] @@ translate ~proj_in_scope rhs in let initialize_neutral = if initialize_neutral then [%expr true] else [%expr false] in let args_for = function | { filler_typ = Grad_of_tensor _; tensor = Some t; _ } -> (t, [%expr true]) | { filler_typ = _; tensor = Some t; _ } -> (t, [%expr false]) | _ -> ( Ast_builder.Default.pexp_extension ~loc @@ Location.error_extensionf ~loc "ppx_ocannl %%cd: cannot use `~logic` (infer shapes) for arrays, use tensors or `.grad` \ notation", [%expr false] ) in let t_expr, lhs_is_grad = args_for setup_l in let t1_expr, rhs_is_grad = args_for setup_r in let body = [%expr Tensor.raw_unop ~initialize_neutral:[%e initialize_neutral] ~accum:[%e accu_op] ~t:[%e t_expr] ~lhs_is_grad:[%e lhs_is_grad] ~op:[%e un_op] ~t1:[%e t1_expr] ~rhs_is_grad:[%e rhs_is_grad] ~logic:[%e logic]] in let setups = List.filter_map ~f:(fun setup -> setup.binding) [ setup_l; setup_r ] in with_forward_args setups body | [%expr [%e? { pexp_desc = Pexp_ident { txt = Lident op_ident; _ }; _ } as accu_op] [%e? lhs] [%e? rhs]] when is_assignment op_ident -> let initialize_neutral, accu_op = assignment_op accu_op in let setup_l = setup_array [%pat? nondiff___lhs] @@ translate ?ident_label ~proj_in_scope lhs in let setup_r = setup_array [%pat? nondiff___rhs] @@ translate ~proj_in_scope rhs in let initialize_neutral = if initialize_neutral then [%expr true] else [%expr false] in let args_for = function | { filler_typ = Grad_of_tensor _; tensor = Some t; _ } -> (t, [%expr true]) | { filler_typ = _; tensor = Some t; _ } -> (t, [%expr false]) | _ -> ( Ast_builder.Default.pexp_extension ~loc @@ Location.error_extensionf ~loc "ppx_ocannl %%cd: cannot use `~logic` (infer shapes) for arrays, use tensors or `.grad` \ notation", [%expr false] ) in let t_expr, lhs_is_grad = args_for setup_l in let t1_expr, rhs_is_grad = args_for setup_r in let body = [%expr Tensor.raw_unop ~initialize_neutral:[%e initialize_neutral] ~accum:[%e accu_op] ~t:[%e t_expr] ~lhs_is_grad:[%e lhs_is_grad] ~op:Arrayjit.Ops.Identity ~t1:[%e t1_expr] ~rhs_is_grad:[%e rhs_is_grad] ~logic:Shape.Pointwise_un] in let setups = List.filter_map ~f:(fun setup -> setup.binding) [ setup_l; setup_r ] in with_forward_args setups body | [%expr [%e? expr1] [%e? expr2] [%e? expr3]] -> let typ1, slot1, expr1 = translate ?ident_label ~proj_in_scope expr1 in let _typ2, slot2, expr2 = translate ~proj_in_scope expr2 in let _typ3, slot3, expr3 = translate ~proj_in_scope expr3 in let slot = Option.value ~default:Undet @@ List.find ~f:(function Undet -> false | _ -> true) [ slot1; slot2; slot3 ] in (typ1, slot, [%expr [%e expr1] [%e expr2] [%e expr3]]) | [%expr [%e? expr1] [%e? expr2]] -> let typ1, slot1, expr1 = translate ?ident_label ~proj_in_scope expr1 in let _typ2, slot2, expr2 = translate ~proj_in_scope expr2 in let slot = Option.value ~default:Undet @@ List.find ~f:(function Undet -> false | _ -> true) [ slot1; slot2 ] in (typ1, slot, [%expr [%e expr1] [%e expr2]]) | { pexp_desc = Pexp_fun ((arg_label : arg_label), arg, opt_val, body); _ } as expr -> let proj_in_scope = proj_in_scope || match arg_label with | (Labelled s | Optional s) when String.equal s "projections" -> true | _ -> false in let typ, slot, body = translate ?ident_label ~proj_in_scope body in (typ, slot, { expr with pexp_desc = Pexp_fun (arg_label, arg, opt_val, body) }) | [%expr while [%e? _test_expr] do [%e? _body] done] -> ( Code, Nonslot, Ast_builder.Default.pexp_extension ~loc @@ Location.error_extensionf ~loc "ppx_ocannl %%cd: while: low-level code embeddings not supported yet" ) | [%expr for [%p? _pat] = [%e? _init] to [%e? _final] do [%e? _body_expr] done] -> ( Code, Nonslot, Ast_builder.Default.pexp_extension ~loc @@ Location.error_extensionf ~loc "ppx_ocannl %%cd: for-to: low-level code embeddings not supported yet" ) | [%expr for [%p? _pat] = [%e? _init] downto [%e? _final] do [%e? _body_expr] done] -> ( Code, Nonslot, Ast_builder.Default.pexp_extension ~loc @@ Location.error_extensionf ~loc "ppx_ocannl %%cd: for-downto: low-level code embeddings not supported yet" ) | [%expr [%e? expr1]; [%e? expr2]] -> let _typ1, _slot1, expr1 = translate ~proj_in_scope expr1 in let _typ2, _slot2, expr2 = translate ?ident_label ~proj_in_scope expr2 in (Code, Nonslot, [%expr Arrayjit.Assignments.Seq ([%e expr1], [%e expr2])]) | [%expr if [%e? expr1] then [%e? expr2] else [%e? expr3]] -> let typ2, slot2, expr2 = translate ?ident_label ~proj_in_scope expr2 in let typ3, slot3, expr3 = translate ?ident_label ~proj_in_scope expr3 in let typ = if is_unknown typ2 then typ3 else typ2 in let slot = Option.value ~default:Undet @@ List.find ~f:(function Undet -> false | _ -> true) [ slot2; slot3 ] in (typ, slot, [%expr if [%e expr1] then [%e expr2] else [%e expr3]]) | [%expr if [%e? expr1] then [%e? expr2]] -> let _typ2, _slot2, expr2 = translate ?ident_label ~proj_in_scope expr2 in (Code, Nonslot, [%expr if [%e expr1] then [%e expr2] else Arrayjit.Assignments.Noop]) | { pexp_desc = Pexp_match (expr1, cases); _ } -> let typs, slots, cases = List.unzip3 @@ List.map cases ~f:(fun ({ pc_rhs; _ } as c) -> let typ, slot, pc_rhs = translate ?ident_label ~proj_in_scope pc_rhs in (typ, slot, { c with pc_rhs })) in let typ = Option.value ~default:Unknown @@ List.find typs ~f:(Fn.non is_unknown) in let slot = Option.value ~default:Undet @@ List.find ~f:(function Undet -> false | _ -> true) slots in (typ, slot, { expr with pexp_desc = Pexp_match (expr1, cases) }) | { pexp_desc = Pexp_let (_recflag, _bindings, _body); _ } -> (* TODO(80): to properly support local bindings, we need to collect the type environment. *) ( Unknown, Undet, Ast_builder.Default.pexp_extension ~loc @@ Location.error_extensionf ~loc "ppx_ocannl %%cd: let-in: local let-bindings not implemented yet" ) (* let bindings = List.map bindings ~f:(fun binding -> {binding with pvb_expr=translate binding.pvb_expr}) in {expr with pexp_desc=Pexp_let (recflag, bindings, translate body)} *) | { pexp_desc = Pexp_open (decl, body); _ } -> let typ, slot, body = translate ?ident_label ~proj_in_scope body in (typ, slot, { expr with pexp_desc = Pexp_open (decl, body) }) | { pexp_desc = Pexp_letmodule (name, module_expr, body); _ } -> let typ, slot, body = translate ?ident_label ~proj_in_scope body in (typ, slot, { expr with pexp_desc = Pexp_letmodule (name, module_expr, body) }) | { pexp_desc = Pexp_ident { txt = Lident op_ident; _ }; _ } when is_operator op_ident -> (Unknown, Undet, [%expr [%e expr] ~label:[%e opt_pat2string_list ~loc ident_label]]) | _ -> (Unknown, Undet, expr) let translate ?ident_label (expr : expression) : expression = let _, _, v = translate ?ident_label ~proj_in_scope:false expr in match ident_label with | Some [%pat? _] -> let loc = v.pexp_loc in [%expr Tensor.with_unchanged_roots ~f:(fun () -> [%e v])] | _ -> v type extension = Cd | Dt | Rs [@@deriving equal, variants] let expr_expander ~loc ~path:_ payload = match payload with | { pexp_desc = Pexp_let (recflag, bindings, body); _ } -> (* We are at the %cd annotation level: do not tranlsate the body. *) let bindings = List.map bindings ~f:(fun vb -> let v = translate ~ident_label:vb.pvb_pat vb.pvb_expr in { vb with pvb_expr = [%expr let open! NTDSL.O in [%e v]]; }) in { payload with pexp_desc = Pexp_let (recflag, bindings, body) } | expr -> let expr = translate expr in [%expr let open! NTDSL.O in [%e expr]] let flatten_str ~loc ~path:_ items = match items with | [ x ] -> x | _ -> Ast_helper.Str.include_ { pincl_mod = Ast_helper.Mod.structure items; pincl_loc = loc; pincl_attributes = [] } let translate_str ({ pstr_desc; _ } as str) = match pstr_desc with | Pstr_eval (expr, attrs) -> let expr = translate expr in let loc = expr.pexp_loc in { str with pstr_desc = Pstr_eval ( [%expr let open! NTDSL.O in [%e expr]], attrs ); } | Pstr_value (recf, bindings) -> let f vb = let loc = vb.pvb_loc in let v = translate ~ident_label:vb.pvb_pat vb.pvb_expr in { vb with pvb_expr = [%expr let open! NTDSL.O in [%e v]]; } in { str with pstr_desc = Pstr_value (recf, List.map bindings ~f) } | _ -> str let str_expander ~loc ~path (payload : structure_item list) = flatten_str ~loc ~path @@ List.map payload ~f:translate_str
sectionYPositions = computeSectionYPositions($el), 10)"
x-init="setTimeout(() => sectionYPositions = computeSectionYPositions($el), 10)"
>