2020-10-02 09:59:58 +08:00
|
|
|
//===- func_builder.cpp ---------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file is licensed under a pytorch-style license
|
|
|
|
// See frontends/pytorch/LICENSE for license information.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "func_builder.h"
|
|
|
|
|
2021-02-02 09:59:42 +08:00
|
|
|
#include "op_builder.h"
|
|
|
|
|
2020-12-12 06:43:38 +08:00
|
|
|
#include "mlir-c/BuiltinAttributes.h"
|
|
|
|
#include "mlir-c/BuiltinTypes.h"
|
2020-11-21 09:03:23 +08:00
|
|
|
#include "mlir-c/Diagnostics.h"
|
2020-10-02 09:59:58 +08:00
|
|
|
#include "npcomp-c/Types.h"
|
|
|
|
|
|
|
|
using namespace torch_mlir;
|
|
|
|
|
|
|
|
std::unique_ptr<FuncBuilder>
|
2020-11-21 09:03:23 +08:00
|
|
|
FuncBuilder::createFunction(FuncBuilder::Inserter &inserter,
|
2020-12-15 00:42:42 +08:00
|
|
|
MlirLocation location, const std::string &name,
|
|
|
|
std::vector<MlirType> &inputTypes) {
|
2020-11-21 09:03:23 +08:00
|
|
|
auto context = mlirLocationGetContext(location);
|
2020-10-02 09:59:58 +08:00
|
|
|
// TODO: Create a dedicated API upstream for creating/manipulating func ops.
|
|
|
|
// (this is fragile and reveals details that are not guaranteed).
|
2020-12-15 00:42:42 +08:00
|
|
|
std::vector<MlirNamedAttribute> funcAttrs;
|
2020-12-15 06:30:51 +08:00
|
|
|
funcAttrs.push_back(toMlirNamedAttribute(
|
|
|
|
"type", mlirTypeAttrGet(mlirFunctionTypeGet(
|
|
|
|
context, inputTypes.size(), inputTypes.data(),
|
|
|
|
/*numResults=*/0, /*results=*/nullptr))));
|
|
|
|
funcAttrs.push_back(toMlirNamedAttribute(
|
|
|
|
"sym_name", mlirStringAttrGet(
|
|
|
|
context, mlirStringRefCreate(name.data(), name.size()))));
|
2020-10-02 09:59:58 +08:00
|
|
|
|
2020-11-24 11:20:26 +08:00
|
|
|
MlirOperationState state =
|
|
|
|
mlirOperationStateGet(toMlirStringRef("func"), location);
|
2020-10-02 09:59:58 +08:00
|
|
|
mlirOperationStateAddAttributes(&state, funcAttrs.size(), funcAttrs.data());
|
|
|
|
{
|
|
|
|
// Don't access these once ownership transferred.
|
|
|
|
MlirRegion newBodyRegion = mlirRegionCreate();
|
|
|
|
MlirBlock newEntryBlock =
|
|
|
|
mlirBlockCreate(inputTypes.size(), inputTypes.data());
|
|
|
|
mlirRegionInsertOwnedBlockAfter(newBodyRegion, {nullptr}, newEntryBlock);
|
|
|
|
mlirOperationStateAddOwnedRegions(&state, 1, &newBodyRegion);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Need to re-lookup the region/block because we relinquished ownership above.
|
|
|
|
MlirOperation funcOp = mlirOperationCreate(&state);
|
|
|
|
MlirRegion bodyRegion = mlirOperationGetRegion(funcOp, 0);
|
|
|
|
MlirBlock entryBlock = mlirRegionGetFirstBlock(bodyRegion);
|
|
|
|
|
2020-11-21 09:03:23 +08:00
|
|
|
inserter(funcOp);
|
2020-10-02 09:59:58 +08:00
|
|
|
return std::unique_ptr<FuncBuilder>(new FuncBuilder(
|
2020-10-06 14:21:21 +08:00
|
|
|
context, funcOp, BlockBuilder(entryBlock, /*returnOp=*/{nullptr}, true)));
|
|
|
|
}
|
|
|
|
|
2020-12-15 00:42:42 +08:00
|
|
|
void FuncBuilder::rewriteFuncReturnTypes(std::vector<MlirType> &resultTypes) {
|
2020-10-06 14:21:21 +08:00
|
|
|
// Get inputs from current function type.
|
2020-11-24 11:20:26 +08:00
|
|
|
MlirAttribute funcTypeAttr =
|
|
|
|
mlirOperationGetAttributeByName(funcOp, toMlirStringRef("type"));
|
2020-10-06 14:21:21 +08:00
|
|
|
assert(!mlirAttributeIsNull(funcTypeAttr) &&
|
|
|
|
"function missing 'type' attribute");
|
|
|
|
assert(mlirAttributeIsAType(funcTypeAttr) &&
|
|
|
|
"function type is not a TypeAttr");
|
|
|
|
MlirType funcType = mlirTypeAttrGetValue(funcTypeAttr);
|
2020-12-15 00:42:42 +08:00
|
|
|
std::vector<MlirType> inputTypes;
|
2020-10-06 14:21:21 +08:00
|
|
|
for (intptr_t i = 0, e = mlirFunctionTypeGetNumInputs(funcType); i < e; ++i) {
|
|
|
|
inputTypes.push_back(mlirFunctionTypeGetInput(funcType, i));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make new function type.
|
|
|
|
MlirType newFuncType =
|
|
|
|
mlirFunctionTypeGet(context, inputTypes.size(), inputTypes.data(),
|
|
|
|
resultTypes.size(), resultTypes.data());
|
|
|
|
MlirAttribute newFuncTypeAttr = mlirTypeAttrGet(newFuncType);
|
2020-11-24 11:20:26 +08:00
|
|
|
mlirOperationSetAttributeByName(funcOp, toMlirStringRef("type"),
|
|
|
|
newFuncTypeAttr);
|
2020-10-06 14:21:21 +08:00
|
|
|
(void)newFuncTypeAttr;
|
|
|
|
}
|
|
|
|
|
|
|
|
MlirValue FuncBuilder::insertConstantOp(MlirOperation op) {
|
|
|
|
mlirBlockInsertOwnedOperationAfter(entryBlock.getBlock(), prevConstantOp, op);
|
|
|
|
prevConstantOp = op;
|
|
|
|
return mlirOperationGetResult(op, 0);
|
2020-10-02 09:59:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
MlirValue FuncBuilder::lookupTensor(at::Tensor tensor) {
|
|
|
|
for (auto it = tensorValueMap.rbegin(), e = tensorValueMap.rend(); it != e;
|
|
|
|
++it) {
|
|
|
|
if (it->first.is_same(tensor))
|
|
|
|
return it->second;
|
|
|
|
}
|
|
|
|
return {nullptr};
|
|
|
|
}
|
2020-10-06 14:21:21 +08:00
|
|
|
|
|
|
|
MlirValue FuncBuilder::getScalarConstant(MlirLocation loc, at::Scalar s) {
|
|
|
|
// Note that interpreter "scalars" match the Python semantics and are
|
|
|
|
// represented as one of double or int64_t, with a special tag for whether
|
|
|
|
// it should be interpreted as a bool.
|
|
|
|
if (s.isIntegral(/*includeBool=*/false)) {
|
|
|
|
// TODO: Switch to a basicpy.constant that works properly with signed
|
|
|
|
// integers and then switch this to a signed integer.
|
|
|
|
MlirType t = mlirIntegerTypeGet(context, 64);
|
2020-10-16 09:28:30 +08:00
|
|
|
MlirAttribute value = mlirIntegerAttrGet(t, s.to<int64_t>());
|
|
|
|
return getGeneralConstant(loc, value);
|
2020-10-06 14:21:21 +08:00
|
|
|
}
|
|
|
|
if (s.isFloatingPoint()) {
|
|
|
|
MlirType t = mlirF64TypeGet(context);
|
2020-10-16 09:28:30 +08:00
|
|
|
MlirAttribute value = mlirFloatAttrDoubleGet(context, t, s.to<double>());
|
|
|
|
return getGeneralConstant(loc, value);
|
|
|
|
}
|
|
|
|
if (s.isBoolean()) {
|
|
|
|
return getBoolConstant(loc, s.to<bool>());
|
2020-10-06 14:21:21 +08:00
|
|
|
}
|
|
|
|
// TODO: s.isComplex()
|
|
|
|
|
|
|
|
throw std::invalid_argument("TODO: Scalar of unknown kind");
|
|
|
|
}
|
2020-10-16 09:28:30 +08:00
|
|
|
|
|
|
|
MlirValue FuncBuilder::getBoolConstant(MlirLocation loc, bool v) {
|
2021-02-02 09:59:42 +08:00
|
|
|
return insertConstantOp(OpBuilder(context).createBoolConstant(loc, v));
|
2020-10-16 09:28:30 +08:00
|
|
|
}
|
|
|
|
|
2020-10-17 08:38:07 +08:00
|
|
|
MlirValue FuncBuilder::getNoneConstant(MlirLocation loc) {
|
2021-02-02 09:59:42 +08:00
|
|
|
return insertConstantOp(OpBuilder(context).createNoneConstant(loc));
|
2020-10-17 08:38:07 +08:00
|
|
|
}
|
|
|
|
|
2020-10-16 09:28:30 +08:00
|
|
|
MlirValue FuncBuilder::getGeneralConstant(MlirLocation loc,
|
|
|
|
MlirAttribute value) {
|
2021-02-02 09:59:42 +08:00
|
|
|
return insertConstantOp(OpBuilder(context).createStdConstant(loc, value));
|
2020-10-16 09:28:30 +08:00
|
|
|
}
|
2020-10-17 08:38:07 +08:00
|
|
|
|
Add a number of kernels and new patterns.
* convolution, convolution_backward, _log_softmax, _log_softmax_backward_data, nll_loss_forward, nll_loss_backward, nll_loss2d_forward, nll_loss2d_backward, copy_
* Extends the recognition logic and metadata for handling inplace transformations, optional tensors, ints, lists and dropped args.
* The kernel_calls generated by test_conv_nllloss_grads.py now convert to ATen.
* The result *almost* comes out as a pure tensor program with the exception of the copy_ op, which I will do some followup work to deal with.
* More progress on #97
2020-11-04 11:24:28 +08:00
|
|
|
MlirValue FuncBuilder::buildList(MlirLocation loc,
|
2020-12-15 00:42:42 +08:00
|
|
|
std::vector<MlirValue> &elements) {
|
2020-10-17 08:38:07 +08:00
|
|
|
MlirType resultType = npcompListTypeGet(context);
|
|
|
|
OperationStateHolder state{"basicpy.build_list", loc};
|
|
|
|
mlirOperationStateAddResults(state, 1, &resultType);
|
|
|
|
mlirOperationStateAddOperands(state, elements.size(), elements.data());
|
|
|
|
MlirOperation op = state.createOperation();
|
2020-11-03 07:30:21 +08:00
|
|
|
entryBlock.insertBeforeTerminator(op);
|
|
|
|
return mlirOperationGetResult(op, 0);
|
2020-10-17 08:38:07 +08:00
|
|
|
}
|