↓ Skip to main content
  1. Tags/

CS336

2026

从倒排索引到 BPE:高性能系统为什么总在避免扫描全集

·2540 words·6 mins
最近在系统学习 Search 和 Information Retrieval 的知识。在看倒排索引(Inverted Index)的时候,我越看越觉得这个结构非常眼熟。直到我想起前段时间写 CS336 作业里 BPE(Byte-Pair Encoding)分词算法的优化逻辑,我才突然意识到:原来这些看似完全不同领域的系统,底层其实都在共享同一种 pattern。

从 CS336 到 ML Infra:理解 Arithmetic Intensity、Accelerator Intensity,以及 Compute Bound / Memory Bound

起因 # 这是 CS336 系列的又一篇。 之前那篇讨论了 FLOPs 到 Latency 之间的鸿沟:FLOPs 相同的两个 GEMM,latency 可能差几倍甚至十几倍。那篇文章的核心结论是 FLOPs 是 workload metric,不是 performance metric,并且引入了 Arithmetic Intensity 和 Roofline Model 作为分析工具。

从 Bytes 到 Tokens:我在 CS336 中实现 BPE Tokenizer

·8149 words·17 mins
最近在做 Stanford CS336(Language Modeling from Scratch)的 Assignment 1,实现了一个 BPE Tokenizer。最初的 trainer 跑 TinyStories 要约 8 分钟,改完后降到约 36 秒。 耗时主要花在 merge 循环里。要判断哪些工作可以省,得先弄清每次 merge 改变了哪些统计量。下面先交代 BPE 的表示和合并规则,再回到这段实现。

从 CS336 的 xfail 看大型工程中的测试设计

·3497 words·7 mins
起因 # 最近在学 Stanford CS336(Language Modeling from Scratch),做 tokenizer 作业时,第一次在测试代码里见到了 pytest.mark.xfail。 具体是这段: 1@pytest.mark.xfail( 2 reason="Tokenizer.encode is expected to take more memory than allotted (1MB)." 3) 4def test_encode_memory_usage(): 5 """ 6 We expect this test to fail, since Tokenizer.encode is not expected to be memory efficient. 7 """ 8 tokenizer = get_tokenizer_from_vocab_merges_path( 9 vocab_path=VOCAB_PATH, 10 merges_path=MERGES_PATH, 11 ) 12 with open(FIXTURES_PATH / "tinystories_sample_5M.txt") as f: 13 contents = f.read() 14 _ = _encode(tokenizer, contents) 我当时第一反应是困惑:既然作者明确知道 encode 会超过 1MB,为什么不直接写成 assert memory > 1MB?或者干脆删掉?为什么要提交一个「预期会失败」的测试?