1 | /** |
2 | * Copyright (c) Glow Contributors. See CONTRIBUTORS file. |
3 | * |
4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | * you may not use this file except in compliance with the License. |
6 | * You may obtain a copy of the License at |
7 | * |
8 | * http://www.apache.org/licenses/LICENSE-2.0 |
9 | * |
10 | * Unless required by applicable law or agreed to in writing, software |
11 | * distributed under the License is distributed on an "AS IS" BASIS, |
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 | * See the License for the specific language governing permissions and |
14 | * limitations under the License. |
15 | */ |
16 | |
17 | #include "glow/Backend/Backend.h" |
18 | #include "glow/Graph/Graph.h" |
19 | #include "glow/Graph/Hook.h" |
20 | #include "glow/Graph/Node.h" |
21 | #include "glow/Graph/Nodes.h" |
22 | #include "glow/Graph/Utils.h" |
23 | |
24 | #include "gtest/gtest.h" |
25 | |
26 | using namespace glow; |
27 | |
28 | /// Test BundleSaver. This unit test is for code coverage. |
29 | TEST(BundleSaver, testAPI) { |
30 | Module M; |
31 | Function *F = M.createFunction("F" ); |
32 | |
33 | // Create a simple graph. |
34 | Node *inputPH = M.createPlaceholder(ElemKind::FloatTy, {1}, "input" , false); |
35 | Node *addConst = M.createConstant(ElemKind::FloatTy, {1}, "const" ); |
36 | Node *addNode = F->createAdd("add" , inputPH, addConst); |
37 | F->createSave("output" , addNode); |
38 | |
39 | // Save bundle. |
40 | llvm::StringRef outputDir = "." ; |
41 | llvm::StringRef bundleName = "testBundle" ; |
42 | llvm::StringRef mainEntryName = "testMainEntry" ; |
43 | std::unique_ptr<Backend> backend(createBackend("CPU" )); |
44 | backend->save(F, outputDir, bundleName, mainEntryName); |
45 | } |
46 | |
47 | TEST(BundleSaver, testSaveMultipleFunctions) { |
48 | Module M; |
49 | |
50 | Node *addConst = M.createConstant(ElemKind::FloatTy, {1}, "const" ); |
51 | // Create a simple graph. |
52 | Function *F1 = M.createFunction("F1" ); |
53 | Node *inputPH1 = M.createPlaceholder(ElemKind::FloatTy, {1}, "input1" , false); |
54 | Node *addConst1 = M.createConstant(ElemKind::FloatTy, {1}, "const1" ); |
55 | Node *addNode11 = F1->createAdd("add" , inputPH1, addConst); |
56 | Node *addNode12 = F1->createAdd("add" , addNode11, addConst1); |
57 | F1->createSave("output" , addNode12); |
58 | |
59 | // Create a simple graph. |
60 | Function *F2 = M.createFunction("F2" ); |
61 | Node *inputPH2 = M.createPlaceholder(ElemKind::FloatTy, {1}, "input2" , false); |
62 | Node *subConst2 = M.createConstant(ElemKind::FloatTy, {1}, "const2" ); |
63 | Node *subNode21 = F2->createSub("sub" , inputPH2, addConst); |
64 | Node *subNode22 = F2->createSub("sub" , subNode21, subConst2); |
65 | F2->createSave("output" , subNode22); |
66 | |
67 | // Save a bundle with multiple functions. |
68 | llvm::StringRef outputDir = "." ; |
69 | llvm::StringRef bundleName = "testBundle" ; |
70 | std::unique_ptr<Backend> backend( |
71 | reinterpret_cast<Backend *>(createBackend("CPU" ))); |
72 | std::vector<BundleEntry> bundleEntries; |
73 | bundleEntries.emplace_back(BundleEntry{"testMainEntry1" , F1}); |
74 | bundleEntries.emplace_back(BundleEntry{"testMainEntry2" , F2}); |
75 | backend->saveFunctions(bundleEntries, outputDir, bundleName); |
76 | } |
77 | |