Skip to content

mlir: enable mincut support for scf.for#2359

Merged
wsmoses merged 4 commits intoEnzymeAD:mainfrom
Pangoraw:for-mincut
Jun 26, 2025
Merged

mlir: enable mincut support for scf.for#2359
wsmoses merged 4 commits intoEnzymeAD:mainfrom
Pangoraw:for-mincut

Conversation

@Pangoraw
Copy link
Collaborator

No description provided.

if (updatedGradients.empty() && caches.empty())
return success();

if (forOp->hasAttr("enzyme.enable_mincut")) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think we should make this the default [and optionally disable]

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we'll also want to add this to affine.for/{scf,affine}.parallel

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

makes sense, i will follow-up with those (i don't think we have the rev rules for these ops yet)

// CHECK-NEXT: %c0 = arith.constant 0 : index
// CHECK-NEXT: %[[v0:.+]] = tensor.empty() : tensor<10xf32>
// CHECK-NEXT: %[[for:.+]]:2 = scf.for %arg2 = %c0 to %c10 step %c1 iter_args(%arg3 = %arg0, %arg4 = %[[v0]]) -> (f32, tensor<10xf32>) {
// CHECK-NEXT: %[[cache:.+]] = tensor.insert %arg3 into %arg4[%arg2] : tensor<10xf32>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should likely prefer using a memref alloca of the correct size, store in fwd, load in reverse, and free after the for

Copy link
Collaborator Author

@Pangoraw Pangoraw Jun 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it will create a cache<memref> for the parent op so the alloca has to be hoisted all the way to the top (and would be invalid for upcoming autodiff deferred). IIRC, that's why I went with an immutable data structure at the time.

We can write a bufferization pass that converts this to memref once all push/pop have been removed ?

@wsmoses
Copy link
Member

wsmoses commented Jun 26, 2025

@Pangoraw I'm going to merge for now, can you address in post commit follow up

@wsmoses wsmoses merged commit 042e567 into EnzymeAD:main Jun 26, 2025
18 of 23 checks passed
gbaraldi pushed a commit to gbaraldi/Enzyme that referenced this pull request Jun 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants