Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
b961e09
[ML] Harden pytorch_inference by validating TorchScript model graphs
edsavage Mar 1, 2026
4364e99
[ML] Move extract_model_ops to its own subdirectory with README and c…
edsavage Mar 1, 2026
df9b6ee
[ML] Add Elastic HuggingFace models to reference config and harden sc…
edsavage Mar 1, 2026
6f0b8ed
[ML] Remove models that fail tracing from reference config
edsavage Mar 2, 2026
900ad10
[ML] Document rationale for dual forbidden/allowed operation lists
edsavage Mar 2, 2026
3a7b090
[ML] Pin extraction to PyTorch 2.7.1 matching libtorch build version
edsavage Mar 2, 2026
ad5c0af
[ML] Log observed TorchScript ops at DEBUG level during validation
edsavage Mar 2, 2026
08b60c5
[ML] Inline TorchScript graph before validation and forbid prim::Call…
edsavage Mar 2, 2026
41305da
[ML] Enforce maximum graph node count to bound resource consumption
edsavage Mar 2, 2026
c816131
[ML] Add integration tests using real TorchScript modules
edsavage Mar 2, 2026
ed0b710
[ML] Fix clang-format in Main.cc node count check
edsavage Mar 2, 2026
542cc46
[ML] Add allowlist validation script for pytorch_inference models
edsavage Mar 2, 2026
ee71e5b
[ML] Add Elasticsearch ecosystem models to validation test suite
edsavage Mar 2, 2026
0b0a7da
[ML] Validate allowlist against Elasticsearch integration test models
edsavage Mar 2, 2026
c496913
[ML] Add integration tests with malicious TorchScript model fixtures
edsavage Mar 2, 2026
6020a5c
[ML] Replace run_validation.sh with portable CMake script
edsavage Mar 2, 2026
f0b269a
[ML] Wire pytorch_inference validation into primary test targets
edsavage Mar 2, 2026
5ffe729
[ML] Fix clang-format in CModelGraphValidatorTest
edsavage Mar 2, 2026
b00638d
[ML] Simplify Python discovery in run-validation.cmake using find_pro…
edsavage Mar 3, 2026
c1c6eb4
[ML] Use angle-bracket includes in pytorch_inference tests
edsavage Mar 3, 2026
61322ed
[ML] Remove stale extract_model_ops.py superseded by subdirectory ver…
edsavage Mar 3, 2026
26f0235
[ML] Extract shared TorchScript utilities into torchscript_utils.py
edsavage Mar 3, 2026
fb28734
[ML] Use CStringUtils::join for operation list formatting in Main.cc
edsavage Mar 3, 2026
3cf1522
Add CHANGELOG entry for TorchScript model graph validation
edsavage Mar 9, 2026
86c5300
[ML] Add sandbox2 attack model fixtures and C++ test cases
edsavage Mar 10, 2026
7736859
[ML] Add Python test scripts for sandbox2 attack model validation
edsavage Mar 10, 2026
16f5a14
[ML] Extract pytorch_inference test utilities into shared module
edsavage Mar 10, 2026
5b455de
Address review feedback: fail fast, harden error handling, tidy up
edsavage Mar 11, 2026
2923564
Add allowlist drift detection test with golden op-set file
edsavage Mar 11, 2026
b027464
Fix clang-format violations in CModelGraphValidatorTest
edsavage Mar 11, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions bin/pytorch_inference/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ ml_add_executable(pytorch_inference
CBufferedIStreamAdapter.cc
CCmdLineParser.cc
CCommandParser.cc
CModelGraphValidator.cc
CResultWriter.cc
CSupportedOperations.cc
CThreadSettings.cc
)

Expand Down
115 changes: 115 additions & 0 deletions bin/pytorch_inference/CModelGraphValidator.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the following additional limitation. Functionality enabled by the
* files subject to the Elastic License 2.0 may only be used in production when
* invoked by an Elasticsearch process with a license key installed that permits
* use of machine learning features. You may not use this file except in
* compliance with the Elastic License 2.0 and the foregoing additional
* limitation.
*/

