|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License |
| 4 | + * 2.0 and the following additional limitation. Functionality enabled by the |
| 5 | + * files subject to the Elastic License 2.0 may only be used in production when |
| 6 | + * invoked by an Elasticsearch process with a license key installed that permits |
| 7 | + * use of machine learning features. You may not use this file except in |
| 8 | + * compliance with the Elastic License 2.0 and the foregoing additional |
| 9 | + * limitation. |
| 10 | + */ |
| 11 | + |
| 12 | +#include "CModelGraphValidator.h" |
| 13 | + |
| 14 | +#include "CSupportedOperations.h" |
| 15 | + |
| 16 | +#include <core/CLogger.h> |
| 17 | + |
| 18 | +#include <torch/csrc/jit/passes/inliner.h> |
| 19 | + |
| 20 | +#include <algorithm> |
| 21 | + |
| 22 | +namespace ml { |
| 23 | +namespace torch { |
| 24 | + |
| 25 | +CModelGraphValidator::SResult CModelGraphValidator::validate(const ::torch::jit::Module& module) { |
| 26 | + |
| 27 | + TStringSet observedOps; |
| 28 | + std::size_t nodeCount{0}; |
| 29 | + collectModuleOps(module, observedOps, nodeCount); |
| 30 | + |
| 31 | + if (nodeCount > MAX_NODE_COUNT) { |
| 32 | + LOG_ERROR(<< "Model graph is too large: " << nodeCount |
| 33 | + << " nodes exceeds limit of " << MAX_NODE_COUNT); |
| 34 | + return {false, {}, {}, nodeCount}; |
| 35 | + } |
| 36 | + |
| 37 | + LOG_DEBUG(<< "Model graph contains " << observedOps.size() |
| 38 | + << " distinct operations across " << nodeCount << " nodes"); |
| 39 | + for (const auto& op : observedOps) { |
| 40 | + LOG_DEBUG(<< " observed op: " << op); |
| 41 | + } |
| 42 | + |
| 43 | + auto result = validate(observedOps, CSupportedOperations::ALLOWED_OPERATIONS, |
| 44 | + CSupportedOperations::FORBIDDEN_OPERATIONS); |
| 45 | + result.s_NodeCount = nodeCount; |
| 46 | + return result; |
| 47 | +} |
| 48 | + |
| 49 | +CModelGraphValidator::SResult |
| 50 | +CModelGraphValidator::validate(const TStringSet& observedOps, |
| 51 | + const std::unordered_set<std::string_view>& allowedOps, |
| 52 | + const std::unordered_set<std::string_view>& forbiddenOps) { |
| 53 | + |
| 54 | + SResult result; |
| 55 | + |
| 56 | + // Two-pass check: forbidden ops first, then unrecognised. This lets us |
| 57 | + // fail fast when a known-dangerous operation is present and avoids the |
| 58 | + // cost of scanning for unrecognised ops on a model we will reject anyway. |
| 59 | + for (const auto& op : observedOps) { |
| 60 | + if (forbiddenOps.contains(op)) { |
| 61 | + result.s_IsValid = false; |
| 62 | + result.s_ForbiddenOps.push_back(op); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + if (result.s_ForbiddenOps.empty()) { |
| 67 | + for (const auto& op : observedOps) { |
| 68 | + if (allowedOps.contains(op) == false) { |
| 69 | + result.s_IsValid = false; |
| 70 | + result.s_UnrecognisedOps.push_back(op); |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + std::sort(result.s_ForbiddenOps.begin(), result.s_ForbiddenOps.end()); |
| 76 | + std::sort(result.s_UnrecognisedOps.begin(), result.s_UnrecognisedOps.end()); |
| 77 | + |
| 78 | + return result; |
| 79 | +} |
| 80 | + |
| 81 | +void CModelGraphValidator::collectBlockOps(const ::torch::jit::Block& block, |
| 82 | + TStringSet& ops, |
| 83 | + std::size_t& nodeCount) { |
| 84 | + for (const auto* node : block.nodes()) { |
| 85 | + if (++nodeCount > MAX_NODE_COUNT) { |
| 86 | + return; |
| 87 | + } |
| 88 | + ops.emplace(node->kind().toQualString()); |
| 89 | + for (const auto* subBlock : node->blocks()) { |
| 90 | + collectBlockOps(*subBlock, ops, nodeCount); |
| 91 | + if (nodeCount > MAX_NODE_COUNT) { |
| 92 | + return; |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +void CModelGraphValidator::collectModuleOps(const ::torch::jit::Module& module, |
| 99 | + TStringSet& ops, |
| 100 | + std::size_t& nodeCount) { |
| 101 | + for (const auto& method : module.get_methods()) { |
| 102 | + // Inline all method calls so that operations hidden behind |
| 103 | + // prim::CallMethod are surfaced. After inlining, any remaining |
| 104 | + // prim::CallMethod indicates a call that could not be resolved |
| 105 | + // statically and will be flagged as unrecognised. |
| 106 | + auto graph = method.graph()->copy(); |
| 107 | + ::torch::jit::Inline(*graph); |
| 108 | + collectBlockOps(*graph->block(), ops, nodeCount); |
| 109 | + if (nodeCount > MAX_NODE_COUNT) { |
| 110 | + return; |
| 111 | + } |
| 112 | + } |
| 113 | +} |
| 114 | +} |
| 115 | +} |
0 commit comments