Spaces:
Running
Running
🔧🐢 Lazy Transformer Patch ⚡️✨
Browse files# Edit description:
# - Switched from `transformers` dynamic loading to direct BitNet package imports
# - Imported BitNetConfig, BitNetTokenizer, BitNetForCausalLM from `bitnet`
# - Removed AutoConfig/AutoTokenizer/AutoModelForCausalLM calls (left imports for no reason; orphaned)
# - Note to update requirements: `pip install bitnet`
https://pypi.org/project/bitnet/
app.py
CHANGED
@@ -2,6 +2,11 @@ import gradio as gr
|
|
2 |
import torch
|
3 |
from transformers import AutoModelForCausalLM, AutoTokenizer, AutoConfig
|
4 |
|
|
|
|
|
|
|
|
|
|
|
5 |
# Singleton for model and tokenizer
|
6 |
_model = None
|
7 |
_tokenizer = None
|
@@ -10,19 +15,13 @@ def load_model():
|
|
10 |
global _model, _tokenizer
|
11 |
if _model is None or _tokenizer is None:
|
12 |
model_id = "microsoft/bitnet-b1.58-2B-4T"
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
config = AutoConfig.from_pretrained(
|
18 |
-
model_id,
|
19 |
-
trust_remote_code=True
|
20 |
-
)
|
21 |
-
_model = AutoModelForCausalLM.from_pretrained(
|
22 |
model_id,
|
23 |
config=config,
|
24 |
-
torch_dtype=torch.bfloat16
|
25 |
-
trust_remote_code=True
|
26 |
)
|
27 |
return _model, _tokenizer
|
28 |
|
|
|
2 |
import torch
|
3 |
from transformers import AutoModelForCausalLM, AutoTokenizer, AutoConfig
|
4 |
|
5 |
+
# use the official bitnet package to supply the missing code
|
6 |
+
from bitnet.configuration_bitnet import BitNetConfig
|
7 |
+
from bitnet.modeling_bitnet import BitNetForCausalLM
|
8 |
+
from bitnet.tokenization_bitnet import BitNetTokenizer
|
9 |
+
|
10 |
# Singleton for model and tokenizer
|
11 |
_model = None
|
12 |
_tokenizer = None
|
|
|
15 |
global _model, _tokenizer
|
16 |
if _model is None or _tokenizer is None:
|
17 |
model_id = "microsoft/bitnet-b1.58-2B-4T"
|
18 |
+
# load tokenizer, config, and model from the bitnet pip package
|
19 |
+
_tokenizer = BitNetTokenizer.from_pretrained(model_id)
|
20 |
+
config = BitNetConfig.from_pretrained(model_id)
|
21 |
+
_model = BitNetForCausalLM.from_pretrained(
|
|
|
|
|
|
|
|
|
|
|
22 |
model_id,
|
23 |
config=config,
|
24 |
+
torch_dtype=torch.bfloat16
|
|
|
25 |
)
|
26 |
return _model, _tokenizer
|
27 |
|