This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from __future__ import annotations | |
| from copy import deepcopy | |
| from typing import List, Optional | |
| class TrieTree(object): | |
| # Char-level Trie Tree | |
| def __init__(self, is_end: bool, val: str = '', parent: Optional[TrieTree] = None): | |
| """ | |
| A Trie Tree |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import re | |
| import sys | |
| def insert_spaces(text): | |
| text = re.sub(r'([a-zA-Z])([\u4e00-\u9fa5])', r'\1 \2', text) | |
| text = re.sub(r'([\u4e00-\u9fa5])([a-zA-Z])', r'\1 \2', text) | |
| return text | |
| if __name__ == '__main__': |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # A minimal example of how to implement byte-pair encoding (BPE) tokenizer from scratch in Python. | |
| # Reference: https://github.com/karpathy/minbpe | |
| # Contact: bingquanxia@qq.com | |
| def get_stats(byte_arr): | |
| # get the frequency of each byte pair in the text | |
| count = {} | |
| for pair in zip(byte_arr[:-1], byte_arr[1:]): # e.g. pair: (b'a', b' ') | |
| count[pair] = count.get(pair, 0) + 1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # A minimal exmaple of flash attention implemented in Numpy | |
| # Contact: bingquanxia AT qq.com | |
| import unittest | |
| from typing import List | |
| import numpy as np | |
| import torch |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # coding=utf-8 | |
| # Contact: bingquanxia@qq.com | |
| import numpy as np | |
| import torch | |
| import torch.nn as nn | |
| def get_len_mask(b: int, max_len: int, feat_lens: torch.Tensor, device: torch.device) -> torch.Tensor: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from argparse import ArgumentParser | |
| import editdistance | |
| from rich.text import Text | |
| from rich.console import Console | |
| def edit_dist_dp(gt, hyp): | |
| """ | |
| A Dynamic Programming based Python program for edit distance problem |
NewerOlder