From 03806af94d80a20b5d4ac5a7f95546552227b51b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Luis=20Castro=20Garc=C3=ADa?= Date: Wed, 31 May 2023 21:43:41 -0600 Subject: [PATCH] Implement function for BERT quantization tutorial, resolves issue #1971 --- .../dynamic_quantization_bert_tutorial.rst | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/intermediate_source/dynamic_quantization_bert_tutorial.rst b/intermediate_source/dynamic_quantization_bert_tutorial.rst index 53ac2cd0af..39cff5a22c 100644 --- a/intermediate_source/dynamic_quantization_bert_tutorial.rst +++ b/intermediate_source/dynamic_quantization_bert_tutorial.rst @@ -255,6 +255,9 @@ model before and after the dynamic quantization. torch.manual_seed(seed) set_seed(42) + # Initialize a global random number generator + global_rng = random.Random() + 2.2 Load the fine-tuned BERT model ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -525,6 +528,21 @@ We can serialize and save the quantized model for the future use using .. code:: python + def ids_tensor(shape, vocab_size, rng=None, name=None): + # Creates a random int32 tensor of the shape within the vocab size + if rng is None: + rng = global_rng + + total_dims = 1 + for dim in shape: + total_dims *= dim + + values = [] + for _ in range(total_dims): + values.append(rng.randint(0, vocab_size - 1)) + + return torch.tensor(data=values, dtype=torch.long, device='cpu').view(shape).contiguous() + input_ids = ids_tensor([8, 128], 2) token_type_ids = ids_tensor([8, 128], 2) attention_mask = ids_tensor([8, 128], vocab_size=2)