ONE - On-device Neural Engine
Loading...
Searching...
No Matches
luci::CircleOptimizer Class Referencefinal

#include <CircleOptimizer.h>

Data Structures

struct  Options
 

Public Member Functions

Optionsoptions (void)
 
void optimize (luci::Module *) const
 
void optimize (loco::Graph *) const
 
void sparsify (loco::Graph *) const
 

Detailed Description

Definition at line 30 of file CircleOptimizer.h.

Member Function Documentation

◆ optimize() [1/2]

void luci::CircleOptimizer::optimize ( loco::Graph g) const

Definition at line 264 of file CircleOptimizer.cpp.

265{
266 canonicalize(g);
267
268 logo::Phase phase;
269
270 // Conversion from NCHW to NHWC is done first to avoid interference with other optimizations.
271 if (_options->query(Options::Algorithm::ConvertNCHWToNHWC))
272 {
273 bool preserve_input =
274 _options->param(Options::AlgorithmParameters::NCHW_to_NHWC_input_shape) != "true";
275 bool preserve_output =
276 _options->param(Options::AlgorithmParameters::NCHW_to_NHWC_output_shape) != "true";
277
278 bool fuse_fc = _options->query(Options::Algorithm::FuseAddWithFullyConnected);
279 bool fuse_gelu = _options->query(Options::Algorithm::FuseGelu);
280
281 convert_nchw_to_nhwc(g, preserve_input, preserve_output, fuse_fc, fuse_gelu);
282 }
283
284 /* TRANSFORM DECLARATION BEGIN */
285 phase.emplace_back(std::make_unique<logo::RemoveDeadNodeWithQueryPass>());
286
287 // Following passes are needed everytime when other passes create new node or modify some nodes.
288 phase.emplace_back(std::make_unique<luci::CircleShapeInferencePass>());
289 phase.emplace_back(std::make_unique<luci::CircleTypeInferencePass>());
290
291 // Forward Reshape/Transpose is done after
292 // 1. SubstituteXXXToReshape
293 // 2. RemoveRedundantReshape/Transpose
294 // See https://github.com/Samsung/ONE/pull/10596 for more details
296 {
297 phase.emplace_back(std::make_unique<luci::SubstituteExpandDimsToReshapePass>());
298 }
300 {
301 phase.emplace_back(std::make_unique<luci::SubstitutePackToReshapePass>());
302 }
304 {
305 phase.emplace_back(std::make_unique<luci::SubstituteSqueezeToReshapePass>());
306 }
308 {
309 phase.emplace_back(std::make_unique<luci::SubstituteStridedSliceToReshapePass>());
310 }
312 {
313 phase.emplace_back(std::make_unique<luci::SubstituteTransposeToReshapePass>());
314 }
316 {
317 phase.emplace_back(std::make_unique<luci::RemoveRedundantReshapePass>());
318 }
320 {
321 phase.emplace_back(std::make_unique<luci::RemoveRedundantTransposePass>());
322 }
323
324 // clang-format off
325 std::map<Options::Algorithm, std::unique_ptr<logo::Pass> (*)(void)> option_to_pass;
326
327 option_to_pass[Options::Algorithm::CommonSubExpressionElimination] = &createPassInstance<luci::CommonSubExpressionEliminationPass>;
328 option_to_pass[Options::Algorithm::ResolveCustomOpAdd] = &createPassInstance<luci::ResolveCustomOpAddPass>;
329 option_to_pass[Options::Algorithm::ResolveCustomOpBatchMatMul] = &createPassInstance<luci::ResolveCustomOpBatchMatMulPass>;
330 option_to_pass[Options::Algorithm::ResolveCustomOpMatMul] = &createPassInstance<luci::ResolveCustomOpMatMulPass>;
331 option_to_pass[Options::Algorithm::ResolveFormerCustomOp] = &createPassInstance<luci::ResolveFormerCustomOpPass>;
332 option_to_pass[Options::Algorithm::FuseMeanWithMean] = &createPassInstance<luci::FuseMeanWithMeanPass>;
333 option_to_pass[Options::Algorithm::FuseMulWithConv] = &createPassInstance<luci::FuseMulWithConvPass>;
334 option_to_pass[Options::Algorithm::FuseMulWithDiv] = &createPassInstance<luci::FuseMulWithDivPass>;
335 option_to_pass[Options::Algorithm::FuseMulWithFullyConnected] = &createPassInstance<luci::FuseMulWithFullyConnectedPass>;
336 option_to_pass[Options::Algorithm::ResolveCustomOpMaxPoolWithArgmax] = &createPassInstance<luci::ResolveCustomOpMaxPoolWithArgmaxPass>;
337 option_to_pass[Options::Algorithm::ResolveCustomOpSplitV] = &createPassInstance<luci::ResolveCustomOpSplitVPass>;
338 option_to_pass[Options::Algorithm::FuseInstanceNorm] = &createPassInstance<luci::FuseInstanceNormPass>;
339 option_to_pass[Options::Algorithm::FuseBatchNormWithConv] = &createPassInstance<luci::FuseBatchNormWithConvPass>;
340 option_to_pass[Options::Algorithm::FuseBatchNormWithDwConv] = &createPassInstance<luci::FuseBatchNormWithDwConvPass>;
341 option_to_pass[Options::Algorithm::FuseBatchNormWithTConv] = &createPassInstance<luci::FuseBatchNormWithTConvPass>;
342 option_to_pass[Options::Algorithm::FuseSliceWithTConv] = &createPassInstance<luci::FuseSliceWithTConvPass>;
343 option_to_pass[Options::Algorithm::FuseAddToFullyConnectedBias] = &createPassInstance<luci::FuseAddToFullyConnectedBiasPass>;
344 option_to_pass[Options::Algorithm::FuseAddWithConv] = &createPassInstance<luci::FuseAddWithConvPass>;
345 option_to_pass[Options::Algorithm::FuseAddWithFullyConnected] = &createPassInstance<luci::FuseAddWithFullyConnectedPass>;
346 option_to_pass[Options::Algorithm::FuseAddWithTConv] = &createPassInstance<luci::FuseAddWithTConvPass>;
347 option_to_pass[Options::Algorithm::FuseActivationFunction] = &createPassInstance<luci::FuseActivationFunctionPass>;
348 option_to_pass[Options::Algorithm::FuseMulToFullyConnectedWeights] = &createPassInstance<luci::FuseMulToFullyConnectedWeightsPass>;
349 option_to_pass[Options::Algorithm::FusePRelu] = &createPassInstance<luci::FusePReluPass>;
350 option_to_pass[Options::Algorithm::FuseGelu] = &createPassInstance<luci::FuseGeluPass>;
351 option_to_pass[Options::Algorithm::FuseRsqrt] = &createPassInstance<luci::FuseRsqrtPass>;
352 option_to_pass[Options::Algorithm::FuseHorizontalFullyConnected] = &createPassInstance<luci::FuseHorizontalFullyConnectedPass>;
353 option_to_pass[Options::Algorithm::FuseTransposeWithMean] = &createPassInstance<luci::FuseTransposeWithMeanPass>;
354 option_to_pass[Options::Algorithm::FuseRmsNorm] = &createPassInstance<luci::FuseRmsNormPass>;
355 option_to_pass[Options::Algorithm::FuseRoPE] = &createPassInstance<luci::FuseRoPEPass>;
356 option_to_pass[Options::Algorithm::FoldAddV2] = &createPassInstance<luci::FoldAddV2Pass>;
357 option_to_pass[Options::Algorithm::FoldCast] = &createPassInstance<luci::FoldCastPass>;
358 option_to_pass[Options::Algorithm::FoldDensify] = &createPassInstance<luci::FoldDensifyPass>;
359 option_to_pass[Options::Algorithm::FoldDepthwiseConv2D] = &createPassInstance<luci::FoldDepthwiseConv2DPass>;
360 option_to_pass[Options::Algorithm::FoldDequantize] = &createPassInstance<luci::FoldDequantizePass>;
361 option_to_pass[Options::Algorithm::FoldFullyConnected] = &createPassInstance<luci::FoldFullyConnectedPass>;
362 option_to_pass[Options::Algorithm::FoldGather] = &createPassInstance<luci::FoldGatherPass>;
363 option_to_pass[Options::Algorithm::FoldMul] = &createPassInstance<luci::FoldMulPass>;
364 option_to_pass[Options::Algorithm::FoldReshape] = &createPassInstance<luci::FoldReshapePass>;
365 option_to_pass[Options::Algorithm::FoldShape] = &createPassInstance<luci::FoldShapePass>;
366 option_to_pass[Options::Algorithm::FoldSparseToDense] = &createPassInstance<luci::FoldSparseToDensePass>;
367 option_to_pass[Options::Algorithm::FoldSqueeze] = &createPassInstance<luci::FoldSqueezePass>;
368 option_to_pass[Options::Algorithm::FusePreActivationBatchNorm] = &createPassInstance<luci::FusePreActivationBatchNormPass>;
369 option_to_pass[Options::Algorithm::MakeBatchNormGammaPositive] = &createPassInstance<luci::MakeBatchNormGammaPositivePass>;
370 option_to_pass[Options::Algorithm::ShuffleWeightTo16x1Float32] = &createPassInstance<luci::ShuffleWeightTo16x1Float32Pass>;
371 option_to_pass[Options::Algorithm::ExpandBroadcastConst] = &createPassInstance<luci::ExpandBroadcastConstPass>;
372 option_to_pass[Options::Algorithm::RemoveDuplicateConst] = &createPassInstance<luci::RemoveDuplicateConstPass>;
373 option_to_pass[Options::Algorithm::RemoveFakeQuant] = &createPassInstance<luci::RemoveFakeQuantPass>;
374 option_to_pass[Options::Algorithm::RemoveGatherGuard] = &createPassInstance<luci::RemoveGatherGuardPass>;
375 option_to_pass[Options::Algorithm::RemoveQDQForMixedPrecisionOp] = &createPassInstance<luci::RemoveQDQForMixedPrecisionOpPass>;
376 option_to_pass[Options::Algorithm::RemoveQuantDequantSeq] = &createPassInstance<luci::RemoveQuantDequantSeqPass>;
377 option_to_pass[Options::Algorithm::RemoveUnnecessaryAdd] = &createPassInstance<luci::RemoveUnnecessaryAddPass>;
378 option_to_pass[Options::Algorithm::RemoveUnnecessaryCast] = &createPassInstance<luci::RemoveUnnecessaryCastPass>;
379 option_to_pass[Options::Algorithm::RemoveUnnecessarySlice] = &createPassInstance<luci::RemoveUnnecessarySlicePass>;
380 option_to_pass[Options::Algorithm::RemoveUnnecessaryStridedSlice] = &createPassInstance<luci::RemoveUnnecessaryStridedSlicePass>;
381 option_to_pass[Options::Algorithm::RemoveUnnecessarySplit] = &createPassInstance<luci::RemoveUnnecessarySplitPass>;
382 option_to_pass[Options::Algorithm::RemoveUnnecessaryTranspose] = &createPassInstance<luci::RemoveUnnecessaryTransposeNetPass>;
383 option_to_pass[Options::Algorithm::RemoveRedundantQuantize] = &createPassInstance<luci::RemoveRedundantQuantizePass>;
384 option_to_pass[Options::Algorithm::ReplaceNonConstFCWithBatchMatMul] = &createPassInstance<luci::ReplaceNonConstFCWithBatchMatMulPass>;
385 option_to_pass[Options::Algorithm::ReplaceMulAddWithDepthwiseConv] = &createPassInstance<luci::ReplaceMulAddWithDepthwiseConvPass>;
386 option_to_pass[Options::Algorithm::ReplaceSubWithAdd] = &createPassInstance<luci::ReplaceSubWithAddPass>;
387 option_to_pass[Options::Algorithm::ReplaceWithFCGeluFC] = &createPassInstance<luci::ReplaceWithFCGeluFCPass>;
388 option_to_pass[Options::Algorithm::SubstitutePadV2ToPad] = &createPassInstance<luci::SubstitutePadV2ToPadPass>;
389 option_to_pass[Options::Algorithm::SubstituteSplitVToSplit] = &createPassInstance<luci::SubstituteSplitVToSplitPass>;
390 option_to_pass[Options::Algorithm::TransformMinMaxToRelu6Pass] = &createPassInstance<luci::TransformMinMaxToRelu6Pass>;
391 option_to_pass[Options::Algorithm::TransformMinReluToRelu6Pass] = &createPassInstance<luci::TransformMinReluToRelu6Pass>;
392 option_to_pass[Options::Algorithm::TransformSqrtDivToRsqrtMul] = &createPassInstance<luci::TransformSqrtDivToRsqrtMulPass>;
393 option_to_pass[Options::Algorithm::DecomposeHardSwishPass] = &createPassInstance<luci::DecomposeHardSwishPass>;
394 option_to_pass[Options::Algorithm::DecomposeSoftmaxPass] = &createPassInstance<luci::DecomposeSoftmaxPass>;
395 option_to_pass[Options::Algorithm::UnrollUnidirSeqLSTM] = &createPassInstance<luci::UnrollUnidirectionalSequenceLSTMPass>;
396 // NOTE Experimental options; these will be removed someday
397 // Add experimental options here
398 option_to_pass[Options::Algorithm::XpSepActFromTransposeConv] = &createPassInstance<luci::XpSepActFromTransposeConvPass>;
399 option_to_pass[Options::Algorithm::ForwardReshapeToUnaryOp] = &createPassInstance<luci::ForwardReshapeToUnaryOpPass>;
400 option_to_pass[Options::Algorithm::ForwardTransposeOp] = &createPassInstance<luci::ForwardTransposeOpPass>;
401 // clang-format on
402
403 for (auto const &m : option_to_pass)
404 {
405 if (_options->query(m.first))
406 {
407 phase.emplace_back(m.second());
408 }
409 }
410
411 // TODO Extend `option_to_pass` to be able to instantiate two or more pass objects.
413 {
414 phase.emplace_back(std::make_unique<luci::RemoveUnnecessaryReshapePass>());
415 phase.emplace_back(std::make_unique<luci::RemoveUnnecessaryReshapeNetPass>());
416 }
417
418 /* TRANSFORM DECLARATION END */
419
422 phase_runner.attach(&prog);
423 phase_runner.run(phase);
424}
std::vector< std::unique_ptr< Pass > > Phase
Definition Phase.h:31

