| 99 | } |
| 100 | |
| 101 | PipelineModule::PipelineModule(std::vector<VARP> inputs, std::vector<VARP> outputs, const Transformer& transformFunction) { |
| 102 | setType(PIPELINE_MODULE); |
| 103 | std::map<EXPRP, int> inputExpr; |
| 104 | for (int i=0; i<inputs.size(); ++i) { |
| 105 | auto expr = inputs[i]->expr().first; |
| 106 | inputExpr.insert(std::make_pair(expr, i)); |
| 107 | } |
| 108 | std::vector<EXPRP> executeOrder = Variable::getExecuteOrder(outputs); |
| 109 | // Set Indexes |
| 110 | std::map<EXPRP, int> indexes; |
| 111 | mInputSize = inputs.size(); |
| 112 | int currentIndexes = inputs.size(); |
| 113 | for (auto expr : executeOrder) { |
| 114 | if (inputExpr.find(expr) != inputExpr.end()) { |
| 115 | indexes[expr] = inputExpr[expr]; |
| 116 | continue; |
| 117 | } |
| 118 | indexes[expr] = currentIndexes; |
| 119 | currentIndexes += expr->outputSize(); |
| 120 | } |
| 121 | std::set<EXPRP> inputSets; |
| 122 | mStackSize = currentIndexes; |
| 123 | for (auto v : inputs) { |
| 124 | auto inputExpr = v->expr(); |
| 125 | inputSets.insert(inputExpr.first); |
| 126 | } |
| 127 | mOutputIndex.clear(); |
| 128 | for (auto output : outputs) { |
| 129 | auto outputExpr = output->expr(); |
| 130 | mOutputIndex.emplace_back(indexes[outputExpr.first] + outputExpr.second); |
| 131 | } |
| 132 | |
| 133 | // Create All SubModule |
| 134 | for (auto expr : executeOrder) { |
| 135 | if (inputSets.find(expr) != inputSets.end()) { |
| 136 | continue; |
| 137 | } |
| 138 | std::pair<std::vector<int>, std::shared_ptr<Module> > moduleResult; |
| 139 | bool extracted = false; |
| 140 | if (!transformFunction) { |
| 141 | moduleResult = std::make_pair(std::vector<int>{}, std::shared_ptr<Module>(nullptr)); |
| 142 | } else { |
| 143 | moduleResult = transformFunction(expr); |
| 144 | } |
| 145 | if (moduleResult.second == nullptr) { |
| 146 | std::shared_ptr<Module> module(new ExprModule(expr)); |
| 147 | moduleResult.first = ((ExprModule*)module.get())->inputIndexes(); |
| 148 | moduleResult.second = module; |
| 149 | } else { |
| 150 | extracted = true; |
| 151 | } |
| 152 | auto subInputs = expr->inputs(); |
| 153 | auto& exprInputIndexes = moduleResult.first; |
| 154 | std::vector<int> inputIndexes; |
| 155 | if (exprInputIndexes.empty() && extracted) { |
| 156 | inputIndexes.resize(subInputs.size()); |
| 157 | for (int i = 0; i < inputIndexes.size(); ++i) { |
| 158 | auto inputExpr = subInputs[i]->expr(); |