| 16 | namespace CoreML { |
| 17 | |
| 18 | static Result validate(const Specification::Model& spec, const Specification::Pipeline& pipelineParams) { |
| 19 | const int nModels = pipelineParams.models_size(); |
| 20 | if (nModels == 0) { |
| 21 | // empty chain is not allowed |
| 22 | return Result(ResultType::INVALID_MODEL_PARAMETERS, "Pipeline must contain one or more models."); |
| 23 | } |
| 24 | |
| 25 | // build a type table from the list of models |
| 26 | std::unordered_map<std::string, const CoreML::Specification::FeatureDescription*> typeTable; |
| 27 | |
| 28 | // First, populate the type table with the given inputs from the pipeline |
| 29 | for(const auto& input : spec.description().input() ) { |
| 30 | typeTable[input.name()] = &input; |
| 31 | } |
| 32 | |
| 33 | // iterate over models and check the types at each one, making sure they match up with previous versions. |
| 34 | for (const auto& model : pipelineParams.models()) { |
| 35 | const auto& inputs = model.description().input(); |
| 36 | |
| 37 | // validate current transform against previously known valid state |
| 38 | for (const CoreML::Specification::FeatureDescription& arg : inputs) { |
| 39 | |
| 40 | auto it = typeTable.find(arg.name()); |
| 41 | |
| 42 | if(it == typeTable.end()) { |
| 43 | return Result(ResultType::INVALID_MODEL_PARAMETERS, |
| 44 | ("Pipeline: the input '" + arg.name() + "' of model '" + model.description().GetTypeName() |
| 45 | + "' does not present in pipeline input or previous model.")); |
| 46 | } |
| 47 | |
| 48 | if(!CoreML::Specification::isEquivalent(arg, *(it->second))) { |
| 49 | return Result(ResultType::TYPE_MISMATCH, |
| 50 | ("Pipeline: the input '" + arg.name() + "' of model '" + model.description().GetTypeName() |
| 51 | + "' does not match the type previously specified by the pipeline input or the output of a previous model." |
| 52 | + " For the second case, make sure the input and previous model's output has the matching name and shapes.")); |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | // validate the model itself and bail out if it's invalid |
| 57 | Result r = Model::validate(model); |
| 58 | if (!r.good()) { return r; } |
| 59 | |
| 60 | // Now add in the outputs of this model to the mix. |
| 61 | const auto& outputs = model.description().output(); |
| 62 | for (const CoreML::Specification::FeatureDescription& arg : outputs) { |
| 63 | typeTable[arg.name()] = &arg; |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | // Finally, validate that the outputs of the pipeline model match what is outputted by the models. |
| 68 | for(const auto& output : spec.description().output() ) { |
| 69 | auto it = typeTable.find(output.name()); |
| 70 | |
| 71 | if(it == typeTable.end()) { |
| 72 | |
| 73 | return Result(ResultType::INVALID_MODEL_PARAMETERS, |
| 74 | ("Pipeline output '" + output.name() + "' not present in pipeline input or a contained model.")); |
| 75 | } |