References luci::CircleOptimizer::Options::CommonSubExpressionElimination, luci::CircleOptimizer::Options::ConvertNCHWToNHWC, luci::CircleOptimizer::Options::DecomposeHardSwishPass, luci::CircleOptimizer::Options::DecomposeSoftmaxPass, luci::CircleOptimizer::Options::ExpandBroadcastConst, luci::CircleOptimizer::Options::FoldAddV2, luci::CircleOptimizer::Options::FoldCast, luci::CircleOptimizer::Options::FoldDensify, luci::CircleOptimizer::Options::FoldDepthwiseConv2D, luci::CircleOptimizer::Options::FoldDequantize, luci::CircleOptimizer::Options::FoldFullyConnected, luci::CircleOptimizer::Options::FoldGather, luci::CircleOptimizer::Options::FoldMul, luci::CircleOptimizer::Options::FoldReshape, luci::CircleOptimizer::Options::FoldShape, luci::CircleOptimizer::Options::FoldSparseToDense, luci::CircleOptimizer::Options::FoldSqueeze, luci::CircleOptimizer::Options::ForwardReshapeToUnaryOp, luci::CircleOptimizer::Options::ForwardTransposeOp, luci::CircleOptimizer::Options::FuseActivationFunction, luci::CircleOptimizer::Options::FuseAddToFullyConnectedBias, luci::CircleOptimizer::Options::FuseAddWithConv, luci::CircleOptimizer::Options::FuseAddWithFullyConnected, luci::CircleOptimizer::Options::FuseAddWithTConv, luci::CircleOptimizer::Options::FuseBatchNormWithConv, luci::CircleOptimizer::Options::FuseBatchNormWithDwConv, luci::CircleOptimizer::Options::FuseBatchNormWithTConv, luci::CircleOptimizer::Options::FuseGelu, luci::CircleOptimizer::Options::FuseHorizontalFullyConnected, luci::CircleOptimizer::Options::FuseInstanceNorm, luci::CircleOptimizer::Options::FuseMeanWithMean, luci::CircleOptimizer::Options::FuseMulToFullyConnectedWeights, luci::CircleOptimizer::Options::FuseMulWithConv, luci::CircleOptimizer::Options::FuseMulWithDiv, luci::CircleOptimizer::Options::FuseMulWithFullyConnected, luci::CircleOptimizer::Options::FusePreActivationBatchNorm, luci::CircleOptimizer::Options::FusePRelu, luci::CircleOptimizer::Options::FuseRmsNorm, luci::CircleOptimizer::Options::FuseRoPE, luci::CircleOptimizer::Options::FuseRsqrt, luci::CircleOptimizer::Options::FuseSliceWithTConv, luci::CircleOptimizer::Options::FuseTransposeWithMean, m, luci::CircleOptimizer::Options::MakeBatchNormGammaPositive, luci::CircleOptimizer::Options::RemoveDuplicateConst, luci::CircleOptimizer::Options::RemoveFakeQuant, luci::CircleOptimizer::Options::RemoveGatherGuard, luci::CircleOptimizer::Options::RemoveQDQForMixedPrecisionOp, luci::CircleOptimizer::Options::RemoveQuantDequantSeq, luci::CircleOptimizer::Options::RemoveRedundantQuantize, luci::CircleOptimizer::Options::RemoveRedundantReshape, luci::CircleOptimizer::Options::RemoveRedundantTranspose, luci::CircleOptimizer::Options::RemoveUnnecessaryAdd, luci::CircleOptimizer::Options::RemoveUnnecessaryCast, luci::CircleOptimizer::Options::RemoveUnnecessaryReshape, luci::CircleOptimizer::Options::RemoveUnnecessarySlice, luci::CircleOptimizer::Options::RemoveUnnecessarySplit, luci::CircleOptimizer::Options::RemoveUnnecessaryStridedSlice, luci::CircleOptimizer::Options::RemoveUnnecessaryTranspose, luci::CircleOptimizer::Options::ReplaceMulAddWithDepthwiseConv, luci::CircleOptimizer::Options::ReplaceNonConstFCWithBatchMatMul, luci::CircleOptimizer::Options::ReplaceSubWithAdd, luci::CircleOptimizer::Options::ReplaceWithFCGeluFC, luci::CircleOptimizer::Options::ResolveCustomOpAdd, luci::CircleOptimizer::Options::ResolveCustomOpBatchMatMul, luci::CircleOptimizer::Options::ResolveCustomOpMatMul, luci::CircleOptimizer::Options::ResolveCustomOpMaxPoolWithArgmax, luci::CircleOptimizer::Options::ResolveCustomOpSplitV, luci::CircleOptimizer::Options::ResolveFormerCustomOp, logo::Restart, luci::CircleOptimizer::Options::ShuffleWeightTo16x1Float32, luci::CircleOptimizer::Options::SubstituteExpandDimsToReshape, luci::CircleOptimizer::Options::SubstitutePackToReshape, luci::CircleOptimizer::Options::SubstitutePadV2ToPad, luci::CircleOptimizer::Options::SubstituteSplitVToSplit, luci::CircleOptimizer::Options::SubstituteSqueezeToReshape, luci::CircleOptimizer::Options::SubstituteStridedSliceToReshape, luci::CircleOptimizer::Options::SubstituteTransposeToReshape, luci::CircleOptimizer::Options::TransformMinMaxToRelu6Pass, luci::CircleOptimizer::Options::TransformMinReluToRelu6Pass, luci::CircleOptimizer::Options::TransformSqrtDivToRsqrtMul, luci::CircleOptimizer::Options::UnrollUnidirSeqLSTM, and luci::CircleOptimizer::Options::XpSepActFromTransposeConv.

