@@ -6,85 +6,100 @@ use criterion::{criterion_group, criterion_main, Criterion};
66use rand:: { thread_rng, Rng } ;
77
88fn counting_benchmark ( c : & mut Criterion ) {
9- let bpe = BytePairEncoding :: cl100k ( ) ;
10- let text = create_test_bytes ( & bpe, 20000 ) ;
9+ for ( name, bpe) in [
10+ ( "cl100k" , BytePairEncoding :: cl100k ( ) ) ,
11+ ( "o200k" , BytePairEncoding :: o200k ( ) ) ,
12+ ] {
13+ let text = create_test_bytes ( & bpe, 20000 ) ;
14+ let fast = IntervalEncoding :: new ( & bpe, & text) ;
1115
12- let fast = IntervalEncoding :: new ( & bpe, & text) ;
13-
14- for bytes in [ 10 , 100 , 1000 , 10000 ] {
15- let mut group = c. benchmark_group ( format ! ( "bytes-{bytes}" ) ) ;
16- group. bench_function ( "hybrid counting" , |b| {
17- b. iter_batched (
18- || thread_rng ( ) . gen_range ( 0 ..text. len ( ) - bytes) ,
19- |start| fast. count ( start..start + bytes) ,
20- criterion:: BatchSize :: SmallInput ,
21- )
22- } ) ;
23- group. bench_function ( "backtrack counting" , |b| {
24- b. iter_batched (
25- || thread_rng ( ) . gen_range ( 0 ..text. len ( ) - bytes) ,
26- |start| bpe. count ( & text[ start..start + bytes] ) ,
27- criterion:: BatchSize :: SmallInput ,
28- )
29- } ) ;
16+ for bytes in [ 10 , 100 , 1000 , 10000 ] {
17+ let mut group = c. benchmark_group ( format ! ( "bpe-{name}-bytes-{bytes}" ) ) ;
18+ group. bench_function ( "hybrid counting" , |b| {
19+ b. iter_batched (
20+ || thread_rng ( ) . gen_range ( 0 ..text. len ( ) - bytes) ,
21+ |start| fast. count ( start..start + bytes) ,
22+ criterion:: BatchSize :: SmallInput ,
23+ )
24+ } ) ;
25+ group. bench_function ( "backtrack counting" , |b| {
26+ b. iter_batched (
27+ || thread_rng ( ) . gen_range ( 0 ..text. len ( ) - bytes) ,
28+ |start| bpe. count ( & text[ start..start + bytes] ) ,
29+ criterion:: BatchSize :: SmallInput ,
30+ )
31+ } ) ;
32+ }
3033 }
3134}
3235
3336fn encoding_benchmark ( c : & mut Criterion ) {
34- let bpe = BytePairEncoding :: cl100k ( ) ;
35- let tiktoken = tiktoken_rs:: cl100k_base ( ) . unwrap ( ) ;
36- let text = create_test_string ( & bpe, 20000 ) ;
37- let input = text. as_bytes ( ) ;
37+ for ( name, bpe, tiktoken) in [
38+ (
39+ "cl100k" ,
40+ BytePairEncoding :: cl100k ( ) ,
41+ tiktoken_rs:: cl100k_base ( ) . unwrap ( ) ,
42+ ) ,
43+ (
44+ "o200k" ,
45+ BytePairEncoding :: o200k ( ) ,
46+ tiktoken_rs:: o200k_base ( ) . unwrap ( ) ,
47+ ) ,
48+ ] {
49+ let text = create_test_string ( & bpe, 20000 ) ;
50+ let input = text. as_bytes ( ) ;
3851
39- for bytes in [ 10 , 100 , 1000 , 10000 ] {
40- let mut group = c. benchmark_group ( format ! ( "bytes-{bytes}" ) ) ;
41- group. bench_function ( "backtracking" , |b| {
42- b. iter_batched (
43- || thread_rng ( ) . gen_range ( 0 ..input. len ( ) - bytes) ,
44- |start| bpe. encode_via_backtracking ( & input[ start..start + bytes] ) ,
45- criterion:: BatchSize :: SmallInput ,
46- )
47- } ) ;
48- group. bench_function ( "heap" , |b| {
49- b. iter_batched (
50- || thread_rng ( ) . gen_range ( 0 ..input. len ( ) - bytes) ,
51- |start| bpe. encode_via_bitfield ( & input[ start..start + bytes] ) ,
52- criterion:: BatchSize :: SmallInput ,
53- )
54- } ) ;
55- group. bench_function ( "dynamic programming" , |b| {
56- b. iter_batched (
57- || thread_rng ( ) . gen_range ( 0 ..input. len ( ) - bytes) ,
58- |start| bpe. encode_via_table ( & input[ start..start + bytes] ) ,
59- criterion:: BatchSize :: SmallInput ,
60- )
61- } ) ;
62- group. bench_function ( "greedy" , |b| {
63- b. iter_batched (
64- || thread_rng ( ) . gen_range ( 0 ..input. len ( ) - bytes) ,
65- |start| bpe. encode_greedy ( & input[ start..start + bytes] ) ,
66- criterion:: BatchSize :: SmallInput ,
67- )
68- } ) ;
69- group. bench_function ( "minimal" , |b| {
70- b. iter_batched (
71- || thread_rng ( ) . gen_range ( 0 ..input. len ( ) - bytes) ,
72- |start| bpe. encode_minimal ( & input[ start..start + bytes] ) ,
73- criterion:: BatchSize :: SmallInput ,
74- )
75- } ) ;
76- group. bench_function ( "tiktoken" , |b| {
77- b. iter_batched (
78- || loop {
79- let start = thread_rng ( ) . gen_range ( 0 ..input. len ( ) - bytes - 1 ) ;
80- if is_char_boundary ( input[ start] ) && is_char_boundary ( input[ start + bytes] ) {
81- return start;
82- }
83- } ,
84- |start| tiktoken. encode_ordinary ( & text[ start..start + bytes] ) ,
85- criterion:: BatchSize :: SmallInput ,
86- )
87- } ) ;
52+ for bytes in [ 10 , 100 , 1000 , 10000 ] {
53+ let mut group = c. benchmark_group ( format ! ( "bpe-{name}-bytes-{bytes}" ) ) ;
54+ group. bench_function ( "backtracking" , |b| {
55+ b. iter_batched (
56+ || thread_rng ( ) . gen_range ( 0 ..input. len ( ) - bytes) ,
57+ |start| bpe. encode_via_backtracking ( & input[ start..start + bytes] ) ,
58+ criterion:: BatchSize :: SmallInput ,
59+ )
60+ } ) ;
61+ group. bench_function ( "heap" , |b| {
62+ b. iter_batched (
63+ || thread_rng ( ) . gen_range ( 0 ..input. len ( ) - bytes) ,
64+ |start| bpe. encode_via_bitfield ( & input[ start..start + bytes] ) ,
65+ criterion:: BatchSize :: SmallInput ,
66+ )
67+ } ) ;
68+ group. bench_function ( "dynamic programming" , |b| {
69+ b. iter_batched (
70+ || thread_rng ( ) . gen_range ( 0 ..input. len ( ) - bytes) ,
71+ |start| bpe. encode_via_table ( & input[ start..start + bytes] ) ,
72+ criterion:: BatchSize :: SmallInput ,
73+ )
74+ } ) ;
75+ group. bench_function ( "greedy" , |b| {
76+ b. iter_batched (
77+ || thread_rng ( ) . gen_range ( 0 ..input. len ( ) - bytes) ,
78+ |start| bpe. encode_greedy ( & input[ start..start + bytes] ) ,
79+ criterion:: BatchSize :: SmallInput ,
80+ )
81+ } ) ;
82+ group. bench_function ( "minimal" , |b| {
83+ b. iter_batched (
84+ || thread_rng ( ) . gen_range ( 0 ..input. len ( ) - bytes) ,
85+ |start| bpe. encode_minimal ( & input[ start..start + bytes] ) ,
86+ criterion:: BatchSize :: SmallInput ,
87+ )
88+ } ) ;
89+ group. bench_function ( "tiktoken" , |b| {
90+ b. iter_batched (
91+ || loop {
92+ let start = thread_rng ( ) . gen_range ( 0 ..input. len ( ) - bytes - 1 ) ;
93+ if is_char_boundary ( input[ start] ) && is_char_boundary ( input[ start + bytes] )
94+ {
95+ return start;
96+ }
97+ } ,
98+ |start| tiktoken. encode_ordinary ( & text[ start..start + bytes] ) ,
99+ criterion:: BatchSize :: SmallInput ,
100+ )
101+ } ) ;
102+ }
88103 }
89104}
90105
0 commit comments