#Copyright (c) 2025 Huawei Technologies Co., Ltd.
#This program is free software, you can redistribute it and/or modify it under the terms and conditions of
#CANN Open Software License Agreement Version 2.0 (the "License").
#Please refer to the License for details. You may not use this file except in compliance with the License.
#THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
#INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
#See LICENSE in the root of the software repository for the full text of the License.
import sys, os
sys.stdout.reconfigure(encoding='utf-8')
cpp = r'D:\AI\Vector_error_testing_angent\output\RmsNormGrad\aclnnRmsNormGrad.cpp'
with open(cpp, 'rb') as f:
data = f.read()
# 1. Normalize line endings: CRLF -> LF (for the file itself)
data = data.replace(b'\r\n', b'\n')
# 2. Check for escaped \n in printf/LOG_PRINT - should be the 2-byte sequence 0x5C 0x6E
# After text-mode write, Python's \n (which is LF=0x0A in the join) becomes CRLF=0x0D0x0A in file
# After step 1, file has LF line endings. Now check the printf arguments.
# The C++ escape \n is: backslash + n = bytes 0x5C 0x6E in the .cpp file
# After binary write (which we now use), the Python source \\\\n becomes 0x5C 0x5C 0x6E
# which is WRONG for C++ (literal \\n, not newline)
# And \\n in Python source becomes 0x5C 0x6E which is CORRECT for C++
# Count patterns
has_cxx_newline = 0 # 0x5C 0x6E = correct C++ \n
has_double = 0 # 0x5C 0x5C 0x6E = wrong, literal \\n
has_cr = 0 # 0x0D 0x0A = CRLF line ending (should be gone after step 1)
idx = 0
while True:
idx = data.find(b'\x5c\x6e', idx)
if idx < 0:
break
# Check if preceded by another backslash
if idx > 0 and data[idx-1:idx] == b'\x5c':
has_double += 1
else:
has_cxx_newline += 1
idx += 2
has_cr = data.count(b'\r\n')
print(f'C++ newline (0x5C 0x6E): {has_cxx_newline}')
print(f'Double backslash (0x5C 0x5C 0x6E): {has_double}')
print(f'CRLF remaining: {has_cr}')
# 3. If we have 0x5C 0x5C 0x6E, replace with 0x5C 0x6E
if has_double > 0:
data = data.replace(b'\x5c\x5c\x6e', b'\x5c\x6e')
print(f'Fixed {has_double} double-backslash -> single')
# 4. Write back as binary (LF line endings)
with open(cpp, 'wb') as f:
f.write(data)
# Verify
with open(cpp, 'rb') as f:
newdata = f.read()
new_double = 0
idx = 0
while True:
idx = newdata.find(b'\x5c\x5c\x6e', idx)
if idx < 0:
break
new_double += 1
idx += 3
new_crlf = newdata.count(b'\r\n')
print(f'After fix: double-backslash={new_double}, CRLF={new_crlf}')
print('Done')