All models

ContraCode

A Transformer encoder for JavaScript code, pre-trained contrastively to embed programs by what they do.

Language · 242 operations Checked

100%

Showing the top corner; full diagram not loaded yetShowing a small copy; full diagram not loaded yetLarge diagram, not loaded yet

Open SVG

Loading diagram (32 KB)

Open in a new tab

Click or tap a spot to read it up close. Scroll or drag inside the diagram to move through it. Zoom with + and − or Ctrl and scroll. Press 0 for the whole graph. Swipe inside the diagram to move through it. Use + and − to zoom.

At a glance

Parameters
23.3M
Operations
242
Framework
PyTorch
Recurrent
No
Published
EMNLP 2021
Cited by
129 (as of 28 October 2026)
Authors
Paras Jain, Ajay Jain, Tianjun Zhang and others
License
Apache-2.0
Checked
Passed: the trace was replayed operation by operation and matched the model's own output for this input.

Files

The diagram is a drawing of a TorchLens trace of the source code. It contains no code or weights.

Details
Stable ID
m15426
Graph hash
be9a00a716f1489fdd1698c0b4147708de902dfffe8f490b80f828af2ab96571
Skeleton hash
a154a74699ecf7f9881436561b9d404c0c45606e34be0da309002983bb3b4945
Architecture hash
f4a03c222b0cd743258ddd86ea2a42d7180ca10d4fc27a83732589ef2c02e217
Diagram SHA-256
56a65fca6cd39a810c4a4d62ad4c7c378fa9e1e088f44097043498db798354c0
TorchLens version
2.36.0
Renderer
graphviz 2.43.0+pyvips 3.2.0+libvips 8.18.7+render-opts@06c1f0d3
Check
Forward pass replayed in PyTorch

What it is

ContraCode is a contrastive pre-training task for source code that is meant to learn what a program does, not how it is written. The paper's motivation is its finding that the reconstruction-based RoBERTa model is sensitive to source code edits even when the edits preserve semantics.

During pre-training the network has to identify functionally similar variants of a program among many non-equivalent distractors. The variants are generated with an automated source-to-source compiler as a form of data augmentation, implemented with the Babel and Terser compiler infrastructures for JavaScript. The training setup extends Momentum Contrast (MoCo), a method designed for contrastive image representation learning, and uses the InfoNCE loss.

The method is agnostic to the encoder's architecture, and the paper evaluates a 6-layer Transformer, for which it reports 23M parameters, and a 2-layer BiLSTM. This model is the Transformer: it averages the 512-dimensional token embeddings across the sequence, then applies a two-layer MLP to extract a 128-dimensional embedding for the loss. On an adversarial code clone detection benchmark the paper reports that contrastive pre-training outperforms RoBERTa by 39% AUROC. Models are pre-trained on CodeSearchNet, which contains 1,843,099 JavaScript programs.

Key idea. ContraCode pre-trains a code encoder to identify functionally similar variants of a program, generated by compiler transformations, among many non-equivalent distractors.

  • ContraCode
  • Contrastive Code Representation Learning
  • contrastive pre-training
  • code representation learning
  • JavaScript
  • code clone detection
  • type inference
  • source code embeddings
  • compiler-based augmentation

AI-written summary, based on the paper and checked automatically against it; not reviewed by a person. Corrections welcome.

Cite the paper

@misc{jain2022contrastivecoderepresentationlearning,
      title={Contrastive Code Representation Learning}, 
      author={Paras Jain and Ajay Jain and Tianjun Zhang and Pieter Abbeel and Joseph E. Gonzalez and Ion Stoica},
      year={2022},
      eprint={2007.04973},
      archivePrefix={arXiv},
      primaryClass={cs.LG},
      doi={https://doi.org/10.18653/v1/2021.emnlp-main.482},
      url={https://arxiv.org/abs/2007.04973}, 
}