MCPcopy Create free account
hub / github.com/pytorch/executorch / preprocess

Function preprocess

backends/vulkan/runtime/gen_vulkan_spv.py:585–658  ·  view source on GitHub ↗
(
    input_text: str, variables: Dict[str, Any], input_path: str = "codegen"
)

Source from the content-addressed store, hash-verified

583
584# https://github.com/google/XNNPACK/blob/master/tools/xngen.py
585def preprocess(
586 input_text: str, variables: Dict[str, Any], input_path: str = "codegen"
587) -> str:
588 # Workaround to handle source files using \ to extend mecros to a new line
589 input_text = re.sub(r"\\$", r"\\\\", input_text, flags=re.MULTILINE)
590
591 input_lines = input_text.splitlines()
592 python_lines = []
593
594 blank_lines = 0
595
596 last_indent = ""
597
598 # List of tuples (total_index, python_indent)
599 indent_stack = [("", "")]
600
601 # Indicates whether this is the first line inside Python
602 # code block (i.e. for, while, if, elif, else)
603 python_block_start = True
604 for input_line in input_lines:
605 if input_line == "":
606 blank_lines += 1
607 continue
608 # Skip lint markers.
609 if "LINT" in input_line:
610 continue
611
612 input_indent = extract_leading_whitespace(input_line)
613 if python_block_start:
614 assert input_indent.startswith(last_indent)
615 extra_python_indent = input_indent[len(last_indent) :]
616 python_indent = indent_stack[-1][1] + extra_python_indent
617 indent_stack.append((input_indent, python_indent))
618 assert input_indent.startswith(indent_stack[-1][0])
619 else:
620 while not input_indent.startswith(indent_stack[-1][0]):
621 del indent_stack[-1]
622 python_block_start = False
623
624 python_indent = indent_stack[-1][1]
625 stripped_input_line = input_line.strip()
626 if stripped_input_line.startswith("$") and not stripped_input_line.startswith(
627 "${"
628 ):
629 if stripped_input_line.endswith(":"):
630 python_block_start = True
631 while blank_lines != 0:
632 python_lines.append(python_indent + "print(file=OUT_STREAM)")
633 blank_lines -= 1
634 python_lines.append(python_indent + stripped_input_line.replace("$", ""))
635 else:
636 assert input_line.startswith(python_indent)
637 while blank_lines != 0:
638 python_lines.append(python_indent + "print(file=OUT_STREAM)")
639 blank_lines -= 1
640 python_lines.append(
641 python_indent
642 + "print(%s, file=OUT_STREAM)"

Callers 2

generate_src_fileMethod · 0.85
get_dog_image_tensorFunction · 0.85

Calls 4

escapeFunction · 0.85
compileFunction · 0.50
appendMethod · 0.45

Tested by

no test coverage detected