Ejemplo n.º 1
0
    def test_generate_project(self, i, flag_hq, flag_thskip) -> None:

        input_path = os.path.abspath(
            os.path.join(os.getcwd(),
                         'tests', 'fixtures',
                         'classification',
                         #  'lmnet_quantize_cifar10_stride_2.20180523.3x3',
                         'minimal_graph_with_shape.pb'))

        output_path = os.path.join(self.build_dir, 'test_generate_project', str(i))
        gp.run(input_path=input_path,
               dest_dir_path=output_path,
               project_name='unittest',
               activate_hard_quantization=flag_hq,
               threshold_skipping=flag_thskip,
               debug=False,
               cache_dma=False
               )

        print("Passed!")
Ejemplo n.º 2
0
    def codegen_cpu(self,
                    model_path,
                    expected_output_set_name,
                    prefix,
                    input_name,
                    output_npy_name,
                    cpu_name='x86_64',
                    hard_quantize=False,
                    threshold_skipping=False,
                    use_run_test_script=False,
                    max_percent_incorrect_values=0.1,
                    from_npy=False,
                    need_arm_compiler=False,
                    cache_dma=False,
                    use_avx=False,
                    test_id=0) -> None:
        """Test code for testing code generation for CPU"""
        # TODO consider better implementation
        if need_arm_compiler:
            if shutil.which('arm-linux-gnueabihf-g++') is None:
                raise unittest.SkipTest('No arm compiler.')

        dir_tags = [str(test_id), prefix, basename(model_path), cpu_name]
        dir_tags = dir_tags + ['hq'] if hard_quantize else dir_tags
        dir_tags = dir_tags + ['thskip'] if threshold_skipping else dir_tags

        output_path = join(self.build_dir, '_'.join(dir_tags))
        input_dir_path = os.path.abspath(os.path.join(os.getcwd(), model_path))

        input_path = os.path.join(input_dir_path,
                                  'minimal_graph_with_shape.pb')
        project_name = 'code_generation'

        gp.run(
            input_path=input_path,
            dest_dir_path=output_path,
            project_name=project_name,
            activate_hard_quantization=hard_quantize,
            threshold_skipping=threshold_skipping,
            debug=False,
            cache_dma=cache_dma,
        )

        if cpu_name == 'arm_fpga':
            lib_base_name = 'fpga'
        elif cpu_name == 'x86_64':
            if use_avx:
                lib_base_name = 'x86_avx'
            else:
                lib_base_name = 'x86'
        else:  # 'aarch64' and 'arm' pass here
            lib_base_name = cpu_name

        project_dir = os.path.join(output_path, project_name + '.prj')
        generated_lib = os.path.join(project_dir,
                                     'libdlk_' + lib_base_name + '.so')
        npy_targz = os.path.join(input_dir_path,
                                 expected_output_set_name + '.tar.gz')

        run_and_check(
            ['tar', 'xvzf',
             str(npy_targz), '-C',
             str(output_path)],
            input_dir_path,
            join(output_path, "tar_xvzf.out"),
            join(output_path, "tar_xvzf.err"),
            self,
            check_stdout_include=[expected_output_set_name + '/raw_image.npy'])

        npy_path = os.path.join(output_path, expected_output_set_name)
        input_path = os.path.join(npy_path, input_name)
        expected_output_path = os.path.join(npy_path, output_npy_name)

        self.assertTrue(os.path.exists(project_dir))

        run_and_check(['make', 'clean'], project_dir,
                      join(output_path, "make_clean.out"),
                      join(output_path, "make_clean.err"), self)

        run_and_check(
            ['make', 'build', 'ARCH=' + lib_base_name, 'TYPE=dynamic', '-j8'],
            project_dir,
            join(output_path, "make.out"),
            join(output_path, "make.err"),
            self,
            check_stderr_block=['error: '])
        self.assertTrue(os.path.exists(generated_lib))

        if not use_run_test_script:
            if cpu_name == 'x86_64':
                percent_failed = self.run_library(generated_lib, input_path,
                                                  expected_output_path)
            elif cpu_name == 'arm' or cpu_name == 'arm_fpga':
                percent_failed = \
                    self.run_library_on_remote(FPGA_HOST, output_path, generated_lib, input_path, expected_output_path)
            elif cpu_name == 'aarch64':
                # Skip run library test, it will run on another test by using saved output files.
                self.save_output(test_id, generated_lib, input_path,
                                 expected_output_path)
                return
            else:
                self.fail("Unexpected cpu_name: %s" % cpu_name)
        else:
            percent_failed = self.run_library_using_script(
                generated_lib, input_path, expected_output_path, from_npy)

        self.assertTrue(
            percent_failed < max_percent_incorrect_values,
            msg=
            f"Test failed: {percent_failed:.3f}% of the values does not match")

        print(
            f"Codegen test {prefix}: passed!  {100.0 - percent_failed:.3f}% "
            f"of the output values are correct\n"
            f"[hard quantize == {hard_quantize}, threshold skipping == {threshold_skipping}, cache == {cache_dma}]"
        )
Ejemplo n.º 3
0
    def test_time_measurement_with_x86(self) -> None:
        """Test code for time measurement on x86."""
        model_path = os.path.join('tests', 'fixtures', 'classification',
                                  'lmnet_quantize_cifar10')
        output_path = self.build_dir
        project_name = 'test_binary'
        project_dir = os.path.join(output_path, project_name + '.prj')
        generated_bin = os.path.join(project_dir, 'lm_x86_64')
        input_dir_path = os.path.abspath(os.path.join(os.getcwd(), model_path))
        input_path = os.path.join(input_dir_path,
                                  'minimal_graph_with_shape.pb')
        debug_data_filename = 'cat.jpg'
        compressed_debug_data_path = os.path.join(
            input_dir_path, debug_data_filename + '.tar.gz')
        debug_data_path = os.path.join(output_path, debug_data_filename)
        debug_data_input = os.path.join(debug_data_path,
                                        '000_images_placeholder:0.npy')
        debug_data_output = os.path.join(debug_data_path, '133_output:0.npy')

        gp.run(
            input_path=input_path,
            dest_dir_path=output_path,
            project_name=project_name,
            activate_hard_quantization=False,
            threshold_skipping=False,
            debug=False,
            cache_dma=False,
        )
        self.assertTrue(os.path.exists(project_dir))

        run_and_check(['cmake', '.'], project_dir,
                      join(project_dir, "make.out"),
                      join(project_dir, "make.err"), self)

        run_and_check(['make', 'lm', '-j8'], project_dir,
                      join(project_dir, "cmake.out"),
                      join(project_dir, "cmake.err"), self)

        self.assertTrue(os.path.exists(generated_bin))

        run_and_check(
            [
                'tar', 'xvzf',
                str(compressed_debug_data_path), '-C',
                str(output_path)
            ],
            output_path,
            join(output_path, "tar_xvzf.out"),
            join(output_path, "tar_xvzf.err"),
            self,
            check_stdout_include=[debug_data_filename + '/raw_image.npy'])

        self.assertTrue(os.path.exists(debug_data_input))
        self.assertTrue(os.path.exists(debug_data_output))

        run_and_check([
            str(generated_bin),
            str(debug_data_input),
            str(debug_data_output)
        ],
                      project_dir,
                      join(project_dir, "elf.out"),
                      join(project_dir, "elf.err"),
                      self,
                      check_stdout_include=['TotalRunTime '])

        print(f"Binary time-measurement test : passed!")