#include "CModelGraphValidator.h"

#include "CSupportedOperations.h"

#include <core/CLogger.h>

#include <torch/csrc/jit/passes/inliner.h>

#include <algorithm>

namespace ml {
namespace torch {

CModelGraphValidator::SResult CModelGraphValidator::validate(const ::torch::jit::Module& module) {

TStringSet observedOps;
std::size_t nodeCount{0};
collectModuleOps(module, observedOps, nodeCount);

if (nodeCount > MAX_NODE_COUNT) {
LOG_ERROR(<< "Model graph is too large: " << nodeCount
<< " nodes exceeds limit of " << MAX_NODE_COUNT);
return {false, {}, {}, nodeCount};
}

LOG_DEBUG(<< "Model graph contains " << observedOps.size()
<< " distinct operations across " << nodeCount << " nodes");
for (const auto& op : observedOps) {
LOG_DEBUG(<< " observed op: " << op);
}

auto result = validate(observedOps, CSupportedOperations::ALLOWED_OPERATIONS,
CSupportedOperations::FORBIDDEN_OPERATIONS);
result.s_NodeCount = nodeCount;
return result;
}

CModelGraphValidator::SResult
CModelGraphValidator::validate(const TStringSet& observedOps,
const std::unordered_set<std::string_view>& allowedOps,
const std::unordered_set<std::string_view>& forbiddenOps) {

SResult result;

// Two-pass check: forbidden ops first, then unrecognised. This lets us
// fail fast when a known-dangerous operation is present and avoids the
// cost of scanning for unrecognised ops on a model we will reject anyway.
for (const auto& op : observedOps) {
if (forbiddenOps.contains(op)) {
result.s_IsValid = false;
result.s_ForbiddenOps.push_back(op);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you early exist when result.s_IsValid is false? We are going to drop this model anyway so there is no reason to go through all 1M operations.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. I restructured this as a two-pass check: forbidden ops first, then unrecognised. If any forbidden ops are found we skip the unrecognised-op scan entirely.

}
}

if (result.s_ForbiddenOps.empty()) {
for (const auto& op : observedOps) {
if (allowedOps.contains(op) == false) {
result.s_IsValid = false;
result.s_UnrecognisedOps.push_back(op);
}
}
}

std::sort(result.s_ForbiddenOps.begin(), result.s_ForbiddenOps.end());
std::sort(result.s_UnrecognisedOps.begin(), result.s_UnrecognisedOps.end());

return result;
}

void CModelGraphValidator::collectBlockOps(const ::torch::jit::Block& block,
TStringSet& ops,
std::size_t& nodeCount) {
for (const auto* node : block.nodes()) {
if (++nodeCount > MAX_NODE_COUNT) {
return;
}
ops.emplace(node->kind().toQualString());
for (const auto* subBlock : node->blocks()) {
collectBlockOps(*subBlock, ops, nodeCount);
if (nodeCount > MAX_NODE_COUNT) {
return;
}
}
}
}

void CModelGraphValidator::collectModuleOps(const ::torch::jit::Module& module,
TStringSet& ops,
std::size_t& nodeCount) {
for (const auto& method : module.get_methods()) {
// Inline all method calls so that operations hidden behind
// prim::CallMethod are surfaced. After inlining, any remaining
// prim::CallMethod indicates a call that could not be resolved
// statically and will be flagged as unrecognised.
auto graph = method.graph()->copy();
::torch::jit::Inline(*graph);
collectBlockOps(*graph->block(), ops, nodeCount);
if (nodeCount > MAX_NODE_COUNT) {
return;
}
}
}
}
}
91 changes: 91 additions & 0 deletions bin/pytorch_inference/CModelGraphValidator.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the following additional limitation. Functionality enabled by the
* files subject to the Elastic License 2.0 may only be used in production when
* invoked by an Elasticsearch process with a license key installed that permits
* use of machine learning features. You may not use this file except in
* compliance with the Elastic License 2.0 and the foregoing additional
* limitation.
*/

