forked from Separius/awesome-fast-attention
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate.py
124 lines (100 loc) · 4.31 KB
/
generate.py
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
import re
import json
import urllib.parse
import urllib.request
from time import gmtime, strftime
from operator import attrgetter, itemgetter
import arxiv
from tqdm import tqdm
from natsort import natsorted
github_prefix = 'https://github.com/'
github_prefix_len = len(github_prefix)
arxiv_papers = set()
def get_and_sort_meta_info(json_file):
with open(json_file) as f:
meta_info = json.load(f)
meta_info = natsorted(meta_info, key=itemgetter('arxiv_id'))
with open(json_file, 'w') as f:
f.write(json.dumps(meta_info, indent=2))
return meta_info
class AttrDict(dict):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.__dict__ = self
def find_nth(haystack, needle, n):
start = haystack.find(needle)
while start >= 0 and n > 1:
start = haystack.find(needle, start + len(needle))
n -= 1
return start
def fancy_code(code_link):
if code_link == 'IN_PAPER':
return code_link
if code_link.startswith(github_prefix):
tmp = code_link[github_prefix_len:]
if tmp.endswith('/'):
tmp = tmp[:-1]
if len(re.findall('/', tmp)) > 2:
tmp = tmp[:find_nth(tmp, '/', 2)]
code_name = tmp.split('/')[1]
github_stars = ' ![](https://img.shields.io/github/stars/{}.svg?style=social )'.format(tmp)
else:
code_name = 'code'
github_stars = ''
return f'[{code_name}]({code_link} ){github_stars}'
def query_semantic_scholar(query):
if query == '':
return 'N/A', '-'
try:
global arxiv_papers
if query in arxiv_papers:
raise ValueError('Duplicate Paper {}'.format(query))
arxiv_papers.add(query)
res = json.loads(urllib.request.urlopen("https://api.semanticscholar.org/v1/paper/" + query).read())
count = len(res['citations'])
return (str(count) if count < 999 else '999+'), str(res['year']) + '/??'
except:
return 'N/A', '-'
def query_arxiv_api(arxiv_id):
res = arxiv.query(id_list=[arxiv_id])[0]
return ' '.join(res['title'].strip().replace('\n', ' ').split()), res['arxiv_url'].replace('http:', 'https:'), res['summary']
def fetch_common_parts(paper):
arxiv_id = paper['arxiv_id']
arxiv_date = arxiv_id.split('.')[0]
date_part = '20' + arxiv_date[:2] + '/' + arxiv_date[2:]
citation_part, _ = query_semantic_scholar('arXiv:{}'.format(arxiv_id))
paper_title, paper_link, paper_abstract = query_arxiv_api(arxiv_id)
paper_part = f'[{paper_title}]({paper_link} )'
return citation_part, date_part, paper_part, paper_abstract
def make_expandable(comment):
return f'<details><summary>EXPAND</summary><p>{comment}</p></details>'
def render_complexity(complexity_latex):
complexity = complexity_latex.replace('*', '\\cdot').replace('+', '%2b')
return '![formula](https://render.githubusercontent.com/render/math?math=\\mathcal{O}(' + f'{complexity}))'
def generate_fast_attention_table():
header = [
'|Paper (citations)|Implementation|Computational Complexity|AutoRegressive|Main Idea|',
'|:---:|:---:|:---:|:---:|:---:|']
generated_lines = []
meta_info = get_and_sort_meta_info('FastAttention.json')
for item in tqdm(meta_info):
citation, date, paper, abstract = fetch_common_parts(item)
if 'code' in item:
code = fancy_code(item['code'])
else:
code = '-'
generated_lines.append(
AttrDict(date=date, name=item['name'], paper=paper, auto=':heavy_check_mark:' if item['causal'] else ':x:',
idea=make_expandable(item['comment']), complexity=render_complexity(item['complexity']),
citation=citation, code=code))
generated_lines = sorted(generated_lines, key=attrgetter('date', 'citation'))
generated_lines = ['|{paper} ({citation})|{code}|{complexity}|{auto}|{idea}|'.format(**x)
for x in generated_lines]
return '\n'.join(header + generated_lines)
if __name__ == '__main__':
with open('README_BASE.md', 'r') as f:
readme = f.read()
readme = readme.replace('{{{generation-date}}}', strftime("%a, %d %b %Y %H:%M:%S +0000", gmtime()))
readme = readme.replace('{{{fast-attention-table}}}', generate_fast_attention_table())
with open('README.md', 'w') as f:
f.write(readme)