Skip to content

Export Uzu traces as safetensors#450

Open
ry2009 wants to merge 1 commit into
mainfrom
ryan/trace-safetensors-export
Open

Export Uzu traces as safetensors#450
ry2009 wants to merge 1 commit into
mainfrom
ryan/trace-safetensors-export

Conversation

@ry2009

@ry2009 ry2009 commented May 26, 2026

Copy link
Copy Markdown

Summary

Reworks the Uzu tracing integration around activation/tokenization export instead of local comparison:

  • removes the old trace comparator/metrics implementation from the Uzu integration tracer
  • exports Uzu activation traces to traces.safetensors-compatible safetensors output
  • exports Uzu tokenization traces with activation_trace.token_ids / activation_trace.token_positions plus rendered request metadata
  • keeps trace input loading strict for token id/position shape
  • adds a minimal safetensors writer for exported trace tensors and metadata
  • keeps the reusable writer byte/schema oriented instead of tying it to backend arrays

This pairs with the Lalamo comparator PR: trymirai/lalamo#285

Validation

  • cargo test -p backend-uzu test_safetensors_metadata_writer --test integration
  • cargo check -p backend-uzu --tests
  • cargo fmt --check -p backend-uzu
  • git diff --check

cargo fmt --check passes but prints existing stable-rustfmt warnings about nightly-only config keys.

@ry2009 ry2009 force-pushed the ryan/trace-safetensors-export branch 3 times, most recently from 0aa5d08 to 558d57c Compare June 1, 2026 08:17
@ry2009 ry2009 force-pushed the ryan/trace-safetensors-export branch 2 times, most recently from 5a7ee19 to ff50b1f Compare June 3, 2026 19:33
@ry2009 ry2009 marked this pull request as ready for review June 3, 2026 20:12
@ry2009 ry2009 requested review from LuckyIYI and uuuvn as code owners June 3, 2026 20:12

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: ff50b1fa64

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

];
let request = json!({
"add_generation_prompt": true,
"messages": [{"role": token_codec_config.user_role_name, "content": message}],

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Preserve injected system prompt in request metadata

When ChatCodecConfig::default_system_prompt is set, InputProcessorDefault::process inserts a system message before rendering, but the exported request metadata records only the user message. In that configuration, consumers that replay the saved request with the saved template will render a different prompt and token sequence than the exported rendered_request/token tensors, making the tokenization trace internally inconsistent. Include the injected system message (or build metadata from the same resolved message list used for rendering).

Useful? React with 👍 / 👎.

name: name.into(),
shape,
data_type: array.data_type(),
data: array.as_bytes().into(),

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Avoid buffering copied activation tensors before writing

For large trace exports, this copies every produced activation tensor into a boxed byte buffer and the caller keeps all of those copies in tensors until write_trace_file finishes. Since the original ActivationTrace arrays are already resident, long prompts or larger models can roughly double the trace payload in memory and fail before the file is written; the writer can compute offsets from borrowed byte slices and stream the existing arrays instead.

Useful? React with 👍 / 👎.

@ry2009 ry2009 force-pushed the ryan/trace-safetensors-export branch from ff50b1f to 8771473 Compare June 3, 2026 21:24

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 87714733b3

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +119 to +123
];
Self::push_activation_trace_tensors(
&mut tensors,
&traces,
&ctx.model_config.decoder_config.transformer_config.layer_configs,

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Export updated cache-state tensors

When exporting decoder traces for models with attention, SSM, short-conv, or DeltaNet layers, this output is built only from token tensors plus ActivationTrace, but ActivationTrace contains activations/logits and not the updated cache state. The existing trace schema includes activation_trace.layer_results.N.updated_state.{keys,values,conv_state,ssm_state} (the previous validator read these from the cache layers), so exported safetensors for those models will be missing required tensors for downstream trace comparison/replay. Add the relevant cache-layer state tensors to tensors before writing the file.

Useful? React with 👍 / 👎.

DataType::U32 => Ok(Dtype::U32),
DataType::I64 => Ok(Dtype::I64),
DataType::U64 => Ok(Dtype::U64),
DataType::F64 | DataType::I4 | DataType::U4 | DataType::I16 | DataType::U16 => Err(dtype),

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Map 16-bit and f64 tensors to safetensors dtypes

When callers use the new public write_safetensors helper with SafeTensorData whose data_type is DataType::I16, DataType::U16, or DataType::F64, this arm returns InvalidInput even though the safetensors dtype enum in this module has corresponding I16/U16/F64 variants and ArrayElement supports those element types. That makes otherwise valid tensors unexportable through this writer; only the sub-byte I4/U4 cases need to be rejected here.

Useful? React with 👍 / 👎.

Comment on lines +450 to +455
let request = json!({
"add_generation_prompt": true,
"messages": messages.iter().map(|message| message.resolve(&token_codec_config)).collect::<Vec<_>>(),
"bos_token": token_codec_config.bos_token,
"enable_thinking": true,
});

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Include eos_token in tokenization request metadata

For chat templates that reference eos_token while rendering this trace, InputProcessorDefault::process receives token_codec_config.eos_token and the exported rendered_request/tokens are produced with that value, but the saved request metadata only carries bos_token. Consumers that replay the saved request with the saved prompt_template will either render with an undefined/null EOS token or fail to reproduce the recorded prompt, so include eos_token alongside the other template inputs.

Useful? React with 👍 / 👎.

@ry2009 ry2009 force-pushed the ryan/trace-safetensors-export branch from 8771473 to 901ad9f Compare June 3, 2026 22:17

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 901ad9fa50

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +614 to +615
if dim != source_dim || history != source_history {
return Err(Error::InvalidModelConfig(format!(

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Support full-sequence conv_state traces

For Mamba/DeltaNet/ShortConv traces where Lalamo records updated_state.conv_state as [1, suffix_len, dim], this equality check rejects any prompt whose suffix is longer than the native convolution history (kernel_size - 1). The previous SsmConvState validation path handled that schema by taking the last history window, so these common traces now fail export with InvalidModelConfig instead of producing a safetensors file.

Useful? React with 👍 / 👎.

@ry2009 ry2009 force-pushed the ryan/trace-safetensors-export branch from 901ad9f to 44e6b4f Compare June 3, 2026 22:44

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 44e6b4f526

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

context,
trace_metadata,
format!("activation_trace.layer_results.{index}.updated_state.conv_state"),
&short_conv.conv_state,

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Export the committed short-conv suffix state

For ShortConv layers with a prompt/suffix longer than one token, this exports conv_state immediately after run_llm_trace, but ShortConvMixer::run_trie_conv writes the per-token post-states into suffix_state and only updates conv_state later via commit_from_suffix_state_if_valid; this export path never calls that commit. In that common multi-token trace scenario, the saved updated_state.conv_state is the stale pre-trace state rather than the final state after the prompt, so replay/comparison will see an incorrect cache state.

Useful? React with 👍 / 👎.

@ry2009 ry2009 force-pushed the ryan/trace-safetensors-export branch from 44e6b4f to 05270e9 Compare June 4, 2026 00:18
@ry2009 ry2009 force-pushed the ryan/trace-safetensors-export branch from 05270e9 to 4cd5210 Compare June 4, 2026 02:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant