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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
# coding=utf-8
# Copyright (c) 2025, Arc Institute. All rights reserved.
# Copyright (c) 2026, CENO Team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Byte-level (character-level) tokenizer for CENO."""

import json
import os
from typing import List, Optional, Tuple, Union, Dict, Any
import numpy as np
import torch
from transformers import PreTrainedTokenizer
from transformers.tokenization_utils_base import BatchEncoding
from transformers.utils import logging

logger = logging.get_logger(__name__)

VOCAB_FILES_NAMES = {"vocab_file": "vocab.json"}


class CENOCharLevelTokenizer(PreTrainedTokenizer):
    """
    HuggingFace-style byte-level (character-level) tokenizer for CENO.
    
    This tokenizer converts text directly to byte values using numpy's fromstring,
    which is perfect for DNA sequences and other character-level tasks.
    
    Args:
        vocab_size (int): Size of the vocabulary (default: 512)
        eos_token (str): End of sequence token
        pad_token (str): Padding token  
        unk_token (str): Unknown token
        **kwargs: Additional arguments passed to PreTrainedTokenizer
    """
    
    vocab_files_names = VOCAB_FILES_NAMES
    
    def __init__(
        self,
        vocab_size: int = 512,
        eos_token: str = "<eos>",
        pad_token: str = "<pad>",
        unk_token: str = "<unk>",
        **kwargs
    ):
        self._vocab_size = vocab_size
        self.eod_id = 0
        self.eos_id = 0
        self.pad_id = 1
        self.unk_id = 2
        
        # Build vocabulary - builds the CENO character mapping
        self._vocab = self._build_vocab()
        self._id_to_token = {v: k for k, v in self._vocab.items()}
        
        super().__init__(
            eos_token=eos_token,
            pad_token=pad_token,
            unk_token=unk_token,
            **kwargs
        )
    
    def _build_vocab(self) -> Dict[str, int]:
        """Build vocabulary mapping characters to IDs"""
        vocab = {}
        
        # Add special tokens
        vocab["<unk>"] = 2
        vocab["<pad>"] = 1
        vocab["<eos>"] = 0
        
        # Add printable ASCII characters (32-126)
        for i in range(32, min(127, self._vocab_size)):
            vocab[chr(i)] = i
        
        # Add extended byte values as special tokens
        for i in range(127, self._vocab_size):
            vocab[f"<byte_{i}>"] = i
            
        return vocab
    
    def clamp(self, n: int) -> int:
        """Clamp token ID to valid range, matching the CENO tokenizer implementation"""
        return max(0, min(n, self._vocab_size - 1))
    
    @property
    def vocab_size(self) -> int:
        """Return vocabulary size"""
        return self._vocab_size
    
    def get_vocab(self) -> Dict[str, int]:
        """Return vocabulary dictionary"""
        return self._vocab.copy()
    
    def _tokenize(self, text: str) -> List[int]:
        """
        Tokenize text using numpy's fromstring (byte-level tokenization).
        Byte-level tokenization: text is converted directly to its ASCII byte IDs.
        """
        # Convert text to byte array using numpy (matches the CENO implementation)
        token_ids = np.frombuffer(text.encode("utf-8"), dtype=np.uint8).tolist()
        return token_ids
    
    def _convert_token_to_id(self, token: Union[str, int]) -> int:
        """Convert token to ID"""
        if isinstance(token, int):
            return self.clamp(token)
        
        # Handle string tokens
        if token in self._vocab:
            return self._vocab[token]
        
        # Handle single characters
        if len(token) == 1:
            return self.clamp(ord(token))
        
        # Handle byte tokens
        if token.startswith("<byte_") and token.endswith(">"):
            try:
                byte_val = int(token[6:-1])
                return self.clamp(byte_val)
            except ValueError:
                pass
        
        # Return unknown token ID
        return self._vocab.get(self.unk_token, 0)
    
    def _convert_id_to_token(self, index: int) -> str:
        """Convert ID to token, CENO decode-token behavior"""
        clamped_index = self.clamp(index)
        
        # Handle special cases before interpreting byte values.
        if clamped_index == self.eos_id:
            return self.eos_token
        if clamped_index == self.pad_id:
            return self.pad_token
        if clamped_index == self.unk_id:
            return self.unk_token
        
        # Convert to character if in printable range
        if 32 <= clamped_index <= 126:
            return chr(clamped_index)
        
        # Return byte token for extended range
        return f"<byte_{clamped_index}>"
    
    def convert_tokens_to_string(self, tokens: List[str]) -> str:
        """Convert tokens back to string"""
        result = []
        for token in tokens:
            if token in [self.pad_token, self.eos_token, self.unk_token]:
                continue
            elif token.startswith("<byte_") and token.endswith(">"):
                try:
                    byte_val = int(token[6:-1])
                    result.append(chr(self.clamp(byte_val)))
                except (ValueError, OverflowError):
                    continue
            else:
                result.append(token)
        return "".join(result)
    
    def tokenize(self, text: str, **kwargs) -> List[str]:
        """
        Tokenize text and return string tokens.
        This wraps the numeric tokenization for HuggingFace compatibility.
        """
        # Get numeric tokens
        numeric_tokens = self._tokenize(text)
        
        # Convert to string tokens
        string_tokens = [self._convert_id_to_token(token_id) for token_id in numeric_tokens]
        
        return string_tokens
    
    def encode(
        self,
        text: str,
        add_special_tokens: bool = True,
        padding: bool = False,
        truncation: bool = False,
        max_length: Optional[int] = None,
        return_tensors: Optional[str] = None,
        **kwargs
    ) -> Union[List[int], torch.Tensor]:
        """
        Encode text to token IDs.
        Core tokenization functionality of the CENO byte-level tokenizer.
        """
        # Tokenize to get numeric IDs directly
        token_ids = self._tokenize(text)
        
        # Handle truncation
        if truncation and max_length is not None:
            token_ids = token_ids[:max_length]
        
        # Handle padding
        if padding and max_length is not None:
            if len(token_ids) < max_length:
                token_ids.extend([self.pad_id] * (max_length - len(token_ids)))
        
        # Convert to tensors if requested
        if return_tensors == "pt":
            return torch.tensor([token_ids], dtype=torch.long)
        elif return_tensors == "np":
            return np.array([token_ids], dtype=np.int64)
        
        return token_ids
    
    def decode(
        self,
        token_ids: Union[List[int], torch.Tensor, np.ndarray],
        skip_special_tokens: bool = False,
        clean_up_tokenization_spaces: bool = True,
        **kwargs
    ) -> str:
        """
        Decode token IDs back to text.
        CENO detokenization.
        """
        # Convert to list if tensor or numpy array
        if isinstance(token_ids, torch.Tensor):
            token_ids = token_ids.tolist()
        elif isinstance(token_ids, np.ndarray):
            token_ids = token_ids.tolist()
        
        # Convert IDs to tokens
        tokens = [self._convert_id_to_token(token_id) for token_id in token_ids]
        
        # Filter special tokens if requested
        if skip_special_tokens:
            tokens = [
                token for token in tokens 
                if token not in [self.pad_token, self.eos_token, self.unk_token]
            ]
        
        # Convert tokens to string
        return self.convert_tokens_to_string(tokens)
    
    def batch_encode_plus(
        self,
        batch_text_or_text_pairs: Union[List[str], List[Tuple[str, str]]],
        add_special_tokens: bool = True,
        padding: bool = False,
        truncation: bool = False,
        max_length: Optional[int] = None,
        return_tensors: Optional[str] = None,
        **kwargs
    ) -> BatchEncoding:
        """Batch encode multiple texts"""
        batch_outputs = []
        
        for text in batch_text_or_text_pairs:
            if isinstance(text, tuple):
                # Handle text pairs (not typically used for DNA sequences)
                text = text[0]  # Just use first text for now
            
            encoded = self.encode(
                text,
                add_special_tokens=add_special_tokens,
                padding=False,  # We'll handle padding after
                truncation=truncation,
                max_length=max_length,
                return_tensors=None,
            )
            batch_outputs.append(encoded)
        
        # Handle batch padding
        if padding and max_length is not None:
            max_len = max_length
        elif padding:
            max_len = max(len(output) for output in batch_outputs)
        else:
            max_len = None
        
        if max_len is not None:
            for i, output in enumerate(batch_outputs):
                if len(output) < max_len:
                    batch_outputs[i] = output + [self.pad_id] * (max_len - len(output))
                elif len(output) > max_len:
                    batch_outputs[i] = output[:max_len]
        
        # Convert to tensors if requested
        if return_tensors == "pt":
            batch_outputs = torch.tensor(batch_outputs, dtype=torch.long)
        elif return_tensors == "np":
            batch_outputs = np.array(batch_outputs)
        
        return BatchEncoding({"input_ids": batch_outputs})
    
    def batch_decode(
        self,
        sequences: Union[List[List[int]], torch.Tensor, np.ndarray],
        skip_special_tokens: bool = False,
        clean_up_tokenization_spaces: bool = True,
        **kwargs
    ) -> List[str]:
        """Batch decode multiple sequences"""
        # Convert to list format
        if isinstance(sequences, torch.Tensor):
            sequences = sequences.tolist()
        elif isinstance(sequences, np.ndarray):
            sequences = sequences.tolist()
        
        return [
            self.decode(
                sequence,
                skip_special_tokens=skip_special_tokens,
                clean_up_tokenization_spaces=clean_up_tokenization_spaces,
                **kwargs
            )
            for sequence in sequences
        ]
    
    def save_pretrained(
        self, 
        save_directory: str, 
        legacy_format: Optional[bool] = None,
        filename_prefix: Optional[str] = None,
        push_to_hub: bool = False,
        **kwargs
    ) -> Tuple[str]:
        """
        Save the tokenizer to a directory.
        
        Args:
            save_directory (str): Directory to save the tokenizer
            legacy_format (bool, optional): Whether to save in legacy format
            filename_prefix (str, optional): Prefix for filenames
            push_to_hub (bool): Whether to push to HuggingFace Hub
            **kwargs: Additional arguments
        
        Returns:
            Tuple[str]: Tuple of saved file paths
        """
        if not os.path.isdir(save_directory):
            os.makedirs(save_directory, exist_ok=True)
        
        # Save vocabulary
        vocab_file = os.path.join(
            save_directory, 
            (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["vocab_file"]
        )
        
        with open(vocab_file, "w", encoding="utf-8") as f:
            f.write(json.dumps(self._vocab, indent=2, sort_keys=True, ensure_ascii=False) + "\n")
        
        # Save tokenizer configuration
        config_file = os.path.join(
            save_directory,
            (filename_prefix + "-" if filename_prefix else "") + "tokenizer_config.json"
        )
        
        tokenizer_config = {
            "tokenizer_class": "CENOCharLevelTokenizer",
            "vocab_size": self._vocab_size,
            "eos_token": self.eos_token,
            "pad_token": self.pad_token,
            "unk_token": self.unk_token,
            "eod_id": self.eod_id,
            "eos_id": self.eos_id,
            "pad_id": self.pad_id,
            "model_max_length": getattr(self, 'model_max_length', 1000000),
            "clean_up_tokenization_spaces": True,
            "tokenize_chinese_chars": False,
            "strip_accents": None,
            "do_lower_case": False,
            "do_basic_tokenize": False,
            "never_split": None,
            "tokenizer_type": "CharLevelTokenizer",
            "name_or_path": save_directory,
        }
        
        with open(config_file, "w", encoding="utf-8") as f:
            json.dump(tokenizer_config, f, indent=2, ensure_ascii=False)
        
        # Save special tokens map
        special_tokens_file = os.path.join(
            save_directory,
            (filename_prefix + "-" if filename_prefix else "") + "special_tokens_map.json"
        )
        
        special_tokens_map = {
            "eos_token": self.eos_token,
            "pad_token": self.pad_token,
            "unk_token": self.unk_token,
        }
        
        with open(special_tokens_file, "w", encoding="utf-8") as f:
            json.dump(special_tokens_map, f, indent=2, ensure_ascii=False)
        
        logger.info(f"Tokenizer saved to {save_directory}")
        
        return (vocab_file, config_file, special_tokens_file)
    
    @classmethod
    def from_pretrained(
        cls,
        pretrained_model_name_or_path: Union[str, os.PathLike],
        cache_dir: Optional[str] = None,
        force_download: bool = False,
        local_files_only: bool = False,
        token: Optional[str] = None,
        revision: str = "main",
        **kwargs
    ):
        """
        Load a tokenizer from a pretrained model.
        
        Args:
            pretrained_model_name_or_path (str): Path to directory containing tokenizer files
                or name of a model on HuggingFace Hub
            cache_dir (str, optional): Directory to cache downloaded files
            force_download (bool): Whether to force download even if cached
            local_files_only (bool): Whether to only use local files
            token (str, optional): HuggingFace access token
            revision (str): Model revision to use
            **kwargs: Additional arguments
        
        Returns:
            CENOCharLevelTokenizer: Loaded tokenizer instance
        """
        # Handle local directory
        if os.path.isdir(pretrained_model_name_or_path):
            model_path = pretrained_model_name_or_path
        else:
            # Try to download from HuggingFace Hub
            try:
                from huggingface_hub import snapshot_download
                
                model_path = snapshot_download(
                    repo_id=pretrained_model_name_or_path,
                    cache_dir=cache_dir,
                    force_download=force_download,
                    local_files_only=local_files_only,
                    token=token,
                    revision=revision,
                )
            except ImportError:
                raise ImportError(
                    "huggingface_hub is required to download models from the Hub. "
                    "Install it with: pip install huggingface_hub"
                )
            except Exception as e:
                logger.warning(f"Failed to download from HuggingFace Hub: {e}")
                logger.warning("Falling back to local initialization...")
                return cls(**kwargs)
        
        # Load tokenizer configuration
        config_file = os.path.join(model_path, "tokenizer_config.json")
        config = {}
        
        if os.path.exists(config_file):
            with open(config_file, "r", encoding="utf-8") as f:
                config = json.load(f)
            logger.info(f"Loaded tokenizer config from {config_file}")
        
        # Load special tokens map
        special_tokens_file = os.path.join(model_path, "special_tokens_map.json")
        special_tokens = {}
        
        if os.path.exists(special_tokens_file):
            with open(special_tokens_file, "r", encoding="utf-8") as f:
                special_tokens = json.load(f)
            logger.info(f"Loaded special tokens from {special_tokens_file}")
        
        # Load vocabulary
        vocab_file = os.path.join(model_path, VOCAB_FILES_NAMES["vocab_file"])
        vocab = None
        
        if os.path.exists(vocab_file):
            with open(vocab_file, "r", encoding="utf-8") as f:
                vocab = json.load(f)
            logger.info(f"Loaded vocabulary from {vocab_file}")
        
        # Merge configurations (kwargs override file config)
        init_kwargs = {
            "vocab_size": config.get("vocab_size", 512),
            "eos_token": special_tokens.get("eos_token", config.get("eos_token", "<eos>")),
            "pad_token": special_tokens.get("pad_token", config.get("pad_token", "<pad>")),
            "unk_token": special_tokens.get("unk_token", config.get("unk_token", "<unk>")),
        }
        
        # Override with any provided kwargs
        init_kwargs.update(kwargs)
        
        # Create tokenizer instance
        tokenizer = cls(**init_kwargs)
        
        # Load custom vocabulary if available
        if vocab is not None:
            tokenizer._vocab = vocab
            tokenizer._id_to_token = {v: k for k, v in vocab.items()}
            logger.info("Loaded custom vocabulary")
        
        # Set additional attributes from config
        if config:
            tokenizer.eod_id = config.get("eod_id", 0)
            tokenizer.eos_id = config.get("eos_id", 0)
            tokenizer.pad_id = config.get("pad_id", 1)
            if hasattr(tokenizer, 'model_max_length'):
                tokenizer.model_max_length = config.get("model_max_length", 1000000)
        
        tokenizer.name_or_path = pretrained_model_name_or_path
        logger.info(f"Successfully loaded tokenizer from {pretrained_model_name_or_path}")
        
        return tokenizer
    
    def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]:
        """Save vocabulary to file (legacy method)"""
        if not os.path.isdir(save_directory):
            logger.error(f"Vocabulary path ({save_directory}) should be a directory")
            return
        
        vocab_file = os.path.join(
            save_directory, 
            (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["vocab_file"]
        )
        
        with open(vocab_file, "w", encoding="utf-8") as f:
            f.write(json.dumps(self._vocab, indent=2, sort_keys=True, ensure_ascii=False) + "\n")
        
        return (vocab_file,)
    
    @property
    def unique_identifiers(self) -> Dict[str,Any]:
        """
        Megatron will call .unique_identifiers when it encounters
        this object during its JSON‐dump of the dataset config.
        Must be JSON-serializable.
        """
        return {
            "tokenizer_class": self.__class__.__name__,
            "name_or_path": getattr(self, "name_or_path", None),
            "vocab_size": self.vocab_size,
        }
    # Compatibility methods for the CENO tokenizer interface
    def tokenize_batch(self, text_batch: Union[List[str], str]) -> Union[List[List[int]], List[int]]:
        """Batch tokenization matching the CENO tokenizer interface"""
        if isinstance(text_batch, str):
            return self._tokenize(text_batch)
        return [self._tokenize(text) for text in text_batch]
    
    def detokenize(self, token_ids: Union[List[int], torch.Tensor]) -> str:
        """Alias for decode method matching the CENO tokenizer interface"""
        return self.decode(token_ids, skip_special_tokens=True)
    
    def detokenize_batch(self, token_ids_batch: Union[List[List[int]], torch.Tensor]) -> List[str]:
        """Batch detokenization matching the CENO tokenizer interface"""
        return self.batch_decode(token_ids_batch, skip_special_tokens=True)
    
    @property
    def eod(self) -> int:
        """End of document token ID"""
        return self.eod_id
    
    @property
    def eos(self) -> int:
        """End of sequence token ID"""
        return self.eos_id