TreeEditDistanceNode for Code Similarity Analysis#107
Closed
mgrange1998 wants to merge 2 commits intofacebookresearch:mainfrom
Closed
TreeEditDistanceNode for Code Similarity Analysis#107mgrange1998 wants to merge 2 commits intofacebookresearch:mainfrom
mgrange1998 wants to merge 2 commits intofacebookresearch:mainfrom
Conversation
|
@mgrange1998 has exported this pull request. If you are a Meta employee, you can view the originating Diff in D93109088. |
Summary: Add code similarity analysis infrastructure to PrivacyGuard for measuring code memorization via AST structural comparison. See https://arxiv.org/html/2404.08817v1 This diff introduces: - `PyTreeSitterAttack`: A new attack node that parses target and model-generated code into Abstract Syntax Trees (ASTs) using tree-sitter, then converts them into zss (Zhang-Shasha) Node trees for downstream tree edit distance analysis. Supports Python and C++ via a language registry with explicit imports. - **Partial AST support**: Instead of rejecting malformed code entirely, `parse_code` now leverages tree-sitter's error recovery to produce partial ASTs by filtering out ERROR and MISSING nodes. This allows downstream similarity analysis to still detect code memorization even when model-generated code contains syntax errors. Each record is tagged with a `parse_status` of `"success"` or `"partial"` so downstream consumers can distinguish clean parses from filtered ones. - `CodeSimilarityAnalysisInput`: A new `BaseAnalysisInput` subclass that stores the generation DataFrame with AST columns (`target_ast`, `generated_ast`, `target_parse_status`, `generated_parse_status`), following the existing `TextInclusionAnalysisInput` pattern. - Pins tree-sitter to v0.25.0 in PACKAGE files for the newer Language/Parser API. Differential Revision: D93109033
Summary: Add the tree edit distance analysis node to PrivacyGuard, completing the code memorization measurement pipeline. See https://arxiv.org/html/2404.08817v1 This diff introduces: - `TreeEditDistanceNode`: A new `BaseAnalysisNode` that computes normalized tree edit distance similarity between AST pairs produced by `PyTreeSitterAttack` (from Diff 1). Uses the Zhang-Shasha algorithm via `zss.simple_distance()` with normalization `max(1 - distance / max(n1, n2), 0)` to produce a 0-1 similarity score. Supports per-language grouping when a `language` column is present. - `TreeEditDistanceNodeOutput`: A `BaseAnalysisOutput` dataclass with fields for `num_samples`, `num_both_parsed`, `per_sample_similarity`, `avg_similarity`, and optional `avg_similarity_by_language`. - Updated to work with the partial AST parsing from Diff 1: since `PyTreeSitterAttack` now always produces an AST (full or partial), the analysis node computes similarity for all rows unconditionally. Consumers can use the `parse_status` columns from the input to distinguish full vs partial parse results. - Adds both targets to the `analysis_library` umbrella. Differential Revision: D93109088
fdcaf92 to
7163f3b
Compare
|
This pull request has been merged in 2324689. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary:
Add the tree edit distance analysis node to PrivacyGuard, completing the code memorization measurement pipeline.
This diff introduces:
TreeEditDistanceNode: A newBaseAnalysisNodethat computes normalized tree edit distance similarity between AST pairs produced byPyTreeSitterAttack(from Diff 1). Uses the Zhang-Shasha algorithm viazss.simple_distance()with normalizationmax(1 - distance / max(n1, n2), 0)to produce a 0-1 similarity score. Supports per-language grouping when alanguagecolumn is present.TreeEditDistanceNodeOutput: ABaseAnalysisOutputdataclass with fields fornum_samples,num_both_parsed,per_sample_similarity,avg_similarity, and optionalavg_similarity_by_language.analysis_libraryumbrella.Differential Revision: D93109088