1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
|
#import <XCTest/XCTest.h>
#include <torch/csrc/jit/mobile/import.h>
#include <torch/csrc/jit/mobile/module.h>
#include <torch/script.h>
@interface TestAppTests : XCTestCase
@end
@implementation TestAppTests {
}
- (void)testCoreML {
NSString* modelPath = [[NSBundle bundleForClass:[self class]] pathForResource:@"model_coreml"
ofType:@"ptl"];
auto module = torch::jit::_load_for_mobile(modelPath.UTF8String);
c10::InferenceMode mode;
auto input = torch::ones({1, 3, 224, 224}, at::kFloat);
auto outputTensor = module.forward({input}).toTensor();
XCTAssertTrue(outputTensor.numel() == 1000);
}
- (void)testModel:(NSString*)modelName {
NSString* modelPath = [[NSBundle bundleForClass:[self class]] pathForResource:modelName
ofType:@"ptl"];
XCTAssertNotNil(modelPath, @"Model not found. See https://github.com/pytorch/pytorch/tree/master/test/mobile/model_test#diagnose-failed-test.");
[self runModel:modelPath];
// model generated on the fly
NSString* onTheFlyModelName = [NSString stringWithFormat:@"%@", modelName];
NSString* onTheFlyModelPath = [[NSBundle bundleForClass:[self class]] pathForResource:onTheFlyModelName
ofType:@"ptl"];
XCTAssertNotNil(onTheFlyModelPath, @"On-the-fly model not found. Follow https://github.com/pytorch/pytorch/tree/master/test/mobile/model_test#diagnose-failed-test to generate them and run the setup.rb script again.");
[self runModel:onTheFlyModelPath];
}
- (void)runModel:(NSString*)modelPath {
c10::InferenceMode mode;
auto module = torch::jit::_load_for_mobile(modelPath.UTF8String);
auto has_bundled_input = module.find_method("get_all_bundled_inputs");
if (has_bundled_input) {
c10::IValue bundled_inputs = module.run_method("get_all_bundled_inputs");
c10::List<at::IValue> all_inputs = bundled_inputs.toList();
std::vector<std::vector<at::IValue>> inputs;
for (at::IValue input : all_inputs) {
inputs.push_back(input.toTupleRef().elements());
}
// run with the first bundled input
XCTAssertNoThrow(module.forward(inputs[0]));
} else {
XCTAssertNoThrow(module.forward({}));
}
}
// TODO remove this once updated test script
- (void)testLiteInterpreter {
XCTAssertTrue(true);
}
- (void)testMobileNetV2 {
[self testModel:@"mobilenet_v2"];
}
- (void)testPointwiseOps {
[self testModel:@"pointwise_ops"];
}
- (void)testReductionOps {
[self testModel:@"reduction_ops"];
}
- (void)testComparisonOps {
[self testModel:@"comparison_ops"];
}
- (void)testOtherMathOps {
[self testModel:@"other_math_ops"];
}
- (void)testSpectralOps {
[self testModel:@"spectral_ops"];
}
- (void)testBlasLapackOps {
[self testModel:@"blas_lapack_ops"];
}
- (void)testSamplingOps {
[self testModel:@"sampling_ops"];
}
- (void)testTensorOps {
[self testModel:@"tensor_general_ops"];
}
- (void)testTensorCreationOps {
[self testModel:@"tensor_creation_ops"];
}
- (void)testTensorIndexingOps {
[self testModel:@"tensor_indexing_ops"];
}
- (void)testTensorTypingOps {
[self testModel:@"tensor_typing_ops"];
}
- (void)testTensorViewOps {
[self testModel:@"tensor_view_ops"];
}
- (void)testConvolutionOps {
[self testModel:@"convolution_ops"];
}
- (void)testPoolingOps {
[self testModel:@"pooling_ops"];
}
- (void)testPaddingOps {
[self testModel:@"padding_ops"];
}
- (void)testActivationOps {
[self testModel:@"activation_ops"];
}
- (void)testNormalizationOps {
[self testModel:@"normalization_ops"];
}
- (void)testRecurrentOps {
[self testModel:@"recurrent_ops"];
}
- (void)testTransformerOps {
[self testModel:@"transformer_ops"];
}
- (void)testLinearOps {
[self testModel:@"linear_ops"];
}
- (void)testDropoutOps {
[self testModel:@"dropout_ops"];
}
- (void)testSparseOps {
[self testModel:@"sparse_ops"];
}
- (void)testDistanceFunctionOps {
[self testModel:@"distance_function_ops"];
}
- (void)testLossFunctionOps {
[self testModel:@"loss_function_ops"];
}
- (void)testVisionFunctionOps {
[self testModel:@"vision_function_ops"];
}
- (void)testShuffleOps {
[self testModel:@"shuffle_ops"];
}
- (void)testNNUtilsOps {
[self testModel:@"nn_utils_ops"];
}
- (void)testQuantOps {
[self testModel:@"general_quant_ops"];
}
- (void)testDynamicQuantOps {
[self testModel:@"dynamic_quant_ops"];
}
- (void)testStaticQuantOps {
[self testModel:@"static_quant_ops"];
}
- (void)testFusedQuantOps {
[self testModel:@"fused_quant_ops"];
}
- (void)testTorchScriptBuiltinQuantOps {
[self testModel:@"torchscript_builtin_ops"];
}
- (void)testTorchScriptCollectionQuantOps {
[self testModel:@"torchscript_collection_ops"];
}
@end
|