sargo = true or serve --sargo, binary built with ./run_build.sh --sargo) and from then on only talks to it over HTTP. Without sargo the routes below are not mounted.
The routes are generic: the type of model is the model_type argument (for now only "gcnii", the GCNII* graph classifier). Every model belongs to one user: the user of a request is the one of the gb-user-id header (or of the API key), never a field of the body, and it is passed on to sargo as user_id. Training one user never changes another’s model.
Routes
All of them act as the user of the request.
|
POST /v1/gb/model/train/dataset | {"model_type": "gcnii", "dataset"?, "classes"?, "class_pick"?, "top_classes"?, "other_class"?, "min_edges"?, "min_graphs_per_class"?, "validation_fraction"?, "split_seed"?, "epochs"?, "lr"?, "batch"?, "hidden"?, "depth"?, "seed"?} | 202 {"user_id", "job", "dataset", "train", "validation", "classes", "run"} |
/train also takes "validation": graphs (each with a class) the model is not trained on; when the job is done it has val_accuracy and val_loss on them.
A graph is n nodes numbered 0..n-1, undirected edges [a, b] and optional nodes ({"name", "type"} each; the types are what the model sees). Defaults: 30 epochs, lr 0.01, batch 4, hidden 16, depth 16, random seed.
Errors: 400 bad request (no user, missing or unknown model_type, no graphs, a graph without class, an edge outside the graph, …), 404 the user has no trained model (infer, embed), 409 the user is already training, 503 sargo has no per-user models, 502 sargo did not answer. A new training replaces the user’s model when it finishes.
Training on a dataset
POST /v1/gb/model/train/dataset trains on the graphs of one of the user’s datasets, labelled with the classes an LLM gave them when they were remembered (classify_graph, see Remember):
- Fetch (SQL): the artifacts, entities, facts and
gb_artifact_classesof the dataset are read (memory.LoadKnowledgeDataset). - Eligible graphs: a graph needs at least
min_edgesedges (default 1; the model learns from structure) and an LLM class. - One class per graph: the classifying training hook (hooks) if the program set one, else the default, by
class_pick:majority(the class given most often to the graph, earliest on a tie) orlatest(the first class of the most recent model run).classesrestricts the choice to those names. - Class set:
top_classesN keeps the N most given classes; the graphs of the others are trained asotherifother_classis true, else left out. Then the classes with fewer thanmin_graphs_per_classgraphs are dropped. At least 2 classes must be left (422). - Partition: per class,
validation_fraction(in[0, 1), default 0 = no validation) of the graphs go to the validation set, in an order fixed bysplit_seed; each class keeps at least one training graph. - Convert and train: entities become nodes (
name,type), facts undirected edges (a fact to an entity outside the graph adds it as a node named after it, without a type; self loops and repeats are dropped), the class the label; both sets go to sargo (/trainwithvalidation). Follow the job withGET /v1/gb/model/train.
/train, plus 404 no such dataset, 422 no graph left to train on or fewer than 2 classes (remember the dataset with classify_graph on).
History
Every training on a dataset is recorded in<sargo_gcnii_dir>/_history/<user>/<dataset>/<run id>/ (the run ID is the start time): run.json (user, dataset, options, training settings, classes, graphs per class in each set, artifact IDs of each set) and classes.json. The answer of the training has the run ID; GET /v1/gb/model/train/dataset/runs?dataset= returns {"runs": [...]}, newest first. The history does not hold the weights; those are in the weights store below.
Weights and the trained models table
Sargo trains and serves the model of a user from a local directory (sargo_gcnii_dir/<hex of the user id>/model.weights and classes.json): its working copy, one model per user, replaced by every training. gligabao keeps every finished model in a durable store and tracks it in the same table as the LoRA models of the sidecar, gb_trained_models (see training/sidecar.md).
Several instances. (The full design, its races and failure handling are in gcnii-cluster.md.) In a cluster the gligabao instances take requests behind a load balancer. Each has its own sargo and its own directory; they share only the database and the bucket. So a training runs in the sargo of ONE instance, and the others learn about it from the database, in the table gb_model_train_jobs (one row per training: job_id, model_type, user_id, instance_id, status running/done/failed/lost, error, job (sargo’s last report), info (dataset, history run, settings), model_id, started_at, heartbeat_at, finished_at):
- Claim. Before sargo is asked to train, the instance inserts the running row. A unique index allows one running row per user and model type in the whole cluster, so a second
POSTof the user, on any instance, is a409(also while the first is still running on another instance). If sargo then refuses the request, the row is ended asfailed(the user can try again). - Watch. The instance follows its sargo in the background (
internal/api/model/weights.go,Recorder) and beats the row (heartbeat_at, with sargo’s latest report) about every 2 seconds. When sargo reports the jobdoneit copies the two files into the store, inserts the model row, writes the marker (below) and ends the job rowdonewith themodel_id. Afailedjob ends the rowfailedwith sargo’s error; storing the weights failing ends itfailedtoo (the model is still in the instance’s directory). - Lost. A running row whose last beat is older than 90 seconds (the instance crashed or was replaced; sargo trains in memory, so the job is gone) is marked
lostby whichever instance looks next (a status request, or the next claim of the user), and the user can train again. - Status.
GET /v1/gb/model/trainshows the newest job of the user as the database has it, the same on every instance:runninguntil the weights are stored and recorded,done, orfailed(alostjob isfailed,errorsays the instance stopped). Only a training that is in this instance’s sargo and not in the table (made before the table existed) is shown from sargo. - Sync. Before a status,
/inferor/embed, the instance compares the model in its directory with the latest row of the user: the directory has amodel_idmarker file with the ID of the row whose weights it holds. If it is not the latest row (another instance trained a newer model, or the directory was lost with its machine) the files of the latest row are written into the directory (each checked against the recorded size and sha256; the weights file last, then the marker). Sargo notices the replaced weights file (it checks its modification time on each use) and loads it. Nothing is written while this instance has a running job of the user: the directory then holds the newest model, not yet recorded. A user with no row gets sargo’s own answer (404when it has no model).
model_weights_s3_bucket, normally the meta bucket, the same as the evals), under its own subdirectory per model type: <model_weights_s3_prefix>/<model_type>/<user>/<job id>/model.weights and classes.json, e.g. model_weights/gcnii/<user>/gcnii_1700000000_1a2b3c4d/model.weights. The user is one encoded path segment (as for the evals); the job id is the start time and a random suffix, chosen at the claim; a key is written once and never overwritten or deleted. Without a bucket the same layout is written under model_weights_local_dir (default model_weights in the working directory), so a local run is tracked too (a local directory is not shared by a cluster). Settings (also MODEL_WEIGHTS_S3_BUCKET, _REGION, _PREFIX, _ENDPOINT): build/gligabao.toml. Credentials come from the AWS chain; the bucket must already exist (the Terraform of gloflow_emergence has the IAM policy: s3:PutObject and s3:GetObject on model_weights/*, s3:ListBucket on the bucket).
The model row. model_type = "gcnii", user_id the user, storage s3 (or local), job_id the id above, path the directory in the store (model_weights/gcnii/<user>/<job id>), files (path, size, sha256 of the two files), size_bytes, dataset the dataset trained on ("" for POST /v1/gb/model/train), num_examples the training graphs, num_train_steps the epochs, final_loss the loss on the training graphs, params (JSON: run the history run, train the settings the request gave, classes, accuracy, loss, val_accuracy, val_loss), base_model and model_size empty, run_id null. LoRA rows have model_type = "lora", user_id = "", storage = "modal_volume" (what every row had before these columns, which an existing database gets at start).
Not the sidecar. The training API (/v1/gb/training/models) lists the rows of every type and user (it is the admin view). A gcnii row cannot be run through the sidecar: inference, evals, audits and deployment answer 409 for it.
Where things are
-
internal/modeltrain: the dataset-to-sets pipeline above (Build,DefaultHook,PickClass,Split,ToGCNIIGraph) and its history (history.go); the handler isinternal/api/model/dataset.go. -
rust/sargo/src/gcnii/users.rs: the models of the users and their background trainings;router.rs: the/v1/gcnii/...endpoints. -
internal/sargo/gcnii.go: the HTTP client methods (GCNIITrain,GCNIITrainStatus,GCNIIInfer,GCNIIEmbed) and types. -
internal/api/model: the handlers of the routes above; mounted byapi.WithGCNII(server.Options.GCNII, set ingligabao.gofrom the sargo client). -
The models are saved in
sargo_gcnii_dir(config; default agcnii_modelsdirectory in the working directory), one subdirectory per user: sargo’s working copy. -
internal/modeltrain/weights.go: the weights store (Weights:Save,Restore,NewModel);internal/api/model/weights.go: theRecorder(claim, watcher, sync,GET /v1/gb/model/models);internal/training/db/db_model_train_job.go: the tablegb_model_train_jobs; the row isinternal/training/db/model.TrainedModel.