Tonic commited on
Commit
4b2de56
·
verified ·
1 Parent(s): 908f3ec

add custom layernorm patch

Browse files
Files changed (1) hide show
  1. tasks/text.py +23 -13
tasks/text.py CHANGED
@@ -7,6 +7,7 @@ import os
7
  from concurrent.futures import ThreadPoolExecutor
8
  from typing import List, Dict, Tuple
9
  import torch
 
10
  from transformers import AutoTokenizer, AutoModelForSequenceClassification, AutoConfig
11
  from huggingface_hub import login
12
  from dotenv import load_dotenv
@@ -30,6 +31,11 @@ router = APIRouter()
30
  DESCRIPTION = "Climate Guard Toxic Agent Classifier"
31
  ROUTE = "/text"
32
 
 
 
 
 
 
33
  class TextClassifier:
34
  def __init__(self):
35
  self.device = "cuda" if torch.cuda.is_available() else "cpu"
@@ -38,27 +44,31 @@ class TextClassifier:
38
 
39
  for attempt in range(max_retries):
40
  try:
41
- # Load config and modify it
42
  self.config = AutoConfig.from_pretrained(model_name)
43
 
44
- # Remove problematic bias parameters
45
- if hasattr(self.config, 'norm_bias'):
46
- delattr(self.config, 'norm_bias')
47
-
48
  # Initialize tokenizer
49
  self.tokenizer = AutoTokenizer.from_pretrained(
50
  model_name,
51
- model_max_length=2048,
52
  padding_side='right',
53
  truncation_side='right'
54
  )
55
 
56
- # Initialize model with modified config
57
- self.model = AutoModelForSequenceClassification.from_pretrained(
58
- model_name,
59
- config=self.config,
60
- ignore_mismatched_sizes=True
61
- )
 
 
 
 
 
 
 
 
62
 
63
  self.model.to(self.device)
64
  self.model.eval()
@@ -81,7 +91,7 @@ class TextClassifier:
81
  batch,
82
  return_tensors="pt",
83
  truncation=True,
84
- max_length=2024,
85
  padding='max_length'
86
  ).to(self.device)
87
 
 
7
  from concurrent.futures import ThreadPoolExecutor
8
  from typing import List, Dict, Tuple
9
  import torch
10
+ import torch.nn as nn
11
  from transformers import AutoTokenizer, AutoModelForSequenceClassification, AutoConfig
12
  from huggingface_hub import login
13
  from dotenv import load_dotenv
 
31
  DESCRIPTION = "Climate Guard Toxic Agent Classifier"
32
  ROUTE = "/text"
33
 
34
+ # Custom LayerNorm that ignores bias parameter
35
+ class CustomLayerNorm(nn.LayerNorm):
36
+ def __init__(self, normalized_shape, eps=1e-5, **kwargs):
37
+ super().__init__(normalized_shape, eps=eps)
38
+
39
  class TextClassifier:
40
  def __init__(self):
41
  self.device = "cuda" if torch.cuda.is_available() else "cpu"
 
44
 
45
  for attempt in range(max_retries):
46
  try:
47
+ # Load config
48
  self.config = AutoConfig.from_pretrained(model_name)
49
 
 
 
 
 
50
  # Initialize tokenizer
51
  self.tokenizer = AutoTokenizer.from_pretrained(
52
  model_name,
53
+ model_max_length=512,
54
  padding_side='right',
55
  truncation_side='right'
56
  )
57
 
58
+ # Patch LayerNorm
59
+ original_layernorm = nn.LayerNorm
60
+ nn.LayerNorm = CustomLayerNorm
61
+
62
+ try:
63
+ # Initialize model with patched LayerNorm
64
+ self.model = AutoModelForSequenceClassification.from_pretrained(
65
+ model_name,
66
+ config=self.config,
67
+ ignore_mismatched_sizes=True
68
+ )
69
+ finally:
70
+ # Restore original LayerNorm
71
+ nn.LayerNorm = original_layernorm
72
 
73
  self.model.to(self.device)
74
  self.model.eval()
 
91
  batch,
92
  return_tensors="pt",
93
  truncation=True,
94
+ max_length=512,
95
  padding='max_length'
96
  ).to(self.device)
97