#ifndef INCLUDED_ml_torch_CModelGraphValidator_h
#define INCLUDED_ml_torch_CModelGraphValidator_h

#include <torch/script.h>

#include <string>
#include <string_view>
#include <unordered_set>
#include <vector>

namespace ml {
namespace torch {

//! \brief
//! Validates TorchScript model computation graphs against a set of
//! allowed operations.
//!
//! DESCRIPTION:\n
//! Provides defense-in-depth by statically inspecting the TorchScript
//! graph of a loaded model and rejecting any model that contains
//! operations not present in the allowlist derived from supported
//! transformer architectures.
//!
//! IMPLEMENTATION DECISIONS:\n
//! The validation walks all methods of the module and its submodules
//! recursively, collecting every distinct operation. Any operation
//! that appears in the forbidden set causes immediate rejection.
//! Any operation not in the allowed set is collected and reported.
//! This ensures that even operations buried in helper methods or
//! nested submodules are inspected.
//!
class CModelGraphValidator {
public:
using TStringSet = std::unordered_set<std::string>;
using TStringVec = std::vector<std::string>;

//! Upper bound on the number of graph nodes we are willing to inspect.
//! Transformer models typically have O(10k) nodes after inlining; a
//! limit of 1M provides generous headroom while preventing a
//! pathologically large graph from consuming unbounded memory or CPU.
static constexpr std::size_t MAX_NODE_COUNT{1000000};

//! Result of validating a model graph.
struct SResult {
bool s_IsValid{true};
TStringVec s_ForbiddenOps;
TStringVec s_UnrecognisedOps;
std::size_t s_NodeCount{0};
};

public:
//! Validate the computation graph of the given module against the
//! supported operation allowlist. Recursively inspects all methods
//! across all submodules.
static SResult validate(const ::torch::jit::Module& module);

//! Validate a pre-collected set of operation names. Useful for
//! unit testing the matching logic without requiring a real model.
static SResult validate(const TStringSet& observedOps,
const std::unordered_set<std::string_view>& allowedOps,
const std::unordered_set<std::string_view>& forbiddenOps);

private:
//! Collect all operation names from a block, recursing into sub-blocks.
static void collectBlockOps(const ::torch::jit::Block& block,
TStringSet& ops,
std::size_t& nodeCount);

//! Inline all method calls and collect ops from the flattened graph.
//! After inlining, prim::CallMethod should not appear; if it does,
//! the call could not be resolved statically and is treated as
//! unrecognised.
static void collectModuleOps(const ::torch::jit::Module& module,
TStringSet& ops,
std::size_t& nodeCount);
};
}
}

#endif // INCLUDED_ml_torch_CModelGraphValidator_h
129 changes: 129 additions & 0 deletions bin/pytorch_inference/CSupportedOperations.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the following additional limitation. Functionality enabled by the
* files subject to the Elastic License 2.0 may only be used in production when
* invoked by an Elasticsearch process with a license key installed that permits
* use of machine learning features. You may not use this file except in
* compliance with the Elastic License 2.0 and the foregoing additional
* limitation.
*/

#include "CSupportedOperations.h"

