This repository was archived by the owner on Dec 2, 2025. It is now read-only.
File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 4747#--
4848#-- 23/01/2024 Lyaaaaa
4949#-- - Removed TOKENIZERS_PATH.
50+ #--
51+ #-- 08/05/2024 Lyaaaaa
52+ #-- - Added TORCH_DTYPE_SAFETY.
5053#---------------------------------------------------------------------------
5154
5255import logging
7982DEVICE_MAP = None # None/see documentation
8083TORCH_DTYPE = None # "Auto"/None/torch.dtype/See torch_dtype.py for more info.
8184
85+ # Safeguards
86+ TORCH_DTYPE_SAFETY = True # True/False. If CUDA isn't available, will enforce Torch_Dtype to float32 to avoir error. See issue https://github.com/LyaaaaaGames/AIdventure_Server/issues/31
Original file line number Diff line number Diff line change 203203#-- - Added a log message to display the model's name and its path.
204204#-- - Added a log message to display if cuda is supported.
205205#--
206+ #--
206207#-- - 07/05/2024 Lyaaaaa
207208#-- - Updated _load_tokens to set add_prefix_space to True. It is needed
208209#-- for using bad_words_ids parameter for generation.
210+ #--
211+ #-- - 08/05/2024 Lyaaaaa
212+ #-- - Updated _load_model to force (if config.TORCH_DTYPE_SAFETY is True)
213+ #-- torch_dtype to be set to float32 if cuda isn't available.
214+ #-- Because otherwise, it will lead to an error during generation.
215+ #-- See https://github.com/LyaaaaaGames/AIdventure_Server/issues/31
209216#------------------------------------------------------------------------------
210217
211218from transformers import AutoModelForCausalLM , AutoModelForSeq2SeqLM , AutoTokenizer
@@ -327,6 +334,11 @@ def _load_model(self):
327334 logger .log .debug ("Model settings:" )
328335 logger .log .debug (args )
329336
337+ if not self .is_cuda_available and config .TORCH_DTYPE_SAFETY :
338+ logger .log .warn ("Cuda isn't available." )
339+ logger .log .warn ("Setting torch_dtype to float 32 to avoid error." )
340+ args ["torch_dtype" ] = Torch_Dtypes .dtypes .value [Torch_Dtypes .FLOAT_32 .value ]
341+
330342 self ._Model = AutoModelForCausalLM .from_pretrained (self ._model_path ,
331343 ** args )
332344 except Exception as e :
You can’t perform that action at this time.
0 commit comments