Mock Tensorflow
Contents
Mock Tensorflow#
This tutorial is available as an IPython notebook at Malaya/example/mock-tensorflow.
Starting with Malaya 5.0.1 and malaya-boilerplate 0.0.24, Tensorflow is no longer necessary to install and if Tensorflow absent, it will be replaced with mock object.
Let say you installed Malaya on a fresh machine or using a virtual environment,
[1]:
!~/huggingface/bin/pip3 freeze | grep 'tensorflow'
This virtual environment does not have Tensorflow installed.
[2]:
import malaya
/home/husein/dev/malaya-boilerplate/malaya_boilerplate/frozen_graph.py:46: UserWarning: Cannot import beam_search_ops from Tensorflow 1, ['malaya.jawi_rumi.deep_model', 'malaya.phoneme.deep_model', 'malaya.rumi_jawi.deep_model', 'malaya.stem.deep_model'] for stemmer will not available to use, make sure Tensorflow 1 version >= 1.15
warnings.warn(
/home/husein/huggingface/lib/python3.8/site-packages/tqdm/auto.py:22: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html
from .autonotebook import tqdm as notebook_tqdm
bs4 is not installed, `malaya.text.function.remove_html_tags` will use regex
[3]:
import tensorflow as tf
tf.__version__
[3]:
<malaya_boilerplate.Mock at 0x7f9b7cdddca0>
As you can see, everything is a Mock object, what happened if you tried to call a model using Tensorflow as backend?
Call Tensorflow model#
[4]:
malaya.sentiment.available_transformer()
[4]:
Size (MB) | Quantized Size (MB) | macro precision | macro recall | macro f1-score | |
---|---|---|---|---|---|
bert | 425.6 | 111.00 | 0.93182 | 0.93442 | 0.93307 |
tiny-bert | 57.4 | 15.40 | 0.93390 | 0.93141 | 0.93262 |
albert | 48.6 | 12.80 | 0.91228 | 0.91929 | 0.91540 |
tiny-albert | 22.4 | 5.98 | 0.91442 | 0.91646 | 0.91521 |
xlnet | 446.6 | 118.00 | 0.92390 | 0.92629 | 0.92444 |
alxlnet | 46.8 | 13.30 | 0.91896 | 0.92589 | 0.92198 |
[5]:
model = malaya.sentiment.transformer(model = 'alxlnet')
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Cell In[5], line 1
----> 1 model = malaya.sentiment.transformer(model = 'alxlnet')
File ~/huggingface/lib/python3.8/site-packages/herpetologist/__init__.py:100, in check_type.<locals>.check(*args, **kwargs)
97 for p, v in kwargs.items():
98 nested_check(v, p)
--> 100 return func(*args, **kwargs)
File ~/dev/malaya/malaya/sentiment.py:112, in transformer(model, quantized, **kwargs)
108 if model not in _transformer_availability:
109 raise ValueError(
110 'model not supported, please check supported models from `malaya.sentiment.available_transformer()`.'
111 )
--> 112 return classification.transformer(
113 module='sentiment-v2',
114 label=label,
115 model=model,
116 quantized=quantized,
117 **kwargs
118 )
File ~/dev/malaya/malaya/supervised/classification.py:141, in transformer(module, label, model, sigmoid, quantized, **kwargs)
122 def transformer(
123 module,
124 label,
(...)
128 **kwargs,
129 ):
130 path = check_file(
131 file=model,
132 module=module,
(...)
139 **kwargs,
140 )
--> 141 g = load_graph(path['model'], **kwargs)
143 if sigmoid:
144 selected_model = SIGMOID_MODEL[model]
File ~/dev/malaya/malaya/function/__init__.py:38, in load_graph(frozen_graph_filename, **kwargs)
37 def load_graph(frozen_graph_filename, **kwargs):
---> 38 return frozen_graph.load_graph(package, frozen_graph_filename, **kwargs)
File ~/dev/malaya-boilerplate/malaya_boilerplate/frozen_graph.py:241, in load_graph(package, frozen_graph_filename, **kwargs)
238 precision_mode = kwargs.get('precision_mode', 'FP32').upper()
239 logger.debug(f'precision_mode: {precision_mode}')
--> 241 device = get_device(**kwargs)
242 logger.debug(f'device: {device}')
244 t5_graph = kwargs.get('t5_graph', False)
File ~/dev/malaya-boilerplate/malaya_boilerplate/frozen_graph.py:111, in get_device(**kwargs)
107 if device_type not in {'XLA_CPU', 'XLA_CPU_JIT', 'CPU', 'GPU', 'XLA_GPU'}:
108 raise ValueError(
109 "`device` from `device:{no}` must one of ['XLA_CPU', 'XLA_CPU_JIT', 'CPU', 'GPU', 'XLA_GPU']"
110 )
--> 111 gpus = available_gpu()
113 if 'GPU' in device:
114 if not len(gpus):
File ~/dev/malaya-boilerplate/malaya_boilerplate/utils.py:117, in available_gpu(refresh)
108 def available_gpu(refresh=False):
109 """
110 Get list of GPUs and memory limit from `tensorflow.python.client.device_lib.list_local_devices()`.
111
(...)
114 result : List[str]
115 """
--> 117 devices = available_device(refresh=refresh)
118 return [d for d in devices if 'GPU' in d[0] and 'XLA' not in d[0]]
File ~/dev/malaya-boilerplate/malaya_boilerplate/utils.py:96, in available_device(refresh)
93 if DEVICES is None and not refresh:
94 from tensorflow.python.client import device_lib
---> 96 DEVICES = device_lib.list_local_devices()
97 DEVICES = [
98 (
99 i.name.replace('/device:', ''),
(...)
102 for i in DEVICES
103 ]
105 return DEVICES
File ~/dev/malaya-boilerplate/malaya_boilerplate/__init__.py:48, in Mock.__call__(self, *args, **kwargs)
47 def __call__(self, *args, **kwargs):
---> 48 raise ValueError(f'{self.parent_name} is not installed. Please install it and try again.')
ValueError: tensorflow is not installed. Please install it and try again.
Call PyTorch model#
Starting from malaya 4.9, we are going to focus Pytorch for main deep learning backend.
[5]:
model = malaya.similarity.semantic.huggingface()
[8]:
model.predict_proba(['tak suka ayam'], ['hate chicken'])
You're using a T5TokenizerFast tokenizer. Please note that with a fast tokenizer, using the `__call__` method is faster than using a method to encode the text followed by a call to the `pad` method to get a padded encoding.
[8]:
array([0.86549413], dtype=float32)