namespace ml {
namespace torch {

using namespace std::string_view_literals;

const CSupportedOperations::TStringViewSet CSupportedOperations::FORBIDDEN_OPERATIONS = {
// Arbitrary memory access — enables heap scanning, address leaks, and
// ROP chain construction.
"aten::as_strided"sv,
"aten::from_file"sv,
"aten::save"sv,
// After graph inlining, method and function calls should be resolved.
// Their presence indicates an opaque call that cannot be validated.
"prim::CallFunction"sv,
"prim::CallMethod"sv,
};

// Generated by dev-tools/extract_model_ops/extract_model_ops.py against PyTorch 2.7.1.
// Reference models: bert-base-uncased, roberta-base, distilbert-base-uncased,
// google/electra-small-discriminator, microsoft/mpnet-base,
// microsoft/deberta-base, facebook/dpr-ctx_encoder-single-nq-base,
// google/mobilebert-uncased, xlm-roberta-base, elastic/bge-m3,
// elastic/distilbert-base-{cased,uncased}-finetuned-conll03-english,
// elastic/eis-elser-v2, elastic/elser-v2, elastic/hugging-face-elser,
// elastic/multilingual-e5-small-optimized, elastic/splade-v3,
// elastic/test-elser-v2.
Comment on lines +31 to +39
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow 😲 This is a long list!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, it's the union across 10+ architectures plus Elastic-specific and ES integration test models. dev-tools/extract_model_ops/extract_model_ops.py can regenerate it when architectures or PyTorch versions change.

// Additional ops from Elasticsearch integration test models
// (PyTorchModelIT, TextExpansionQueryIT, TextEmbeddingQueryIT).
const CSupportedOperations::TStringViewSet CSupportedOperations::ALLOWED_OPERATIONS = {
// aten operations (core tensor computations)
"aten::Int"sv,
"aten::IntImplicit"sv,
"aten::ScalarImplicit"sv,
"aten::__and__"sv,
"aten::abs"sv,
"aten::add"sv,
"aten::add_"sv,
"aten::arange"sv,
"aten::bitwise_not"sv,
"aten::cat"sv,
"aten::chunk"sv,
"aten::clamp"sv,
"aten::contiguous"sv,
"aten::cumsum"sv,
"aten::div"sv,
"aten::div_"sv,
"aten::dropout"sv,
"aten::embedding"sv,
"aten::expand"sv,
"aten::full_like"sv,
"aten::gather"sv,
"aten::ge"sv,
"aten::gelu"sv,
"aten::hash"sv,
"aten::index"sv,
"aten::index_put_"sv,
"aten::layer_norm"sv,
"aten::len"sv,
"aten::linear"sv,
"aten::log"sv,
"aten::lt"sv,
"aten::manual_seed"sv,
"aten::masked_fill"sv,
"aten::matmul"sv,
"aten::max"sv,
"aten::mean"sv,
"aten::min"sv,
"aten::mul"sv,
"aten::ne"sv,
"aten::neg"sv,
"aten::new_ones"sv,
"aten::ones"sv,
"aten::pad"sv,
"aten::permute"sv,
"aten::pow"sv,
"aten::rand"sv,
"aten::relu"sv,
"aten::repeat"sv,
"aten::reshape"sv,
"aten::rsub"sv,
"aten::scaled_dot_product_attention"sv,
"aten::select"sv,
"aten::size"sv,
"aten::slice"sv,
"aten::softmax"sv,
"aten::sqrt"sv,
"aten::squeeze"sv,
"aten::str"sv,
"aten::sub"sv,
"aten::tanh"sv,
"aten::tensor"sv,
"aten::to"sv,
"aten::transpose"sv,
"aten::type_as"sv,
"aten::unsqueeze"sv,
"aten::view"sv,
"aten::where"sv,
"aten::zeros"sv,
// prim operations (TorchScript graph infrastructure)
"prim::Constant"sv,
"prim::DictConstruct"sv,
"prim::GetAttr"sv,
"prim::If"sv,
"prim::ListConstruct"sv,
"prim::ListUnpack"sv,
"prim::Loop"sv,
"prim::NumToTensor"sv,
"prim::TupleConstruct"sv,
"prim::TupleUnpack"sv,
"prim::device"sv,
"prim::dtype"sv,
"prim::max"sv,
"prim::min"sv,
};
}
}
Loading