/session/quantization/quantize-constituency-model.ipynb

https://github.com/huseinzol05/Malaya · Jupyter · 471 lines · 471 code · 0 blank · 0 comment · 0 complexity · 620ff2d6942eb1c7423a043c77a10a75 MD5 · raw file

  1. {
  2. "cells": [
  3. {
  4. "cell_type": "code",
  5. "execution_count": 1,
  6. "metadata": {},
  7. "outputs": [],
  8. "source": [
  9. "import os\n",
  10. "os.environ['CUDA_VISIBLE_DEVICES'] = ''"
  11. ]
  12. },
  13. {
  14. "cell_type": "code",
  15. "execution_count": 2,
  16. "metadata": {},
  17. "outputs": [],
  18. "source": [
  19. "S3_PATH_CONSTITUENCY = {\n",
  20. " 'bert': {\n",
  21. " 'model': 'v38/constituency/bert-base.pb',\n",
  22. " 'dictionary': 'v38/constituency/vocab-bert-base.json',\n",
  23. " 'vocab': 'tokenizer/sp10m.cased.bert.vocab',\n",
  24. " 'tokenizer': 'tokenizer/sp10m.cased.bert.model',\n",
  25. " },\n",
  26. " 'tiny-bert': {\n",
  27. " 'model': 'v38/constituency/tiny-bert.pb',\n",
  28. " 'dictionary': 'v38/constituency/vocab-tiny-bert.json',\n",
  29. " 'vocab': 'tokenizer/sp10m.cased.bert.vocab',\n",
  30. " 'tokenizer': 'tokenizer/sp10m.cased.bert.model',\n",
  31. " },\n",
  32. "# 'albert': {\n",
  33. "# 'model': 'v38/constituency/albert-base.pb',\n",
  34. "# 'dictionary': 'v38/constituency/vocab-albert-base.json',\n",
  35. "# 'vocab': 'tokenizer/sp10m.cased.v10.vocab',\n",
  36. "# 'tokenizer': 'tokenizer/sp10m.cased.v10.model',\n",
  37. "# },\n",
  38. "# 'tiny-albert': {\n",
  39. "# 'model': 'v38/constituency/albert-tiny.pb',\n",
  40. "# 'dictionary': 'v38/constituency/vocab-albert-tiny.json',\n",
  41. "# 'vocab': 'tokenizer/sp10m.cased.v10.vocab',\n",
  42. "# 'tokenizer': 'tokenizer/sp10m.cased.v10.model',\n",
  43. "# },\n",
  44. "# 'xlnet': {\n",
  45. "# 'model': 'v40/constituency/xlnet-base.pb',\n",
  46. "# 'quantized': 'v40/constituency/xlnet-base.pb.quantized',\n",
  47. "# 'dictionary': 'v40/constituency/vocab-xlnet-base.json',\n",
  48. "# 'vocab': 'tokenizer/sp10m.cased.v9.vocab',\n",
  49. "# 'tokenizer': 'tokenizer/sp10m.cased.v9.model',\n",
  50. "# },\n",
  51. "}"
  52. ]
  53. },
  54. {
  55. "cell_type": "code",
  56. "execution_count": 3,
  57. "metadata": {},
  58. "outputs": [
  59. {
  60. "name": "stdout",
  61. "output_type": "stream",
  62. "text": [
  63. "bert\n",
  64. "tiny-bert\n"
  65. ]
  66. }
  67. ],
  68. "source": [
  69. "for k in S3_PATH_CONSTITUENCY.keys():\n",
  70. " if k not in ['multinomial']:\n",
  71. " print(k)\n",
  72. " os.system(f\"wget https://f000.backblazeb2.com/file/malaya-model/{S3_PATH_CONSTITUENCY[k]['model']}\")"
  73. ]
  74. },
  75. {
  76. "cell_type": "code",
  77. "execution_count": 4,
  78. "metadata": {},
  79. "outputs": [],
  80. "source": [
  81. "import tensorflow as tf\n",
  82. "from tensorflow.tools.graph_transforms import TransformGraph\n",
  83. "from glob import glob\n",
  84. "tf.set_random_seed(0)"
  85. ]
  86. },
  87. {
  88. "cell_type": "code",
  89. "execution_count": 5,
  90. "metadata": {},
  91. "outputs": [
  92. {
  93. "data": {
  94. "text/plain": [
  95. "['tiny-bert.pb',\n",
  96. " 'albert-tiny-similarity.pb',\n",
  97. " 'bert-base.pb',\n",
  98. " 'xlnet-base-similarity.pb',\n",
  99. " 'albert-base-similarity.pb',\n",
  100. " 'bert-base-similarity.pb',\n",
  101. " 'alxlnet-base-similarity.pb',\n",
  102. " 'tiny-bert-similarity.pb']"
  103. ]
  104. },
  105. "execution_count": 5,
  106. "metadata": {},
  107. "output_type": "execute_result"
  108. }
  109. ],
  110. "source": [
  111. "pbs = glob('*.pb')\n",
  112. "pbs"
  113. ]
  114. },
  115. {
  116. "cell_type": "code",
  117. "execution_count": 7,
  118. "metadata": {},
  119. "outputs": [
  120. {
  121. "data": {
  122. "text/plain": [
  123. "[(<tf.Tensor 'import/bert/encoder/layer_8/attention/output/LayerNorm/batchnorm/mul_2:0' shape=(?, ?, 312) dtype=float32>,),\n",
  124. " (<tf.Tensor 'import/bert/encoder/layer_8/attention/output/LayerNorm/batchnorm/sub:0' shape=(?, ?, 312) dtype=float32>,),\n",
  125. " (<tf.Tensor 'import/bert/encoder/layer_8/attention/output/LayerNorm/batchnorm/add_1:0' shape=(?, ?, 312) dtype=float32>,),\n",
  126. " (<tf.Tensor 'import/bert/encoder/layer_8/intermediate/einsum/Reshape:0' shape=(?, 312) dtype=float32>,),\n",
  127. " (<tf.Tensor 'import/bert/encoder/layer_8/output/einsum/MatMul:0' shape=(?, 312) dtype=float32>,),\n",
  128. " (<tf.Tensor 'import/bert/encoder/layer_8/output/einsum/Reshape_1:0' shape=(?, ?, 312) dtype=float32>,),\n",
  129. " (<tf.Tensor 'import/bert/encoder/layer_8/output/add:0' shape=(?, ?, 312) dtype=float32>,),\n",
  130. " (<tf.Tensor 'import/bert/encoder/layer_8/output/add_1:0' shape=(?, ?, 312) dtype=float32>,),\n",
  131. " (<tf.Tensor 'import/bert/encoder/layer_8/output/LayerNorm/moments/SquaredDifference:0' shape=(?, ?, 312) dtype=float32>,),\n",
  132. " (<tf.Tensor 'import/bert/encoder/layer_8/output/LayerNorm/batchnorm/mul:0' shape=(?, ?, 312) dtype=float32>,),\n",
  133. " (<tf.Tensor 'import/bert/encoder/layer_8/output/LayerNorm/batchnorm/mul_1:0' shape=(?, ?, 312) dtype=float32>,),\n",
  134. " (<tf.Tensor 'import/bert/encoder/layer_8/output/LayerNorm/batchnorm/mul_2:0' shape=(?, ?, 312) dtype=float32>,),\n",
  135. " (<tf.Tensor 'import/bert/encoder/layer_8/output/LayerNorm/batchnorm/sub:0' shape=(?, ?, 312) dtype=float32>,),\n",
  136. " (<tf.Tensor 'import/bert/encoder/layer_8/output/LayerNorm/batchnorm/add_1:0' shape=(?, ?, 312) dtype=float32>,),\n",
  137. " (<tf.Tensor 'import/bert/encoder/layer_9/attention/self/query/einsum/Reshape:0' shape=(?, 312) dtype=float32>,),\n",
  138. " (<tf.Tensor 'import/bert/encoder/layer_9/attention/self/query/einsum/MatMul:0' shape=(?, 312) dtype=float32>,),\n",
  139. " (<tf.Tensor 'import/bert/encoder/layer_9/attention/self/key/einsum/Reshape:0' shape=(?, 312) dtype=float32>,),\n",
  140. " (<tf.Tensor 'import/bert/encoder/layer_9/attention/self/key/einsum/MatMul:0' shape=(?, 312) dtype=float32>,),\n",
  141. " (<tf.Tensor 'import/bert/encoder/layer_9/attention/self/value/einsum/Reshape:0' shape=(?, 312) dtype=float32>,),\n",
  142. " (<tf.Tensor 'import/bert/encoder/layer_9/attention/self/value/einsum/MatMul:0' shape=(?, 312) dtype=float32>,),\n",
  143. " (<tf.Tensor 'import/bert/encoder/layer_9/attention/output/einsum/Reshape:0' shape=(?, 312) dtype=float32>,),\n",
  144. " (<tf.Tensor 'import/bert/encoder/layer_9/attention/output/einsum/MatMul:0' shape=(?, 312) dtype=float32>,),\n",
  145. " (<tf.Tensor 'import/bert/encoder/layer_9/attention/output/einsum/Reshape_2:0' shape=(?, ?, 312) dtype=float32>,),\n",
  146. " (<tf.Tensor 'import/bert/encoder/layer_9/attention/output/add:0' shape=(?, ?, 312) dtype=float32>,),\n",
  147. " (<tf.Tensor 'import/bert/encoder/layer_9/attention/output/add_1:0' shape=(?, ?, 312) dtype=float32>,),\n",
  148. " (<tf.Tensor 'import/bert/encoder/layer_9/attention/output/LayerNorm/moments/SquaredDifference:0' shape=(?, ?, 312) dtype=float32>,),\n",
  149. " (<tf.Tensor 'import/bert/encoder/layer_9/attention/output/LayerNorm/batchnorm/mul:0' shape=(?, ?, 312) dtype=float32>,),\n",
  150. " (<tf.Tensor 'import/bert/encoder/layer_9/attention/output/LayerNorm/batchnorm/mul_1:0' shape=(?, ?, 312) dtype=float32>,),\n",
  151. " (<tf.Tensor 'import/bert/encoder/layer_9/attention/output/LayerNorm/batchnorm/mul_2:0' shape=(?, ?, 312) dtype=float32>,),\n",
  152. " (<tf.Tensor 'import/bert/encoder/layer_9/attention/output/LayerNorm/batchnorm/sub:0' shape=(?, ?, 312) dtype=float32>,),\n",
  153. " (<tf.Tensor 'import/bert/encoder/layer_9/attention/output/LayerNorm/batchnorm/add_1:0' shape=(?, ?, 312) dtype=float32>,),\n",
  154. " (<tf.Tensor 'import/bert/encoder/layer_9/intermediate/einsum/Reshape:0' shape=(?, 312) dtype=float32>,),\n",
  155. " (<tf.Tensor 'import/bert/encoder/layer_9/output/einsum/MatMul:0' shape=(?, 312) dtype=float32>,),\n",
  156. " (<tf.Tensor 'import/bert/encoder/layer_9/output/einsum/Reshape_1:0' shape=(?, ?, 312) dtype=float32>,),\n",
  157. " (<tf.Tensor 'import/bert/encoder/layer_9/output/add:0' shape=(?, ?, 312) dtype=float32>,),\n",
  158. " (<tf.Tensor 'import/bert/encoder/layer_9/output/add_1:0' shape=(?, ?, 312) dtype=float32>,),\n",
  159. " (<tf.Tensor 'import/bert/encoder/layer_9/output/LayerNorm/moments/SquaredDifference:0' shape=(?, ?, 312) dtype=float32>,),\n",
  160. " (<tf.Tensor 'import/bert/encoder/layer_9/output/LayerNorm/batchnorm/mul:0' shape=(?, ?, 312) dtype=float32>,),\n",
  161. " (<tf.Tensor 'import/bert/encoder/layer_9/output/LayerNorm/batchnorm/mul_1:0' shape=(?, ?, 312) dtype=float32>,),\n",
  162. " (<tf.Tensor 'import/bert/encoder/layer_9/output/LayerNorm/batchnorm/mul_2:0' shape=(?, ?, 312) dtype=float32>,),\n",
  163. " (<tf.Tensor 'import/bert/encoder/layer_9/output/LayerNorm/batchnorm/sub:0' shape=(?, ?, 312) dtype=float32>,),\n",
  164. " (<tf.Tensor 'import/bert/encoder/layer_9/output/LayerNorm/batchnorm/add_1:0' shape=(?, ?, 312) dtype=float32>,),\n",
  165. " (<tf.Tensor 'import/bert/encoder/layer_10/attention/self/query/einsum/Reshape:0' shape=(?, 312) dtype=float32>,),\n",
  166. " (<tf.Tensor 'import/bert/encoder/layer_10/attention/self/query/einsum/MatMul:0' shape=(?, 312) dtype=float32>,),\n",
  167. " (<tf.Tensor 'import/bert/encoder/layer_10/attention/self/key/einsum/Reshape:0' shape=(?, 312) dtype=float32>,),\n",
  168. " (<tf.Tensor 'import/bert/encoder/layer_10/attention/self/key/einsum/MatMul:0' shape=(?, 312) dtype=float32>,),\n",
  169. " (<tf.Tensor 'import/bert/encoder/layer_10/attention/self/value/einsum/Reshape:0' shape=(?, 312) dtype=float32>,),\n",
  170. " (<tf.Tensor 'import/bert/encoder/layer_10/attention/self/value/einsum/MatMul:0' shape=(?, 312) dtype=float32>,),\n",
  171. " (<tf.Tensor 'import/bert/encoder/layer_10/attention/output/einsum/Reshape:0' shape=(?, 312) dtype=float32>,),\n",
  172. " (<tf.Tensor 'import/bert/encoder/layer_10/attention/output/einsum/MatMul:0' shape=(?, 312) dtype=float32>,),\n",
  173. " (<tf.Tensor 'import/bert/encoder/layer_10/attention/output/einsum/Reshape_2:0' shape=(?, ?, 312) dtype=float32>,),\n",
  174. " (<tf.Tensor 'import/bert/encoder/layer_10/attention/output/add:0' shape=(?, ?, 312) dtype=float32>,),\n",
  175. " (<tf.Tensor 'import/bert/encoder/layer_10/attention/output/add_1:0' shape=(?, ?, 312) dtype=float32>,),\n",
  176. " (<tf.Tensor 'import/bert/encoder/layer_10/attention/output/LayerNorm/moments/SquaredDifference:0' shape=(?, ?, 312) dtype=float32>,),\n",
  177. " (<tf.Tensor 'import/bert/encoder/layer_10/attention/output/LayerNorm/batchnorm/mul:0' shape=(?, ?, 312) dtype=float32>,),\n",
  178. " (<tf.Tensor 'import/bert/encoder/layer_10/attention/output/LayerNorm/batchnorm/mul_1:0' shape=(?, ?, 312) dtype=float32>,),\n",
  179. " (<tf.Tensor 'import/bert/encoder/layer_10/attention/output/LayerNorm/batchnorm/mul_2:0' shape=(?, ?, 312) dtype=float32>,),\n",
  180. " (<tf.Tensor 'import/bert/encoder/layer_10/attention/output/LayerNorm/batchnorm/sub:0' shape=(?, ?, 312) dtype=float32>,),\n",
  181. " (<tf.Tensor 'import/bert/encoder/layer_10/attention/output/LayerNorm/batchnorm/add_1:0' shape=(?, ?, 312) dtype=float32>,),\n",
  182. " (<tf.Tensor 'import/bert/encoder/layer_10/intermediate/einsum/Reshape:0' shape=(?, 312) dtype=float32>,),\n",
  183. " (<tf.Tensor 'import/bert/encoder/layer_10/output/einsum/MatMul:0' shape=(?, 312) dtype=float32>,),\n",
  184. " (<tf.Tensor 'import/bert/encoder/layer_10/output/einsum/Reshape_1:0' shape=(?, ?, 312) dtype=float32>,),\n",
  185. " (<tf.Tensor 'import/bert/encoder/layer_10/output/add:0' shape=(?, ?, 312) dtype=float32>,),\n",
  186. " (<tf.Tensor 'import/bert/encoder/layer_10/output/add_1:0' shape=(?, ?, 312) dtype=float32>,),\n",
  187. " (<tf.Tensor 'import/bert/encoder/layer_10/output/LayerNorm/moments/SquaredDifference:0' shape=(?, ?, 312) dtype=float32>,),\n",
  188. " (<tf.Tensor 'import/bert/encoder/layer_10/output/LayerNorm/batchnorm/mul:0' shape=(?, ?, 312) dtype=float32>,),\n",
  189. " (<tf.Tensor 'import/bert/encoder/layer_10/output/LayerNorm/batchnorm/mul_1:0' shape=(?, ?, 312) dtype=float32>,),\n",
  190. " (<tf.Tensor 'import/bert/encoder/layer_10/output/LayerNorm/batchnorm/mul_2:0' shape=(?, ?, 312) dtype=float32>,),\n",
  191. " (<tf.Tensor 'import/bert/encoder/layer_10/output/LayerNorm/batchnorm/sub:0' shape=(?, ?, 312) dtype=float32>,),\n",
  192. " (<tf.Tensor 'import/bert/encoder/layer_10/output/LayerNorm/batchnorm/add_1:0' shape=(?, ?, 312) dtype=float32>,),\n",
  193. " (<tf.Tensor 'import/bert/encoder/layer_11/attention/self/query/einsum/Reshape:0' shape=(?, 312) dtype=float32>,),\n",
  194. " (<tf.Tensor 'import/bert/encoder/layer_11/attention/self/query/einsum/MatMul:0' shape=(?, 312) dtype=float32>,),\n",
  195. " (<tf.Tensor 'import/bert/encoder/layer_11/attention/self/key/einsum/Reshape:0' shape=(?, 312) dtype=float32>,),\n",
  196. " (<tf.Tensor 'import/bert/encoder/layer_11/attention/self/key/einsum/MatMul:0' shape=(?, 312) dtype=float32>,),\n",
  197. " (<tf.Tensor 'import/bert/encoder/layer_11/attention/self/value/einsum/Reshape:0' shape=(?, 312) dtype=float32>,),\n",
  198. " (<tf.Tensor 'import/bert/encoder/layer_11/attention/self/value/einsum/MatMul:0' shape=(?, 312) dtype=float32>,),\n",
  199. " (<tf.Tensor 'import/bert/encoder/layer_11/attention/output/einsum/Reshape:0' shape=(?, 312) dtype=float32>,),\n",
  200. " (<tf.Tensor 'import/bert/encoder/layer_11/attention/output/einsum/MatMul:0' shape=(?, 312) dtype=float32>,),\n",
  201. " (<tf.Tensor 'import/bert/encoder/layer_11/attention/output/einsum/Reshape_2:0' shape=(?, ?, 312) dtype=float32>,),\n",
  202. " (<tf.Tensor 'import/bert/encoder/layer_11/attention/output/add:0' shape=(?, ?, 312) dtype=float32>,),\n",
  203. " (<tf.Tensor 'import/bert/encoder/layer_11/attention/output/add_1:0' shape=(?, ?, 312) dtype=float32>,),\n",
  204. " (<tf.Tensor 'import/bert/encoder/layer_11/attention/output/LayerNorm/moments/SquaredDifference:0' shape=(?, ?, 312) dtype=float32>,),\n",
  205. " (<tf.Tensor 'import/bert/encoder/layer_11/attention/output/LayerNorm/batchnorm/mul:0' shape=(?, ?, 312) dtype=float32>,),\n",
  206. " (<tf.Tensor 'import/bert/encoder/layer_11/attention/output/LayerNorm/batchnorm/mul_1:0' shape=(?, ?, 312) dtype=float32>,),\n",
  207. " (<tf.Tensor 'import/bert/encoder/layer_11/attention/output/LayerNorm/batchnorm/mul_2:0' shape=(?, ?, 312) dtype=float32>,),\n",
  208. " (<tf.Tensor 'import/bert/encoder/layer_11/attention/output/LayerNorm/batchnorm/sub:0' shape=(?, ?, 312) dtype=float32>,),\n",
  209. " (<tf.Tensor 'import/bert/encoder/layer_11/attention/output/LayerNorm/batchnorm/add_1:0' shape=(?, ?, 312) dtype=float32>,),\n",
  210. " (<tf.Tensor 'import/bert/encoder/layer_11/intermediate/einsum/Reshape:0' shape=(?, 312) dtype=float32>,),\n",
  211. " (<tf.Tensor 'import/bert/encoder/layer_11/output/einsum/MatMul:0' shape=(?, 312) dtype=float32>,),\n",
  212. " (<tf.Tensor 'import/bert/encoder/layer_11/output/einsum/Reshape_1:0' shape=(?, ?, 312) dtype=float32>,),\n",
  213. " (<tf.Tensor 'import/bert/encoder/layer_11/output/add:0' shape=(?, ?, 312) dtype=float32>,),\n",
  214. " (<tf.Tensor 'import/bert/encoder/layer_11/output/add_1:0' shape=(?, ?, 312) dtype=float32>,),\n",
  215. " (<tf.Tensor 'import/bert/encoder/layer_11/output/LayerNorm/moments/SquaredDifference:0' shape=(?, ?, 312) dtype=float32>,),\n",
  216. " (<tf.Tensor 'import/bert/encoder/layer_11/output/LayerNorm/batchnorm/mul:0' shape=(?, ?, 312) dtype=float32>,),\n",
  217. " (<tf.Tensor 'import/bert/encoder/layer_11/output/LayerNorm/batchnorm/mul_1:0' shape=(?, ?, 312) dtype=float32>,),\n",
  218. " (<tf.Tensor 'import/bert/encoder/layer_11/output/LayerNorm/batchnorm/mul_2:0' shape=(?, ?, 312) dtype=float32>,),\n",
  219. " (<tf.Tensor 'import/bert/encoder/layer_11/output/LayerNorm/batchnorm/sub:0' shape=(?, ?, 312) dtype=float32>,),\n",
  220. " (<tf.Tensor 'import/bert/encoder/layer_11/output/LayerNorm/batchnorm/add_1:0' shape=(?, ?, 312) dtype=float32>,),\n",
  221. " (<tf.Tensor 'import/Reshape:0' shape=(?, 312) dtype=float32>,),\n",
  222. " (<tf.Tensor 'import/GatherV2:0' shape=(?, 312) dtype=float32>,)]"
  223. ]
  224. },
  225. "execution_count": 7,
  226. "metadata": {},
  227. "output_type": "execute_result"
  228. }
  229. ],
  230. "source": [
  231. "with tf.gfile.GFile('tiny-bert.pb', \"rb\") as f:\n",
  232. " graph_def = tf.GraphDef()\n",
  233. " graph_def.ParseFromString(f.read())\n",
  234. "\n",
  235. "with tf.Graph().as_default() as graph:\n",
  236. " tf.import_graph_def(graph_def)\n",
  237. "\n",
  238. "op = graph.get_operations()\n",
  239. "x = []\n",
  240. "for i in op:\n",
  241. " try:\n",
  242. " if i.values()[0].shape[-1] == 312:\n",
  243. " #if 'batchnorm/add_1' in i.values()[0].name:\n",
  244. " x.append(i.values())\n",
  245. " except:\n",
  246. " pass\n",
  247. " \n",
  248. "x[-100:]"
  249. ]
  250. },
  251. {
  252. "cell_type": "code",
  253. "execution_count": 33,
  254. "metadata": {},
  255. "outputs": [],
  256. "source": [
  257. "mapping = {'albert-base.pb': 'import/bert/encoder/transformer/group_0_11/layer_11/inner_group_0/LayerNorm_1/batchnorm/add_1:0',\n",
  258. " 'albert-tiny.pb': 'import/bert/encoder/transformer/group_0_3/layer_3/inner_group_0/LayerNorm_1/batchnorm/add_1:0',\n",
  259. " 'bert-base.pb': 'import/bert/encoder/layer_11/output/LayerNorm/batchnorm/add_1:0',\n",
  260. " 'tiny-bert.pb': 'import/bert/encoder/layer_11/output/LayerNorm/batchnorm/add_1:0',\n",
  261. " 'xlnet-base.pb': 'import/model/transformer/layer_11/ff/LayerNorm/batchnorm/add_1:0'}"
  262. ]
  263. },
  264. {
  265. "cell_type": "code",
  266. "execution_count": 35,
  267. "metadata": {},
  268. "outputs": [
  269. {
  270. "data": {
  271. "text/plain": [
  272. "'bert/encoder/transformer/group_0_11/layer_11/inner_group_0/LayerNorm_1/batchnorm/add_1'"
  273. ]
  274. },
  275. "execution_count": 35,
  276. "metadata": {},
  277. "output_type": "execute_result"
  278. }
  279. ],
  280. "source": [
  281. "mapping[pbs[0]].replace('import/','').replace(':0','')"
  282. ]
  283. },
  284. {
  285. "cell_type": "code",
  286. "execution_count": 37,
  287. "metadata": {},
  288. "outputs": [],
  289. "source": [
  290. "transforms = ['add_default_attributes',\n",
  291. " 'remove_nodes(op=Identity, op=CheckNumerics, op=Dropout)',\n",
  292. " 'fold_batch_norms',\n",
  293. " 'fold_old_batch_norms',\n",
  294. " 'quantize_weights(fallback_min=-10, fallback_max=10)',\n",
  295. " 'strip_unused_nodes',\n",
  296. " 'sort_by_execution_order']\n",
  297. "\n",
  298. "for pb in pbs:\n",
  299. " input_graph_def = tf.GraphDef()\n",
  300. " with tf.gfile.FastGFile(pb, 'rb') as f:\n",
  301. " input_graph_def.ParseFromString(f.read())\n",
  302. " \n",
  303. " a = [mapping[pb].replace('import/','').replace(':0','')]\n",
  304. " \n",
  305. " transformed_graph_def = TransformGraph(input_graph_def, \n",
  306. " ['input_ids', 'word_end_mask'],\n",
  307. " ['charts', 'tags'] + a, transforms)\n",
  308. " \n",
  309. " with tf.gfile.GFile(f'{pb}.quantized', 'wb') as f:\n",
  310. " f.write(transformed_graph_def.SerializeToString())"
  311. ]
  312. },
  313. {
  314. "cell_type": "code",
  315. "execution_count": 38,
  316. "metadata": {},
  317. "outputs": [],
  318. "source": [
  319. "def load_graph(frozen_graph_filename, **kwargs):\n",
  320. " with tf.gfile.GFile(frozen_graph_filename, 'rb') as f:\n",
  321. " graph_def = tf.GraphDef()\n",
  322. " graph_def.ParseFromString(f.read())\n",
  323. "\n",
  324. " # https://github.com/onnx/tensorflow-onnx/issues/77#issuecomment-445066091\n",
  325. " # to fix import T5\n",
  326. " for node in graph_def.node:\n",
  327. " if node.op == 'RefSwitch':\n",
  328. " node.op = 'Switch'\n",
  329. " for index in xrange(len(node.input)):\n",
  330. " if 'moving_' in node.input[index]:\n",
  331. " node.input[index] = node.input[index] + '/read'\n",
  332. " elif node.op == 'AssignSub':\n",
  333. " node.op = 'Sub'\n",
  334. " if 'use_locking' in node.attr:\n",
  335. " del node.attr['use_locking']\n",
  336. " elif node.op == 'AssignAdd':\n",
  337. " node.op = 'Add'\n",
  338. " if 'use_locking' in node.attr:\n",
  339. " del node.attr['use_locking']\n",
  340. " elif node.op == 'Assign':\n",
  341. " node.op = 'Identity'\n",
  342. " if 'use_locking' in node.attr:\n",
  343. " del node.attr['use_locking']\n",
  344. " if 'validate_shape' in node.attr:\n",
  345. " del node.attr['validate_shape']\n",
  346. " if len(node.input) == 2:\n",
  347. " node.input[0] = node.input[1]\n",
  348. " del node.input[1]\n",
  349. "\n",
  350. " with tf.Graph().as_default() as graph:\n",
  351. " tf.import_graph_def(graph_def)\n",
  352. " return graph"
  353. ]
  354. },
  355. {
  356. "cell_type": "code",
  357. "execution_count": 39,
  358. "metadata": {},
  359. "outputs": [
  360. {
  361. "data": {
  362. "text/plain": [
  363. "['albert-base.pb.quantized',\n",
  364. " 'xlnet-base.pb.quantized',\n",
  365. " 'albert-tiny.pb.quantized',\n",
  366. " 'tiny-bert.pb.quantized',\n",
  367. " 'bert-base.pb.quantized']"
  368. ]
  369. },
  370. "execution_count": 39,
  371. "metadata": {},
  372. "output_type": "execute_result"
  373. }
  374. ],
  375. "source": [
  376. "quantized = glob('*.pb.quantized')\n",
  377. "quantized"
  378. ]
  379. },
  380. {
  381. "cell_type": "code",
  382. "execution_count": null,
  383. "metadata": {},
  384. "outputs": [],
  385. "source": [
  386. "# converter = tf.compat.v1.lite.TFLiteConverter.from_frozen_graph(\n",
  387. "# graph_def_file='test.pb',\n",
  388. "# input_arrays=['Placeholder', 'Placeholder_1'],\n",
  389. "# input_shapes={'Placeholder' : [None, 512], 'Placeholder_1': [None, 512]},\n",
  390. "# output_arrays=['logits'],\n",
  391. "# )\n",
  392. "# # converter.allow_custom_ops=True"
  393. ]
  394. },
  395. {
  396. "cell_type": "code",
  397. "execution_count": null,
  398. "metadata": {},
  399. "outputs": [],
  400. "source": [
  401. "# converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS, tf.lite.OpsSet.SELECT_TF_OPS]\n",
  402. "# converter.target_spec.supported_types = [tf.float16]\n",
  403. "# converter.optimizations = [tf.lite.Optimize.DEFAULT]\n",
  404. "# converter.experimental_new_converter = True\n",
  405. "# tflite_model = converter.convert()"
  406. ]
  407. },
  408. {
  409. "cell_type": "code",
  410. "execution_count": null,
  411. "metadata": {},
  412. "outputs": [],
  413. "source": [
  414. "# converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS, \n",
  415. "# tf.lite.OpsSet.SELECT_TF_OPS]\n",
  416. "# converter.target_spec.supported_types = [tf.float16]\n",
  417. "# converter.optimizations = [tf.lite.Optimize.DEFAULT]\n",
  418. "# tflite_model = converter.convert()\n",
  419. "\n",
  420. "# with open('tiny-bert-sentiment-float16.tflite', 'wb') as f:\n",
  421. "# f.write(tflite_model)"
  422. ]
  423. },
  424. {
  425. "cell_type": "code",
  426. "execution_count": null,
  427. "metadata": {},
  428. "outputs": [],
  429. "source": [
  430. "# converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS, \n",
  431. "# tf.lite.OpsSet.SELECT_TF_OPS]\n",
  432. "# converter.optimizations = [tf.lite.Optimize.OPTIMIZE_FOR_SIZE]\n",
  433. "# tflite_model = converter.convert()\n",
  434. "\n",
  435. "# with open('tiny-bert-sentiment-hybrid.tflite', 'wb') as f:\n",
  436. "# f.write(tflite_model)"
  437. ]
  438. },
  439. {
  440. "cell_type": "code",
  441. "execution_count": null,
  442. "metadata": {},
  443. "outputs": [],
  444. "source": [
  445. "# interpreter = tf.lite.Interpreter(model_path='tiny-bert-sentiment-hybrid.tflite')\n",
  446. "# interpreter.allocate_tensors()"
  447. ]
  448. }
  449. ],
  450. "metadata": {
  451. "kernelspec": {
  452. "display_name": "Python 3",
  453. "language": "python",
  454. "name": "python3"
  455. },
  456. "language_info": {
  457. "codemirror_mode": {
  458. "name": "ipython",
  459. "version": 3
  460. },
  461. "file_extension": ".py",
  462. "mimetype": "text/x-python",
  463. "name": "python",
  464. "nbconvert_exporter": "python",
  465. "pygments_lexer": "ipython3",
  466. "version": "3.6.8"
  467. }
  468. },
  469. "nbformat": 4,
  470. "nbformat_minor": 2
  471. }