Coverage for src/bioimageio/core/_settings.py: 88%

32 statements  

« prev     ^ index     » next       coverage.py v7.17.3, created at 2026-11-20 09:46 +0000

1from __future__ import annotations 

2 

3import os 

4import platform 

5import warnings 

6from typing import Annotated, Literal 

7 

8from pydantic import Field, field_validator 

9 

10from bioimageio.spec._internal._settings import Settings as SpecSettings 

11 

12 

13class Settings(SpecSettings): 

14 """environment variables for bioimageio.spec and bioimageio.core""" 

15 

16 keras_backend: Annotated[ 

17 Literal["torch", "tensorflow", "jax"], Field(alias="KERAS_BACKEND") 

18 ] = "torch" 

19 

20 pytorch_enable_mps_fallback: Annotated[ 

21 bool | None, Field(alias="PYTORCH_ENABLE_MPS_FALLBACK") 

22 ] = None 

23 

24 @field_validator("pytorch_enable_mps_fallback", mode="after") 

25 @classmethod 

26 def _set_default_mps_fallback(cls, value: bool | None): 

27 # pytorch versions up to the 2.6 don't support all operations (esp 3d) on MPS 

28 # this env variable allows falling back to CPU for those networks instead of failing 

29 # see for current status https://github.com/pytorch/pytorch/issues/141287 

30 if ( 30 ↛ 35line 30 didn't jump to line 35 because the condition on line 30 was never true

31 value is None 

32 and platform.system().lower() == "darwin" 

33 and platform.machine().lower() == "arm64" 

34 ): 

35 warnings.warn( 

36 "environment variable 'PYTORCH_ENABLE_MPS_FALLBACK' not set, setting 'PYTORCH_ENABLE_MPS_FALLBACK=1'." 

37 ) 

38 os.environ["PYTORCH_ENABLE_MPS_FALLBACK"] = "1" 

39 return True 

40 

41 return value 

42 

43 collection_index_url: str = "https://bioimage-io.github.io/collection/index.json" 

44 """URL to the bioimageio collection index""" 

45 

46 collection_config_url: str = ( 

47 "https://bioimage-io.github.io/collection/bioimageio_collection_config.json" 

48 ) 

49 """URL to the bioimageio collection config""" 

50 

51 processing_chunk_min_bytes: int = 104_857_600 # 100 MiB 

52 """Minimum number of bytes to process in a single (dask) chunk when processing large tensors blockwise.""" 

53 

54 gradio_server: str | None = None 

55 """URL or Hugging Face space name to connect to with the remote gradio model adapter or remote gradio prediction pipeline. 

56 

57 Example: "bioimage-io/bioimage-io-gradio-server" 

58 """ 

59 

60 gradio_server_model_cache_max_size: int = 10 

61 """Max number of models to cache in the gradio server for prediction pipelines using the gradio backend.""" 

62 

63 gradio_server_model_cache_max_memory: str = "40GB" 

64 """Max memory to use for model caching in the gradio server for prediction pipelines using the gradio backend.""" 

65 

66 

67settings = Settings() 

68"""parsed environment variables for bioimageio.spec and bioimageio.core"""