◆ optimize() [2/2]

void luci::CircleOptimizer::optimize ( luci::Module m) const

Definition at line 240 of file CircleOptimizer.cpp.

241{
242 luci::Phase phase;
243
244 // Following passes are needed everytime when other passes create new node or modify some nodes.
245 phase.emplace_back(std::make_unique<luci::CircleShapeInferencePass>());
246 phase.emplace_back(std::make_unique<luci::CircleTypeInferencePass>());
247
248 if (_options->query(Options::Algorithm::FuseBCQ))
249 {
250 phase.emplace_back(std::make_unique<FuseBCQPass>());
251 }
252
255 phase_runner.attach(&prog);
256 phase_runner.run(phase);
257}
std::vector< std::unique_ptr< Pass > > Phase
Definition ModulePhase.h:29

References luci::CircleOptimizer::Options::FuseBCQ, m, and logo::Restart.

Referenced by entry().

◆ options()

CircleOptimizer::Options * luci::CircleOptimizer::options ( void  )

Definition at line 215 of file CircleOptimizer.cpp.

216{
217 if (_options == nullptr)
218 {
219 _options = std::make_unique<OptimizeOptionsImpl>();
220 }
221
222 return _options.get();
223}

Referenced by entry().

◆ sparsify()

void luci::CircleOptimizer::sparsify ( loco::Graph g) const

