| 141 | } |
| 142 | |
| 143 | void Revert::initialize(float spasity, int sparseBlockOC, bool rewrite, bool quantizedModel) { |
| 144 | if (mMNNNet->bizCode == "benchmark" || rewrite) { |
| 145 | randStart(); |
| 146 | bool useSparse = spasity > 0.5f; |
| 147 | for (auto& op : mMNNNet->oplists) { |
| 148 | const auto opType = op->type; |
| 149 | switch (opType) { |
| 150 | case MNN::OpType_Convolution: |
| 151 | case MNN::OpType_Deconvolution: |
| 152 | case MNN::OpType_ConvolutionDepthwise: { |
| 153 | auto param = op->main.AsConvolution2D(); |
| 154 | auto& convCommon = param->common; |
| 155 | const int weightReduceStride = convCommon->kernelX * convCommon->kernelY * convCommon->inputCount; |
| 156 | const int oc = convCommon->outputCount / convCommon->group; |
| 157 | param->weight.resize(oc * weightReduceStride); |
| 158 | ::memset(param->weight.data(), 0, param->weight.size() * sizeof(float)); |
| 159 | param->bias.resize(convCommon->outputCount); |
| 160 | ::memset(param->bias.data(), 0, param->bias.size() * sizeof(float)); |
| 161 | if (useSparse) { |
| 162 | size_t weightNNZElement, weightBlockNumber = 0; |
| 163 | MNN::CommonCompute::fillRandValueAsSparsity(weightNNZElement, weightBlockNumber, param->weight.data(), oc, weightReduceStride, spasity, sparseBlockOC); |
| 164 | |
| 165 | MNN::AttributeT* arg1(new MNN::AttributeT); |
| 166 | arg1->key = "sparseBlockOC"; |
| 167 | arg1->i = sparseBlockOC; |
| 168 | |
| 169 | MNN::AttributeT* arg2(new MNN::AttributeT); |
| 170 | arg2->key = "sparseBlockKernel"; |
| 171 | arg2->i = 1; |
| 172 | |
| 173 | MNN::AttributeT* arg3(new MNN::AttributeT); |
| 174 | arg3->key = "NNZElement"; |
| 175 | arg3->i = weightNNZElement; |
| 176 | |
| 177 | MNN::AttributeT* arg4(new MNN::AttributeT); |
| 178 | arg4->key = "blockNumber"; |
| 179 | arg4->i = weightBlockNumber; |
| 180 | |
| 181 | flatbuffers::FlatBufferBuilder builder; |
| 182 | std::vector<flatbuffers::Offset<MNN::Attribute>> argsVector; |
| 183 | auto sparseArg1 = MNN::CreateAttribute(builder, arg1); |
| 184 | auto sparseArg2 = MNN::CreateAttribute(builder, arg2); |
| 185 | auto sparseArg3 = MNN::CreateAttribute(builder, arg3); |
| 186 | auto sparseArg4 = MNN::CreateAttribute(builder, arg4); |
| 187 | |
| 188 | argsVector.emplace_back(sparseArg1); |
| 189 | argsVector.emplace_back(sparseArg2); |
| 190 | argsVector.emplace_back(sparseArg3); |
| 191 | argsVector.emplace_back(sparseArg4); |
| 192 | |
| 193 | auto sparseArgs = builder.CreateVectorOfSortedTables<MNN::Attribute>(&argsVector); |
| 194 | MNN::SparseAlgo prune_algo_type; |
| 195 | if (sparseBlockOC == 4) { |
| 196 | prune_algo_type = MNN::SparseAlgo_SIMD_OC; |
| 197 | } else { |
| 198 | prune_algo_type = MNN::SparseAlgo_RANDOM; |
| 199 | } |
| 200 | auto sparseCom = MNN::CreateSparseCommon(builder, prune_algo_type, sparseArgs); |
no test coverage detected