Non-autoregressive System 1 decision engine. Typed choice, score and yes/no decisions over any text in a single forward pass, in 100+ languages, with a router that picks the right checkpoint per request.
Python calibrationclassificationdecision-modelhuggingfacejevmodernbertmultilingualnlppythonpytorchroutingtyped-decisionszero-shot
fix: keep transformers from importing TensorFlow, which crashes a load when it cannot
PerryLinkc807d5ded6bb
diff --git a/laya/__init__.py b/laya/__init__.pyindex 71852513..d21c23c8 100644--- a/laya/__init__.py+++ b/laya/__init__.py@@ -1,5 +1,26 @@"""Laya: Fast, non-autoregressive System 1 decision engine with calibrated probabilities."""+import os as _os++# Tell transformers not to import TensorFlow, before anything can import transformers.+#+# Laya's model path is torch-only, but on transformers 4.x `build_model` reaches+# `transformers.modeling_utils` for `no_init_weights`, and importing that module pulls a chain+# (`loss_utils` -> `loss_d_fine` -> `loss_for_object_detection` -> `image_transforms`) that ends in+# `import tensorflow`. In an environment where TensorFlow is installed but cannot load, that is a+# native crash rather than an exception -- `Fatal Python error: Bus error` -- from a library Laya+# never uses (#915).+#+# `setdefault`, not assignment: a caller who set `USE_TF` to something deliberate keeps it, and one+# who wants TensorFlow in the same process can still have it by setting the variable themselves. CI,+# the Dockerfile and the test suite already set `USE_TF=0` for exactly this reason; this makes the+# package behave the same way without the caller having to know.+#+# It has to be here, above the imports below, because transformers decides TensorFlow's+# availability when it is first imported -- and `import laya` deliberately imports no transformers+# (the torch-backed names are lazy, see `_LAZY_ATTRS`), so this runs first in the normal case.+_os.environ.setdefault("USE_TF", "0")+from .email import clean_email_body, email_statefrom .hooks import AsyncHook, BaseHook, Hook, PredictContext, PredictHookfrom .lang import analyse as detect_languagediff --git a/tests/test_lazy_import.py b/tests/test_lazy_import.pyindex d920c261..870268c6 100644--- a/tests/test_lazy_import.py+++ b/tests/test_lazy_import.py@@ -79,6 +79,56 @@ check("from-import of a lazy name", laya.Agent.__name__, "Agent")check("__dir__ lists lazy names", "load" in dir(laya), True)+# ------------------------------------------------------------------ USE_TF guard (#915)+# On transformers 4.x the model path reaches `transformers.modeling_utils` for `no_init_weights`,+# and importing that module runs a chain (`loss_utils` -> `loss_d_fine` ->+# `loss_for_object_detection` -> `image_transforms`) that ends in `import tensorflow`. Where+# TensorFlow is installed but cannot load, that is a native crash, not an exception -- `Fatal+# Python error: Bus error` -- out of a library laya never uses. `laya/__init__.py` now sets+# `USE_TF=0` before anything can import transformers.+#+# The check is done in a subprocess because transformers decides TensorFlow's availability when it+# is first imported: by the time this file is running, transformers is already in `sys.modules`, so+# an in-process assertion could not tell "the guard worked" from "it was too late but harmless".+TF_GUARD = r'''+import os, sys+sys.path.insert(0, %r)+os.environ.pop("USE_TF", None)++# Make TensorFlow unavailable the way a broken install is, but as an ImportError rather than a+# segfault: if laya's import chain reaches for it at all, the import fails loudly here.+class Blocker:+ def find_spec(self, name, path=None, target=None):+ if name == "tensorflow" or name.startswith("tensorflow."):+ raise ImportError("tensorflow blocked")+ return None+sys.meta_path.insert(0, Blocker())++import laya+print(os.environ.get("USE_TF"), "tensorflow" in sys.modules)+''' % ROOT++guard = subprocess.run([sys.executable, "-c", TF_GUARD], capture_output=True, text=True)+check("USE_TF/import laya succeeds with tensorflow blocked", guard.returncode, 0)+gout = guard.stdout.strip().split()+check("USE_TF/is set to 0 on import", gout[0] if gout else None, "0")+check("USE_TF/tensorflow is not imported", gout[1] if len(gout) > 1 else None, "False")++# ...and a caller who set it deliberately keeps their value: `setdefault`, not assignment. `1` is+# the interesting one, because it is the value that asks FOR tensorflow and must survive.+RESPECT = r'''+import os, sys+sys.path.insert(0, %r)+import laya+print(os.environ["USE_TF"])+''' % ROOT++for preset in ("1", "0"):+ env = dict(os.environ, USE_TF=preset)+ resp = subprocess.run([sys.executable, "-c", RESPECT], capture_output=True, text=True, env=env)+ check("USE_TF/a caller's %r is preserved" % preset, resp.stdout.strip(), preset)++print("\n%d passed, %d failed" % (len(PASS), len(FAIL)))for f in FAIL:print(" FAIL", f)