[mlir] Multi-result reverse mode tablegen implementation #3538
Use llvm::StringLiteral
Be consistent about CamelCase and camelBack
Why hardcode the 2 smallset size?
Why do we need this dance with -1 instead of just using 0 for DiffeRet? Or is -1 somehow intended to mean "all results", which it doesn't here.
Thou shalt not hardcode SmallVector stack size without reason.
This is highly inefficient due to excessive dynamic allocation in std string concatenation. Use raw_string_ostream instead.
Error messages should start from a lower case
I'd highly suggest combining a raw string literal with formatv here, can't parse all the backslash escaping...
Moved to a SetVector since we need the vector afterwards.
This is an artifact of the llvm DiffeRetIndex where -1 is a sentinel that signifies the result value whereas in mlir the result could always be considered a tuple. I removed it.
so @Antipath1 and I talked about this the other week during the weekly enzyme call and came up with a slightly different array of array design
Could you give more details or have a draft somewhere?
Could you give more details or have a draft somewhere?
I did not really write down anything. From my recollection of the meeting (@wsmoses, correct me if I forgot something): for an op with several results, the reverse rule should be an array of arrays. The outer array has one entry per result, so [i][j] is result i’s contribution to operand j, with (DiffeRet) in row i meaning result i’s adjoint. The generated code only evaluates the rows of active results and sums them per operand, so inactive results never produce a term.
For sincos (one operand, two results):
[
[ (CheckedMulF (DiffeRet), (CosF $x)) ], // sin
[ (NegF (CheckedMulF (DiffeRet), (SinF $x))) ] // cos
]
The design matters a bit more for ops with multiple arguments and return values. For example,
(r, θ) -> (x, y) = (r·cos θ, r·sin θ) has a full 2*2 table, one row per result:[
[ dx·cos θ, -dx·r·sin θ ], // x = r·cos θ
[ dy·sin θ, dy·r·cos θ ] // y = r·sin θ
]
Single-result rules stay as they are (a flat list is the one-row case). Idk if that is the final syntax of this that we would want, but like along these lines. We basically want to avoid multiplying by 0 in the case of an inactive result.
For this to work, we would probably also need to have the number of return values (we already have the number of operands).
Happy to implement it myself if that is easier.
with (DiffeRet) in row i meaning result i’s adjoint.
This is reasonable to avoid accidentally referencing irrelevant adjoints. However, we'd better have detailed error messages to check that we have the right array-of-arrays structure.
ftynse
Pangoraw
No description provided by author.