sm3455 commited on
Commit
3424103
·
verified ·
1 Parent(s): 75a6520

Delete generate_from_the_stack.py

Browse files
Files changed (1) hide show
  1. generate_from_the_stack.py +0 -150
generate_from_the_stack.py DELETED
@@ -1,150 +0,0 @@
1
- from tree_sitter_parser import LANGUAGE, make_parser, node_to_string
2
- import datasets
3
- import os
4
- import signal
5
- from multiprocessing import Pool
6
-
7
-
8
- TOPLEVEL_DOCSTRING_QUERY = LANGUAGE.query("""
9
- (
10
- (function_definition
11
- name: (identifier)
12
- body: (block .
13
- (expression_statement
14
- (string
15
- (string_start) @docstring.start
16
- (string_content)
17
- (string_end) @docstring.end)))) @function.def
18
- (#eq? @docstring.start "\\\"\\\"\\\"")
19
- (#eq? @docstring.end "\\\"\\\"\\\"")
20
- )
21
- """)
22
-
23
-
24
- def get_fns_with_docstrings(src, tree):
25
- captures = TOPLEVEL_DOCSTRING_QUERY.captures(tree.root_node)
26
- res = []
27
- for capture in captures:
28
- node, ty = capture
29
- if ty != "function.def":
30
- continue
31
- # if the starting col is not 0, then it's not a top-level fn
32
- _, col = node.start_point
33
- if col != 0:
34
- continue
35
- res.append(node_to_string(src, node))
36
- return res
37
-
38
-
39
- def parse_ex(parser, ex):
40
- ex = ex["content"]
41
- try:
42
- buf = bytes(ex, "utf8")
43
- tree = parser.parse(buf)
44
- return get_fns_with_docstrings(buf, tree)
45
- except:
46
- return []
47
-
48
-
49
- # if one parser segfaults, we can just make a new one and other parsers will still be fine
50
- # WE LOVE TREE SITTER!
51
- PARSERS = None
52
-
53
-
54
- def process_chunk(idx_and_chunk):
55
- assert PARSERS is not None
56
- idx, chunk = idx_and_chunk
57
- parser = PARSERS[idx]
58
- chunk_new_funs = set()
59
- for ex in chunk:
60
- chunk_new_funs.update(parse_ex(parser, ex))
61
- return chunk_new_funs
62
-
63
-
64
- def main(args):
65
- global PARSERS
66
- ds = datasets.load_dataset(
67
- args.dataset,
68
- data_dir=args.data_dir,
69
- split="train",
70
- )
71
- funs = set()
72
- PARSERS = [make_parser() for _ in range(args.num_workers)]
73
- total_len = len(ds)
74
- CHUNK_SIZE = 1000 * args.num_workers
75
-
76
- print(f"Total length: {total_len}")
77
- print(f"Chunk size: {CHUNK_SIZE}")
78
-
79
- chunk = []
80
- p = Pool(args.num_workers)
81
- for i, ex in enumerate(ds):
82
- if i % (total_len // 100) == 0:
83
- print(f"{i}/{total_len}")
84
- try:
85
- chunk.append(ex)
86
- if len(chunk) == CHUNK_SIZE or i == total_len - 1:
87
- print(f"Processing chunk {i // CHUNK_SIZE}")
88
- # divide the chunk into NUM_WORKERS chunks
89
- subchunk_size = len(chunk) // args.num_workers
90
- subchunks = [chunk[i:i + subchunk_size]
91
- for i in range(0, len(chunk), subchunk_size)]
92
- new_funs_iter = p.imap(
93
- process_chunk, [(i, subchunk) for i, subchunk in enumerate(subchunks)])
94
- print("Getting new functions")
95
- len_before = len(funs)
96
- while True:
97
- try:
98
- def timeout_handler(_, __):
99
- raise KeyboardInterrupt # it's fineeeeeee
100
- signal.signal(signal.SIGALRM, timeout_handler)
101
- signal.alarm(60)
102
- funs.update(next(new_funs_iter))
103
- signal.alarm(0)
104
- except KeyboardInterrupt:
105
- signal.alarm(0)
106
- print("Keyboard interrupt. Terminating pool")
107
- p.terminate()
108
- p = Pool(args.num_workers)
109
- break
110
- except StopIteration:
111
- break
112
- except Exception as e:
113
- print(e)
114
-
115
- signal.alarm(0)
116
-
117
- PARSERS = [make_parser() for _ in range(args.num_workers)]
118
-
119
- print(
120
- f"Done processing chunk {i // CHUNK_SIZE}. Got {len(funs) - len_before} new functions")
121
-
122
- chunk = []
123
- except Exception as e:
124
- print(e)
125
- chunk = []
126
-
127
- if i == total_len - 1:
128
- break
129
-
130
- p.close()
131
-
132
- new_ds_dict = {
133
- "content": list(funs),
134
- "id": list(range(len(funs)))
135
- }
136
-
137
- new_ds = datasets.Dataset.from_dict(new_ds_dict)
138
- new_ds.push_to_hub(args.push, private=True)
139
-
140
-
141
- if __name__ == "__main__":
142
- import argparse
143
- parser = argparse.ArgumentParser()
144
- parser.add_argument("--num_workers", type=int, default=os.cpu_count())
145
- parser.add_argument("--dataset", type=str,
146
- default="bigcode/the-stack-dedup")
147
- parser.add_argument("--data_dir", type=str, default="data/python")
148
- parser.add_argument("--push", type=str, required=True)
149
- args = parser.parse_args()
150
- main(args)