Skip to content

[BE] Simplify ids_tensor #2431

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 2 additions & 15 deletions intermediate_source/dynamic_quantization_bert_tutorial.rst
Original file line number Diff line number Diff line change
Expand Up @@ -253,8 +253,6 @@ 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
Expand Down Expand Up @@ -526,20 +524,9 @@ 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):
def ids_tensor(shape, vocab_size):
# 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()
return torch.randint(0, vocab_size, shape=shape, dtype=torch.int, device='cpu')

input_ids = ids_tensor([8, 128], 2)
token_type_ids = ids_tensor([8, 128], 2)
Expand Down
17 changes: 2 additions & 15 deletions prototype_source/graph_mode_dynamic_bert_tutorial.rst
Original file line number Diff line number Diff line change
Expand Up @@ -60,22 +60,9 @@ Once all the necesessary packages are downloaded and installed we setup the code
from torch.quantization import per_channel_dynamic_qconfig
from torch.quantization import quantize_dynamic_jit

global_rng = random.Random()

def ids_tensor(shape, vocab_size, rng=None, name=None):
def ids_tensor(shape, vocab_size):
# 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()
return torch.randint(0, vocab_size, shape=shape, dtype=torch.int, device='cpu')

# Setup logging
logger = logging.getLogger(__name__)
Expand Down