Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions chex/_src/asserts_chexify.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import dataclasses
import functools
import re
import threading
from typing import Any, Callable, FrozenSet

from absl import logging
Expand Down Expand Up @@ -178,6 +179,8 @@ def logp1_abs_safe(x: chex.Array) -> chex.Array:
thread_pool = futures.ThreadPoolExecutor(1, f'async_chex_{func_name}')
# A deque for futures.
async_check_futures = collections.deque()
# Protect the futures from concurrent access.
async_check_futures_lock = threading.Lock()

# Checkification.
checkified_fn = checkify.checkify(fn, errors=errors)
Expand All @@ -191,8 +194,9 @@ def _chexified_fn(*args, **kwargs):

if async_check:
# Check completed calls.
while async_check_futures and async_check_futures[0].done():
_check_error(async_check_futures.popleft().result(async_timeout))
with async_check_futures_lock:
while async_check_futures and async_check_futures[0].done():
_check_error(async_check_futures.popleft().result(async_timeout))

# Run the checkified function.
_ai.CHEXIFY_STORAGE.level += 1
Expand All @@ -214,8 +218,9 @@ def _chexified_fn(*args, **kwargs):

def _wait_checks():
if async_check:
while async_check_futures:
_check_error(async_check_futures.popleft().result(async_timeout))
with async_check_futures_lock:
while async_check_futures:
_check_error(async_check_futures.popleft().result(async_timeout))

# Add a barrier callback to the global storage.
_ai.CHEXIFY_STORAGE.wait_fns.append(_wait_checks)
Expand Down