mlwong commited on
Commit
8e24969
·
1 Parent(s): 6ece534

Fall back to use CPU

Browse files
npc_bert_models/cls_module.py CHANGED
@@ -48,14 +48,8 @@ class NpcBertCLS():
48
 
49
  self.model = AutoModelForSequenceClassification.from_pretrained(self.pretrained_model)
50
  self.tokenizer = AutoTokenizer.from_pretrained(self.pretrained_model)
51
- try:
52
- self.pipeline = hf_pipeline("text-classification", model=self.model, tokenizer=self.tokenizer, device='cuda')
53
- self.pipeline.model.to('cuda')
54
- except Exception as e:
55
- self.pipeline = hf_pipeline("text-classification", model=self.model, tokenizer=self.tokenizer, device='cpu')
56
- self.logger.warning("No GPU!")
57
- self.logger.exception(e)
58
-
59
  @spaces.GPU
60
  def __call__(self, *args: Any) -> Any:
61
  """Performs classification on the given reports.
@@ -82,6 +76,8 @@ class NpcBertCLS():
82
  if len(args[0]) < 10:
83
  return "Not enough text for classification!"
84
 
 
 
85
  pipe_out = self.pipeline(*args)
86
  pipe_out = {o['label']: o['score'] for o in pipe_out}
87
  return pipe_out
 
48
 
49
  self.model = AutoModelForSequenceClassification.from_pretrained(self.pretrained_model)
50
  self.tokenizer = AutoTokenizer.from_pretrained(self.pretrained_model)
51
+ self.pipeline = hf_pipeline("text-classification", model=self.model, tokenizer=self.tokenizer, device_map='auto')
52
+
 
 
 
 
 
 
53
  @spaces.GPU
54
  def __call__(self, *args: Any) -> Any:
55
  """Performs classification on the given reports.
 
76
  if len(args[0]) < 10:
77
  return "Not enough text for classification!"
78
 
79
+ self.logger.info(f"{self.pipeline.model.device = }")
80
+
81
  pipe_out = self.pipeline(*args)
82
  pipe_out = {o['label']: o['score'] for o in pipe_out}
83
  return pipe_out
npc_bert_models/mlm_module.py CHANGED
@@ -47,13 +47,7 @@ class NpcBertMLM():
47
 
48
  self.model = AutoModelForMaskedLM.from_pretrained(self.pretrained_model)
49
  self.tokenizer = AutoTokenizer.from_pretrained(self.pretrained_model)
50
- try:
51
- self.pipeline = hf_pipeline("fill-mask", model=self.model, tokenizer=self.tokenizer, device='cuda')
52
- self.pipeline.model.to('cuda')
53
- except Exception as e:
54
- self.pipeline = hf_pipeline("fill-mask", model=self.model, tokenizer=self.tokenizer, device='cpu')
55
- self.logger.warning("No GPU")
56
- self.logger.exception(e)
57
 
58
  @spaces.GPU
59
  def __call__(self, *args):
@@ -77,6 +71,7 @@ class NpcBertMLM():
77
  msg = "Model was not initialized, have you run load()?"
78
  raise BrokenPipeError(msg)
79
 
 
80
  pipe_out = self.pipeline(*args)
81
  # Just use the first output
82
  if not isinstance(pipe_out[0], dict):
 
47
 
48
  self.model = AutoModelForMaskedLM.from_pretrained(self.pretrained_model)
49
  self.tokenizer = AutoTokenizer.from_pretrained(self.pretrained_model)
50
+ self.pipeline = hf_pipeline("fill-mask", model=self.model, tokenizer=self.tokenizer, device_map='auto')
 
 
 
 
 
 
51
 
52
  @spaces.GPU
53
  def __call__(self, *args):
 
71
  msg = "Model was not initialized, have you run load()?"
72
  raise BrokenPipeError(msg)
73
 
74
+ self.logger.info(f"{self.pipeline.model.device = }")
75
  pipe_out = self.pipeline(*args)
76
  # Just use the first output
77
  if not isinstance(pipe_out[0], dict):
npc_bert_models/summary_module.py CHANGED
@@ -30,25 +30,11 @@ class NpcBertGPT2():
30
 
31
  self.model = EncoderDecoderModel.from_pretrained(self.pretrained_model)
32
  self.tokenizer = AutoTokenizer.from_pretrained(self.pretrained_model)
33
-
34
- try:
35
- self.pipeline = hf_pipeline("text2text-generation",
36
- model=self.model,
37
- tokenizer=self.tokenizer,
38
- device='cuda',
39
- num_beams=4,
40
- do_sample=True,
41
- top_k = 5,
42
- temperature=.95,
43
- early_stopping=True,
44
- no_repeat_ngram_size=5,
45
- max_new_tokens=60)
46
- self.pipeline.model.to('cuda')
47
- except Exception as e:
48
- self.pipeline = hf_pipeline("text2text-generation",
49
  model=self.model,
50
  tokenizer=self.tokenizer,
51
- device='cpu',
52
  num_beams=4,
53
  do_sample=True,
54
  top_k = 5,
@@ -56,8 +42,7 @@ class NpcBertGPT2():
56
  early_stopping=True,
57
  no_repeat_ngram_size=5,
58
  max_new_tokens=60)
59
- self.logger.warning("No GPU!")
60
- self.logger.exception(e)
61
 
62
  @spaces.GPU
63
  def __call__(self, *args):
@@ -80,8 +65,7 @@ class NpcBertGPT2():
80
  msg = "Model was not initialized, have you run load()?"
81
  raise BrokenPipeError(msg)
82
 
83
- self.logger.info(f"Called with arguments {args = }")
84
- self.logger.info("Model: {self.pipeline.model}")
85
  pipe_out, = self.pipeline(*args)
86
  pipe_out = pipe_out['generated_text']
87
  self.logger.info(f"Generated text: {pipe_out}")
 
30
 
31
  self.model = EncoderDecoderModel.from_pretrained(self.pretrained_model)
32
  self.tokenizer = AutoTokenizer.from_pretrained(self.pretrained_model)
33
+
34
+ self.pipeline = hf_pipeline("text2text-generation",
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35
  model=self.model,
36
  tokenizer=self.tokenizer,
37
+ device_map='auto',
38
  num_beams=4,
39
  do_sample=True,
40
  top_k = 5,
 
42
  early_stopping=True,
43
  no_repeat_ngram_size=5,
44
  max_new_tokens=60)
45
+
 
46
 
47
  @spaces.GPU
48
  def __call__(self, *args):
 
65
  msg = "Model was not initialized, have you run load()?"
66
  raise BrokenPipeError(msg)
67
 
68
+ self.logger.info(f"Model: {self.pipeline.model.device = }")
 
69
  pipe_out, = self.pipeline(*args)
70
  pipe_out = pipe_out['generated_text']
71
  self.logger.info(f"Generated text: {pipe_out}")