AgentSkillsCN

paper-to-code-implementation

将研究论文转化为可运行的代码实现,采用系统化的操作流程。适用于从论文中实现算法、复现研究成果,或基于已发表的研究进行二次开发时使用。

SKILL.md
--- frontmatter
name: paper-to-code-implementation
description: Systematic methodology for translating research papers into working code implementations. Use when implementing algorithms from papers, reproducing results, or building on published research.

Paper to Code Implementation

Analyze research papers and implement their core algorithms, architectures, or methods in working code with proper verification.

Overview

This skill provides a structured approach for turning research papers into implementations. Use this skill when you need to:

  • Implement a paper's core algorithm or architecture
  • Reproduce published results
  • Adapt a paper's method for a new domain
  • Verify understanding through implementation
  • Build on published research

Implementation Workflow

1. Paper Analysis Phase

Systematically extract implementation details:

A. Core Contribution Identification

  • What is the novel component? (architecture, loss function, training procedure, etc.)
  • What problem does it solve?
  • What are the key equations/algorithms?

B. Architecture Details

  • Layer configurations and dimensions
  • Activation functions and normalization
  • Connection patterns (residual, skip, attention)
  • Input/output specifications

C. Training Procedure

  • Loss function(s) and their components
  • Optimizer and learning rate schedule
  • Regularization techniques
  • Data augmentation strategy

D. Evaluation Protocol

  • Datasets and splits used
  • Metrics and how they're computed
  • Baseline comparisons

2. Reference Gathering

Before implementing, search for:

  • Official code repository (check paper, author websites, GitHub)
  • Third-party implementations (Papers With Code, GitHub)
  • Related implementations that share components
  • Author clarifications (Twitter, OpenReview, GitHub issues)
  • Blog posts or tutorials explaining the method

3. Implementation Strategy

Skeleton First Approach:

python
class PaperModel(nn.Module):
    """
    Implementation of [Paper Title]
    Paper: [URL]

    Key components:
    - [Component 1]: [Brief description]
    - [Component 2]: [Brief description]
    """

    def __init__(self, config):
        super().__init__()
        # TODO: Initialize layers
        pass

    def forward(self, x):
        # TODO: Implement forward pass
        # Equation (1): ...
        # Equation (2): ...
        pass

# Step 2: Implement each component separately
class NovelAttention(nn.Module):
    """
    Implements Equation (3) from the paper:
    Attention(Q, K, V) = softmax(QK^T / sqrt(d_k)) * V
    With modification: [describe paper's modification]
    """
    pass

# Step 3: Implement the loss function
class PaperLoss(nn.Module):
    """
    Implements the training objective from Section X.X
    L = L_main + lambda * L_aux
    """
    pass

4. Common Implementation Patterns

Attention Mechanisms:

python
class MultiHeadAttention(nn.Module):
    def __init__(self, d_model: int, n_heads: int, dropout: float = 0.1):
        super().__init__()
        assert d_model % n_heads == 0
        self.d_k = d_model // n_heads
        self.W_q = nn.Linear(d_model, d_model)
        self.W_k = nn.Linear(d_model, d_model)
        self.W_v = nn.Linear(d_model, d_model)
        self.W_o = nn.Linear(d_model, d_model)
        self.dropout = nn.Dropout(dropout)

    def forward(self, q, k, v, mask=None):
        batch_size = q.size(0)
        q = self.W_q(q).view(batch_size, -1, self.n_heads, self.d_k).transpose(1, 2)
        k = self.W_k(k).view(batch_size, -1, self.n_heads, self.d_k).transpose(1, 2)
        v = self.W_v(v).view(batch_size, -1, self.n_heads, self.d_k).transpose(1, 2)
        scores = torch.matmul(q, k.transpose(-2, -1)) / math.sqrt(self.d_k)
        if mask is not None:
            scores = scores.masked_fill(mask == 0, float('-inf'))
        attn = self.dropout(F.softmax(scores, dim=-1))
        out = torch.matmul(attn, v)
        return self.W_o(out.transpose(1, 2).contiguous().view(batch_size, -1, self.d_model))

