Marcin-XStudio commited on
Commit
567d5c6
·
1 Parent(s): 6b8ad8c

speed up inference

Browse files
Files changed (2) hide show
  1. .DS_Store +0 -0
  2. app.py +12 -8
.DS_Store ADDED
Binary file (8.2 kB). View file
 
app.py CHANGED
@@ -47,11 +47,11 @@ print("Device name:", torch.cuda.get_device_name(0))
47
 
48
  # If lower memory usage needed:
49
 
50
- # bnb_config = BitsAndBytesConfig(
51
- # load_in_4bit=True,
52
- # bnb_4bit_use_double_quant=True,
53
- # bnb_4bit_quant_type="nf4"
54
- # )
55
  # model = AutoModelForCausalLM.from_pretrained(
56
  # MODEL_CACHE,
57
  # quantization_config=bnb_config,
@@ -81,8 +81,8 @@ def load_model():
81
  local_files_only=True,
82
  torch_dtype=dtype,
83
  trust_remote_code=True,
84
- # quantization_config=bnb_config,
85
- # no_split_module_classes=["Block"],
86
  device_map="auto"
87
  ).to(device).eval()
88
  # tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
@@ -94,6 +94,9 @@ def load_model():
94
  )
95
  print("✅ Model and tokenizer loaded from", MODEL_CACHE)
96
 
 
 
 
97
  def predict_NuExtract(texts, template, batch_size=1, max_length=5096, max_new_tokens=1024):
98
  print("Starting NuExtract prediction...", flush=True)
99
  start_time = time.perf_counter()
@@ -103,6 +106,7 @@ def predict_NuExtract(texts, template, batch_size=1, max_length=5096, max_new_to
103
  "### Instruction:\n"
104
  "Remplis la template JSON avec les informations extraits du texte.\n"
105
  "Exemples types de formations : CAP Boucherie, Licence Pro Métiers de l’Énergétique, Baccalauréat Général\n"
 
106
  "Exemples catégories de formations : Transport, énergie, langues, esthétique\n"
107
  "Exemples mobilités : permis B, permis C, permis D. si y'a juste la mention de permis on considère que c'est le permis B\n"
108
  "Output *only* the completed JSON.\n"
@@ -126,7 +130,7 @@ def predict_NuExtract(texts, template, batch_size=1, max_length=5096, max_new_to
126
  max_length=max_length
127
  ).to(device)
128
  print(f"Generating outputs with model for batch {i//batch_size+1}...", flush=True)
129
- ids = model.generate(**enc, max_new_tokens=max_new_tokens, use_cache=False)
130
  outputs += tokenizer.batch_decode(ids, skip_special_tokens=True)
131
  print("Outputs generated.", flush=True)
132
  elapsed = time.perf_counter() - start_time
 
47
 
48
  # If lower memory usage needed:
49
 
50
+ bnb_config = BitsAndBytesConfig(
51
+ load_in_4bit=True,
52
+ bnb_4bit_use_double_quant=True,
53
+ bnb_4bit_quant_type="nf4"
54
+ )
55
  # model = AutoModelForCausalLM.from_pretrained(
56
  # MODEL_CACHE,
57
  # quantization_config=bnb_config,
 
81
  local_files_only=True,
82
  torch_dtype=dtype,
83
  trust_remote_code=True,
84
+ quantization_config=bnb_config,
85
+ no_split_module_classes=["Block"],
86
  device_map="auto"
87
  ).to(device).eval()
88
  # tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
 
94
  )
95
  print("✅ Model and tokenizer loaded from", MODEL_CACHE)
96
 
97
+ if torch.__version__ >= "2.0":
98
+ model = torch.compile(model)
99
+
100
  def predict_NuExtract(texts, template, batch_size=1, max_length=5096, max_new_tokens=1024):
101
  print("Starting NuExtract prediction...", flush=True)
102
  start_time = time.perf_counter()
 
106
  "### Instruction:\n"
107
  "Remplis la template JSON avec les informations extraits du texte.\n"
108
  "Exemples types de formations : CAP Boucherie, Licence Pro Métiers de l’Énergétique, Baccalauréat Général\n"
109
+ "Si le text contient des mentions de diplomes ou de titres de formations, certificats ou Attestation on les considère comme un item éducation\n"
110
  "Exemples catégories de formations : Transport, énergie, langues, esthétique\n"
111
  "Exemples mobilités : permis B, permis C, permis D. si y'a juste la mention de permis on considère que c'est le permis B\n"
112
  "Output *only* the completed JSON.\n"
 
130
  max_length=max_length
131
  ).to(device)
132
  print(f"Generating outputs with model for batch {i//batch_size+1}...", flush=True)
133
+ ids = model.generate(**enc, max_new_tokens=max_new_tokens, num_beams=1, use_cache=True)
134
  outputs += tokenizer.batch_decode(ids, skip_special_tokens=True)
135
  print("Outputs generated.", flush=True)
136
  elapsed = time.perf_counter() - start_time