-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[MLIR] Mark
debug.callbacks
as inactive functions. (#706)
**Context:** `debug.callbacks` will always be inactive functions when taking the gradient of them. **Description of the Change:** Change `pyregistry` to `inactive_callback` and mark all `inactive_callback`s as inactive. **Benefits:** Inactive callbacks are inactive. Notes: * Future PRs will make active_callbacks through specialization and will call inactive_callback as a primitive. * Future PR will re-enable callbacks in gradients. [sc-60496]
- Loading branch information
1 parent
715523e
commit 2636257
Showing
14 changed files
with
175 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// Copyright 2024 Xanadu Quantum Technologies Inc. | ||
|
||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
|
||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
#pragma once | ||
|
||
namespace catalyst { | ||
namespace gradient { | ||
|
||
static constexpr const char *enzyme_autodiff_func_name = "__enzyme_autodiff"; | ||
static constexpr const char *enzyme_allocation_key = "__enzyme_allocation_like"; | ||
static constexpr const char *enzyme_custom_gradient_key = "__enzyme_register_gradient_"; | ||
static constexpr const char *enzyme_like_free_key = "__enzyme_function_like_free"; | ||
static constexpr const char *enzyme_const_key = "enzyme_const"; | ||
static constexpr const char *enzyme_dupnoneed_key = "enzyme_dupnoneed"; | ||
static constexpr const char *enzyme_inactivefn_key = "__enzyme_inactivefn"; | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
mlir/lib/Catalyst/Transforms/RegisterInactiveCallbackPass.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// Copyright 2024 Xanadu Quantum Technologies Inc. | ||
|
||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
|
||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#include "mlir/Dialect/LLVMIR/LLVMDialect.h" | ||
#include "mlir/Transforms/GreedyPatternRewriteDriver.h" | ||
|
||
#include "Catalyst/Transforms/Passes.h" | ||
#include "Catalyst/Transforms/Patterns.h" | ||
#include "Gradient/Transforms/EnzymeConstants.h" | ||
|
||
using namespace mlir; | ||
|
||
namespace catalyst { | ||
|
||
#define GEN_PASS_DEF_REGISTERINACTIVECALLBACKPASS | ||
#define GEN_PASS_DECL_REGISTERINACTIVECALLBACKPASS | ||
#include "Catalyst/Transforms/Passes.h.inc" | ||
|
||
struct RegisterInactiveCallbackPass | ||
: impl::RegisterInactiveCallbackPassBase<RegisterInactiveCallbackPass> { | ||
using RegisterInactiveCallbackPassBase::RegisterInactiveCallbackPassBase; | ||
void runOnOperation() final | ||
{ | ||
auto mod = getOperation(); | ||
StringRef inactive_callbackFnName = "inactive_callback"; | ||
auto fnDecl = mod.lookupSymbol<LLVM::LLVMFuncOp>(inactive_callbackFnName); | ||
if (!fnDecl) { | ||
return; | ||
} | ||
MLIRContext *context = &getContext(); | ||
auto builder = OpBuilder(context); | ||
builder.setInsertionPointToStart(mod.getBody()); | ||
auto ptrTy = LLVM::LLVMPointerType::get(context); | ||
auto arrTy = LLVM::LLVMArrayType::get(ptrTy, 1); | ||
auto loc = mod.getLoc(); | ||
auto isConstant = false; | ||
auto linkage = LLVM::Linkage::External; | ||
auto key = catalyst::gradient::enzyme_inactivefn_key; | ||
auto glb = builder.create<LLVM::GlobalOp>(loc, arrTy, isConstant, linkage, key, nullptr); | ||
// Create a block and push it to the global | ||
Block *block = new Block(); | ||
glb.getInitializerRegion().push_back(block); | ||
builder.setInsertionPointToStart(block); | ||
auto undef = builder.create<LLVM::UndefOp>(glb.getLoc(), arrTy); | ||
auto fnSym = SymbolRefAttr::get(context, inactive_callbackFnName); | ||
auto fnPtr = builder.create<LLVM::AddressOfOp>(glb.getLoc(), ptrTy, fnSym); | ||
auto filledInArray = builder.create<LLVM::InsertValueOp>(glb.getLoc(), undef, fnPtr, 0); | ||
builder.create<LLVM::ReturnOp>(glb.getLoc(), filledInArray); | ||
} | ||
}; | ||
|
||
std::unique_ptr<Pass> createRegisterInactiveCallbackPass() | ||
{ | ||
return std::make_unique<RegisterInactiveCallbackPass>(); | ||
} | ||
} // namespace catalyst |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// Copyright 2024 Xanadu Quantum Technologies Inc. | ||
|
||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
|
||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
// RUN: quantum-opt %s --register-inactive-callback --split-input-file --verify-diagnostics | FileCheck %s | ||
|
||
// This test just makes sure that we can | ||
// run the compiler with the option | ||
// | ||
// --register-inactive-callback | ||
// | ||
// and that if there are no callbacks present | ||
// it doesn't change anything | ||
|
||
// CHECK-LABEL: @test0 | ||
module @test0 { | ||
// CHECK-NOT: llvm.mlir.global external @__enzyme_inactivefn | ||
// CHECK-LABEL: @foo | ||
func.func @foo() { | ||
return | ||
} | ||
// CHECK-NOT: llvm.mlir.global external @__enzyme_inactivefn | ||
} | ||
|
||
// ----- | ||
|
||
// This test checks the invariant that after the transformation | ||
// the attribute has been removed. | ||
|
||
// CHECK-LABEL: @test1 | ||
module @test1 { | ||
|
||
// CHECK: llvm.mlir.global external @__enzyme_inactivefn | ||
// CHECK: [[undef:%.+]] = llvm.mlir.undef | ||
// CHECK: [[ptr:%.+]] = llvm.mlir.addressof @inactive_callback | ||
// CHECK: [[retval:%.+]] = llvm.insertvalue [[ptr]], [[undef]][0] | ||
// CHECK: llvm.return [[retval]] | ||
|
||
llvm.func @inactive_callback(i64, i64, i64, ...) | ||
llvm.func @wrapper() { | ||
%0 = llvm.mlir.constant(139935726668624 : i64) : i64 | ||
%1 = llvm.mlir.constant(0 : i64) : i64 | ||
llvm.call @inactive_callback(%0, %1, %1) vararg(!llvm.func<void (i64, i64, i64, ...)>) : (i64, i64, i64) -> () | ||
llvm.return | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters