-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathBufferizableOpInterfaceImpl.cpp
More file actions
345 lines (302 loc) · 14.4 KB
/
BufferizableOpInterfaceImpl.cpp
File metadata and controls
345 lines (302 loc) · 14.4 KB
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
// Copyright 2024-2025 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/Bufferization/IR/BufferizableOpInterface.h"
#include "mlir/Dialect/Bufferization/IR/Bufferization.h"
#include "mlir/Dialect/LLVMIR/LLVMDialect.h"
#include "mlir/Dialect/MemRef/IR/MemRef.h"
#include "mlir/Transforms/DialectConversion.h"
#include "Catalyst/IR/CatalystOps.h"
#include "Catalyst/Transforms/BufferizableOpInterfaceImpl.h"
using namespace mlir;
using namespace catalyst;
/**
* Implementation of the BufferizableOpInterface for use with one-shot bufferization.
* For more information on the interface, refer to the documentation below:
* https://mlir.llvm.org/docs/Bufferization/#extending-one-shot-bufferize
* https://github.com/llvm/llvm-project/blob/main/mlir/include/mlir/Dialect/Bufferization/IR/BufferizableOpInterface.td#L14
*/
namespace {
/// Bufferization of catalyst.print. Get memref of printOp.val.
struct PrintOpInterface
: public bufferization::BufferizableOpInterface::ExternalModel<PrintOpInterface, PrintOp> {
bool bufferizesToMemoryRead(Operation *op, OpOperand &opOperand,
const bufferization::AnalysisState &state) const
{
return true;
}
bool bufferizesToMemoryWrite(Operation *op, OpOperand &opOperand,
const bufferization::AnalysisState &state) const
{
return false;
}
bufferization::AliasingValueList
getAliasingValues(Operation *op, OpOperand &opOperand,
const bufferization::AnalysisState &state) const
{
return {};
}
LogicalResult bufferize(Operation *op, RewriterBase &rewriter,
const bufferization::BufferizationOptions &options,
bufferization::BufferizationState &state) const
{
auto printOp = cast<PrintOp>(op);
if (printOp.getVal()) {
FailureOr<Value> source = getBuffer(rewriter, printOp.getVal(), options, state);
if (failed(source)) {
return failure();
}
bufferization::replaceOpWithNewBufferizedOp<PrintOp>(
rewriter, op, *source, printOp.getConstValAttr(), printOp.getPrintDescriptorAttr());
}
return success();
}
};
/// Bufferization of catalyst.custom_call. Mainly get buffers for arguments.
struct CustomCallOpInterface
: public bufferization::BufferizableOpInterface::ExternalModel<CustomCallOpInterface,
CustomCallOp> {
bool bufferizesToAllocation(Operation *op, Value value) const { return true; }
bool bufferizesToMemoryRead(Operation *op, OpOperand &opOperand,
const bufferization::AnalysisState &state) const
{
// Custom Call Op always reads the operand memory no matter what.
return true;
}
bool bufferizesToMemoryWrite(Operation *op, OpOperand &opOperand,
const bufferization::AnalysisState &state) const
{
// We only use custom call for the jax lapack kernels.
// This is actually hard-guarded: in the lowering pattern for custom call
// we check that the name of the callee is a jax symbol for a lapack kernel.
//
// The lapack kernels themselves might overwrite some of the input arrays.
// However, in jax's shim wrapper layer, a memcpy is already performed.
// See
// https://github.com/PennyLaneAI/catalyst/blob/main/frontend/catalyst/utils/jax_cpu_lapack_kernels/lapack_kernels.cpp
//
// The arguments to the underlying lapack kernel are denoted by the jax wrapper
// function as `data`. The `data` args already contain the output array that
// the lapack kernel is supposed to write into. The other input arrays are all marked const.
// Jax then purifies the function by adding a new argument `out` to hold the
// output array.
//
// In other words, the jax wrappers we call here with custom call op
// are already pure, and we won't have side effects on the input tensors.
return false;
}
bufferization::AliasingValueList
getAliasingValues(Operation *op, OpOperand &opOperand,
const bufferization::AnalysisState &state) const
{
return {};
}
LogicalResult bufferize(Operation *op, RewriterBase &rewriter,
const bufferization::BufferizationOptions &options,
bufferization::BufferizationState &state) const
{
auto customCallOp = cast<CustomCallOp>(op);
// Add bufferized arguments
SmallVector<Value> bufferArgs;
ValueRange operands = customCallOp.getOperands();
for (Value operand : operands) {
FailureOr<Value> opBuffer = getBuffer(rewriter, operand, options, state);
if (failed(opBuffer)) {
return failure();
}
// If any of the input buffers have non-identity layout, we need to make a copy
// This is because the lapack kernel runtime demands contiguous arrays,
// aka identity, non-strided, non-offset memory layout
//
// TODO: reuse this copy for the result buffer whenever the semantics of the kernel
// allow this reuse.
// The kernel runtime performs a copy from the input array to the output array
// if they are separate memrefs. We can avoid the extra copy there by just passing in
// the newly allocated memref here into the kernel as the result buffer too.
// However, we need to check that the kernels' semantics allow this.
MemRefType bufferedOperandMemrefType = cast<MemRefType>(opBuffer->getType());
if (!bufferedOperandMemrefType.getLayout().isIdentity()) {
MemRefType copiedOperandMemrefType =
MemRefType::get(bufferedOperandMemrefType.getShape(),
bufferedOperandMemrefType.getElementType());
auto allocOp =
rewriter.create<memref::AllocOp>(op->getLoc(), copiedOperandMemrefType);
auto copyOp =
rewriter.create<memref::CopyOp>(op->getLoc(), *opBuffer, allocOp.getResult());
bufferArgs.push_back(copyOp.getTarget());
}
else {
bufferArgs.push_back(*opBuffer);
}
}
// Add bufferized return values to the arguments
ValueRange results = customCallOp.getResults();
for (Value result : results) {
Type resultType = result.getType();
RankedTensorType tensorType = dyn_cast<RankedTensorType>(resultType);
if (!tensorType) {
return failure();
}
auto options = bufferization::BufferizationOptions();
FailureOr<Value> tensorAlloc = bufferization::allocateTensorForShapedValue(
rewriter, op->getLoc(), result, options, state, false);
MemRefType memrefType =
MemRefType::get(tensorType.getShape(), tensorType.getElementType());
auto newBuffer =
rewriter.create<bufferization::ToBufferOp>(op->getLoc(), memrefType, *tensorAlloc);
bufferArgs.push_back(newBuffer);
}
// Add the initial number of arguments
int32_t numArguments = static_cast<int32_t>(customCallOp.getNumOperands());
DenseI32ArrayAttr numArgumentsDenseAttr = rewriter.getDenseI32ArrayAttr({numArguments});
// Create an updated custom call operation
rewriter.create<CustomCallOp>(op->getLoc(), TypeRange{}, bufferArgs,
customCallOp.getCallTargetName(), numArgumentsDenseAttr);
size_t startIndex = bufferArgs.size() - customCallOp.getNumResults();
SmallVector<Value> bufferResults(bufferArgs.begin() + startIndex, bufferArgs.end());
bufferization::replaceOpWithBufferizedValues(rewriter, op, bufferResults);
return success();
}
};
struct CallbackOpInterface
: public bufferization::BufferizableOpInterface::ExternalModel<CallbackOpInterface,
CallbackOp> {
bool hasTensorSemantics(Operation *op) const
{
auto isaTensor = llvm::IsaPred<TensorType>;
// A function has tensor semantics if it has tensor arguments/results.
auto callbackOp = cast<CallbackOp>(op);
bool hasTensorArg = any_of(callbackOp.getArgumentTypes(), isaTensor);
bool hasTensorResult = any_of(callbackOp.getResultTypes(), isaTensor);
if (hasTensorArg || hasTensorResult) {
return true;
}
return false;
}
LogicalResult bufferize(Operation *op, RewriterBase &rewriter,
const bufferization::BufferizationOptions &options,
bufferization::BufferizationState &state) const
{
auto callbackOp = cast<CallbackOp>(op);
auto argTys = callbackOp.getArgumentTypes();
auto retTys = callbackOp.getResultTypes();
SmallVector<Type> emptyRets;
SmallVector<Type> args(argTys.begin(), argTys.end());
args.insert(args.end(), retTys.begin(), retTys.end());
SmallVector<Type> bufferArgs;
for (Type ty : args) {
auto tensorType = dyn_cast<RankedTensorType>(ty);
if (!tensorType) {
bufferArgs.push_back(ty);
}
else {
bufferArgs.push_back(
MemRefType::get(tensorType.getShape(), tensorType.getElementType()));
}
}
auto callbackTy = rewriter.getFunctionType(bufferArgs, emptyRets);
rewriter.modifyOpInPlace(op, [&] { callbackOp.setFunctionType(callbackTy); });
return success();
}
};
void convertTypes(SmallVector<Type> inTypes, SmallVector<Type> &convertedResults)
{
// See https://github.com/llvm/llvm-project/pull/114155/files
for (Type inType : inTypes) {
if (isa<TensorType>(inType)) {
convertedResults.push_back(
bufferization::getMemRefTypeWithStaticIdentityLayout(cast<TensorType>(inType)));
}
else {
convertedResults.push_back(inType);
}
}
}
struct CallbackCallOpInterface
: public bufferization::BufferizableOpInterface::ExternalModel<CallbackCallOpInterface,
CallbackCallOp> {
bool bufferizesToAllocation(Operation *op, Value value) const { return true; }
bool bufferizesToMemoryRead(Operation *op, OpOperand &opOperand,
const bufferization::AnalysisState &state) const
{
return true;
}
bool bufferizesToMemoryWrite(Operation *op, OpOperand &opOperand,
const bufferization::AnalysisState &state) const
{
// We can safely say false because CallbackCallOp's memrefs
// will be put in a JAX array and JAX arrays are immutable.
//
// Unlike NumPy arrays, JAX arrays are always immutable.
//
// https://jax.readthedocs.io/en/latest/notebooks/thinking_in_jax.html
return false;
}
bufferization::AliasingValueList
getAliasingValues(Operation *op, OpOperand &opOperand,
const bufferization::AnalysisState &state) const
{
return {};
}
LogicalResult bufferize(Operation *op, RewriterBase &rewriter,
const bufferization::BufferizationOptions &options,
bufferization::BufferizationState &state) const
{
auto callOp = cast<CallbackCallOp>(op);
SmallVector<Type> convertedResults;
convertTypes(SmallVector<Type>(callOp.getResultTypes()), convertedResults);
if (callOp->getNumResults() != convertedResults.size()) {
return failure();
}
SmallVector<Value> newInputs;
auto operands = callOp.getOperands();
for (Value operand : operands) {
FailureOr<Value> opBuffer = getBuffer(rewriter, operand, options, state);
if (failed(opBuffer)) {
return failure();
}
newInputs.push_back(*opBuffer);
}
auto results = callOp.getResults();
auto loc = callOp->getLoc();
SmallVector<Value> outmemrefs;
for (auto result : results) {
FailureOr<Value> tensorAlloc = bufferization::allocateTensorForShapedValue(
rewriter, loc, result, options, state, false);
if (failed(tensorAlloc)) {
return failure();
}
auto tensor = *tensorAlloc;
RankedTensorType tensorTy = cast<RankedTensorType>(tensor.getType());
auto shape = tensorTy.getShape();
auto elementTy = tensorTy.getElementType();
auto memrefType = MemRefType::get(shape, elementTy);
auto toBufferOp = rewriter.create<bufferization::ToBufferOp>(loc, memrefType, tensor);
auto memref = toBufferOp.getResult();
outmemrefs.push_back(memref);
newInputs.push_back(memref);
}
SmallVector<Type> emptyRets;
rewriter.create<CallbackCallOp>(loc, emptyRets, callOp.getCallee(), newInputs,
/*arg_attrs=*/nullptr, /*res_attrs=*/nullptr);
bufferization::replaceOpWithBufferizedValues(rewriter, op, outmemrefs);
return success();
}
};
} // namespace
void catalyst::registerBufferizableOpInterfaceExternalModels(DialectRegistry ®istry)
{
registry.addExtension(+[](MLIRContext *ctx, CatalystDialect *dialect) {
CustomCallOp::attachInterface<CustomCallOpInterface>(*ctx);
PrintOp::attachInterface<PrintOpInterface>(*ctx);
CallbackOp::attachInterface<CallbackOpInterface>(*ctx);
CallbackCallOp::attachInterface<CallbackCallOpInterface>(*ctx);
});
}