File: test_upgrader_codegen.py

package info (click to toggle)
pytorch 1.13.1%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 139,252 kB
  • sloc: cpp: 1,100,274; python: 706,454; ansic: 83,052; asm: 7,618; java: 3,273; sh: 2,841; javascript: 612; makefile: 323; xml: 269; ruby: 185; yacc: 144; objc: 68; lex: 44
file content (34 lines) | stat: -rw-r--r-- 1,453 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# Owner(s): ["oncall: mobile"]

from torch.testing._internal.common_utils import TestCase, run_tests

from torchgen.operator_versions.gen_mobile_upgraders import (
    sort_upgrader,
    write_cpp,
)
from pathlib import Path
import tempfile
import os
from torch.jit.generate_bytecode import generate_upgraders_bytecode

pytorch_caffe2_dir = Path(__file__).resolve().parents[2]

class TestLiteScriptModule(TestCase):

    def test_generate_bytecode(self):
        upgrader_list = generate_upgraders_bytecode()
        sorted_upgrader_list = sort_upgrader(upgrader_list)
        upgrader_mobile_cpp_path = pytorch_caffe2_dir / "torch" / "csrc" / "jit" / "mobile" / "upgrader_mobile.cpp"
        with tempfile.TemporaryDirectory() as tmpdirname:
            write_cpp(tmpdirname, sorted_upgrader_list)
            with open(os.path.join(tmpdirname, 'upgrader_mobile.cpp'), 'r') as file_name:
                actual_output = [line.strip() for line in file_name.readlines() if line]
            with open(str(upgrader_mobile_cpp_path), 'r') as file_name:
                expect_output = [line.strip() for line in file_name.readlines() if line]
            actual_output_filtered = list(filter(lambda token: len(token) != 0, actual_output))
            expect_output_filtered = list(filter(lambda token: len(token) != 0, expect_output))

            self.assertEqual(actual_output_filtered, expect_output_filtered)

if __name__ == '__main__':
    run_tests()