Approximating CKY with Transformers

Ghazal Khalighinejad, Ollie Liu, Sam Wiseman

Abstract

We investigate the ability of transformer models to approximate the CKY algorithm, using them to directly predict a sentence's parse and thus avoid the CKY algorithm's cubic dependence on sentence length. We find that on standard constituency parsing benchmarks this approach achieves competitive or better performance than comparable parsers that make use of CKY, while being faster. We also evaluate the viability of this approach for parsing under random PCFGs. Here we find that performance declines as the grammar becomes more ambiguous, suggesting that the transformer is not fully capturing the CKY computation. However, we also find that incorporating additional inductive bias is helpful, and we propose a novel approach that makes use of gradients with respect to chart representations in predicting the parse, in analogy with the CKY algorithm being a subgradient of a partition function variant with respect to the chart.

Introduction

Constituency parsing—the task of breaking a sentence into its nested hierarchical structure—has traditionally been constrained by the $O(n^3)$ time complexity of the Cocke-Kasami-Younger (CKY) algorithm. While modern parsers leverage Transformers for rich word representations, they often retain a CKY layer for final tree decoding, preserving the cubic bottleneck. This research explores whether a Transformer can bypass CKY entirely, predicting parse trees in $O(n^2)$ time, and whether such models truly learn the underlying algorithmic logic or merely exploit the statistical regularities of natural language.

Methods

The study investigates two primary architectures for direct parse prediction:

  1. Independent Span Labeling: The model predicts labels for all $O(n^2)$ possible spans in a sentence independently, avoiding global optimization algorithms.
  2. Grad Decoding: This novel approach introduces an inductive bias by treating the CKY algorithm as a subgradient of a partition function. The model defines a scalar "inner score" based on the Transformer's representations and utilizes gradients of this score with respect to hidden layers to inform parse prediction.

Additionally, the authors test Shared Layers (similar to Universal Transformers), where parameters are applied recursively to mimic the iterative nature of chart-based parsing.

Experimental Setup

The models were evaluated on two distinct data types:

  • Standard Benchmarks: English Penn Treebank (PTB), Chinese Penn Treebank (CTB), and various SPMRL datasets.
  • Synthetic PCFGs: Randomly generated Probabilistic Context-Free Grammars with varying ambiguity levels were used to isolate the model's algorithmic reasoning capabilities from natural language semantics.

Results and Discussion

Speed and Accuracy on Natural Language

On standard benchmarks, the Transformer-only approach demonstrated significant efficiency gains. As shown in the table below, the proposed method achieved approximately a 2x speedup over CKY-based baselines.

Inference speed comparison on PTB and CTB datasets.

Despite the increased speed, accuracy remained competitive. On the English PTB, the model achieved an $F_1$ score of 95.64, slightly exceeding the 95.48 $F_1$ of the CKY-based baseline. This suggests that for natural language, local patterns captured by the Transformer are often sufficient for accurate parsing.

Algorithmic Limitations and Inductive Bias

Experiments on synthetic grammars revealed that Transformers struggle as ambiguity increases, indicating they do not inherently "learn" the CKY algorithm. However, the inclusion of shared layers (SL) and grad decoding (GD) significantly improved performance on these challenging tasks.

Performance on synthetic PCFGs with varying rule counts and ambiguity.

As the number of rules $|\mathcal{R}|$ increases from 100 to 800, the standard Transformer's $F_1$ drops from 98.31 to 74.80. The full "Transformer + SL + GD" model maintains a much higher $F_1$ of 81.78 on the most complex grammar, highlighting the importance of algorithm-inspired inductive biases.

Conclusion

While Transformers can effectively approximate CKY for natural language parsing at $O(n^2)$ complexity, they rely heavily on linguistic patterns rather than general algorithmic reasoning. The proposed "grad decoding" technique offers a promising path toward embedding algorithmic inductive biases into neural architectures, though challenges remain in generalizing these models to arbitrary grammars without specialized retraining.

Summary updated

BibTeX
@inproceedings{khalighinejad2023approximating,
  author = {Ghazal Khalighinejad and Ollie Liu and Sam Wiseman},
  title = {{Approximating CKY with Transformers}},
  booktitle = {Conference on Empirical Methods in Natural Language Processing},
  year = {2023},
  url = {https://openreview.net/forum?id=7UvOkmrB8V},
}
Figure