Definition at line 426 of file CircleOptimizer.cpp.

427{
428 if (_options->query(Options::Algorithm::SparsifyTensorPass))
429 {
430 std::string tensor_name = _options->param(Options::AlgorithmParameters::Sparsify_tensor_name);
431 std::string str_tarversal_order =
432 _options->param(Options::AlgorithmParameters::Sparsify_traversal_order);
433 std::string str_format = _options->param(Options::AlgorithmParameters::Sparsify_format);
434 std::string str_block_size = _options->param(Options::AlgorithmParameters::Sparsify_block_size);
435 std::string str_block_map = _options->param(Options::AlgorithmParameters::Sparsify_block_map);
436
437 // traversal order
438 std::vector<int32_t> traversal_order = pepper::csv_to_vector<int32_t>(str_tarversal_order);
439 // format
440 std::vector<DimensionType> format;
441 std::istringstream is(str_format);
442 for (char c; is >> c;)
443 {
444 assert(c != ',');
445 if (c == 'd')
446 format.push_back(DimensionType::DENSE);
447 else if (c == 's')
448 format.push_back(DimensionType::SPARSE_CSR);
449 if (is.peek() == ',')
450 is.ignore();
451 }
452 // block size
453 std::vector<int32_t> block_size = pepper::csv_to_vector<int32_t>(str_block_size);
454 // block map
455 std::vector<int32_t> block_map = pepper::csv_to_vector<int32_t>(str_block_map);
456
457 luci::SparsifyTensorPass sparsifier{tensor_name, traversal_order, format, block_size,
458 block_map};
459 sparsifier.run(g);
460 }
461}
const char * tensor_name(const circle::Tensor *tensor)
@ SPARSE_CSR
Pass to sparsify tensor.
bool run(loco::Graph *g) final
Run the pass.

References luci::DENSE, luci::SparsifyTensorPass::run(), luci::SPARSE_CSR, luci::CircleOptimizer::Options::SparsifyTensorPass, and luci::tensor_name().

Referenced by entry().


The documentation for this class was generated from the following files: