1#!/usr/bin/env python3 2# -*- coding: utf-8 -*- 3 4# Copyright (c) 2021-2024 Huawei Device Co., Ltd. 5# Licensed under the Apache License, Version 2.0 (the "License"); 6# you may not use this file except in compliance with the License. 7# You may obtain a copy of the License at 8# 9# http://www.apache.org/licenses/LICENSE-2.0 10# 11# Unless required by applicable law or agreed to in writing, software 12# distributed under the License is distributed on an "AS IS" BASIS, 13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14# See the License for the specific language governing permissions and 15# limitations under the License. 16# 17 18from functools import cached_property 19from typing import Dict, Optional 20 21from runner.options.decorator_value import value, _to_bool, _to_jit_preheats, _to_str 22 23 24class JitOptions: 25 __DEFAULT_REPEATS = 0 26 __DEFAULT_REPEATS_IF_EMPTY = 30 27 __DEFAULT_THRESHOLD_IF_EMPTY = 20 28 29 def __str__(self) -> str: 30 return _to_str(self, 2) 31 32 def to_dict(self) -> Dict[str, object]: 33 return { 34 "enable": self.enable, 35 "num_repeats": self.num_repeats, 36 "compiler_threshold": self.compiler_threshold, 37 } 38 39 @cached_property 40 @value(yaml_path="ark.jit.enable", cli_name="jit", cast_to_type=_to_bool) 41 def enable(self) -> bool: 42 return False 43 44 @cached_property 45 @value( 46 yaml_path="ark.jit.num_repeats", 47 cli_name="jit_preheat_repeats", 48 cast_to_type=lambda x: _to_jit_preheats( 49 cli_value=x, prop="num_repeats", 50 default_if_empty=JitOptions.__DEFAULT_REPEATS_IF_EMPTY) 51 ) 52 def num_repeats(self) -> int: 53 return JitOptions.__DEFAULT_REPEATS 54 55 @cached_property 56 @value( 57 yaml_path="ark.jit.compiler_threshold", 58 cli_name="jit_preheat_repeats", 59 cast_to_type=lambda x: _to_jit_preheats( 60 cli_value=x, prop="compiler_threshold", 61 default_if_empty=JitOptions.__DEFAULT_THRESHOLD_IF_EMPTY) 62 ) 63 def compiler_threshold(self) -> Optional[int]: 64 return None 65 66 def get_command_line(self) -> str: 67 options = '--jit' if self.enable else '' 68 jit_options = [ 69 f'num_repeats={self.num_repeats}' if self.num_repeats != JitOptions.__DEFAULT_REPEATS else '', 70 f'compiler_threshold={self.compiler_threshold}' if self.compiler_threshold is not None else '', 71 ] 72 jit_options = [option for option in jit_options if option] 73 74 if jit_options: 75 options += f' --jit-preheat-repeats="{",".join(jit_options)}"' 76 return options 77