Skip to content

Commit 86c5c2e

Browse files
committed
skip writing unchanged file
1 parent 73365c1 commit 86c5c2e

File tree

3 files changed

+25
-12
lines changed

3 files changed

+25
-12
lines changed

scripts/cpu/common/codegen.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import os
2+
3+
def write_or_skip(filepath, content):
4+
try:
5+
with open(filepath, 'r') as f:
6+
old_content = f.read()
7+
except IOError:
8+
old_content = None
9+
10+
if old_content != content:
11+
with open(filepath, 'w') as f:
12+
print('writing', filepath)
13+
f.write(content)
14+
else:
15+
print('skipped writing', filepath)

scripts/cpu/gen-dense-cpu-ops.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import sys
1010
import json
1111

12+
from common.codegen import write_or_skip
1213
from common.cpp_sig_parser import CPPSig
1314
from common.aten_sig_parser import AtenSig
1415

@@ -453,8 +454,7 @@ def gen_head_dec_code(self, cpp_func_str_h):
453454

454455
def gen_cpu_ops_shard(self, func_defs, cpp_path, header_path, num_shards=1):
455456
head_file_content = _H_HEADER.format(gen=os.path.basename(sys.argv[0]), hfuncs=''.join([f['dec'] for f in func_defs]))
456-
with open(header_path, 'w') as header_file:
457-
print(head_file_content, file=header_file)
457+
write_or_skip(header_path, head_file_content)
458458

459459
shards = [[] for _ in range(num_shards)]
460460
for idx, func in enumerate(func_defs):
@@ -465,10 +465,9 @@ def gen_cpu_ops_shard(self, func_defs, cpp_path, header_path, num_shards=1):
465465
defs_code = ''.join([f['def'] for f in shard])
466466

467467
filename, ext = os.path.splitext(cpp_path)
468-
shard_filename = f'{filename}_{idx}{ext}'
469-
with open(shard_filename, 'w') as source_file:
470-
source_file_content = _CPP_HEADER.format(gen=os.path.basename(sys.argv[0]), funcs=defs_code, regs=regs_code)
471-
print(source_file_content, file=source_file)
468+
shard_filepath = f'{filename}_{idx}{ext}'
469+
shard_content = _CPP_HEADER.format(gen=os.path.basename(sys.argv[0]), funcs=defs_code, regs=regs_code)
470+
write_or_skip(shard_filepath, shard_content)
472471

473472
def gen_code(self):
474473
self.prepare_functions()

scripts/cpu/gen-sparse-cpu-ops.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import sys
1010
import json
1111

12+
from common.codegen import write_or_skip
1213
from common.cpp_sig_parser import CPPSig
1314
from common.aten_sig_parser import AtenSig
1415

@@ -371,8 +372,7 @@ def gen_head_dec_code(self, cpp_func_str_h):
371372

372373
def gen_cpu_ops_shard(self, func_defs, cpp_path, header_path, num_shards=1):
373374
head_file_content = _H_HEADER.format(gen=os.path.basename(sys.argv[0]), hfuncs=''.join([f['dec'] for f in func_defs]))
374-
with open(header_path, 'w') as header_file:
375-
print(head_file_content, file=header_file)
375+
write_or_skip(header_path, head_file_content)
376376

377377
shards = [[] for _ in range(num_shards)]
378378
for idx, func in enumerate(func_defs):
@@ -383,10 +383,9 @@ def gen_cpu_ops_shard(self, func_defs, cpp_path, header_path, num_shards=1):
383383
defs_code = ''.join([f['def'] for f in shard])
384384

385385
filename, ext = os.path.splitext(cpp_path)
386-
shard_filename = f'{filename}_{idx}{ext}'
387-
with open(shard_filename, 'w') as source_file:
388-
source_file_content = _CPP_HEADER.format(gen=os.path.basename(sys.argv[0]), funcs=defs_code, regs=regs_code)
389-
print(source_file_content, file=source_file)
386+
shard_filepath = f'{filename}_{idx}{ext}'
387+
shard_content = _CPP_HEADER.format(gen=os.path.basename(sys.argv[0]), funcs=defs_code, regs=regs_code)
388+
write_or_skip(shard_filepath, shard_content)
390389

391390
def gen_code(self):
392391
self.prepare_functions()

0 commit comments

Comments
 (0)