torch-mlir/test/Dialect/Torch/globalize-object-graph-meth...

40 lines
1.3 KiB
MLIR
Raw Normal View History

// RUN: npcomp-opt -torch-globalize-object-graph -split-input-file %s | FileCheck %s
torch.class_type @c {
torch.attr "float" : f64
torch.method "test_get", @test_get
torch.method "test_set", @test_set
torch.method "test_call", @test_call
}
// CHECK-LABEL: func @test_get() -> f64 {
// CHECK: %[[V:.*]] = torch.global_slot.get @float : f64
// CHECK: return %[[V]] : f64
func private @test_get(%arg0: !torch.nn.Module<"c">) -> f64 {
%0 = torch.prim.GetAttr %arg0["float"] : !torch.nn.Module<"c"> -> f64
return %0 : f64
}
// CHECK-LABEL: func @test_set(
// CHECK-SAME: %[[A:.*]]: f64) {
// CHECK: torch.global_slot.set @float = %[[A]] : f64
// CHECK: return
func private @test_set(%arg0: !torch.nn.Module<"c">, %arg1: f64) {
torch.prim.SetAttr %arg0["float"] = %arg1 : !torch.nn.Module<"c">, f64
return
}
// CHECK-LABEL: func @test_call(
// CHECK-SAME: %[[A:.*]]: f64) -> f64 {
// CHECK: %[[V:.*]] = call @test_call(%[[A]]) : (f64) -> f64
// CHECK: return %[[V]] : f64
func private @test_call(%arg0: !torch.nn.Module<"c">, %arg1: f64) -> f64 {
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
%0 = call @test_call(%arg0, %arg1) : (!torch.nn.Module<"c">, f64) -> f64
return %0 : f64
}
%c42 = std.constant 42.0 : f64
torch.nn_module {
torch.slot "float", %c42 : f64
} : !torch.nn.Module<"c">