michaelfeil commited on
Commit
bb623b4
·
verified ·
1 Parent(s): f441375

Create README.md

Browse files
Files changed (1) hide show
  1. README.md +67 -0
README.md ADDED
@@ -0,0 +1,67 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ```
2
+ #!/usr/bin/env python
3
+ import torch
4
+ from transformers import (
5
+ AutoConfig,
6
+ AutoTokenizer,
7
+ AutoModelForCausalLM,
8
+ LlamaForSequenceClassification,
9
+ )
10
+ # install torch, transformers, accelerate
11
+
12
+ def main():
13
+ # Define the input and output repository names.
14
+ input_model_id = "meta-llama/Meta-Llama-3-70B-Instruct"
15
+ split_2 = input_model_id.split("/")[1]
16
+ output_model_id = f"baseten/example-{split_2}ForSequenceClassification"
17
+
18
+ # Load the original configuration.
19
+ # (If needed, add trust_remote_code=True for custom implementations.)
20
+ config = AutoConfig.from_pretrained(input_model_id)
21
+
22
+ # Update the config for a sequence classification task with 10 labels.
23
+ num_labels = 30
24
+ config.num_labels = num_labels
25
+ config.id2label = {i: f"token activation {i}" for i in range(num_labels)}
26
+ config.label2id = {f"token activation {i}": i for i in range(num_labels)}
27
+
28
+ # Download the tokenizer from the original model.
29
+ tokenizer = AutoTokenizer.from_pretrained(input_model_id)
30
+
31
+ # Load the original causal LM model.
32
+ lm_model = AutoModelForCausalLM.from_pretrained(input_model_id, config=config, device_map="auto", low_cpu_mem_usage=True)
33
+ config.architectures = ["LlamaForSequenceClassification"]
34
+ del lm_model.model
35
+ print("loaded lm model")
36
+ # Initialize the sequence classification model.
37
+ # NOTE: We are using the built-in LlamaForSequenceClassification,
38
+ # which uses a `.score` attribute as the output head.
39
+ seq_cls_model = LlamaForSequenceClassification.from_pretrained(input_model_id, config=config, device_map="auto", low_cpu_mem_usage=True)
40
+
41
+ # --- Initialize the Classification Head ---
42
+ # Here we re-use the first 10 rows from the original LM head
43
+ # (i.e. rows 0 to 9) to initialize the new classification head.
44
+ with torch.no_grad():
45
+ # lm_model.lm_head.weight has shape [vocab_size, hidden_size]
46
+ # We take the first 10 rows to form a [10, hidden_size] weight matrix.
47
+ seq_cls_model.score.weight.copy_(lm_model.lm_head.weight.data[:num_labels, :])
48
+ if lm_model.lm_head.bias is not None:
49
+ seq_cls_model.score.bias.copy_(lm_model.lm_head.bias.data[:num_labels])
50
+
51
+ # Optionally, save the new model locally.
52
+ # save_directory = f"./{output_model_id.replace('/','_')}"
53
+ # seq_cls_model.save_pretrained(save_directory)
54
+ # tokenizer.save_pretrained(save_directory)
55
+
56
+ # Push the new model and tokenizer to the Hub.
57
+ # (Ensure you are authenticated with Hugging Face Hub via `huggingface-cli login`.)
58
+ tokenizer.push_to_hub(output_model_id)
59
+ seq_cls_model.push_to_hub(output_model_id)
60
+
61
+
62
+ print(f"New model pushed to the Hub: {output_model_id}")
63
+
64
+ if __name__ == "__main__":
65
+ main()
66
+
67
+ ```