MCPcopy
hub / github.com/huggingface/transformers / find_code_and_splits

Function find_code_and_splits

utils/check_copies.py:498–538  ·  view source on GitHub ↗

Find the code of an object (specified by `object_name`) and split it into blocks. Args: object_name (`str`): The name of the object, e.g. `transformers.models.bert.modeling_bert.BertAttention` or `tests.models.llama.test_modeling_llama.LlamaModelTest.test_config`

(object_name: str, base_path: str, buffer: dict | None = None)

Source from the content-addressed store, hash-verified

496
497
498def find_code_and_splits(object_name: str, base_path: str, buffer: dict | None = None):
499 """Find the code of an object (specified by `object_name`) and split it into blocks.
500
501 Args:
502 object_name (`str`):
503 The name of the object, e.g. `transformers.models.bert.modeling_bert.BertAttention` or
504 `tests.models.llama.test_modeling_llama.LlamaModelTest.test_config`.
505 base_path (`str`):
506 The path to the base directory within which the search will be performed. It could be either
507 `TRANSFORMERS_PATH` or `MODEL_TEST_PATH`.
508 buffer (`dict`, *optional*):
509 The buffer used to store the previous results in order to speed up the process.
510
511 Returns:
512 lines (`List[str]`):
513 The lines of the whole file where the object is defined.
514 code (`str`):
515 The object's code.
516 code_splits (`List[Tuple[str, int, int]]`):
517 `code` split into blocks. See `split_code_into_blocks`.
518 """
519 if buffer is None:
520 buffer = {}
521
522 if (object_name, base_path) in buffer:
523 lines, code, code_splits = buffer[(object_name, base_path)]
524 else:
525 code, (lines, target_start_index, target_end_index) = find_code_in_transformers(
526 object_name, base_path=base_path, return_indices=True
527 )
528 indent = get_indent(code)
529
530 # Split the code into blocks
531 # `indent` is the indent of the class/func definition header, but `code_splits` expects the indent level of the
532 # block body.
533 code_splits = split_code_into_blocks(
534 lines, target_start_index, target_end_index, len(indent) + 4, backtrace=True
535 )
536 buffer[(object_name, base_path)] = lines, code, code_splits
537
538 return lines, code, code_splits
539
540
541_re_copy_warning = re.compile(r"^(\s*)#\s*Copied from\s+transformers\.(\S+\.\S+)\s*($|\S.*$)")

Callers 1

is_copy_consistentFunction · 0.85

Calls 3

split_code_into_blocksFunction · 0.85
get_indentFunction · 0.70

Tested by

no test coverage detected