2021-02-20 08:21:21 +08:00
|
|
|
//===- class_annotator.cpp ------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file is licensed under a pytorch-style license
|
|
|
|
// See frontends/pytorch/LICENSE for license information.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "class_annotator.h"
|
|
|
|
|
|
|
|
#include <stdexcept>
|
|
|
|
|
2021-03-31 07:11:41 +08:00
|
|
|
#include "torch/csrc/Dtype.h"
|
|
|
|
|
2021-02-20 08:21:21 +08:00
|
|
|
using namespace torch_mlir;
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Utilities
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
// Prefix every line of `s` with `linePrefix`.
|
|
|
|
static std::string indentString(const std::string &linePrefix,
|
|
|
|
const std::string &s) {
|
|
|
|
std::stringstream is(s);
|
|
|
|
std::stringstream os;
|
|
|
|
std::string line;
|
|
|
|
while (std::getline(is, line)) {
|
|
|
|
os << linePrefix << line << "\n";
|
|
|
|
}
|
|
|
|
return os.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// ClassAnnotation
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
ClassAnnotation::ClassAnnotation(c10::ClassTypePtr classType)
|
|
|
|
: classType(classType) {
|
|
|
|
attributeAnnotations.resize(classType->getAttributes().size());
|
|
|
|
methodAnnotations.resize(classType->methods().size());
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<AttributeAnnotation> &
|
|
|
|
ClassAnnotation::getAttributeAnnotations() {
|
|
|
|
// Halfhearted attempt to ensure consistency if the class type has
|
|
|
|
// been mutated.
|
|
|
|
//
|
|
|
|
// We can't easily guard against attributes being removed and
|
|
|
|
// then other attributes being added, or types changed, etc. without
|
|
|
|
// effectively mirroring the entire ClassType.
|
|
|
|
assert(attributeAnnotations.size() == classType->getAttributes().size() &&
|
|
|
|
"annotations out of sync. class has been mutated");
|
|
|
|
|
|
|
|
return attributeAnnotations;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<MethodAnnotation> &
|
|
|
|
ClassAnnotation::getMethodAnnotations() {
|
|
|
|
// Halfhearted attempt to ensure consistency if the class type has
|
|
|
|
// been mutated.
|
|
|
|
//
|
|
|
|
// We can't easily guard against methods being removed, added, or changed.
|
|
|
|
assert(methodAnnotations.size() == classType->methods().size() &&
|
|
|
|
"annotations out of sync. class has been mutated");
|
|
|
|
|
|
|
|
return methodAnnotations;
|
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// ClassAnnotator
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
static void exportNoneRecurse(ClassAnnotator &classAnnotator,
|
|
|
|
c10::ClassType *classType) {
|
|
|
|
ClassAnnotation &classAnnotation =
|
|
|
|
classAnnotator.getOrCreateClassAnnotation(classType);
|
|
|
|
for (auto &attributeAnnotation : classAnnotation.getAttributeAnnotations()) {
|
|
|
|
attributeAnnotation.isExported = false;
|
|
|
|
}
|
|
|
|
for (auto &methodAnnotation : classAnnotation.getMethodAnnotations()) {
|
|
|
|
methodAnnotation.isExported = false;
|
|
|
|
}
|
|
|
|
for (auto &classAttribute : classType->getAttributes()) {
|
|
|
|
if (auto childClassType = classAttribute.getType()->cast<c10::ClassType>()) {
|
|
|
|
exportNoneRecurse(classAnnotator, childClassType.get());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ClassAnnotator::exportNone(c10::ClassType &rootClassType) {
|
|
|
|
exportNoneRecurse(*this, &rootClassType);
|
2021-03-31 07:38:19 +08:00
|
|
|
}
|
2021-02-20 08:21:21 +08:00
|
|
|
|
2021-03-31 07:38:19 +08:00
|
|
|
void ClassAnnotator::exportPath(c10::ClassType &rootClassType,
|
|
|
|
std::vector<std::string> exportedPath) {
|
2021-02-20 08:21:21 +08:00
|
|
|
if (exportedPath.size() == 0) {
|
|
|
|
throw std::invalid_argument(
|
|
|
|
"Empty exported path. Can only export a property of a class.");
|
|
|
|
}
|
2021-03-31 07:11:41 +08:00
|
|
|
c10::ClassType *classType =
|
|
|
|
getClassAtPath(&rootClassType, c10::ArrayRef<std::string>(exportedPath)
|
|
|
|
.slice(0, exportedPath.size() - 1)
|
|
|
|
.vec());
|
2021-02-20 08:21:21 +08:00
|
|
|
|
|
|
|
if (!classType->findAttribute(exportedPath.back()) &&
|
|
|
|
!classType->findMethod(exportedPath.back())) {
|
|
|
|
std::stringstream ss;
|
|
|
|
ss << "class '" << classType->name()->qualifiedName()
|
|
|
|
<< "' does not have a method or attribute called '"
|
|
|
|
<< exportedPath.back() << "'";
|
|
|
|
throw std::invalid_argument(ss.str());
|
|
|
|
}
|
|
|
|
ClassAnnotation &classAnnotation = getOrCreateClassAnnotation(classType);
|
|
|
|
std::vector<AttributeAnnotation> &attributeAnnotations =
|
|
|
|
classAnnotation.getAttributeAnnotations();
|
|
|
|
const std::vector<c10::ClassAttribute> &classAttributes =
|
|
|
|
classType->getAttributes();
|
|
|
|
for (int i = 0, e = classAttributes.size(); i != e; i++) {
|
|
|
|
if (classAttributes[i].getName() == exportedPath.back()) {
|
|
|
|
attributeAnnotations[i].isExported = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<MethodAnnotation> &methodAnnotations =
|
|
|
|
classAnnotation.getMethodAnnotations();
|
|
|
|
const std::vector<torch::jit::Function *> &methods = classType->methods();
|
|
|
|
for (int i = 0, e = methods.size(); i != e; i++) {
|
|
|
|
if (methods[i]->name() == exportedPath.back()) {
|
|
|
|
methodAnnotations[i].isExported = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const ClassAnnotationMap &ClassAnnotator::getAnnotationMap() {
|
|
|
|
return classAnnotations;
|
|
|
|
}
|
|
|
|
|
|
|
|
ClassAnnotation &
|
|
|
|
ClassAnnotator::getOrCreateClassAnnotation(c10::ClassType *classType) {
|
|
|
|
auto it = classAnnotations.find(classType);
|
|
|
|
if (it == classAnnotations.end()) {
|
|
|
|
auto newAnnotation = std::make_unique<ClassAnnotation>(
|
|
|
|
classType->shared_from_this()->cast<c10::ClassType>());
|
|
|
|
it = classAnnotations.insert({classType, std::move(newAnnotation)}).first;
|
2021-03-31 07:11:41 +08:00
|
|
|
for (int i = 0, e = classType->methods().size(); i != e; i++) {
|
|
|
|
functionToMethodMap[classType->methods()[i]] =
|
|
|
|
&it->second->getMethodAnnotations()[i];
|
|
|
|
}
|
2021-02-20 08:21:21 +08:00
|
|
|
}
|
|
|
|
return *it->second;
|
|
|
|
}
|
|
|
|
|
2021-03-31 07:11:41 +08:00
|
|
|
static c10::ScalarType convertToC10ScalarType(py::object obj) {
|
|
|
|
if (THPDtype_Check(obj.ptr())) {
|
|
|
|
// Need reinterpret_cast, since no C++-level inheritance is involved.
|
|
|
|
THPDtype *dtype = reinterpret_cast<THPDtype *>(obj.ptr());
|
|
|
|
return dtype->scalar_type;
|
|
|
|
}
|
|
|
|
std::stringstream ss;
|
|
|
|
ss << "unsupported scalar type '" << obj << "'";
|
|
|
|
throw std::invalid_argument(ss.str());
|
|
|
|
}
|
|
|
|
|
|
|
|
static void fillArgAnnotations(MethodAnnotation &methodAnnotation,
|
|
|
|
py::list pyArgAnnotations,
|
|
|
|
torch::jit::Function *function) {
|
|
|
|
if (pyArgAnnotations.size() != function->num_inputs()) {
|
|
|
|
throw std::invalid_argument("Arg annotations should have one entry per "
|
|
|
|
"function parameter (including self).");
|
|
|
|
}
|
|
|
|
if (!methodAnnotation.argAnnotations.has_value()) {
|
|
|
|
methodAnnotation.argAnnotations.emplace(function->num_inputs(),
|
|
|
|
ArgAnnotation{});
|
|
|
|
}
|
|
|
|
std::vector<ArgAnnotation> &argAnnotations =
|
|
|
|
methodAnnotation.argAnnotations.value();
|
|
|
|
for (int i = 0, e = argAnnotations.size(); i != e; i++) {
|
|
|
|
if (pyArgAnnotations[i].is_none()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
auto tuple = py::cast<py::tuple>(pyArgAnnotations[i]);
|
|
|
|
auto shape = tuple[0];
|
|
|
|
auto dtype = tuple[1];
|
Introduce `!torch.tensor` / `!torch.vtensor` types.
This removes our reliance on the numpy dialect and avoids our off-label
use of the builtin tnesor type for modeling unknown dtypes. The
`!torch.vtensor` (`ValueTensorType`) type is a value-semantic tensor.
The `!torch.tensor` (`NonValueTensorType`) type is a non-value-semantic
tensor. The new types look as follows syntactically:
```
// Least-static-information, non-value-semantic tensor.
!torch.tensor
// Explicit form of least-static-information variant.
!torch.tensor<*,unk>
// Least-static-information, value-semantic tensor.
!torch.vtensor
// Explicit form of least-static-information variant.
!torch.vtensor<*,unk>
// Fixed-set of allowable element types, with first-class support for
// Torch's frontend signedness semantics.
!torch.tensor<*,si32>
// First-class support for unknown dtypes.
!torch.tensor<[?,?,?],unk>
// Standard MLIR representation of `?` for unknown dimensions.
!torch.tensor<[?,2,?,4],unk>
// Statically shaped / dtyped example.
!torch.vtensor<[1,2,3,4],f32>
```
This required fairly significant changes throughout the compiler, but
overall it is a big cleanup. We now have a much clearer layering of "the
Torch frontend lowering" vs "lowering to std + linalg + etc.".
At the C++ level, there is `ValueTensorType`, `NonValueTensorType`.
We also have a helper `BaseTensorType` (kind of like ShapedType) which
interoperates with those two.
Included changes:
- New `torch.tensor(dense<0.0> : tensor<5xf32>) : !torch.tensor` op for
creating torch tensor literals in the frontend.
- Consistently use signedness for the types (except i1 which I didn't
touch -- we need to sort out the situation with !basicpy.BoolType
there anyway so will be attending to that soon)
- Frontend can annotate whether an argument to the function has value
semantics. We currently require this, as our backend contract does not
currently allow us to even model the non-value-semantic case. Before,
the value-semantic assumption was randomly injected in the middle of
the pass pipeline.
- Move ArrayToTensor (now called MaximizeValueSemantics) and
RefinePublicReturn passes to torch dialect.
- The TorchToStd and TorchToLinalg passes are now type conversions from
`!torch.vtensor` to `tensor` and use the dialect conversion infra.
The overall conversion pipeline is set up following the best practices
of the "Type Conversions the Not-So-Hard Way" talk. This required
introducing `torch-func-builtin-tensorize` and
`torch-finalizing-builtin-tensorize` passes analogous to the upstream
bufferization passes with the corresponding names (mostly just
copypasta from there).
- Misc Torch-level canonicalizations -- we now cleanly layer the
lowering to std later in the pipeline, so we are gradually lessening
our reliance on random std constant folding before we get to that
point.
Recommended review order:
- New types in TorchTypes.td/TorchTypes.h/TorchDialect.cpp
- New ops in TorchOps.td / TorchOps.cpp
- Less important / more mechanical stuff
- Frontend changes.
- Pass changes/additions in `Torch/Transforms` and `Conversion/`
2021-05-21 08:07:18 +08:00
|
|
|
auto hasValueSemantics = tuple[2];
|
2021-03-31 07:11:41 +08:00
|
|
|
if (!shape.is_none()) {
|
|
|
|
argAnnotations[i].shape = py::cast<std::vector<int64_t>>(shape);
|
|
|
|
}
|
|
|
|
if (!dtype.is_none()) {
|
|
|
|
argAnnotations[i].dtype = convertToC10ScalarType(dtype);
|
|
|
|
}
|
Introduce `!torch.tensor` / `!torch.vtensor` types.
This removes our reliance on the numpy dialect and avoids our off-label
use of the builtin tnesor type for modeling unknown dtypes. The
`!torch.vtensor` (`ValueTensorType`) type is a value-semantic tensor.
The `!torch.tensor` (`NonValueTensorType`) type is a non-value-semantic
tensor. The new types look as follows syntactically:
```
// Least-static-information, non-value-semantic tensor.
!torch.tensor
// Explicit form of least-static-information variant.
!torch.tensor<*,unk>
// Least-static-information, value-semantic tensor.
!torch.vtensor
// Explicit form of least-static-information variant.
!torch.vtensor<*,unk>
// Fixed-set of allowable element types, with first-class support for
// Torch's frontend signedness semantics.
!torch.tensor<*,si32>
// First-class support for unknown dtypes.
!torch.tensor<[?,?,?],unk>
// Standard MLIR representation of `?` for unknown dimensions.
!torch.tensor<[?,2,?,4],unk>
// Statically shaped / dtyped example.
!torch.vtensor<[1,2,3,4],f32>
```
This required fairly significant changes throughout the compiler, but
overall it is a big cleanup. We now have a much clearer layering of "the
Torch frontend lowering" vs "lowering to std + linalg + etc.".
At the C++ level, there is `ValueTensorType`, `NonValueTensorType`.
We also have a helper `BaseTensorType` (kind of like ShapedType) which
interoperates with those two.
Included changes:
- New `torch.tensor(dense<0.0> : tensor<5xf32>) : !torch.tensor` op for
creating torch tensor literals in the frontend.
- Consistently use signedness for the types (except i1 which I didn't
touch -- we need to sort out the situation with !basicpy.BoolType
there anyway so will be attending to that soon)
- Frontend can annotate whether an argument to the function has value
semantics. We currently require this, as our backend contract does not
currently allow us to even model the non-value-semantic case. Before,
the value-semantic assumption was randomly injected in the middle of
the pass pipeline.
- Move ArrayToTensor (now called MaximizeValueSemantics) and
RefinePublicReturn passes to torch dialect.
- The TorchToStd and TorchToLinalg passes are now type conversions from
`!torch.vtensor` to `tensor` and use the dialect conversion infra.
The overall conversion pipeline is set up following the best practices
of the "Type Conversions the Not-So-Hard Way" talk. This required
introducing `torch-func-builtin-tensorize` and
`torch-finalizing-builtin-tensorize` passes analogous to the upstream
bufferization passes with the corresponding names (mostly just
copypasta from there).
- Misc Torch-level canonicalizations -- we now cleanly layer the
lowering to std later in the pipeline, so we are gradually lessening
our reliance on random std constant folding before we get to that
point.
Recommended review order:
- New types in TorchTypes.td/TorchTypes.h/TorchDialect.cpp
- New ops in TorchOps.td / TorchOps.cpp
- Less important / more mechanical stuff
- Frontend changes.
- Pass changes/additions in `Torch/Transforms` and `Conversion/`
2021-05-21 08:07:18 +08:00
|
|
|
argAnnotations[i].hasValueSemantics = py::cast<bool>(hasValueSemantics);
|
2021-03-31 07:11:41 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
Introduce `!torch.tensor` / `!torch.vtensor` types.
This removes our reliance on the numpy dialect and avoids our off-label
use of the builtin tnesor type for modeling unknown dtypes. The
`!torch.vtensor` (`ValueTensorType`) type is a value-semantic tensor.
The `!torch.tensor` (`NonValueTensorType`) type is a non-value-semantic
tensor. The new types look as follows syntactically:
```
// Least-static-information, non-value-semantic tensor.
!torch.tensor
// Explicit form of least-static-information variant.
!torch.tensor<*,unk>
// Least-static-information, value-semantic tensor.
!torch.vtensor
// Explicit form of least-static-information variant.
!torch.vtensor<*,unk>
// Fixed-set of allowable element types, with first-class support for
// Torch's frontend signedness semantics.
!torch.tensor<*,si32>
// First-class support for unknown dtypes.
!torch.tensor<[?,?,?],unk>
// Standard MLIR representation of `?` for unknown dimensions.
!torch.tensor<[?,2,?,4],unk>
// Statically shaped / dtyped example.
!torch.vtensor<[1,2,3,4],f32>
```
This required fairly significant changes throughout the compiler, but
overall it is a big cleanup. We now have a much clearer layering of "the
Torch frontend lowering" vs "lowering to std + linalg + etc.".
At the C++ level, there is `ValueTensorType`, `NonValueTensorType`.
We also have a helper `BaseTensorType` (kind of like ShapedType) which
interoperates with those two.
Included changes:
- New `torch.tensor(dense<0.0> : tensor<5xf32>) : !torch.tensor` op for
creating torch tensor literals in the frontend.
- Consistently use signedness for the types (except i1 which I didn't
touch -- we need to sort out the situation with !basicpy.BoolType
there anyway so will be attending to that soon)
- Frontend can annotate whether an argument to the function has value
semantics. We currently require this, as our backend contract does not
currently allow us to even model the non-value-semantic case. Before,
the value-semantic assumption was randomly injected in the middle of
the pass pipeline.
- Move ArrayToTensor (now called MaximizeValueSemantics) and
RefinePublicReturn passes to torch dialect.
- The TorchToStd and TorchToLinalg passes are now type conversions from
`!torch.vtensor` to `tensor` and use the dialect conversion infra.
The overall conversion pipeline is set up following the best practices
of the "Type Conversions the Not-So-Hard Way" talk. This required
introducing `torch-func-builtin-tensorize` and
`torch-finalizing-builtin-tensorize` passes analogous to the upstream
bufferization passes with the corresponding names (mostly just
copypasta from there).
- Misc Torch-level canonicalizations -- we now cleanly layer the
lowering to std later in the pipeline, so we are gradually lessening
our reliance on random std constant folding before we get to that
point.
Recommended review order:
- New types in TorchTypes.td/TorchTypes.h/TorchDialect.cpp
- New ops in TorchOps.td / TorchOps.cpp
- Less important / more mechanical stuff
- Frontend changes.
- Pass changes/additions in `Torch/Transforms` and `Conversion/`
2021-05-21 08:07:18 +08:00
|
|
|
void ClassAnnotator::annotateArgs(c10::ClassType &rootClassType,
|
|
|
|
std::vector<std::string> path,
|
|
|
|
py::list argAnnotations) {
|
2021-03-31 07:11:41 +08:00
|
|
|
if (path.size() == 0) {
|
|
|
|
throw std::invalid_argument("Empty annotated path. Can only annotate "
|
|
|
|
"shapes/dtypes of a method of a class.");
|
|
|
|
}
|
|
|
|
c10::ClassType *classType =
|
|
|
|
getClassAtPath(&rootClassType, c10::ArrayRef<std::string>(path)
|
|
|
|
.slice(0, path.size() - 1)
|
|
|
|
.vec());
|
|
|
|
|
|
|
|
// Throw error if no method on the class of the specified name.
|
|
|
|
torch::jit::Function *function = &classType->getMethod(path.back());
|
|
|
|
|
|
|
|
ClassAnnotation &classAnnotation = getOrCreateClassAnnotation(classType);
|
|
|
|
std::vector<MethodAnnotation> &methodAnnotations =
|
|
|
|
classAnnotation.getMethodAnnotations();
|
|
|
|
const std::vector<torch::jit::Function *> &methods = classType->methods();
|
|
|
|
for (int i = 0, e = methods.size(); i != e; i++) {
|
|
|
|
if (methods[i]->name() == path.back()) {
|
|
|
|
fillArgAnnotations(methodAnnotations[i], argAnnotations, function);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
c10::ClassType *ClassAnnotator::getClassAtPath(c10::ClassType *rootClassType,
|
|
|
|
std::vector<std::string> path) {
|
|
|
|
c10::ClassType *classType = rootClassType;
|
|
|
|
// Reverse so that pop_back gives us the initial atoms first.
|
|
|
|
std::reverse(path.begin(), path.end());
|
|
|
|
while (!path.empty()) {
|
|
|
|
// This will throw in case of missing attribute.
|
|
|
|
c10::TypePtr childType = classType->getAttribute(path.back());
|
|
|
|
c10::ClassTypePtr childClassType = childType->cast<c10::ClassType>();
|
|
|
|
if (!childClassType) {
|
|
|
|
std::stringstream ss;
|
|
|
|
ss << "class '" << classType->name()->qualifiedName()
|
|
|
|
<< "' does not have a submodule in attribute '" << path.back() << "'";
|
|
|
|
throw std::invalid_argument(ss.str());
|
|
|
|
}
|
|
|
|
path.pop_back();
|
|
|
|
classType = childClassType.get();
|
|
|
|
}
|
|
|
|
return classType;
|
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Helper methods
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
MethodAnnotation *
|
|
|
|
ClassAnnotator::getMethodAnnotationForFunction(torch::jit::Function *function) {
|
|
|
|
auto it = functionToMethodMap.find(function);
|
|
|
|
if (it == functionToMethodMap.end()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return it->second;
|
|
|
|
}
|
|
|
|
|
2021-02-20 08:21:21 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// toString methods
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
std::string AttributeAnnotation::toString(const std::string &name) {
|
|
|
|
std::stringstream ss;
|
|
|
|
ss << "AttributeAnnotation('" << name << "') {\n";
|
|
|
|
ss << " isExported = " << (isExported ? "true" : "false") << "\n";
|
|
|
|
ss << "}\n";
|
|
|
|
return ss.str();
|
|
|
|
}
|
|
|
|
|
2021-03-31 07:11:41 +08:00
|
|
|
std::string ArgAnnotation::toString(int argIndex) {
|
|
|
|
std::stringstream ss;
|
|
|
|
ss << "ArgAnnotation(" << argIndex << ") {\n";
|
|
|
|
ss << " dtype = " << (dtype ? c10::toString(*dtype) : "<none>") << "\n";
|
|
|
|
ss << " shape = ";
|
|
|
|
if (shape) {
|
|
|
|
ss << "[";
|
|
|
|
for (int i = 0, e = shape.value().size(); i != e; i++) {
|
|
|
|
if (i) {
|
|
|
|
ss << ", ";
|
|
|
|
}
|
|
|
|
ss << shape.value()[i];
|
|
|
|
}
|
|
|
|
ss << "]\n";
|
|
|
|
} else {
|
|
|
|
ss << "<none>\n";
|
|
|
|
}
|
Introduce `!torch.tensor` / `!torch.vtensor` types.
This removes our reliance on the numpy dialect and avoids our off-label
use of the builtin tnesor type for modeling unknown dtypes. The
`!torch.vtensor` (`ValueTensorType`) type is a value-semantic tensor.
The `!torch.tensor` (`NonValueTensorType`) type is a non-value-semantic
tensor. The new types look as follows syntactically:
```
// Least-static-information, non-value-semantic tensor.
!torch.tensor
// Explicit form of least-static-information variant.
!torch.tensor<*,unk>
// Least-static-information, value-semantic tensor.
!torch.vtensor
// Explicit form of least-static-information variant.
!torch.vtensor<*,unk>
// Fixed-set of allowable element types, with first-class support for
// Torch's frontend signedness semantics.
!torch.tensor<*,si32>
// First-class support for unknown dtypes.
!torch.tensor<[?,?,?],unk>
// Standard MLIR representation of `?` for unknown dimensions.
!torch.tensor<[?,2,?,4],unk>
// Statically shaped / dtyped example.
!torch.vtensor<[1,2,3,4],f32>
```
This required fairly significant changes throughout the compiler, but
overall it is a big cleanup. We now have a much clearer layering of "the
Torch frontend lowering" vs "lowering to std + linalg + etc.".
At the C++ level, there is `ValueTensorType`, `NonValueTensorType`.
We also have a helper `BaseTensorType` (kind of like ShapedType) which
interoperates with those two.
Included changes:
- New `torch.tensor(dense<0.0> : tensor<5xf32>) : !torch.tensor` op for
creating torch tensor literals in the frontend.
- Consistently use signedness for the types (except i1 which I didn't
touch -- we need to sort out the situation with !basicpy.BoolType
there anyway so will be attending to that soon)
- Frontend can annotate whether an argument to the function has value
semantics. We currently require this, as our backend contract does not
currently allow us to even model the non-value-semantic case. Before,
the value-semantic assumption was randomly injected in the middle of
the pass pipeline.
- Move ArrayToTensor (now called MaximizeValueSemantics) and
RefinePublicReturn passes to torch dialect.
- The TorchToStd and TorchToLinalg passes are now type conversions from
`!torch.vtensor` to `tensor` and use the dialect conversion infra.
The overall conversion pipeline is set up following the best practices
of the "Type Conversions the Not-So-Hard Way" talk. This required
introducing `torch-func-builtin-tensorize` and
`torch-finalizing-builtin-tensorize` passes analogous to the upstream
bufferization passes with the corresponding names (mostly just
copypasta from there).
- Misc Torch-level canonicalizations -- we now cleanly layer the
lowering to std later in the pipeline, so we are gradually lessening
our reliance on random std constant folding before we get to that
point.
Recommended review order:
- New types in TorchTypes.td/TorchTypes.h/TorchDialect.cpp
- New ops in TorchOps.td / TorchOps.cpp
- Less important / more mechanical stuff
- Frontend changes.
- Pass changes/additions in `Torch/Transforms` and `Conversion/`
2021-05-21 08:07:18 +08:00
|
|
|
ss << " hasValueSemantics = " << (hasValueSemantics ? "true" : "false")
|
|
|
|
<< "\n";
|
2021-03-31 07:11:41 +08:00
|
|
|
ss << "}\n";
|
|
|
|
return ss.str();
|
|
|
|
}
|
|
|
|
|
2021-02-20 08:21:21 +08:00
|
|
|
std::string MethodAnnotation::toString(const std::string &name) {
|
|
|
|
std::stringstream ss;
|
|
|
|
ss << "MethodAnnotation('" << name << "') {\n";
|
|
|
|
ss << " isExported = " << (isExported ? "true" : "false") << "\n";
|
2021-03-31 07:11:41 +08:00
|
|
|
ss << " argAnnotations =";
|
|
|
|
if (argAnnotations) {
|
|
|
|
ss << "\n";
|
|
|
|
for (int i = 0, e = argAnnotations.value().size(); i < e; i++) {
|
|
|
|
ss << indentString(" ", argAnnotations.value()[i].toString(i));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ss << " <none>\n";
|
|
|
|
}
|
2021-02-20 08:21:21 +08:00
|
|
|
ss << "}\n";
|
|
|
|
return ss.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string ClassAnnotation::toString() {
|
|
|
|
std::stringstream ss;
|
|
|
|
ss << "ClassAnnotation('" << classType->name()->qualifiedName() << "') {\n";
|
|
|
|
|
|
|
|
const std::vector<c10::ClassAttribute> &classAttributes =
|
|
|
|
classType->getAttributes();
|
|
|
|
for (int i = 0, e = classAttributes.size(); i != e; i++) {
|
|
|
|
ss << indentString(
|
|
|
|
" ", attributeAnnotations[i].toString(classAttributes[i].getName()));
|
|
|
|
}
|
|
|
|
const std::vector<torch::jit::Function *> &methods = classType->methods();
|
|
|
|
for (int i = 0, e = methods.size(); i != e; i++) {
|
|
|
|
ss << indentString(" ", methodAnnotations[i].toString(methods[i]->name()));
|
|
|
|
}
|
|
|
|
ss << "}\n";
|
|
|
|
return ss.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string ClassAnnotator::toString() {
|
|
|
|
std::stringstream ss;
|
|
|
|
ss << "ClassAnnotator {\n";
|
|
|
|
for (auto &p : classAnnotations) {
|
|
|
|
ss << indentString(" ", p.second->toString());
|
|
|
|
}
|
|
|
|
ss << "}\n";
|
|
|
|
return ss.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
void torch_mlir::initClassAnnotatorBindings(py::module &m) {
|
|
|
|
py::class_<ClassAnnotator>(m, "ClassAnnotator")
|
|
|
|
.def(py::init<>())
|
|
|
|
.def("exportPath", &ClassAnnotator::exportPath)
|
|
|
|
.def("exportNone", &ClassAnnotator::exportNone)
|
Introduce `!torch.tensor` / `!torch.vtensor` types.
This removes our reliance on the numpy dialect and avoids our off-label
use of the builtin tnesor type for modeling unknown dtypes. The
`!torch.vtensor` (`ValueTensorType`) type is a value-semantic tensor.
The `!torch.tensor` (`NonValueTensorType`) type is a non-value-semantic
tensor. The new types look as follows syntactically:
```
// Least-static-information, non-value-semantic tensor.
!torch.tensor
// Explicit form of least-static-information variant.
!torch.tensor<*,unk>
// Least-static-information, value-semantic tensor.
!torch.vtensor
// Explicit form of least-static-information variant.
!torch.vtensor<*,unk>
// Fixed-set of allowable element types, with first-class support for
// Torch's frontend signedness semantics.
!torch.tensor<*,si32>
// First-class support for unknown dtypes.
!torch.tensor<[?,?,?],unk>
// Standard MLIR representation of `?` for unknown dimensions.
!torch.tensor<[?,2,?,4],unk>
// Statically shaped / dtyped example.
!torch.vtensor<[1,2,3,4],f32>
```
This required fairly significant changes throughout the compiler, but
overall it is a big cleanup. We now have a much clearer layering of "the
Torch frontend lowering" vs "lowering to std + linalg + etc.".
At the C++ level, there is `ValueTensorType`, `NonValueTensorType`.
We also have a helper `BaseTensorType` (kind of like ShapedType) which
interoperates with those two.
Included changes:
- New `torch.tensor(dense<0.0> : tensor<5xf32>) : !torch.tensor` op for
creating torch tensor literals in the frontend.
- Consistently use signedness for the types (except i1 which I didn't
touch -- we need to sort out the situation with !basicpy.BoolType
there anyway so will be attending to that soon)
- Frontend can annotate whether an argument to the function has value
semantics. We currently require this, as our backend contract does not
currently allow us to even model the non-value-semantic case. Before,
the value-semantic assumption was randomly injected in the middle of
the pass pipeline.
- Move ArrayToTensor (now called MaximizeValueSemantics) and
RefinePublicReturn passes to torch dialect.
- The TorchToStd and TorchToLinalg passes are now type conversions from
`!torch.vtensor` to `tensor` and use the dialect conversion infra.
The overall conversion pipeline is set up following the best practices
of the "Type Conversions the Not-So-Hard Way" talk. This required
introducing `torch-func-builtin-tensorize` and
`torch-finalizing-builtin-tensorize` passes analogous to the upstream
bufferization passes with the corresponding names (mostly just
copypasta from there).
- Misc Torch-level canonicalizations -- we now cleanly layer the
lowering to std later in the pipeline, so we are gradually lessening
our reliance on random std constant folding before we get to that
point.
Recommended review order:
- New types in TorchTypes.td/TorchTypes.h/TorchDialect.cpp
- New ops in TorchOps.td / TorchOps.cpp
- Less important / more mechanical stuff
- Frontend changes.
- Pass changes/additions in `Torch/Transforms` and `Conversion/`
2021-05-21 08:07:18 +08:00
|
|
|
.def("annotateArgs", &ClassAnnotator::annotateArgs)
|
2021-02-20 08:21:21 +08:00
|
|
|
.def("__repr__", &ClassAnnotator::toString);
|
|
|
|
}
|