Skip to content

write to tmp file & replace, avoid writing a module per process in PointwiseDynamic#532

Merged
iclementine merged 5 commits intoflagos-ai:masterfrom
iclementine:filelock
May 16, 2025
Merged

write to tmp file & replace, avoid writing a module per process in PointwiseDynamic#532
iclementine merged 5 commits intoflagos-ai:masterfrom
iclementine:filelock

Conversation

@iclementine
Copy link
Collaborator

PR Category

Other

Type of Change

Other

Description

  1. use filelock to avoid writing a module per process in PointwiseDynamiccFunction,
  2. add tests for multiprocessing & multithreading

Previously, PointwiseDynamic woule generate module for pointwise functions with the process id as suffix in filename to avoid conflicts in file-writing. It has 2 drawbacks: filenames with pid prevent file reuse between different runs, it generate duplicated files with different names, which is redundant.

Issue

Progress

  • Change is properly reviewed (1 reviewer required, 2 recommended).
  • Change is responded to an issue.
  • Change is fully covered by a UT.

Performance

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR refactors code caching to use file locks instead of per-process module files and adds tests for concurrent execution of pointwise dynamic functions.

  • Introduce filelock to guard writes and remove PID suffix from generated filenames in pointwise_dynamic and various ops.
  • Update module loading to use stable filenames and drop PID from spec names.
  • Add multithreading and multiprocessing tests for pointwise_dynamic to ensure correct behavior under concurrency.

Reviewed Changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
tests/test_pointwise_dynamic.py Add multithread/multiprocess tests for pointwise dynamic ops
src/flag_gems/utils/pointwise_dynamic.py Use filelock around code cache writes; remove PID suffix
src/flag_gems/ops/tile.py Add file locking and drop PID in cache filenames
src/flag_gems/ops/scatter.py Add file locking and drop PID in cache filenames
src/flag_gems/ops/repeat.py Add file locking and drop PID in cache filenames
src/flag_gems/ops/pad.py Add file locking and drop PID in cache filenames
src/flag_gems/ops/index_put.py Add file locking and drop PID in cache filenames
src/flag_gems/ops/gather.py Add file locking and drop PID in cache filenames
Comments suppressed due to low confidence (1)

src/flag_gems/utils/pointwise_dynamic.py:1263

  • Since filenames no longer include a process-unique suffix, the cache may serve stale code when regeneration is needed. Consider incorporating a hash of code.getvalue() into the filename or invalidating the cache when the code changes.
file_name = ( f"pointwise_dynamic_{self._scalar_fn_cache_key}_{kernel_name}_... .py")

Comment on lines +1273 to +1276
with filelock.FileLock(lock_path):
if not os.path.exists(file_path):
with open(file_path, "wt", encoding="utf-8") as f:
f.write(code.getvalue())
Copy link

Copilot AI May 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] The file-locking and write logic is duplicated across multiple operators. Extract this pattern into a shared utility to reduce duplication and simplify future updates.

Suggested change
with filelock.FileLock(lock_path):
if not os.path.exists(file_path):
with open(file_path, "wt", encoding="utf-8") as f:
f.write(code.getvalue())
write_with_lock(lock_path, file_path, code.getvalue())

Copilot uses AI. Check for mistakes.
iclementine and others added 2 commits May 15, 2025 16:38
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Bowen12992
Bowen12992 previously approved these changes May 15, 2025
Copy link
Collaborator

@Bowen12992 Bowen12992 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Collaborator

@Bowen12992 Bowen12992 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@iclementine iclementine changed the title use filelock to avoid writing a module per process in PointwiseDynamic write to tmp file & replace, avoid writing a module per process in PointwiseDynamic May 16, 2025
@iclementine iclementine merged commit 7d46236 into flagos-ai:master May 16, 2025
11 of 14 checks passed
nicelynice pushed a commit to nicelynice/FlagGems that referenced this pull request Feb 24, 2026
flagos-ai#532)

* write to tmp file & os.replace, so as to avoid writing a module per process in PointwiseDynamicFunction, add test for multiprocessing & multithreading
* update for other operators
* Update tests/test_pointwise_dynamic.py
* use os.replace to write the same contents to the same path concurrently

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants