rashmi commited on
Commit
c1671d1
1 Parent(s): 4b09f99
Files changed (1) hide show
  1. app.py +97 -97
app.py CHANGED
@@ -42,118 +42,118 @@ theme = gr.themes.Monochrome(
42
  )
43
 
44
  ### Load the model
45
- class CFG:
46
- num_workers = os.cpu_count()
47
- llm_backbone = "HuggingFaceH4/zephyr-7b-beta"
48
- tokenizer_path = "HuggingFaceH4/zephyr-7b-beta"
49
- tokenizer = AutoTokenizer.from_pretrained(
50
- tokenizer_path, add_prefix_space=False, use_fast=True, trust_remote_code=True, add_eos_token=True
51
- )
52
- batch_size = 1
53
- max_len = 650
54
- seed = 42
55
 
56
- num_labels = 7
57
 
58
- lora = True
59
- lora_r = 4
60
- lora_alpha = 16
61
- lora_dropout = 0.05
62
- lora_target_modules = ""
63
- gradient_checkpointing = True
64
 
65
 
66
- class CustomModel(nn.Module):
67
- """
68
- Model for causal language modeling problem type.
69
- """
70
 
71
- def __init__(self):
72
- super().__init__()
73
 
74
- self.backbone_config = AutoConfig.from_pretrained(
75
- CFG.llm_backbone, trust_remote_code=True
76
- )
77
 
78
- quantization_config = BitsAndBytesConfig(
79
- load_in_4bit=True,
80
- bnb_4bit_compute_dtype=torch.float16,
81
- bnb_4bit_quant_type="nf4",
82
- )
83
 
84
- self.model = AutoModelForCausalLM.from_pretrained(
85
- CFG.llm_backbone,
86
- config=self.backbone_config,
87
- quantization_config=quantization_config,
88
- )
89
 
90
- if CFG.lora:
91
- target_modules = []
92
- for name, module in self.model.named_modules():
93
- if (
94
- isinstance(module, (torch.nn.Linear, torch.nn.Conv1d))
95
- and "head" not in name
96
- ):
97
- name = name.split(".")[-1]
98
- if name not in target_modules:
99
- target_modules.append(name)
100
-
101
- lora_config = LoraConfig(
102
- r=CFG.lora_r,
103
- lora_alpha=CFG.lora_alpha,
104
- target_modules=target_modules,
105
- lora_dropout=CFG.lora_dropout,
106
- bias="none",
107
- task_type="CAUSAL_LM",
108
- )
109
- if CFG.gradient_checkpointing:
110
- self.model.enable_input_require_grads()
111
- self.model = get_peft_model(self.model, lora_config)
112
- self.model.print_trainable_parameters()
113
-
114
- self.classification_head = nn.Linear(
115
- self.backbone_config.vocab_size, CFG.num_labels, bias=False
116
  )
117
- self._init_weights(self.classification_head)
118
-
119
- def _init_weights(self, module):
120
- if isinstance(module, nn.Linear):
121
- module.weight.data.normal_(mean=0.0, std=self.backbone_config.initializer_range)
122
- if module.bias is not None:
123
- module.bias.data.zero_()
124
- elif isinstance(module, nn.Embedding):
125
- module.weight.data.normal_(mean=0.0, std=self.backbone_config.initializer_range)
126
- if module.padding_idx is not None:
127
- module.weight.data[module.padding_idx].zero_()
128
- elif isinstance(module, nn.LayerNorm):
129
- module.bias.data.zero_()
130
- module.weight.data.fill_(1.0)
131
-
132
- def forward(
133
- self,
134
- batch
135
- ):
136
- # disable cache if gradient checkpointing is enabled
137
  if CFG.gradient_checkpointing:
138
- self.model.config.use_cache = False
139
-
140
- self.model.config.pretraining_tp = 1
141
 
142
- output = self.model(
143
- input_ids=batch["input_ids"],
144
- attention_mask=batch["attention_mask"],
145
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
146
 
147
- output.logits = self.classification_head(output[0][:, -1].float())
148
 
149
- # enable cache again if gradient checkpointing is enabled
150
- if CFG.gradient_checkpointing:
151
- self.model.config.use_cache = True
152
 
153
- return output.logits
154
-
155
- model = CustomModel()
156
- ### End Load the model
157
 
158
  def do_inference(full_text):
159
 
 
42
  )
