2021-02-18 03:28:51 +08:00
|
|
|
//===------------------------------------------------------------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// 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
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef NPCOMP_DIALECT_TORCH_TRANSFORMS_PASSES_H
|
|
|
|
#define NPCOMP_DIALECT_TORCH_TRANSFORMS_PASSES_H
|
|
|
|
|
|
|
|
#include "mlir/Pass/Pass.h"
|
|
|
|
|
|
|
|
#include <memory>
|
|
|
|
|
|
|
|
namespace mlir {
|
|
|
|
namespace NPCOMP {
|
|
|
|
namespace Torch {
|
|
|
|
|
|
|
|
std::unique_ptr<OperationPass<ModuleOp>> createGlobalizeObjectGraphPass();
|
|
|
|
|
Support multiple instances of a class in GlobalizeObjectGraph.
This happens in practice with e.g. ResNet from torchvision (multiple
instances of the same BatchNorm class).
The key observation is that for this program, and the expected set of
programs, we can convert the program to the same globalized form with a
bit more static analysis and effort to suitably monomorphize the
program. Though what we are doing here is fairly annoying to implement,
it saves any nontrivial later pass from having to do similar analyses
(or worse). E.g. shape inference would need to be object-graph aware,
mutation/lifetime analyses would have to be aware, etc. Additionally, it
would make us front-load what it means to have a !torch.nn.Module type
on an ABI boundary, which we are just not ready to handle.
I'm really, really hoping that in practice we can get away with
this, otherwise it's going to be really rough designing a representation
(and implementing everything to back it) that is convenient to transform
and gracefully scales from full object graph (in the most dynamic case)
down to a fixed set of global slots like we have here (in the most
static case, which we presume a lot of practical programs fall into).
This also involved introducing a
`torch-prepare-for-globalize-object-graph` pass that does a minimal set of
lowerings to simplify the IR into a more orthogonal and analyzable form,
and a `torch-globalize-pipeline` helper.
Recommended review order:
- updated documentation in Passes.td
- new tests in `globalize-object-graph-multiple-instances*.mlir`
- implementation of GlobalizeObjectGraph.cpp
- PrepareForGlobalizeObjectGraph.cpp + prepare-for-globalize-object-graph.mlir
- misc stuff like torch-globalize-pipeline pipeline definition.
With this, we can import, globalize, and inline resnet18 from
torchvision:
https://gist.github.com/silvasean/821586afc19b67d9fb72030b2e0adeb8
2021-03-10 12:33:21 +08:00
|
|
|
std::unique_ptr<OperationPass<ModuleOp>>
|
|
|
|
createPrepareForGlobalizeObjectGraphPass();
|
|
|
|
|
2021-04-27 05:22:50 +08:00
|
|
|
struct TorchLoweringPipelineOptions
|
|
|
|
: public PassPipelineOptions<TorchLoweringPipelineOptions> {
|
|
|
|
// If this option is true, then perform optimizations.
|
|
|
|
// If this option is false, only do the bare minimum for correctness.
|
|
|
|
Option<bool> optimize{*this, "optimize", llvm::cl::desc("Do optimizations."),
|
|
|
|
llvm::cl::init(true)};
|
|
|
|
};
|
|
|
|
|
2021-04-22 06:07:15 +08:00
|
|
|
/// Creates a pipeline that lowers the object graph IR that is produced by
|
|
|
|
/// TorchScript import into the form expected by npcomp-verify-backend-contract.
|
2021-04-27 05:22:50 +08:00
|
|
|
void createLowerObjectGraphPipeline(
|
|
|
|
OpPassManager &pm, const TorchLoweringPipelineOptions &options);
|
2021-04-22 06:07:15 +08:00
|
|
|
|
|
|
|
/// Creates a pipeline that lowers a flat list of funcs and global slots
|
|
|
|
/// with the torch and aten dialects and mutable arrays and converts it to
|
|
|
|
/// the form required by npcomp-verify-backend-contract, in particular
|
|
|
|
/// lowering most arrays to ranked tensors of known dtype, lowering aten ops to
|
|
|
|
/// linalg, converting torch.prim.* ops to elementary math operations.
|
2021-04-27 05:22:50 +08:00
|
|
|
void createLowerToNpcompBackendPipeline(
|
|
|
|
OpPassManager &pm, const TorchLoweringPipelineOptions &options);
|
2021-04-22 06:07:15 +08:00
|
|
|
|
2021-04-02 08:36:18 +08:00
|
|
|
std::unique_ptr<OperationPass<ModuleOp>> createAdjustCallingConventionsPass();
|
|
|
|
|
2021-04-06 08:43:23 +08:00
|
|
|
std::unique_ptr<OperationPass<FuncOp>> createRefineTypesPass();
|
|
|
|
|
2021-04-24 04:35:44 +08:00
|
|
|
std::unique_ptr<OperationPass<ModuleOp>> createInlineGlobalSlotsPass();
|
|
|
|
|
2021-02-18 03:28:51 +08:00
|
|
|
} // namespace Torch
|
|
|
|
|
|
|
|
/// Registers all Torch transformation passes.
|
|
|
|
void registerTorchPasses();
|
|
|
|
|
|
|
|
} // namespace NPCOMP
|
|
|
|
} // namespace mlir
|
|
|
|
|
|
|
|
#endif // NPCOMP_DIALECT_TORCH_TRANSFORMS_PASSES_H
|