Skip to content

Commit ed130b2

Browse files
committed
Make the serving model importable without flax
1 parent 7b63b64 commit ed130b2

1 file changed

Lines changed: 11 additions & 4 deletions

File tree

keras_hub/src/vllm/keras_hub_for_causal_lm.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,28 @@
33
`KerasHubForCausalLM` lives here in keras-hub and is registered with
44
tpu-inference's model registry (and vLLM's) by tpu-inference's plugin hook,
55
so the native `flax_nnx` loader resolves it by architecture name like any
6-
other model. It is only ever imported on the serving path, where flax and
7-
tpu-inference are installed.
6+
other model. It is only ever instantiated on the serving path, where flax
7+
and tpu-inference are installed; importing it works anywhere (api-gen walks
8+
every module), with the nnx base class swapped in only when flax exists.
89
"""
910

1011
import jax
11-
from flax import nnx
1212
from jax.sharding import Mesh
1313
from keras import ops
1414

15+
try:
16+
from flax import nnx
17+
18+
_NnxModule = nnx.Module
19+
except ImportError: # flax is only present on the serving path
20+
_NnxModule = object
21+
1522
from keras_hub.src.models.causal_lm import CausalLM
1623
from keras_hub.src.vllm.context import get_vllm_context
1724
from keras_hub.src.vllm.context import vllm_context_scope
1825

1926

20-
class KerasHubForCausalLM(nnx.Module):
27+
class KerasHubForCausalLM(_NnxModule):
2128
"""Serves a KerasHub `CausalLM` on tpu-inference's native JAX path.
2229
2330
An adapter, not a conversion: it implements the model interface the

0 commit comments

Comments
 (0)