2020-09-29 03:02:35 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "npcomp/Dialect/Torch/IR/TorchOps.h"
|
|
|
|
|
2020-09-30 05:17:34 +08:00
|
|
|
#include "mlir/IR/Builders.h"
|
2021-01-28 08:35:44 +08:00
|
|
|
#include "mlir/IR/BuiltinOps.h"
|
2020-09-30 05:17:34 +08:00
|
|
|
#include "npcomp/Dialect/Basicpy/IR/BasicpyDialect.h"
|
|
|
|
#include "npcomp/Dialect/Basicpy/IR/BasicpyOps.h"
|
|
|
|
#include "npcomp/Dialect/Numpy/IR/NumpyDialect.h"
|
|
|
|
#include "npcomp/Dialect/Numpy/IR/NumpyOps.h"
|
2021-02-18 03:28:51 +08:00
|
|
|
#include "llvm/ADT/StringMap.h"
|
2020-09-30 05:17:34 +08:00
|
|
|
|
2020-09-29 03:02:35 +08:00
|
|
|
using namespace mlir;
|
|
|
|
using namespace mlir::NPCOMP;
|
2021-01-28 08:35:44 +08:00
|
|
|
using namespace mlir::NPCOMP::Torch;
|
2020-10-23 14:31:34 +08:00
|
|
|
|
|
|
|
static SmallVector<StringRef, 4> strArrayAttrToVector(ArrayAttr array) {
|
|
|
|
SmallVector<StringRef, 4> strings;
|
|
|
|
strings.reserve(array.size());
|
|
|
|
for (auto stringAttr : array) {
|
|
|
|
strings.push_back(stringAttr.cast<StringAttr>().getValue());
|
|
|
|
}
|
|
|
|
return strings;
|
|
|
|
}
|
|
|
|
|
2021-01-28 08:35:44 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// KernelCallOp
|
|
|
|
//===----------------------------------------------------------------------===//
|
2020-10-23 14:31:34 +08:00
|
|
|
|
2021-01-28 08:35:44 +08:00
|
|
|
KernelMetadata KernelCallOp::getTorchKernelMetadata() {
|
|
|
|
return KernelMetadata{
|
2020-10-23 14:31:34 +08:00
|
|
|
.kernelName = kernelName(),
|
|
|
|
.isVararg = sigIsVararg(),
|
|
|
|
.isVarret = sigIsVarret(),
|
|
|
|
.argTypes = strArrayAttrToVector(sigArgTypes()),
|
|
|
|
.returnTypes = strArrayAttrToVector(sigRetTypes()),
|
|
|
|
};
|
|
|
|
}
|
2021-01-28 08:35:44 +08:00
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// MethodOp
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
LogicalResult MethodOp::verifySymbolUses(SymbolTableCollection &symbolTable) {
|
|
|
|
auto func = symbolTable.lookupNearestSymbolFrom<FuncOp>(*this, function());
|
|
|
|
if (!func)
|
2021-02-18 03:28:51 +08:00
|
|
|
return emitError() << "'@" << function()
|
2021-01-28 08:35:44 +08:00
|
|
|
<< "' does not reference a valid function";
|
2021-02-18 03:28:51 +08:00
|
|
|
if (func.getVisibility() != SymbolTable::Visibility::Private)
|
|
|
|
return emitError() << "'@" << function()
|
|
|
|
<< "' must reference a private function";
|
|
|
|
if (func.isDeclaration())
|
|
|
|
return emitError() << "'@" << function()
|
|
|
|
<< "' must reference a function that is defined (not "
|
|
|
|
"merely declared)";
|
|
|
|
auto expectedReceiverArgType = NnModuleType::get(
|
|
|
|
getContext(), getOperation()->getParentOfType<ClassTypeOp>().getName());
|
|
|
|
if (func.getType().getNumInputs() == 0 ||
|
|
|
|
func.getType().getInput(0) != expectedReceiverArgType) {
|
|
|
|
return emitError() << "the referenced function '" << function()
|
|
|
|
<< "' must have a first argument of type "
|
|
|
|
<< expectedReceiverArgType;
|
|
|
|
}
|
2021-01-28 08:35:44 +08:00
|
|
|
return success();
|
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// NnModuleOp
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
static LogicalResult verify(NnModuleOp op) {
|
|
|
|
for (Operation &child : *op.getBody())
|
2021-02-18 03:28:51 +08:00
|
|
|
if (!isa<SlotOp, NnModuleTerminatorOp>(&child))
|
|
|
|
return child.emitOpError() << "is not allowed inside 'torch.nn_module'";
|
|
|
|
return success();
|
|
|
|
}
|
|
|
|
|
|
|
|
// PyTorch has a well-developed notion of subtyping.
|
|
|
|
//
|
|
|
|
// This is a restricted subset of it.
|
|
|
|
//
|
|
|
|
// TODO: Flesh this out.
|
|
|
|
bool isValidSubtype(Type subtype, Type type) {
|
|
|
|
if (subtype == type)
|
|
|
|
return true;
|
|
|
|
if (auto optional = type.dyn_cast<OptionalType>())
|
|
|
|
return subtype == optional.getContainedType() ||
|
|
|
|
subtype.isa<Basicpy::NoneType>();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
LogicalResult NnModuleOp::verifySymbolUses(SymbolTableCollection &symbolTable) {
|
|
|
|
auto classType =
|
|
|
|
symbolTable.lookupNearestSymbolFrom<ClassTypeOp>(*this, getClassName());
|
|
|
|
if (!classType)
|
|
|
|
return emitError() << "'" << getClassName()
|
|
|
|
<< "' does not reference a valid class type";
|
|
|
|
|
|
|
|
auto attrs = llvm::to_vector<6>(getBody()->getOps<SlotOp>());
|
|
|
|
auto attrDefs = llvm::to_vector<6>(classType.getBody()->getOps<AttrOp>());
|
|
|
|
if (attrs.size() != attrDefs.size())
|
|
|
|
return emitError() << "number of 'torch.slot's in a 'torch.nn_module' must "
|
|
|
|
"match number of 'torch.attr's in "
|
|
|
|
"the corresponding 'torch.class_type'";
|
|
|
|
for (int i = 0, e = attrs.size(); i != e; i++) {
|
|
|
|
SlotOp attr = attrs[i];
|
|
|
|
AttrOp attrDef = attrDefs[i];
|
|
|
|
if (!isValidSubtype(attr.value().getType(), attrDef.type()) ||
|
|
|
|
attr.name() != attrDef.name()) {
|
|
|
|
return attr.emitOpError()
|
|
|
|
.append("is expected to match type and name of '",
|
|
|
|
attrDef.getOperation(), "'")
|
|
|
|
.attachNote(attrDef.getLoc())
|
|
|
|
.append("see torch.attr at corresponding index ", i, " here");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return success();
|
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// ClassTypeOp
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
static LogicalResult verify(ClassTypeOp op) {
|
|
|
|
llvm::StringMap<Operation *> namesToOps;
|
|
|
|
for (Operation &child : op.getBody()->without_terminator()) {
|
|
|
|
if (!isa<AttrOp, MethodOp>(&child))
|
|
|
|
return child.emitOpError() << "is not allowed inside `torch.class_type`";
|
|
|
|
StringRef name;
|
|
|
|
if (auto attr = dyn_cast<AttrOp>(child))
|
|
|
|
name = attr.name();
|
|
|
|
else
|
|
|
|
name = cast<MethodOp>(child).name();
|
|
|
|
auto itAndWasInserted = namesToOps.insert({name, &child});
|
|
|
|
auto it = itAndWasInserted.first;
|
|
|
|
bool wasInserted = itAndWasInserted.second;
|
|
|
|
if (!wasInserted) {
|
|
|
|
auto diag = op.emitOpError().append(
|
|
|
|
"has duplicate attr/method with name '", name, "'");
|
|
|
|
diag.attachNote(it->second->getLoc())
|
|
|
|
.append("see first conflicting attr/method here");
|
|
|
|
diag.attachNote(child.getLoc())
|
|
|
|
.append("see second conflicting attr/method here");
|
|
|
|
return failure();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-28 08:35:44 +08:00
|
|
|
return success();
|
|
|
|
}
|
|
|
|
|
2021-03-02 07:00:32 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// PrimLoopOp
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
OperandRange PrimLoopOp::getSuccessorEntryOperands(unsigned index) {
|
|
|
|
assert(index == 0);
|
|
|
|
return iterArgsInit();
|
|
|
|
}
|
|
|
|
|
|
|
|
void PrimLoopOp::getSuccessorRegions(
|
|
|
|
Optional<unsigned> index, ArrayRef<Attribute> operands,
|
|
|
|
SmallVectorImpl<RegionSuccessor> ®ions) {
|
|
|
|
(void)operands;
|
|
|
|
|
|
|
|
if (!index.hasValue()) {
|
|
|
|
regions.emplace_back(®ion(), region().getArguments().slice(1));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
assert(*index == 0);
|
|
|
|
regions.emplace_back(®ion(), region().getArguments().slice(1));
|
|
|
|
regions.emplace_back(getResults());
|
|
|
|
}
|
|
|
|
|
2021-01-28 08:35:44 +08:00
|
|
|
#define GET_OP_CLASSES
|
|
|
|
#include "npcomp/Dialect/Torch/IR/TorchOps.cpp.inc"
|