Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
chenpangpang
transformers
Commits
c783e148
Unverified
Commit
c783e148
authored
Sep 15, 2021
by
elishowk
Committed by
GitHub
Sep 15, 2021
Browse files
upgrade sentencepiece version (#13564)
parent
e86c02ea
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
105 additions
and
94 deletions
+105
-94
examples/research_projects/lxmert/demo.ipynb
examples/research_projects/lxmert/demo.ipynb
+10
-10
examples/research_projects/movement-pruning/Saving_PruneBERT.ipynb
...research_projects/movement-pruning/Saving_PruneBERT.ipynb
+83
-72
examples/research_projects/visual_bert/demo.ipynb
examples/research_projects/visual_bert/demo.ipynb
+10
-10
setup.py
setup.py
+1
-1
src/transformers/dependency_versions_table.py
src/transformers/dependency_versions_table.py
+1
-1
No files found.
examples/research_projects/lxmert/demo.ipynb
View file @
c783e148
...
@@ -46,10 +46,10 @@
...
@@ -46,10 +46,10 @@
"ATTR_URL = \"https://raw.githubusercontent.com/airsplay/py-bottom-up-attention/master/demo/data/genome/1600-400-20/attributes_vocab.txt\"\n",
"ATTR_URL = \"https://raw.githubusercontent.com/airsplay/py-bottom-up-attention/master/demo/data/genome/1600-400-20/attributes_vocab.txt\"\n",
"GQA_URL = \"https://raw.githubusercontent.com/airsplay/lxmert/master/data/gqa/trainval_label2ans.json\"\n",
"GQA_URL = \"https://raw.githubusercontent.com/airsplay/lxmert/master/data/gqa/trainval_label2ans.json\"\n",
"VQA_URL = \"https://raw.githubusercontent.com/airsplay/lxmert/master/data/vqa/trainval_label2ans.json\"\n",
"VQA_URL = \"https://raw.githubusercontent.com/airsplay/lxmert/master/data/vqa/trainval_label2ans.json\"\n",
"
\n",
"\n",
"\n",
"\n",
"# for visualizing output\n",
"# for visualizing output\n",
"def showarray(a, fmt=
'
jpeg
'
):\n",
"def showarray(a, fmt=
\"
jpeg
\"
):\n",
" a = np.uint8(np.clip(a, 0, 255))\n",
" a = np.uint8(np.clip(a, 0, 255))\n",
" f = io.BytesIO()\n",
" f = io.BytesIO()\n",
" PIL.Image.fromarray(a).save(f, fmt)\n",
" PIL.Image.fromarray(a).save(f, fmt)\n",
...
@@ -118,17 +118,17 @@
...
@@ -118,17 +118,17 @@
}
}
],
],
"source": [
"source": [
"#image viz\n",
"#
image viz\n",
"frcnn_visualizer = SingleImageViz(URL, id2obj=objids, id2attr=attrids)\n",
"frcnn_visualizer = SingleImageViz(URL, id2obj=objids, id2attr=attrids)\n",
"# run frcnn\n",
"# run frcnn\n",
"images, sizes, scales_yx = image_preprocess(URL)\n",
"images, sizes, scales_yx = image_preprocess(URL)\n",
"output_dict = frcnn(\n",
"output_dict = frcnn(\n",
" images,
\n",
" images,\n",
" sizes,
\n",
" sizes,\n",
" scales_yx=scales_yx,
\n",
" scales_yx=scales_yx,\n",
" padding=\"max_detections\",\n",
" padding=\"max_detections\",\n",
" max_detections=frcnn_cfg.max_detections,\n",
" max_detections=frcnn_cfg.max_detections,\n",
" return_tensors=\"pt\"\n",
" return_tensors=\"pt\"
,
\n",
")\n",
")\n",
"# add boxes and labels to the image\n",
"# add boxes and labels to the image\n",
"\n",
"\n",
...
@@ -174,7 +174,7 @@
...
@@ -174,7 +174,7 @@
" \"Where is this scene?\",\n",
" \"Where is this scene?\",\n",
" \"what is the man riding?\",\n",
" \"what is the man riding?\",\n",
" \"What is the man wearing?\",\n",
" \"What is the man wearing?\",\n",
" \"What is the color of the horse?\"\n",
" \"What is the color of the horse?\"
,
\n",
"]\n",
"]\n",
"test_questions_for_url2 = [\n",
"test_questions_for_url2 = [\n",
" \"Where is the cat?\",\n",
" \"Where is the cat?\",\n",
...
@@ -184,7 +184,7 @@
...
@@ -184,7 +184,7 @@
" \"What is the shape of the monitor?\",\n",
" \"What is the shape of the monitor?\",\n",
"]\n",
"]\n",
"\n",
"\n",
"#Very important that the boxes are normalized\n",
"#
Very important that the boxes are normalized\n",
"normalized_boxes = output_dict.get(\"normalized_boxes\")\n",
"normalized_boxes = output_dict.get(\"normalized_boxes\")\n",
"features = output_dict.get(\"roi_features\")\n",
"features = output_dict.get(\"roi_features\")\n",
"\n",
"\n",
...
@@ -200,7 +200,7 @@
...
@@ -200,7 +200,7 @@
" return_token_type_ids=True,\n",
" return_token_type_ids=True,\n",
" return_attention_mask=True,\n",
" return_attention_mask=True,\n",
" add_special_tokens=True,\n",
" add_special_tokens=True,\n",
" return_tensors=\"pt\"\n",
" return_tensors=\"pt\"
,
\n",
" )\n",
" )\n",
"\n",
"\n",
" # run lxmert(s)\n",
" # run lxmert(s)\n",
...
...
examples/research_projects/movement-pruning/Saving_PruneBERT.ipynb
View file @
c783e148
...
@@ -44,7 +44,7 @@
...
@@ -44,7 +44,7 @@
"\n",
"\n",
"from transformers import *\n",
"from transformers import *\n",
"\n",
"\n",
"os.chdir(
'
../../
'
)"
"os.chdir(
\"
../../
\"
)"
]
]
},
},
{
{
...
@@ -70,15 +70,15 @@
...
@@ -70,15 +70,15 @@
"# Load fine-pruned model and quantize the model\n",
"# Load fine-pruned model and quantize the model\n",
"\n",
"\n",
"model = BertForQuestionAnswering.from_pretrained(\"huggingface/prunebert-base-uncased-6-finepruned-w-distil-squad\")\n",
"model = BertForQuestionAnswering.from_pretrained(\"huggingface/prunebert-base-uncased-6-finepruned-w-distil-squad\")\n",
"model.to(
'
cpu
'
)\n",
"model.to(
\"
cpu
\"
)\n",
"\n",
"\n",
"quantized_model = torch.quantization.quantize_dynamic(\n",
"quantized_model = torch.quantization.quantize_dynamic(\n",
"
model=model,\n",
" model=model,\n",
"
qconfig_spec
=
{\n",
" qconfig_spec
=
{\n",
"
nn.Linear
: torch.quantization.default_dynamic_qconfig,\n",
" nn.Linear: torch.quantization.default_dynamic_qconfig,\n",
"
},\n",
" },\n",
"
dtype=torch.qint8,\n",
" dtype=torch.qint8,\n",
"
)\n",
")\n",
"# print(quantized_model)\n",
"# print(quantized_model)\n",
"\n",
"\n",
"qtz_st = quantized_model.state_dict()"
"qtz_st = quantized_model.state_dict()"
...
@@ -92,10 +92,14 @@
...
@@ -92,10 +92,14 @@
"source": [
"source": [
"# Saving the original (encoder + classifier) in the standard torch.save format\n",
"# Saving the original (encoder + classifier) in the standard torch.save format\n",
"\n",
"\n",
"dense_st = {name: param for name, param in model.state_dict().items() \n",
"dense_st = {\n",
" if \"embedding\" not in name and \"pooler\" not in name}\n",
" name: param for name, param in model.state_dict().items() if \"embedding\" not in name and \"pooler\" not in name\n",
"torch.save(dense_st, 'dbg/dense_squad.pt',)\n",
"}\n",
"dense_mb_size = os.path.getsize(\"dbg/dense_squad.pt\")\n"
"torch.save(\n",
" dense_st,\n",
" \"dbg/dense_squad.pt\",\n",
")\n",
"dense_mb_size = os.path.getsize(\"dbg/dense_squad.pt\")"
]
]
},
},
{
{
...
@@ -198,23 +202,23 @@
...
@@ -198,23 +202,23 @@
" if \"dtype\" not in name and param.is_quantized:\n",
" if \"dtype\" not in name and param.is_quantized:\n",
" print(\"Decompose quantization for\", name)\n",
" print(\"Decompose quantization for\", name)\n",
" # We need to extract the scale, the zero_point and the int_repr for the quantized tensor and modules\n",
" # We need to extract the scale, the zero_point and the int_repr for the quantized tensor and modules\n",
" scale = param.q_scale()
# torch.tensor(1,) - float32\n",
" scale = param.q_scale() # torch.tensor(1,) - float32\n",
" zero_point = param.q_zero_point()
# torch.tensor(1,) - int32\n",
" zero_point = param.q_zero_point() # torch.tensor(1,) - int32\n",
" elementary_qtz_st[f\"{name}.scale\"] = scale\n",
" elementary_qtz_st[f\"{name}.scale\"] = scale\n",
" elementary_qtz_st[f\"{name}.zero_point\"] = zero_point\n",
" elementary_qtz_st[f\"{name}.zero_point\"] = zero_point\n",
"\n",
"\n",
" # We assume the int_repr is sparse and compute its CSR representation\n",
" # We assume the int_repr is sparse and compute its CSR representation\n",
" # Only the FCs in the encoder are actually sparse\n",
" # Only the FCs in the encoder are actually sparse\n",
" int_repr = param.int_repr()
# torch.tensor(nb_rows, nb_columns) - int8\n",
" int_repr = param.int_repr() # torch.tensor(nb_rows, nb_columns) - int8\n",
" int_repr_cs = sparse.csr_matrix(int_repr)
# scipy.sparse.csr.csr_matrix\n",
" int_repr_cs = sparse.csr_matrix(int_repr) # scipy.sparse.csr.csr_matrix\n",
"\n",
"\n",
" elementary_qtz_st[f\"{name}.int_repr.data\"] = int_repr_cs.data
# np.array int8\n",
" elementary_qtz_st[f\"{name}.int_repr.data\"] = int_repr_cs.data # np.array int8\n",
" elementary_qtz_st[f\"{name}.int_repr.indptr\"] = int_repr_cs.indptr
# np.array int32\n",
" elementary_qtz_st[f\"{name}.int_repr.indptr\"] = int_repr_cs.indptr # np.array int32\n",
" assert max(int_repr_cs.indices) < 65535 # If not, we shall fall back to int32\n",
" assert max(int_repr_cs.indices) < 65535
# If not, we shall fall back to int32\n",
" elementary_qtz_st[f\"{name}.int_repr.indices\"] = np.uint16(int_repr_cs.indices) # np.array uint16\n",
" elementary_qtz_st[f\"{name}.int_repr.indices\"] = np.uint16(int_repr_cs.indices)
# np.array uint16\n",
" elementary_qtz_st[f\"{name}.int_repr.shape\"] = int_repr_cs.shape
# tuple(int, int)\n",
" elementary_qtz_st[f\"{name}.int_repr.shape\"] = int_repr_cs.shape # tuple(int, int)\n",
" else:\n",
" else:\n",
" elementary_qtz_st[name] = param
\n
"
" elementary_qtz_st[name] = param"
]
]
},
},
{
{
...
@@ -225,7 +229,7 @@
...
@@ -225,7 +229,7 @@
"source": [
"source": [
"# Create mapping from torch.dtype to string description (we could also used an int8 instead of string)\n",
"# Create mapping from torch.dtype to string description (we could also used an int8 instead of string)\n",
"str_2_dtype = {\"qint8\": torch.qint8}\n",
"str_2_dtype = {\"qint8\": torch.qint8}\n",
"dtype_2_str = {torch.qint8: \"qint8\"}
\n
"
"dtype_2_str = {torch.qint8: \"qint8\"}"
]
]
},
},
{
{
...
@@ -246,11 +250,17 @@
...
@@ -246,11 +250,17 @@
"source": [
"source": [
"# Saving the pruned (encoder + classifier) in the standard torch.save format\n",
"# Saving the pruned (encoder + classifier) in the standard torch.save format\n",
"\n",
"\n",
"dense_optimized_st = {name: param for name, param in elementary_qtz_st.items() \n",
"dense_optimized_st = {\n",
" if \"embedding\" not in name and \"pooler\" not in name}\n",
" name: param for name, param in elementary_qtz_st.items() if \"embedding\" not in name and \"pooler\" not in name\n",
"torch.save(dense_optimized_st, 'dbg/dense_squad_optimized.pt',)\n",
"}\n",
"print(\"Encoder Size (MB) - Sparse & Quantized - `torch.save`:\",\n",
"torch.save(\n",
" round(os.path.getsize(\"dbg/dense_squad_optimized.pt\")/1e6, 2))\n"
" dense_optimized_st,\n",
" \"dbg/dense_squad_optimized.pt\",\n",
")\n",
"print(\n",
" \"Encoder Size (MB) - Sparse & Quantized - `torch.save`:\",\n",
" round(os.path.getsize(\"dbg/dense_squad_optimized.pt\") / 1e6, 2),\n",
")"
]
]
},
},
{
{
...
@@ -287,7 +297,7 @@
...
@@ -287,7 +297,7 @@
"# Save the decomposed state_dict with an HDF5 file\n",
"# Save the decomposed state_dict with an HDF5 file\n",
"# Saving only the encoder + QA Head\n",
"# Saving only the encoder + QA Head\n",
"\n",
"\n",
"with h5py.File(
'
dbg/squad_sparse.h5
','w'
) as hf:\n",
"with h5py.File(
\"
dbg/squad_sparse.h5
\", \"w\"
) as hf:\n",
" for name, param in elementary_qtz_st.items():\n",
" for name, param in elementary_qtz_st.items():\n",
" if \"embedding\" in name:\n",
" if \"embedding\" in name:\n",
" print(f\"Skip {name}\")\n",
" print(f\"Skip {name}\")\n",
...
@@ -318,18 +328,18 @@
...
@@ -318,18 +328,18 @@
" elif type(param) == torch.dtype:\n",
" elif type(param) == torch.dtype:\n",
" # dtype - tensor _packed_params.dtype\n",
" # dtype - tensor _packed_params.dtype\n",
" hf.attrs[name] = dtype_2_str[param]\n",
" hf.attrs[name] = dtype_2_str[param]\n",
"
\n",
"\n",
" else:\n",
" else:\n",
" hf.create_dataset(name, data=param, compression=\"gzip\", compression_opts=9)\n",
" hf.create_dataset(name, data=param, compression=\"gzip\", compression_opts=9)\n",
"\n",
"\n",
"\n",
"\n",
"with open(
'
dbg/metadata.json
', 'w'
) as f:\n",
"with open(
\"
dbg/metadata.json
\", \"w\"
) as f:\n",
" f.write(json.dumps(qtz_st._metadata))
\n",
" f.write(json.dumps(qtz_st._metadata))\n",
"\n",
"\n",
"size = os.path.getsize(\"dbg/squad_sparse.h5\") + os.path.getsize(\"dbg/metadata.json\")\n",
"size = os.path.getsize(\"dbg/squad_sparse.h5\") + os.path.getsize(\"dbg/metadata.json\")\n",
"print(\"\")\n",
"print(\"\")\n",
"print(\"Encoder Size (MB) - Dense: \", round(dense_mb_size
/
1e6, 2))\n",
"print(\"Encoder Size (MB) - Dense: \", round(dense_mb_size
/
1e6, 2))\n",
"print(\"Encoder Size (MB) - Sparse & Quantized:\", round(size
/
1e6, 2))
\n
"
"print(\"Encoder Size (MB) - Sparse & Quantized:\", round(size
/
1e6, 2))"
]
]
},
},
{
{
...
@@ -350,15 +360,15 @@
...
@@ -350,15 +360,15 @@
"# Save the decomposed state_dict to HDF5 storage\n",
"# Save the decomposed state_dict to HDF5 storage\n",
"# Save everything in the architecutre (embedding + encoder + QA Head)\n",
"# Save everything in the architecutre (embedding + encoder + QA Head)\n",
"\n",
"\n",
"with h5py.File(
'
dbg/squad_sparse_with_embs.h5
','w'
) as hf:\n",
"with h5py.File(
\"
dbg/squad_sparse_with_embs.h5
\", \"w\"
) as hf:\n",
" for name, param in elementary_qtz_st.items():\n",
" for name, param in elementary_qtz_st.items():\n",
"# if \"embedding\" in name:\n",
"
# if \"embedding\" in name:\n",
"# print(f\"Skip {name}\")\n",
"
# print(f\"Skip {name}\")\n",
"# continue\n",
"
# continue\n",
"\n",
"\n",
"# if \"pooler\" in name:\n",
"
# if \"pooler\" in name:\n",
"# print(f\"Skip {name}\")\n",
"
# print(f\"Skip {name}\")\n",
"# continue\n",
"
# continue\n",
"\n",
"\n",
" if type(param) == torch.Tensor:\n",
" if type(param) == torch.Tensor:\n",
" if param.numel() == 1:\n",
" if param.numel() == 1:\n",
...
@@ -381,17 +391,16 @@
...
@@ -381,17 +391,16 @@
" elif type(param) == torch.dtype:\n",
" elif type(param) == torch.dtype:\n",
" # dtype - tensor _packed_params.dtype\n",
" # dtype - tensor _packed_params.dtype\n",
" hf.attrs[name] = dtype_2_str[param]\n",
" hf.attrs[name] = dtype_2_str[param]\n",
"
\n",
"\n",
" else:\n",
" else:\n",
" hf.create_dataset(name, data=param, compression=\"gzip\", compression_opts=9)\n",
" hf.create_dataset(name, data=param, compression=\"gzip\", compression_opts=9)\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"with open(\"dbg/metadata.json\", \"w\") as f:\n",
"with open('dbg/metadata.json', 'w') as f:\n",
" f.write(json.dumps(qtz_st._metadata))\n",
" f.write(json.dumps(qtz_st._metadata)) \n",
"\n",
"\n",
"size = os.path.getsize(\"dbg/squad_sparse_with_embs.h5\") + os.path.getsize(\"dbg/metadata.json\")\n",
"size = os.path.getsize(\"dbg/squad_sparse_with_embs.h5\") + os.path.getsize(\"dbg/metadata.json\")\n",
"print(
'
\\nSize (MB):
'
, round(size
/
1e6, 2))
\n
"
"print(
\"
\\nSize (MB):
\"
, round(size
/
1e6, 2))"
]
]
},
},
{
{
...
@@ -411,10 +420,10 @@
...
@@ -411,10 +420,10 @@
"\n",
"\n",
"reconstructed_elementary_qtz_st = {}\n",
"reconstructed_elementary_qtz_st = {}\n",
"\n",
"\n",
"hf = h5py.File(
'
dbg/squad_sparse_with_embs.h5
','r'
)\n",
"hf = h5py.File(
\"
dbg/squad_sparse_with_embs.h5
\", \"r\"
)\n",
"\n",
"\n",
"for attr_name, attr_param in hf.attrs.items():\n",
"for attr_name, attr_param in hf.attrs.items():\n",
" if
'
shape
'
in attr_name:\n",
" if
\"
shape
\"
in attr_name:\n",
" attr_param = tuple(attr_param)\n",
" attr_param = tuple(attr_param)\n",
" elif \".scale\" in attr_name:\n",
" elif \".scale\" in attr_name:\n",
" if \"_packed_params\" in attr_name:\n",
" if \"_packed_params\" in attr_name:\n",
...
@@ -430,20 +439,20 @@
...
@@ -430,20 +439,20 @@
" attr_param = str_2_dtype[attr_param]\n",
" attr_param = str_2_dtype[attr_param]\n",
" reconstructed_elementary_qtz_st[attr_name] = attr_param\n",
" reconstructed_elementary_qtz_st[attr_name] = attr_param\n",
" # print(f\"Unpack {attr_name}\")\n",
" # print(f\"Unpack {attr_name}\")\n",
"
\n",
"\n",
"# Get the tensors/arrays\n",
"# Get the tensors/arrays\n",
"for data_name, data_param in hf.items():\n",
"for data_name, data_param in hf.items():\n",
" if \"LayerNorm\" in data_name or \"_packed_params.bias\" in data_name:\n",
" if \"LayerNorm\" in data_name or \"_packed_params.bias\" in data_name:\n",
" reconstructed_elementary_qtz_st[data_name] = torch.from_numpy(np.array(data_param))\n",
" reconstructed_elementary_qtz_st[data_name] = torch.from_numpy(np.array(data_param))\n",
" elif \"embedding\" in data_name:\n",
" elif \"embedding\" in data_name:\n",
" reconstructed_elementary_qtz_st[data_name] = torch.from_numpy(np.array(data_param))\n",
" reconstructed_elementary_qtz_st[data_name] = torch.from_numpy(np.array(data_param))\n",
" else: # _packed_params.weight.int_repr.data, _packed_params.weight.int_repr.indices and _packed_params.weight.int_repr.indptr\n",
" else:
# _packed_params.weight.int_repr.data, _packed_params.weight.int_repr.indices and _packed_params.weight.int_repr.indptr\n",
" data_param = np.array(data_param)\n",
" data_param = np.array(data_param)\n",
" if \"indices\" in data_name:\n",
" if \"indices\" in data_name:\n",
" data_param = np.array(data_param, dtype=np.int32)\n",
" data_param = np.array(data_param, dtype=np.int32)\n",
" reconstructed_elementary_qtz_st[data_name] = data_param\n",
" reconstructed_elementary_qtz_st[data_name] = data_param\n",
" # print(f\"Unpack {data_name}\")\n",
" # print(f\"Unpack {data_name}\")\n",
"
\n",
"\n",
"\n",
"\n",
"hf.close()"
"hf.close()"
]
]
...
@@ -484,27 +493,29 @@
...
@@ -484,27 +493,29 @@
"for name, param in reconstructed_elementary_qtz_st.items():\n",
"for name, param in reconstructed_elementary_qtz_st.items():\n",
" if \"weight.int_repr.indptr\" in name:\n",
" if \"weight.int_repr.indptr\" in name:\n",
" prefix_ = name[:-16]\n",
" prefix_ = name[:-16]\n",
" data
= reconstructed_elementary_qtz_st[f\"{prefix_}.int_repr.data\"]\n",
" data = reconstructed_elementary_qtz_st[f\"{prefix_}.int_repr.data\"]\n",
" indptr
= reconstructed_elementary_qtz_st[f\"{prefix_}.int_repr.indptr\"]\n",
" indptr = reconstructed_elementary_qtz_st[f\"{prefix_}.int_repr.indptr\"]\n",
" indices = reconstructed_elementary_qtz_st[f\"{prefix_}.int_repr.indices\"]\n",
" indices = reconstructed_elementary_qtz_st[f\"{prefix_}.int_repr.indices\"]\n",
" shape
= reconstructed_elementary_qtz_st[f\"{prefix_}.int_repr.shape\"]\n",
" shape = reconstructed_elementary_qtz_st[f\"{prefix_}.int_repr.shape\"]\n",
"\n",
"\n",
" int_repr = sparse.csr_matrix(arg1=(data, indices, indptr),\n",
" int_repr = sparse.csr_matrix(arg1=(data, indices, indptr), shape=shape)\n",
" shape=shape)\n",
" int_repr = torch.tensor(int_repr.todense())\n",
" int_repr = torch.tensor(int_repr.todense())\n",
"\n",
"\n",
" scale = reconstructed_elementary_qtz_st[f\"{prefix_}.scale\"]\n",
" scale = reconstructed_elementary_qtz_st[f\"{prefix_}.scale\"]\n",
" zero_point = reconstructed_elementary_qtz_st[f\"{prefix_}.zero_point\"]\n",
" zero_point = reconstructed_elementary_qtz_st[f\"{prefix_}.zero_point\"]\n",
" weight = torch._make_per_tensor_quantized_tensor(int_repr,\n",
" weight = torch._make_per_tensor_quantized_tensor(int_repr, scale, zero_point)\n",
" scale,\n",
" zero_point)\n",
"\n",
"\n",
" reconstructed_qtz_st[f\"{prefix_}\"] = weight\n",
" reconstructed_qtz_st[f\"{prefix_}\"] = weight\n",
" elif \"int_repr.data\" in name or \"int_repr.shape\" in name or \"int_repr.indices\" in name or \\\n",
" elif (\n",
" \"weight.scale\" in name or \"weight.zero_point\" in name:\n",
" \"int_repr.data\" in name\n",
" or \"int_repr.shape\" in name\n",
" or \"int_repr.indices\" in name\n",
" or \"weight.scale\" in name\n",
" or \"weight.zero_point\" in name\n",
" ):\n",
" continue\n",
" continue\n",
" else:\n",
" else:\n",
" reconstructed_qtz_st[name] = param
\n
"
" reconstructed_qtz_st[name] = param"
]
]
},
},
{
{
...
@@ -556,17 +567,17 @@
...
@@ -556,17 +567,17 @@
"source": [
"source": [
"# Load the re-constructed state dict into a model\n",
"# Load the re-constructed state dict into a model\n",
"\n",
"\n",
"dummy_model = BertForQuestionAnswering.from_pretrained(
'
bert-base-uncased
'
)\n",
"dummy_model = BertForQuestionAnswering.from_pretrained(
\"
bert-base-uncased
\"
)\n",
"dummy_model.to(
'
cpu
'
)\n",
"dummy_model.to(
\"
cpu
\"
)\n",
"\n",
"\n",
"reconstructed_qtz_model = torch.quantization.quantize_dynamic(\n",
"reconstructed_qtz_model = torch.quantization.quantize_dynamic(\n",
"
model=dummy_model,\n",
" model=dummy_model,\n",
"
qconfig_spec
=
None,\n",
" qconfig_spec
=
None,\n",
"
dtype=torch.qint8,\n",
" dtype=torch.qint8,\n",
"
)\n",
")\n",
"\n",
"\n",
"reconstructed_qtz_st = OrderedDict(reconstructed_qtz_st)\n",
"reconstructed_qtz_st = OrderedDict(reconstructed_qtz_st)\n",
"with open(
'
dbg/metadata.json
', 'r'
) as read_file:\n",
"with open(
\"
dbg/metadata.json
\", \"r\"
) as read_file:\n",
" metadata = json.loads(read_file.read())\n",
" metadata = json.loads(read_file.read())\n",
"reconstructed_qtz_st._metadata = metadata\n",
"reconstructed_qtz_st._metadata = metadata\n",
"\n",
"\n",
...
@@ -596,8 +607,8 @@
...
@@ -596,8 +607,8 @@
" mask = torch.ones(size=(N, 128))\n",
" mask = torch.ones(size=(N, 128))\n",
"\n",
"\n",
" y_reconstructed = reconstructed_qtz_model(input_ids=inputs, attention_mask=mask)[0]\n",
" y_reconstructed = reconstructed_qtz_model(input_ids=inputs, attention_mask=mask)[0]\n",
" y
= quantized_model(input_ids=inputs, attention_mask=mask)[0]\n",
" y = quantized_model(input_ids=inputs, attention_mask=mask)[0]\n",
"
\n",
"\n",
" assert torch.all(torch.eq(y, y_reconstructed))\n",
" assert torch.all(torch.eq(y, y_reconstructed))\n",
"print(\"Sanity check passed\")"
"print(\"Sanity check passed\")"
]
]
...
...
examples/research_projects/visual_bert/demo.ipynb
View file @
c783e148
...
@@ -37,10 +37,10 @@
...
@@ -37,10 +37,10 @@
"OBJ_URL = \"https://raw.githubusercontent.com/airsplay/py-bottom-up-attention/master/demo/data/genome/1600-400-20/objects_vocab.txt\"\n",
"OBJ_URL = \"https://raw.githubusercontent.com/airsplay/py-bottom-up-attention/master/demo/data/genome/1600-400-20/objects_vocab.txt\"\n",
"ATTR_URL = \"https://raw.githubusercontent.com/airsplay/py-bottom-up-attention/master/demo/data/genome/1600-400-20/attributes_vocab.txt\"\n",
"ATTR_URL = \"https://raw.githubusercontent.com/airsplay/py-bottom-up-attention/master/demo/data/genome/1600-400-20/attributes_vocab.txt\"\n",
"VQA_URL = \"https://dl.fbaipublicfiles.com/pythia/data/answers_vqa.txt\"\n",
"VQA_URL = \"https://dl.fbaipublicfiles.com/pythia/data/answers_vqa.txt\"\n",
"
\n",
"\n",
"\n",
"\n",
"# for visualizing output\n",
"# for visualizing output\n",
"def showarray(a, fmt=
'
jpeg
'
):\n",
"def showarray(a, fmt=
\"
jpeg
\"
):\n",
" a = np.uint8(np.clip(a, 0, 255))\n",
" a = np.uint8(np.clip(a, 0, 255))\n",
" f = io.BytesIO()\n",
" f = io.BytesIO()\n",
" PIL.Image.fromarray(a).save(f, fmt)\n",
" PIL.Image.fromarray(a).save(f, fmt)\n",
...
@@ -82,7 +82,7 @@
...
@@ -82,7 +82,7 @@
"image_preprocess = Preprocess(frcnn_cfg)\n",
"image_preprocess = Preprocess(frcnn_cfg)\n",
"\n",
"\n",
"bert_tokenizer = BertTokenizerFast.from_pretrained(\"bert-base-uncased\")\n",
"bert_tokenizer = BertTokenizerFast.from_pretrained(\"bert-base-uncased\")\n",
"visualbert_vqa = VisualBertForQuestionAnswering.from_pretrained(\"uclanlp/visualbert-vqa\")
\n
"
"visualbert_vqa = VisualBertForQuestionAnswering.from_pretrained(\"uclanlp/visualbert-vqa\")"
],
],
"outputs": [
"outputs": [
{
{
...
@@ -104,17 +104,17 @@
...
@@ -104,17 +104,17 @@
"cell_type": "code",
"cell_type": "code",
"execution_count": 5,
"execution_count": 5,
"source": [
"source": [
"#image viz\n",
"#
image viz\n",
"frcnn_visualizer = SingleImageViz(URL, id2obj=objids, id2attr=attrids)\n",
"frcnn_visualizer = SingleImageViz(URL, id2obj=objids, id2attr=attrids)\n",
"# run frcnn\n",
"# run frcnn\n",
"images, sizes, scales_yx = image_preprocess(URL)\n",
"images, sizes, scales_yx = image_preprocess(URL)\n",
"output_dict = frcnn(\n",
"output_dict = frcnn(\n",
" images,
\n",
" images,\n",
" sizes,
\n",
" sizes,\n",
" scales_yx=scales_yx,
\n",
" scales_yx=scales_yx,\n",
" padding=\"max_detections\",\n",
" padding=\"max_detections\",\n",
" max_detections=frcnn_cfg.max_detections,\n",
" max_detections=frcnn_cfg.max_detections,\n",
" return_tensors=\"pt\"\n",
" return_tensors=\"pt\"
,
\n",
")\n",
")\n",
"# add boxes and labels to the image\n",
"# add boxes and labels to the image\n",
"\n",
"\n",
...
@@ -167,7 +167,7 @@
...
@@ -167,7 +167,7 @@
" \"What is the shape of the monitor?\",\n",
" \"What is the shape of the monitor?\",\n",
"]\n",
"]\n",
"\n",
"\n",
"#Very important that the boxes are normalized\n",
"#
Very important that the boxes are normalized\n",
"# normalized_boxes = output_dict.get(\"normalized_boxes\")\n",
"# normalized_boxes = output_dict.get(\"normalized_boxes\")\n",
"features = output_dict.get(\"roi_features\")"
"features = output_dict.get(\"roi_features\")"
],
],
...
@@ -189,7 +189,7 @@
...
@@ -189,7 +189,7 @@
" return_token_type_ids=True,\n",
" return_token_type_ids=True,\n",
" return_attention_mask=True,\n",
" return_attention_mask=True,\n",
" add_special_tokens=True,\n",
" add_special_tokens=True,\n",
" return_tensors=\"pt\"\n",
" return_tensors=\"pt\"
,
\n",
" )\n",
" )\n",
"\n",
"\n",
" output_vqa = visualbert_vqa(\n",
" output_vqa = visualbert_vqa(\n",
...
...
setup.py
View file @
c783e148
...
@@ -134,7 +134,7 @@ _deps = [
...
@@ -134,7 +134,7 @@ _deps = [
"sacremoses"
,
"sacremoses"
,
"sagemaker>=2.31.0"
,
"sagemaker>=2.31.0"
,
"scikit-learn"
,
"scikit-learn"
,
"sentencepiece
=
=0.1.9
1
"
,
"sentencepiece
>=0.1.91,!
=0.1.9
2
"
,
"soundfile"
,
"soundfile"
,
"sphinx-copybutton"
,
"sphinx-copybutton"
,
"sphinx-markdown-tables"
,
"sphinx-markdown-tables"
,
...
...
src/transformers/dependency_versions_table.py
View file @
c783e148
...
@@ -52,7 +52,7 @@ deps = {
...
@@ -52,7 +52,7 @@ deps = {
"sacremoses"
:
"sacremoses"
,
"sacremoses"
:
"sacremoses"
,
"sagemaker"
:
"sagemaker>=2.31.0"
,
"sagemaker"
:
"sagemaker>=2.31.0"
,
"scikit-learn"
:
"scikit-learn"
,
"scikit-learn"
:
"scikit-learn"
,
"sentencepiece"
:
"sentencepiece
=
=0.1.9
1
"
,
"sentencepiece"
:
"sentencepiece
>=0.1.91,!
=0.1.9
2
"
,
"soundfile"
:
"soundfile"
,
"soundfile"
:
"soundfile"
,
"sphinx-copybutton"
:
"sphinx-copybutton"
,
"sphinx-copybutton"
:
"sphinx-copybutton"
,
"sphinx-markdown-tables"
:
"sphinx-markdown-tables"
,
"sphinx-markdown-tables"
:
"sphinx-markdown-tables"
,
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment