-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathstash.py
More file actions
97 lines (77 loc) · 3.12 KB
/
Copy pathstash.py
File metadata and controls
97 lines (77 loc) · 3.12 KB
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
"""Copyright (2025) Tilde Research Inc.
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.
"""
from pipeline.data.dataset import load_dataset_by_key
from pipeline.data.dataloader import DataLoader
from pipeline.config import Config
from pipeline.setup import (
setup_model_and_tokenizer,
setup_uploaders,
calculate_machine_params,
display_job_stats,
maybe_add_mlp_attn_hooks,
)
from pipeline.generate import generate_activations
import logging
import time
logging.basicConfig(
level=logging.INFO, format="[%(asctime)s] [Worker-%(process)d] %(levelname)s: %(message)s"
)
logger = logging.getLogger(__name__)
def main():
# Load config from command line args or default
config = Config.from_args()
# Calculate machine-specific parameters
batches_per_machine, start_batch_skip = calculate_machine_params(
config.machine_index, config.data_config["n_batches"], config.num_runs
)
# Add the global start_batch to the machine-specific start_batch
start_batch_skip += config.data_config["start_batch"]
# Load dataset
dataset = load_dataset_by_key(config.data_config["data_key"])
# Setup model and tokenizer with hooks for truncation
model, tokenizer, hooks = setup_model_and_tokenizer(
config.transformer_config, hooks=config.upload_config.get("hooks")
)
logger.info(f"Model loaded: {model}")
added_hooks, added_hook_activations = maybe_add_mlp_attn_hooks(model, hooks)
config.upload_config["hooks"] = hooks
# Display job statistics
display_job_stats(model, hooks, config, batches_per_machine)
# Setup uploaders (one per hook)
uploaders = setup_uploaders(
run_name=config.run_name,
hooks=hooks,
batches_per_upload=config.upload_config["batches_per_upload"],
bucket_name=config.data_config["bucket_name"],
)
logger.info(f"Uploaders loaded: {uploaders}")
# Create dataloader
loader = DataLoader(
dataset=dataset,
tokenizer=tokenizer,
max_length=config.data_config["seq_length"],
batch_size=config.data_config["batch_size"],
start_batch_skip=start_batch_skip,
batches_per_machine=batches_per_machine,
dataset_key=config.data_config["data_key"],
skip_cache=config.data_config["skip_cache"],
clean_added_tokens=config.data_config["clean_added_tokens"],
)
# Run generation
generate_activations(model, loader, config, uploaders, added_hook_activations)
# just in case...
logger.info(
"DONE generating activations. Waiting 5 minutes before exiting, in case there are any remaining uploads."
)
time.sleep(5 * 60)
if __name__ == "__main__":
main()