"""
本脚本有 2 种执行模式:
1. CI批跑时, 由 cmake/scripts/golden_ctrl.py 调用, 为避免日志过多, 此时 logging 级别为 logging.INFO;
2. 单独调试时, 本脚本单独被调用, 此时 logging 级别为 logging.DEBUG;
"""
import sys
import logging
from pathlib import Path
from typing import List
import numpy as np
if __name__ == "__main__":
""" 单独调试时配置 """
logging.basicConfig(format='%(asctime)s - %(filename)s:%(lineno)d - %(levelname)s: %(message)s',
level=logging.DEBUG)
g_src_root: Path = Path(Path(__file__).parent, "../../../../../").resolve()
logging.debug("SrcRoot: %s", g_src_root)
g_ctrl_path: Path = Path(g_src_root, "cmake/scripts")
if str(g_ctrl_path) not in sys.path:
sys.path.append(str(g_ctrl_path))
from golden_register import GoldenRegister
else:
from golden_register import GoldenRegister
@GoldenRegister.reg_golden_func(
case_names=[
"MoEGatePart1OnBoardTest.test_moe_gate_part1",
]
)
def gen_moe_part1_golden(case_name: str, output: Path) -> bool:
input_e_score_bias_path = Path(output, "input_e_score_bias.bin")
input_hidden_state_path = Path(output, "input_hidden_state.bin")
input_weight_path = Path(output, "input_weight.bin")
output_score_path = Path(output, "output_score.bin")
output_score4choice_path = Path(output, "output_score4choice.bin")
if case_name == "MoEGatePart1OnBoardTest.test_moe_gate_part1":
b, s, h, n = [16, 1, 7168, 256]
input_e_score_bias = np.random.uniform(0, 2, [1, n]).astype(dtype=np.float32)
input_hidden_state = np.random.uniform(0.01, 0.02, [b * s, h]).astype(dtype=np.float16)
input_weight = np.random.uniform(0.01, 0.02, [n, h]).astype(dtype=np.float16)
output_logit = np.matmul(input_hidden_state, input_weight.transpose(1, 0)).reshape([b * s, n]).astype(
np.float32)
output_score = 1 / (1 + np.exp(-output_logit))
output_score4choice = output_score + input_e_score_bias
else:
logging.error("Can't get func to gen golden, Case(%s)", case_name)
return False
input_e_score_bias.tofile(input_e_score_bias_path)
input_hidden_state.tofile(input_hidden_state_path)
input_weight.tofile(input_weight_path)
output_score.tofile(output_score_path)
output_score4choice.tofile(output_score4choice_path)
return True
def main() -> bool:
"""
单独调试 入口函数
"""
case_name_list: List[str] = [
"MoEGatePart1OnBoardTest.test_moe_gate_part1",
]
ret: bool = True
for cs in case_name_list:
output: Path = Path(g_src_root, "build/output/bin/golden", cs).resolve()
output.mkdir(parents=True, exist_ok=True)
ret = gen_moe_part1_golden(case_name=cs, output=output)
return ret
if __name__ == "__main__":
exit(0 if main() else 1)