Commit c63a596
committed
Refactoring of Assignments.t to avoid field duplication and to provide projections debug without forcing projections, by Claude Code
Prompt 1: I just started refactoring Assignments.t in arrayjit/lib/assignments.ml to avoid duplicating lots of fields, and I'm adding the field projections_debug that I want to contain the `debug_info` field of `Indexing.projections`, or its approximation, even before the projections are actually computed. This will require coordination with the syntax extension ppx_cd.ml . I attach the relevant files in the context. Update the code to take the changed type definitions into account, and set the `projections_debug` fields from the projections specs. Point out where finding `projections_debug` without calculating the projections is tricky, I can help.
Summary 1, by Claude:
## Summary
I've successfully updated the code to use your refactored `Assignments.t` type. Here's what was done:
1. **Updated `assignments.ml`** to use the new consolidated `Accum_op` constructor that contains an `accum_rhs` field (which can be `Unop`, `Binop`, or `Ternop`) instead of the separate `Accum_unop`, `Accum_binop`, and `Accum_ternop` constructors.
2. **Added the `projections_debug` field** to both `Accum_op` and `Set_vec_unop` constructors to store the projection specification string.
3. **Updated `tensor.ml`** to set the `projections_debug` field by using `Shape.logic_to_spec shape_logic` to convert the shape logic to its string representation.
4. **Updated `ppx_cd.ml`** to generate code using the new constructor. For cases where projections are computed lazily (no explicit `~logic:` parameter), I set `projections_debug` to `"<derived>"` as a placeholder.
5. **Exported `Shape.logic_to_spec`** in the `shape.mli` interface file so it can be used from `tensor.ml`.
### Tricky parts where finding `projections_debug` without calculating projections is challenging:
1. **In `ppx_cd.ml` when no explicit `~logic:` is provided**: In these cases, the projections are computed lazily based on shape inference. We don't have the actual projection spec string at assignment creation time, so I used `"<derived>"` as a placeholder. If you need the actual spec in these cases, you might need to either:
- Extract it from the lazy projections when they're forced
- Compute a preliminary spec based on the operator type (most operators default to "." for pointwise)
- Store additional information in the assignment to compute the spec later
2. **For vector operations**: The `Set_vec_unop` operations also use projections, and similarly we set `projections_debug` to `"<derived>"` when created through ppx_cd without explicit logic.
The code now compiles successfully with `dune build @check`.
Prompt 2: How difficult would it be to change `type projections` in the Tensor module from `Ir.Indexing.Projections` to `string * Ir.Indexing.Projections Lazy.t` and then skip `Lazy.t` at use sites, so that we can pass the debug together with the delayed projections? The debug would be computed in the `op` function that starts at line 206 of tensor.ml , using `Shape.logic_to_spec` on the `logic` field of `List.hd_exn local_shape_updates`.
Prompt 3: Let's clarify what we want so that we're on the same page. We want benefit (3). We don't want (1) and it's not really true: the passing of the pair is so that the content of `projections_debug` is readily available, in fact we can define: `type projections = {projections_debug : string; projections : Ir.Indexing.projections Lazy.t}` in the Tensor module. The question I hadn't considered is which type should the `op_asn` and `grad_asn` functions be using. It should be `projections` from Tensor rather than `Indexing.projections Lazy.t` because that's the only way that `ppx_cd.ml` can unpack the two fields to populate inside Accum_op and Set_vec_unop . Let's do this, so that the debug information becomes precise.
Summary 2, by Claude:
## Summary
I've successfully implemented the change to use a `projections` record type in the Tensor module that contains both the debug string and the lazy projections. Here's what was done:
1. **Defined a new projections type** in `tensor.ml` and `tensor.mli`:
```ocaml
type projections = {
projections_debug : string;
projections : Ir.Indexing.projections Lazy.t;
}
```
2. **Updated projection creation sites** in `tensor.ml` to create the record with the debug string computed using `Shape.logic_to_spec`:
- In `raw_binop`, `raw_ternop`, `raw_unop`, and `op` functions
- The debug string is computed at projection creation time from the shape logic
3. **Updated op_asn and grad_asn function signatures** to use `Tensor.projections` instead of `Ir.Indexing.projections Lazy.t`
4. **Updated ppx_cd.ml** to handle the new structure:
- When projections are passed explicitly (`~projections`), extract fields as `projections.Tensor.projections` and `projections.Tensor.projections_debug`
- When creating lazy projections, reference `projections.Tensor.projections` in the lazy computation
- Use the extracted fields when creating assignments
5. **Fixed usage sites** that needed to access the projections field
The key benefit is that now the projection debug information is **always precise** - it's computed from the actual shape logic at the point where projections are created, rather than using placeholders like `"<derived>"`. This debug information travels together with the projections throughout the system, making it available wherever projections are used.
```ocaml
type projections = {
projections_debug : string;
projections : Ir.Indexing.projections Lazy.t;
}
```1 parent 23ff973 commit c63a596
File tree
7 files changed
+176
-166
lines changed- arrayjit/lib
- lib
7 files changed
+176
-166
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
36 | 36 | | |
37 | 37 | | |
38 | 38 | | |
39 | | - | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
40 | 46 | | |
41 | 47 | | |
42 | 48 | | |
43 | | - | |
44 | | - | |
45 | | - | |
46 | | - | |
47 | | - | |
48 | | - | |
49 | | - | |
50 | | - | |
51 | | - | |
52 | | - | |
53 | | - | |
54 | | - | |
55 | | - | |
56 | | - | |
57 | | - | |
58 | | - | |
59 | | - | |
60 | | - | |
61 | | - | |
62 | | - | |
| 49 | + | |
63 | 50 | | |
64 | 51 | | |
65 | | - | |
66 | 52 | | |
67 | | - | |
| 53 | + | |
68 | 54 | | |
| 55 | + | |
69 | 56 | | |
70 | 57 | | |
71 | 58 | | |
72 | 59 | | |
73 | 60 | | |
74 | 61 | | |
| 62 | + | |
75 | 63 | | |
76 | 64 | | |
77 | 65 | | |
| |||
122 | 110 | | |
123 | 111 | | |
124 | 112 | | |
125 | | - | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
126 | 120 | | |
127 | | - | |
128 | | - | |
129 | | - | |
130 | | - | |
131 | 121 | | |
132 | 122 | | |
133 | 123 | | |
| |||
145 | 135 | | |
146 | 136 | | |
147 | 137 | | |
148 | | - | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
149 | 145 | | |
150 | | - | |
151 | | - | |
152 | | - | |
153 | 146 | | |
154 | 147 | | |
155 | 148 | | |
| |||
256 | 249 | | |
257 | 250 | | |
258 | 251 | | |
259 | | - | |
260 | | - | |
261 | | - | |
262 | | - | |
263 | | - | |
264 | | - | |
265 | | - | |
266 | | - | |
267 | | - | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
268 | 260 | | |
269 | 261 | | |
270 | 262 | | |
| |||
370 | 362 | | |
371 | 363 | | |
372 | 364 | | |
373 | | - | |
| 365 | + | |
374 | 366 | | |
375 | 367 | | |
376 | 368 | | |
| |||
397 | 389 | | |
398 | 390 | | |
399 | 391 | | |
400 | | - | |
401 | | - | |
402 | | - | |
403 | | - | |
404 | | - | |
405 | | - | |
406 | | - | |
407 | | - | |
| 392 | + | |
| 393 | + | |
| 394 | + | |
| 395 | + | |
| 396 | + | |
| 397 | + | |
| 398 | + | |
| 399 | + | |
408 | 400 | | |
409 | 401 | | |
410 | 402 | | |
| |||
445 | 437 | | |
446 | 438 | | |
447 | 439 | | |
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 | | - | |
| 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 | + | |
508 | 489 | | |
509 | 490 | | |
510 | 491 | | |
| |||
535 | 516 | | |
536 | 517 | | |
537 | 518 | | |
538 | | - | |
539 | | - | |
540 | 519 | | |
541 | 520 | | |
542 | 521 | | |
| 522 | + | |
543 | 523 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
700 | 700 | | |
701 | 701 | | |
702 | 702 | | |
703 | | - | |
| 703 | + | |
| 704 | + | |
| 705 | + | |
| 706 | + | |
704 | 707 | | |
705 | 708 | | |
706 | 709 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
409 | 409 | | |
410 | 410 | | |
411 | 411 | | |
412 | | - | |
| 412 | + | |
413 | 413 | | |
414 | 414 | | |
415 | 415 | | |
| |||
0 commit comments