MCPcopy Create free account
hub / github.com/Turing-Project/WriteGPT / _top_p_sample

Function _top_p_sample

LanguageNetwork/GPT2/train/modeling.py:323–373  ·  view source on GitHub ↗

Does top-p sampling. if ignore_ids is on, then we will zero out those logits. :param logits: [batch_size, vocab_size] tensor :param ignore_ids: [vocab_size] one-hot representation of the indices we'd like to ignore and never predict, like padding maybe :param

(logits, ignore_ids=None, num_samples=1, p=0.9)

Source from the content-addressed store, hash-verified

321
322
323def _top_p_sample(logits, ignore_ids=None, num_samples=1, p=0.9):
324 """
325 Does top-p sampling. if ignore_ids is on, then we will zero out those logits.
326 :param logits: [batch_size, vocab_size] tensor
327 :param ignore_ids: [vocab_size] one-hot representation of the indices we'd like to ignore and never predict,
328 like padding maybe
329 :param p: topp threshold to use, either a float or a [batch_size] vector
330 :return: [batch_size, num_samples] samples
331 # TODO FIGURE OUT HOW TO DO THIS ON TPUS. IT'S HELLA SLOW RIGHT NOW, DUE TO ARGSORT I THINK
332 """
333 with tf.variable_scope('top_p_sample'):
334 batch_size, vocab_size = get_shape_list(logits, expected_rank=2)
335
336 probs = tf.nn.softmax(logits if ignore_ids is None else logits - tf.cast(ignore_ids[None], tf.float32) * 1e10,
337 axis=-1)
338
339 if isinstance(p, float) and p > 0.999999:
340 # Don't do top-p sampling in this case
341 print("Top-p sampling DISABLED", flush=True)
342 return {
343 'probs': probs,
344 'sample': tf.random.categorical(
345 logits=logits if ignore_ids is None else logits - tf.cast(ignore_ids[None], tf.float32) * 1e10,
346 num_samples=num_samples, dtype=tf.int32),
347 }
348
349 # [batch_size, vocab_perm]
350 indices = tf.argsort(probs, direction='DESCENDING')
351 cumulative_probabilities = tf.math.cumsum(tf.batch_gather(probs, indices), axis=-1, exclusive=False)
352
353 # find the top pth index to cut off. careful we don't want to cutoff everything!
354 # result will be [batch_size, vocab_perm]
355 p_expanded = p if isinstance(p, float) else p[:, None]
356 exclude_mask = tf.logical_not(
357 tf.logical_or(cumulative_probabilities < p_expanded, tf.range(vocab_size)[None] < 1))
358
359 # OPTION A - sample in the sorted space, then unsort.
360 logits_to_use = tf.batch_gather(logits, indices) - tf.cast(exclude_mask, tf.float32) * 1e10
361 sample_perm = tf.random.categorical(logits=logits_to_use, num_samples=num_samples)
362 sample = tf.batch_gather(indices, sample_perm)
363
364 # OPTION B - unsort first - Indices need to go back to 0 -> N-1 -- then sample
365 # unperm_indices = tf.argsort(indices, direction='ASCENDING')
366 # include_mask_unperm = tf.batch_gather(include_mask, unperm_indices)
367 # logits_to_use = logits - (1 - tf.cast(include_mask_unperm, tf.float32)) * 1e10
368 # sample = tf.random.categorical(logits=logits_to_use, num_samples=num_samples, dtype=tf.int32)
369
370 return {
371 'probs': probs,
372 'sample': sample,
373 }
374
375
376def _top_k_sample(logits, ignore_ids=None, num_samples=1, k=10):

Callers 2

model_fnFunction · 0.70
sample_stepFunction · 0.70

Calls 1

get_shape_listFunction · 0.90

Tested by

no test coverage detected