Code on the server¶
Your block is Python that runs on NDIF's machines. This page covers what it can import, how to send a library of your own, what it can download, and what needs trusted access.
Packages¶
A block can import any package installed on the server. nnsight.compare() lists them next to
the versions on your machine.
import nnsight
print(nnsight.compare())
# Python Version:
# Local: 3.12.13
# Remote: 3.12.14 ≠
#
# Package Local Version Remote Version Status
# ------------------------------------- ------------- ----------------------- ------
# nnsight 0.8.4 0.8.4 ✓
# torch 2.14.1+cu126 2.14.1+cu126 ✓
# transformers 5.24.0 5.24.0 ✓
# accelerate 1.16.0 1.15.0 ≠
# ...
Every package the server has is listed. - in the local column means you do not have it.
torch, transformers, datasets, peft, einops and nnterp are installed. Import a
package inside the block or session that uses it.
An import of a package the server lacks fails on the server, and the error comes back to you:
with model.trace("The Eiffel Tower is in the city of", remote=True):
import sklearn
# RemoteError: ...
# ModuleNotFoundError: No module named 'sklearn'
Your own library¶
nnsight sends your own code with the block when it can tell the code is yours.
| Where the code is | Sent with the block |
|---|---|
| Defined in your script or notebook | Yes |
In a .py file beside your script |
Yes |
In a library you installed with pip install, including pip install -e |
No |
An installed library is expected to be on the server. When it is not, the job fails:
from nnsight import TransformersModel
model = TransformersModel("Qwen/Qwen3-8B")
import my_lib # your library, installed on your machine only
with model.trace("The Eiffel Tower is in the city of", remote=True):
token = my_lib.last_token(model.lm_head.output).save()
# RemoteError: Your request payload could not be read (ModuleNotFoundError: No module named 'my_lib'). ...
nnsight.register tells nnsight to send a library's code. Call it once, before the trace.
import nnsight
nnsight.register(my_lib) # or nnsight.register("my_lib")
with model.trace("The Eiffel Tower is in the city of", remote=True):
token = my_lib.last_token(model.lm_head.output).save()
print(model.tokenizer.decode(token))
# Paris
- nnsight sends the functions and classes the block uses from the library, not the whole library.
- The library stays registered for the rest of the Python process.
- Packages the library imports have to be installed on the server.
Files from the Hugging Face Hub¶
A block can download files from public repositories on the Hugging Face Hub: datasets with
load_dataset, weights with hf_hub_download. Dataset in a session and
Weights from the Hub show both.
The connection is read-only, and the Hub is the only site a block can reach. A request to another site, or an upload, is refused:
with model.trace("The Eiffel Tower is in the city of", remote=True):
import urllib.request
urllib.request.urlopen("https://example.com")
# RemoteError: ...
# urllib.error.URLError: <urlopen error Tunnel connection failed: 403 Forbidden>
A block has no Hugging Face token, so it cannot read a private or gated repository. To use your own weights or data, put them in a public repository.
Access levels¶
Every API key can run blocks. Some operations need trusted access, which NDIF grants to an account. The server's messages call a request without it untrusted.
| Operation | Any key | Trusted access |
|---|---|---|
Read and write activations, .source, tracer.cache, generation, sessions, model.edit(), adapters |
Yes | Yes |
loss.backward(), .grad of an activation, training tensors your block creates |
Yes | Yes |
torch.autograd.grad, create_graph=True, register_hook() or retain_grad() on a value read from the model |
Refused with NotImplementedError |
Yes |
| The values of a model weight | You get the shape, not the numbers. Saving one fails with Cannot copy out of meta tensor; no data! |
Yes |
| Changing a model weight, or an optimizer over the model's own parameters | Nothing changes, and no error is raised | Yes |
| The gradient of a model weight | Refused with NotImplementedError |
Yes |
Loading a model that needs trust_remote_code=True |
Refused. You can use the model once it is loaded. | Yes |
Without trusted access, do the work on activations instead of weights. Write a module's output to change what it computes. Train tensors of your own, added to or multiplied into activations (Train in one session).
A refusal names what to use instead:
with model.trace("The Eiffel Tower is in the city of", remote=True):
hidden = model.model.layers[10].output
hidden.requires_grad_(True)
loss = model.lm_head.output[0, -1].float().max()
grad = torch.autograd.grad(loss, hidden)[0].save()
# RemoteError: ...
# NotImplementedError: torch.autograd.grad is not supported on untrusted requests. Use loss.backward() and read .grad instead, or ask the maintainers of this NDIF server for trusted access.
To see what your account has:
trusted in the list means trusted access.
To ask for trusted access, ask on the NDIF Discord and say what you need it for.