ndif¶
ndif
¶
Top-level NDIF helpers.
register— ship a local module with a remote request (serialize by value).status/is_model_running— query the NDIF service and its models.compare— diff the local Python environment against the remote one.
Tables render as plain text with optional ANSI color (matching
nnsight.intervention.backends.display); no rich dependency. Network
libraries are imported lazily so importing nnsight stays light.
NdifStatus
¶
A view of NDIF's deployed models, with a formatted table on print.
deployments maps repo id -> {model_class, repo_id, revision, level,
state} for each HOT/WARM model; status is the derived service state.
Indexing/iteration delegate to deployments for convenience.
Status
¶
EnvComparison
¶
A local-vs-remote environment diff, inspectable and printable.
Printing shows the Python-version line and the package table. Inspect the structured result directly:
local/remote— the raw env dicts.local_python/remote_python/python_matches.packages—{pkg: {"local", "remote", "match", "critical"}}for every package the server has.mismatches/critical_mismatches— the differing subset.
local_python
instance-attribute
¶
remote_python
instance-attribute
¶
packages
instance-attribute
¶
packages = {pkg: {'local': local_pkgs.get(pkg, '-'), 'remote': remote_pkgs[pkg], 'match': local_pkgs.get(pkg, '-') == remote_pkgs[pkg], 'critical': pkg.lower() in CRITICAL_PACKAGES} for pkg in remote_pkgs}
register
¶
Register a local module for serialization by value in remote execution.
Code submitted to NDIF that imports a module not installed on the server
would raise ModuleNotFoundError. Registering the module ships its class
and function source with the request so it's rebuilt server-side. Thin
wrapper over cloudpickle.register_pickle_by_value (the serializer nnsight
uses honors it).
| PARAMETER | DESCRIPTION |
|---|---|
module
|
The module object, or its name as a string.
TYPE:
|
Example::
import mymodule
import nnsight
nnsight.register(mymodule) # or nnsight.register("mymodule")
with model.trace("Hello", remote=True):
out = mymodule.myfn(model).save()
status
¶
status(raw: bool = False) -> Union[dict, NdifStatus]
Query the NDIF service and its deployed models.
Printing the returned NdifStatus shows a table of the currently
deployed (HOT/WARM) models and their state.
| PARAMETER | DESCRIPTION |
|---|---|
raw
|
If
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Union[dict, NdifStatus]
|
The raw dict ( |
Union[dict, NdifStatus]
|
|
Examples:
is_model_running
¶
whoami
¶
Resolve the identity NDIF associates with an API key.
Calls the service's /whoami endpoint and returns {"email": ..., "tags":
[...]}. email is None when the key is unrecognized (or the server has
key validation disabled). Uses the configured key (CONFIG.API.APIKEY) when
api_key isn't given.
| PARAMETER | DESCRIPTION |
|---|---|
api_key
|
The key to resolve; defaults to the configured one.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
dict
|
The |
login
¶
Store your NDIF API key so future sessions can use it (HuggingFace-style).
Prompts for the key with getpass (never echoed) when not given, verifies it
against the service via whoami, then persists it with
CONFIG.set_default_api_key (which writes config.yaml). Verification is
best-effort — the key is still saved if the service can't be reached or doesn't
recognize it, with a note — so a typo is surfaced without blocking login.
| PARAMETER | DESCRIPTION |
|---|---|
api_key
|
The NDIF API key. Prompted for (hidden) when not given; empty input is a no-op that saves nothing.
TYPE:
|
Examples:
get_local_env
¶
The local Python version and installed packages (by import name).
get_remote_env
¶
The NDIF server's Python version and installed packages (cached).
pull_env
¶
Auto-register local (non-installed) modules for serialize-by-value, once.
The remote backend calls this before its first request: every module
get_local_env discovers as "local" (importable from the working
tree, not a pip install) is passed to register, so its source ships
with remote requests. Without it, remote code that imports a local module
raises ModuleNotFoundError server-side. Cached via _PULLED_ENV so the
(cheap but non-trivial) local-env scan runs only once per process.
build_table
¶
A package version-comparison table (local vs remote) as plain text.
compare
¶
compare() -> EnvComparison
Compare the local and remote NDIF Python environments.
Package or Python-version drift between client and server can make interventions behave differently remotely than locally; this surfaces it.
Returns an EnvComparison — print it for the table, or inspect
.mismatches / .critical_mismatches / .python_matches.
Examples: