4.2 KiB
4.2 KiB
Adding New CLI Arguments for llama_send_tensors / llama_recv_tensors
This note walks through how to add new CLI arguments that flow into the distributed communication path in src/llama.cpp, including all touchpoints you must update.
Key Data Flow
- CLI parse →
gpt_params(common/arg.cpp) - Copy into context params →
llama_context_params(common/common.cppviallama_context_params_from_gpt_params) - Propagate into runtime context →
lctx.cparams(src/llama.cppinitialization) - Used by send/recv →
llama_send_tensors/llama_recv_tensors(src/llama.cpp)
Files to Update
common/arg.cpp: register the CLI flag(s), set defaults, validate ranges.common/common.h: add fields togpt_params(string/int/bool as needed).common/common.cpp:llama_context_params_from_gpt_params: copy new fields fromgpt_paramsintollama_context_params(allocate/copy strings if needed).- Ensure
llama_context_default_params(insrc/llama.cpp) sets sensible defaults.
include/llama.handspm-headers/llama.h: extendstruct llama_context_paramswith the new field(s).src/llama.cpp:- Thread new params through any places that consume them (e.g.,
llama_send_tensors/llama_recv_tensors). - Validate inputs at use-site (range checks, supported values).
- If the param affects compression/sparsity/decompression, branch in
llama_send_tensorsand interpret tags inllama_recv_tensors.
- Thread new params through any places that consume them (e.g.,
Step-by-Step Template
-
Define parameter storage
- Add to
struct gpt_paramsincommon/common.h. - Set default values there.
- Add to
-
Expose via CLI
- In
common/arg.cpp, add allama_argentry:- Flag names (short/long), help text, value hints.
- Handler writes into
gpt_params(string/int/bool handler). - Validate ranges here if you want early failure.
- In
-
Copy into runtime context
- In
llama_context_params_from_gpt_params(common/common.cpp):- For strings, allocate and
strcpyintocparams. - For scalars, direct assignment.
- For strings, allocate and
- In
-
API surface
- Add the field to
struct llama_context_paramsin both headers:include/llama.hspm-headers/llama.h
- Update
llama_context_default_paramsinsrc/llama.cppwith defaults (nullptr/0/false, or a literal default).
- Add the field to
-
Use in send/recv
src/llama.cpp:- Accept the new argument in
llama_send_tensors(andllama_recv_tensorsif needed). - Pass
lctx.cparams.<field>when callingllama_send_tensorsfrom the main decode loop. - Implement behavior (e.g., choosing compression mode, sparse ratio, thresholds).
- Add validation guards near use; log or error out cleanly.
- Accept the new argument in
-
(Optional) Docs/CHANGES
- Document the new flag in
CHANGES.mdand any user-facing README if needed.
- Document the new flag in
Example: Existing comm_datatype / comm_sparse_percentage
- CLI:
--comm-datatype,--comm-sparse-percentage(common/arg.cpp). - Params: Stored in
gpt_params(common/common.h) with defaults. - Context copy:
llama_context_params_from_gpt_paramshandles string allocation and scalar copy (common/common.cpp). - Headers:
llama_context_paramsexposesconst char * comm_datatype; int comm_sparse_percentage;(include/llama.h,spm-headers/llama.h). - Defaults:
llama_context_default_paramssets them tonullptr/ 0 (src/llama.cpp). - Usage:
llama_send_tensorsinspectscomm_datatypeand usescomm_sparse_percentagewhenf32_sparsityis selected;llama_recv_tensorsreads the tag and auto-decompresses.
Validation Tips
- For ranged ints, check on parse and on use; fail fast with a clear message.
- For enums/strings, normalize and validate against a small allowed list before hitting the hot path.
- Keep the wire format self-describing: include a datatype tag in the multipart messages so receivers can branch correctly.
Quick checklist
- Field in
gpt_paramswith default - CLI flag in
common/arg.cpp - Copy into
llama_context_params_from_gpt_params - Field added to both
llama.hheaders - Default set in
llama_context_default_params - Passed into
llama_send_tensors/llama_recv_tensors - Behavior implemented + input validation
- Docs updated (optional)