Nasty.Statistics.Neural.Transformers.Config (Nasty v0.3.0)

View Source

Configuration management for transformer models.

Provides centralized configuration via:

  • Application config (config.exs)
  • Environment variables
  • Runtime options

Environment Variables

  • NASTY_MODEL_CACHE_DIR - Model cache location
  • NASTY_USE_GPU - Enable GPU acceleration (true/false)
  • NASTY_TRANSFORMER_MODEL - Default transformer model
  • NASTY_HF_HOME - HuggingFace cache directory

Application Configuration

config :nasty, :transformers,
  cache_dir: "priv/models/transformers",
  default_model: :roberta_base,
  backend: :exla,
  device: :cpu,
  offline_mode: false

Summary

Functions

Gets the numerical backend.

Gets the model cache directory.

Gets the default transformer model.

Gets the computation device (CPU or CUDA).

Gets the current transformer configuration.

Checks if offline mode is enabled.

Returns configuration as keyword list for passing to functions.

Validates configuration and provides helpful error messages.

Gets configuration with runtime options merged.

Types

backend()

@type backend() :: :exla | :nx_binary

config()

@type config() :: %{
  cache_dir: String.t(),
  default_model: atom(),
  backend: backend(),
  device: device(),
  offline_mode: boolean()
}

device()

@type device() :: :cpu | :cuda

Functions

backend()

@spec backend() :: backend()

Gets the numerical backend.

Examples

Config.backend()
# => :exla

cache_dir()

@spec cache_dir() :: String.t()

Gets the model cache directory.

Checks in order:

  1. NASTY_MODEL_CACHE_DIR env var
  2. NASTY_HF_HOME env var (for compatibility)
  3. Application config
  4. Default: priv/models/transformers

Examples

Config.cache_dir()
# => "/home/user/.cache/nasty/transformers"

default_model()

@spec default_model() :: atom()

Gets the default transformer model.

Examples

Config.default_model()
# => :roberta_base

device()

@spec device() :: device()

Gets the computation device (CPU or CUDA).

Examples

Config.device()
# => :cpu

get()

@spec get() :: config()

Gets the current transformer configuration.

Precedence (highest to lowest):

  1. Runtime options passed to functions
  2. Environment variables
  3. Application config
  4. Default config

Examples

Config.get()
# => %{cache_dir: "priv/models/transformers", ...}

Config.get(:cache_dir)
# => "priv/models/transformers"

get(key)

@spec get(atom()) :: term()

offline_mode?()

@spec offline_mode?() :: boolean()

Checks if offline mode is enabled.

In offline mode, only cached models are used and no network requests are made to HuggingFace Hub.

Examples

Config.offline_mode?()
# => false

to_keyword()

@spec to_keyword() :: keyword()

Returns configuration as keyword list for passing to functions.

Examples

Config.to_keyword()
# => [cache_dir: "...", default_model: :roberta_base, ...]

validate!()

@spec validate!() :: :ok

Validates configuration and provides helpful error messages.

Examples

Config.validate!()
# => :ok (or raises if invalid)

with_opts(opts)

@spec with_opts(keyword()) :: config()

Gets configuration with runtime options merged.

Examples

Config.with_opts(cache_dir: "/tmp/models", device: :cuda)
# => %{cache_dir: "/tmp/models", device: :cuda, ...}