43
 
44
  ### Load the model
45
+ class CFG:
46
+ num_workers = os.cpu_count()
47
+ llm_backbone = "HuggingFaceH4/zephyr-7b-beta"
48
+ tokenizer_path = "HuggingFaceH4/zephyr-7b-beta"
49
+ tokenizer = AutoTokenizer.from_pretrained(
50
+ tokenizer_path, add_prefix_space=False, use_fast=True, trust_remote_code=True, add_eos_token=True
51
+ )
52
+ batch_size = 1
53
+ max_len = 650
54
+ seed = 42
55
 
56
+ num_labels = 7
57
 
58
+ lora = True
59
+ lora_r = 4
60
+ lora_alpha = 16
61
+ lora_dropout = 0.05
62
+ lora_target_modules = ""
63
+ gradient_checkpointing = True
64
 
65
 
66
+ class CustomModel(nn.Module):
67
+ """
68
+ Model for causal language modeling problem type.
69
+ """
70
 
71
+ def __init__(self):
72
+ super().__init__()
73
 
74
+ self.backbone_config = AutoConfig.from_pretrained(
75
+ CFG.llm_backbone, trust_remote_code=True
76
+ )
77
 
78
+ quantization_config = BitsAndBytesConfig(
79
+ load_in_4bit=True,
80
+ bnb_4bit_compute_dtype=torch.float16,
81
+ bnb_4bit_quant_type="nf4",
82
+ )
83
 
84
+ self.model = AutoModelForCausalLM.from_pretrained(
85
+ CFG.llm_backbone,
86
+ config=self.backbone_config,
87
+ quantization_config=quantization_config,
88
+ )
89
 
90
+ if CFG.lora:
91
+ target_modules = []
92
+ for name, module in self.model.named_modules():
93
+ if (
94
+ isinstance(module, (torch.nn.Linear, torch.nn.Conv1d))
95
+ and "head" not in name
96
+ ):
97
+ name = name.split(".")[-1]
98
+ if name not in target_modules:
99
+ target_modules.append(name)
100
+
101
+ lora_config = LoraConfig(
102
+ r=CFG.lora_r,
103
+ lora_alpha=CFG.lora_alpha,
104
+ target_modules=target_modules,
105
+ lora_dropout=CFG.lora_dropout,
106
+ bias="none",
107
+ task_type="CAUSAL_LM",
 
 
 
 
 
 
 
 
108
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
109
  if CFG.gradient_checkpointing:
110
+ self.model.enable_input_require_grads()
111
+ self.model = get_peft_model(self.model, lora_config)
112
+ self.model.print_trainable_parameters()
113
 
114
+ self.classification_head = nn.Linear(
115
+ self.backbone_config.vocab_size, CFG.num_labels, bias=False
116
+ )
117
+ self._init_weights(self.classification_head)
118
+
119
+ def _init_weights(self, module):
120
+ if isinstance(module, nn.Linear):
121
+ module.weight.data.normal_(mean=0.0, std=self.backbone_config.initializer_range)
122
+ if module.bias is not None:
123
+ module.bias.data.zero_()
124
+ elif isinstance(module, nn.Embedding):
125
+ module.weight.data.normal_(mean=0.0, std=self.backbone_config.initializer_range)
126
+ if module.padding_idx is not None:
127
+ module.weight.data[module.padding_idx].zero_()
128
+ elif isinstance(module, nn.LayerNorm):
129
+ module.bias.data.zero_()
130
+ module.weight.data.fill_(1.0)
131
+
132
+ def forward(
133
+ self,
134
+ batch
135
+ ):
136
+ # disable cache if gradient checkpointing is enabled
137
+ if CFG.gradient_checkpointing:
138
+ self.model.config.use_cache = False
139
+
140
+ self.model.config.pretraining_tp = 1
141
+
142
+ output = self.model(
143
+ input_ids=batch["input_ids"],
144
+ attention_mask=batch["attention_mask"],
145
+ )
146
 
147
+ output.logits = self.classification_head(output[0][:, -1].float())
148
 
149
+ # enable cache again if gradient checkpointing is enabled
150
+ if CFG.gradient_checkpointing:
151
+ self.model.config.use_cache = True
152
 
153
+ return output.logits
154
+
155
+ model = CustomModel()
156
+ ### End Load the model
157
 
158
  def do_inference(full_text):
159