/
torch_base.py
58 lines (51 loc) · 2.02 KB
/
torch_base.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import warnings
class TorchBase:
"""
Utility methods for working pretrained Torch models
"""
def __init__(self, device, quantize=False, min_transformers_version=None):
if min_transformers_version is not None:
import transformers
from packaging import version
if version.parse(transformers.__version__) < version.parse(
min_transformers_version
):
raise Exception(
f"This feature requires transformers>={min_transformers_version}. "
+ "It is usually safe for you to manually upgrade transformers even if ktrain installed a lower version."
)
try:
import torch
except (ImportError, OSError):
raise Exception(
"This capability in ktrain requires PyTorch to be installed. Please install for your environment: "
+ "https://pytorch.org/get-started/locally/"
)
self.quantize = quantize
self.torch_device = device
if self.torch_device is None:
self.torch_device = "cuda" if torch.cuda.is_available() else "cpu"
def quantize_model(self, model):
"""
quantize a model
"""
import torch
if self.torch_device == "cpu":
return torch.quantization.quantize_dynamic(
model, {torch.nn.Linear}, dtype=torch.qint8
)
elif self.torch_device != "cpu":
return model.half()
def device_to_id(self, device_str=None):
device_str = self.torch_device if device_str is None else device_str
if device_str.lower() == "cpu":
return -1
elif device_str.lower() == "cuda":
return 0
elif device_str.lower().startswith("cuda:"):
_, device_id = device_str.split(":")[1]
device_id = int(device_id)
return device_id
else:
warnings.warn("Could not determine device ID - defaulting to -1")
return -1