Regular expression inference (REI) is a supervised machine learning and program synthesis problem that takes a cost metric for regular expressions, and positive and negative examples of strings as input. It outputs a regular expression that is precise (i.e., accepts all positive and rejects all negative examples), and minimal w.r.t. to the cost metric. We present a novel algorithm for REI over arbitrary alphabets that is enumerative and trades off time for space. Our main algorithmic idea is to implement the search space of regular expressions succinctly as a contiguous matrix of bitvectors. Collectively, the bitvectors represent, as characteristic sequences, all sub-languages of the infix-closure of the union of positive and negative examples. Mathematically, this is a semiring of (a variant of) formal power series. Infix-closure enables bottom-up compositional construction of larger from smaller regular expressions using the operations of our semiring. This minimises data movement and data-dependent branching, hence maximises data-parallelism. In addition, the infix-closure remains unchanged during the search, hence search can be staged: first pre-compute various expensive operations, and then run the compute intensive search process. We provide two C++ implementations, one for general purpose CPUs and one for Nvidia GPUs (using CUDA). We benchmark both on Google Colab Pro: the GPU implementation is on average over 1000x faster than the CPU implementation on the hardest benchmarks.
翻译:正则表达式推理(REI)是一个有监督的机器学习和程序综合问题,它以待推断正则表达式的代价度量、以及正例和反例字符串集合作为输入,输出一个精确(即接受所有正例并拒绝所有反例)且相对于代价度量最小的正则表达式。我们提出一种新颖的、适用于任意字母表的REI算法,该算法采用枚举策略,以时间换取空间。我们主要的算法思想是将正则表达式的搜索空间紧致地表示为一个连续的比特向量矩阵。这些比特向量以特征序列的形式共同表示正例与反例并集的闭包中所有子语言。从数学上看,这对应于一种(某种变体的)形式幂级数的半环。闭包使得我们能够利用半环的运算,以自底向上的组合方式从较小的正则表达式构造出更大的正则表达式。这种方法最大限度地减少了数据移动和依赖于数据的分支,从而最大化数据并行性。此外,在搜索过程中闭包保持不变,因此搜索可以分阶段进行:首先预计算各类高开销操作,然后运行计算密集型的搜索过程。我们提供了两种C++实现,一种适用于通用CPU,另一种适用于Nvidia GPU(使用CUDA)。我们在Google Colab Pro上对两者进行了基准测试:在最困难的基准测试中,GPU实现平均比CPU实现快1000倍以上。