Positional Encodings:

python
class SinusoidalPositionalEncoding(nn.Module):
    def __init__(self, d_model: int, max_len: int = 5000):
        super().__init__()
        pe = torch.zeros(max_len, d_model)
        position = torch.arange(0, max_len, dtype=torch.float).unsqueeze(1)
        div_term = torch.exp(torch.arange(0, d_model, 2).float() * (-math.log(10000.0) / d_model))
        pe[:, 0::2] = torch.sin(position * div_term)
        pe[:, 1::2] = torch.cos(position * div_term)
        self.register_buffer('pe', pe.unsqueeze(0))

    def forward(self, x):
        return x + self.pe[:, :x.size(1)]


class RotaryPositionalEmbedding(nn.Module):
    def __init__(self, dim: int, max_len: int = 5000, base: int = 10000):
        super().__init__()
        inv_freq = 1.0 / (base ** (torch.arange(0, dim, 2).float() / dim))
        self.register_buffer('inv_freq', inv_freq)

    def forward(self, x, seq_len: int):
        t = torch.arange(seq_len, device=x.device).type_as(self.inv_freq)
        freqs = torch.einsum('i,j->ij', t, self.inv_freq)
        return torch.cat((freqs, freqs), dim=-1)[None, :, :]

Loss Functions:

python
class ContrastiveLoss(nn.Module):
    """InfoNCE / Contrastive loss for representation learning."""
    def __init__(self, temperature: float = 0.07):
        super().__init__()
        self.temperature = temperature

    def forward(self, z_i, z_j):
        z_i = F.normalize(z_i, dim=1)
        z_j = F.normalize(z_j, dim=1)
        batch_size = z_i.size(0)
        representations = torch.cat([z_i, z_j], dim=0)
        similarity = torch.mm(representations, representations.t()) / self.temperature
        labels = torch.cat([torch.arange(batch_size) + batch_size,
                           torch.arange(batch_size)]).to(z_i.device)
        mask = torch.eye(2 * batch_size, device=z_i.device).bool()
        similarity.masked_fill_(mask, float('-inf'))
        return F.cross_entropy(similarity, labels)

5. Verification Steps

Unit Tests for Components:

python
def test_attention_shapes():
    attn = MultiHeadAttention(d_model=512, n_heads=8)
    x = torch.randn(2, 10, 512)
    out = attn(x, x, x)
    assert out.shape == x.shape

def test_forward_backward():
    model = PaperModel(config)
    x = torch.randn(2, 3, 224, 224)
    y = model(x)
    loss = y.sum()
    loss.backward()  # Should not error

Compare Against Reference:

python
def compare_with_reference(our_model, ref_model, test_input):
    our_model.eval()
    ref_model.eval()
    with torch.no_grad():
        our_out = our_model(test_input)
        ref_out = ref_model(test_input)
    diff = (our_out - ref_out).abs()
    assert diff.max() < 1e-5, f"Max diff: {diff.max():.6f}"

6. Documentation Template

python
"""
Implementation of: [Paper Title]
Authors: [Authors]
Paper: [URL]
Official code: [URL or "Not available"]

This implementation covers:
- [x] Core architecture (Section X)
- [x] Training procedure (Section Y)
- [ ] [Optional component not implemented]

Known differences from paper:
- [Difference 1]: [Reason]

Reproduction status:
- Dataset: [name] - [Achieved metric] vs [Paper metric]
"""

Output Checklist

  1. Paper summary with key algorithmic components identified
  2. Architecture diagram (ASCII or description)
  3. Complete implementation with comments linking to paper sections/equations
  4. Training script with hyperparameters from paper
  5. Verification tests to validate correctness
  6. Known gaps or ambiguities in the paper
  7. References to any external code consulted