MCPcopy Create free account
hub / github.com/apple/axlearn / process_fn

Function process_fn

axlearn/common/input_lm.py:425–465  ·  view source on GitHub ↗
(example: dict[str, tf.Tensor])

Source from the content-addressed store, hash-verified

423
424 @seqio.utils.map_over_dataset
425 def process_fn(example: dict[str, tf.Tensor]) -> dict[str, tf.Tensor]:
426 target_labels = example["target_labels"]
427 prefix = example["prefix"] # This should match the "prefix" key used in decoding.
428 input_ids = example["input_ids"]
429
430 tf.debugging.assert_rank(target_labels, 1)
431 tf.debugging.assert_rank(prefix, 1)
432 tf.debugging.assert_rank(input_ids, 1)
433
434 orig_target_labels_len = tf.size(target_labels)
435 orig_input_ids_len = tf.size(input_ids)
436 orig_prefix_len = tf.size(prefix)
437
438 # Requires max_sequence_length to be greater than or equal to target_labels.
439 tf.debugging.assert_less_equal(
440 orig_target_labels_len,
441 max_sequence_length,
442 "max_sequence_length must be greater than target_labels length!",
443 )
444 total_tokens_to_truncate = tf.maximum(
445 orig_input_ids_len + orig_target_labels_len + orig_prefix_len - 1 - max_sequence_length,
446 0,
447 )
448 # Truncation starts from prefix.
449 prefix_tokens_to_truncate = tf.minimum(total_tokens_to_truncate, orig_prefix_len)
450 # Truncation continues on input_ids.
451 input_ids_tokens_to_truncate = tf.minimum(
452 total_tokens_to_truncate - prefix_tokens_to_truncate, orig_input_ids_len
453 )
454 # Truncate prefix from the left.
455 prefix = prefix[prefix_tokens_to_truncate:]
456 # Truncate input_ids from the left.
457 input_ids = input_ids[input_ids_tokens_to_truncate:]
458 # Under the above assertion, no target_labels should be truncated.
459 return dict(
460 prefix=prefix,
461 target_labels=target_labels,
462 input_ids=input_ids,
463 prefix_tokens_to_truncate=prefix_tokens_to_truncate,
464 input_ids_tokens_to_truncate=input_ids_tokens_to_truncate,
465 )
466
467 return process_fn
468

Callers 2

test_normalizeMethod · 0.70

Calls

no outgoing calls

Tested by 2

test_normalizeMethod · 0.56