Skip to main content

Compiler Reference

The NeuroScript CLI provides four commands for working with .ns source files: parse, validate, compile, and list.

neuroscript parse

Parse a NeuroScript file and display its internal IR structure. Useful for understanding how the compiler sees your code.

neuroscript parse <FILE> [OPTIONS]

Arguments [parse]

ArgumentDescription
<FILE>Path to a .ns source file

Options [parse]

OptionDescription
-v, --verboseShow detailed IR structure output

Example [parse]

# Quick check that a file parses
neuroscript parse my_model.ns

# See the full IR
neuroscript parse my_model.ns --verbose

neuroscript validate

Parse a file and run all validation checks: neuron existence, connection arity, cycle detection, and shape compatibility.

neuroscript validate <FILE> [OPTIONS]

Arguments [validate]

ArgumentDescription
<FILE>Path to a .ns source file

Options [validate]

OptionDescription
-v, --verboseShow detailed validation output
--no-stdlibSkip loading the standard library
--no-depsSkip loading fetched dependencies

Example [validate]

neuroscript validate my_model.ns --verbose

neuroscript compile

Full compilation pipeline: parse, validate, optimize, and generate a PyTorch nn.Module.

neuroscript compile <FILE> [OPTIONS]

Arguments [compile]

ArgumentDescription
<FILE>Path to a .ns source file

Options [compile]

OptionDescription
-n, --neuron <NAME>Neuron to compile (defaults to PascalCase of filename)
-o, --output <FILE>Write output to a file instead of stdout
-v, --verboseShow optimization stats and detailed output
--bundleInline primitive class definitions for a self-contained output
--no-optimizeDisable all optimizations
--no-dead-elimDisable dead branch elimination only
--no-stdlibSkip loading the standard library
--no-depsSkip loading fetched dependencies

Examples [compile]

# Compile to stdout (auto-detects neuron from filename)
neuroscript compile my_model.ns

# Compile a specific neuron to a file
neuroscript compile my_model.ns --neuron Encoder -o encoder.py

# See optimization details
neuroscript compile my_model.ns --verbose

neuroscript list

List all neurons defined in a file, the standard library, or fetched packages.

neuroscript list [FILE] [OPTIONS]

Arguments [list]

ArgumentDescription
[FILE]NeuroScript file (optional when using --stdlib or --available)

Options [list]

OptionDescription
-v, --verboseShow connection details
--stdlibList all primitives and stdlib composites
--package <NAME>List neurons from a specific fetched dependency
--availableList everything: stdlib + all dependencies

Examples [list]

neuroscript list my_model.ns
neuroscript list --stdlib --verbose
neuroscript list --available

Bundle Mode

By default, compiled output imports primitives from the neuroscript_runtime Python package:

from neuroscript_runtime.primitives.linear import Linear
from neuroscript_runtime.primitives.activations import GELU

This requires pip install neuroscript-runtime in the target environment.

The --bundle flag eliminates that dependency by inlining the required primitive class definitions directly into the generated file. The output is a single, self-contained Python module that only needs torch:

neuroscript compile my_model.ns --bundle -o model.py

The bundled file includes:

  • A unified import block (torch, torch.nn, torch.nn.functional, math, typing)
  • Only the primitive classes actually used by your neuron (not the entire library)
  • Your compiled neuron class(es) at the bottom

When to use --bundle

ScenarioRecommended
Development with the NeuroScript toolchain installedDefault (no flag)
Sharing a generated file with collaborators--bundle
Deploying to an environment without neuroscript_runtime--bundle
Embedding generated code in another project--bundle
Minimizing output sizeDefault (imports are smaller)

Example

# Generate a self-contained model file
neuroscript compile transformer.ns --bundle -o transformer.py

# Verify it works without neuroscript_runtime installed
python -c "
import importlib.util, torch
spec = importlib.util.spec_from_file_location('model', 'transformer.py')
mod = importlib.util.module_from_spec(spec)
spec.loader.exec_module(mod)
print('Classes:', [c for c in dir(mod) if not c.startswith('_')])
"