Code generator scripts check if file changed and skip write if it is the same.

- speeds up build process:
  - Was g4 edit, run generators, g4 revert -a takes 46 seconds.
  - Now run generators takes 13 seconds.
- reduces output avoids missing legitimate errors

PiperOrigin-RevId: 395327404
diff --git a/tools/generate-spmm-test.py b/tools/generate-spmm-test.py
index 884594e..565c02d 100755
--- a/tools/generate-spmm-test.py
+++ b/tools/generate-spmm-test.py
@@ -455,8 +455,14 @@
       test_case = generate_test_cases(name, mr, nr, k_block, pipelined, isa)
       tests += "\n\n" + xnncommon.postprocess_test_case(test_case, arch, isa)
 
-    with codecs.open(options.output, "w", encoding="utf-8") as output_file:
-      output_file.write(tests)
+    txt_changed = True
+    if os.path.exists(options.output):
+      with codecs.open(options.output, "r", encoding="utf-8") as output_file:
+        txt_changed = output_file.read() != tests
+
+    if txt_changed:
+      with codecs.open(options.output, "w", encoding="utf-8") as output_file:
+        output_file.write(tests)
 
 
 if __name__ == "__main__":