pass_utils.cc 3.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
// Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "lite/core/mir/pass_utils.h"
16
#include <map>
17 18
#include <set>
#include <string>
19
#include "lite/core/op_registry.h"
20 21 22 23

namespace paddle {
namespace lite {

24 25
using lite_api::Place;

26
void ExpandPlaces(std::set<Place>* places, const Place& place) {
27 28 29 30
  for (const auto& target : lite_api::ExpandValidTargets(place.target)) {
    for (const auto& precision :
         lite_api::ExpandValidPrecisions(place.precision)) {
      for (const auto& layout : lite_api::ExpandValidLayouts(place.layout)) {
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
        places->insert(Place(target, precision, layout));
      }
    }
  }
}

bool KernelRegistered(const std::string name, const Place& place) {
  std::set<Place> places;
  ExpandPlaces(&places, place);
  for (const auto& p : places) {
    if (!KernelRegistry::Global()
             .Create(name, p.target, p.precision, p.layout)
             .empty()) {
      return true;
    }
  }
  return false;
}

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
bool PassMatchesTarget(const mir::Pass& pass,
                       const std::set<TargetType>& targets) {
  // Whether the pass is suitable for targets ? The condition is the
  // intersection of targets and pass's bound targets is not empty, besides the
  // intersection of targets and pass's excluded targets is empty. The formula
  // is as follows: matched = !empty(targets ^ pass.bound_targets) &&
  // empty(targets ^ pass.excluded_targets), where ^ is intersection operation.
  const auto& bound_targets = pass.BoundTargets();
  bool matched = bound_targets.find(TARGET(kAny)) != bound_targets.end();
  std::set<TargetType> inter_bound_targets;
  std::set_intersection(
      bound_targets.begin(),
      bound_targets.end(),
      targets.begin(),
      targets.end(),
      std::inserter(inter_bound_targets, inter_bound_targets.begin()));
  matched |= !inter_bound_targets.empty();
  const auto& excluded_targets = pass.ExcludedTargets();
  matched &= excluded_targets.find(TARGET(kAny)) == excluded_targets.end();
  std::set<TargetType> inter_excluded_targets;
  std::set_intersection(
      excluded_targets.begin(),
      excluded_targets.end(),
      targets.begin(),
      targets.end(),
      std::inserter(inter_excluded_targets, inter_excluded_targets.begin()));
  matched &= inter_excluded_targets.empty();
  return matched;
78 79 80 81 82 83
}

bool PassMatchesKernels(const mir::Pass& pass) {
  const auto& kernels = pass.GetBoundKernels();
  for (const auto& kernel : kernels) {
    for (const auto& place : kernel.second) {
84
      if (!KernelRegistered(kernel.first, place)) {
85
        return false;
86
      }
87 88 89 90 91 92 93
    }
  }
  return true;
}

}  // namespace lite
}  // namespace paddle