Skip to content

Warn about safetensors CUDA alignment issue - #248

Merged
justinchuby merged 2 commits into
mainfrom
fix-safetensors-cuda-alignment
May 4, 2026
Merged

Warn about safetensors CUDA alignment issue#248
justinchuby merged 2 commits into
mainfrom
fix-safetensors-cuda-alignment

Conversation

@justinchuby

Copy link
Copy Markdown
Member

Summary

Add documentation and runtime warning about safetensors external data format causing CUBLAS misaligned address errors on CUDA.

Root Cause

Safetensors header size (variable, ~34KB) + 8-byte length prefix creates a data start offset that is not 256-byte aligned. ALL subsequent tensor offsets inherit this misalignment. When ORT memory-maps the safetensors file and passes the pointer to cuBLAS, the misaligned address triggers CUBLAS_STATUS_INVALID_VALUE.

Verified on gemma-4-e2b-it:

  • Safetensors: 0/543 tensors 256-byte aligned ❌
  • ONNX .data: 543/543 tensors 256-byte aligned ✅

Changes

  • Add warning in ModelPackage.save() docstring about safetensors alignment
  • Add runtime warning in CLI when --external-data safetensors + --ep cuda

Recommendation

Use --external-data onnx (the default) for CUDA builds. The safetensors format is fine for CPU and for inference via cudaMemcpy (which handles alignment internally), but can fail with mmap-based loading paths.

Ref: onnxruntime/mobius#2120

Safetensors format does not guarantee 256-byte offset alignment for
tensor data within the file. The header size (variable, typically
~34KB) plus the 8-byte length prefix creates a data start offset
that is not aligned to 256 bytes, causing ALL subsequent tensor
offsets to be misaligned. This can trigger CUBLAS_STATUS_INVALID_VALUE
('misaligned address') errors on CUDA when weights are loaded via
memory-mapped I/O.

ONNX external data format (.onnx.data) uses onnx_ir's alignment
logic which ensures 256-byte alignment for all tensors.

Changes:
- Add warning in ModelPackage.save() docstring about safetensors
  alignment limitation
- Add runtime warning in CLI when --external-data safetensors is
  used with --ep cuda

Signed-off-by: Justin Chu <justinchu@microsoft.com>
@github-actions

github-actions Bot commented May 4, 2026

Copy link
Copy Markdown

🏗️ Architecture Diff

Comparing 4c617461f70e26

Model Sub-model Changes Status

No architecture changes detected.


Legend: ⚪ No change · 🔵 Minor (attrs/inits) · 🟡 Moderate (nodes added/removed) · 🔴 Major (interface changed)

@github-actions

github-actions Bot commented May 4, 2026

Copy link
Copy Markdown

Performance Comparison

Comparing 4c617461f70e26

Model Metric Baseline Current Delta
bert (feature-extraction) model_size_bytes 359 KB 359 KB +0.0%
bert (feature-extraction) num_nodes 60 60 +0.0%
falcon model_size_bytes 364 KB 364 KB +0.0%
falcon num_nodes 66 66 +0.0%
gemma2 model_size_bytes 428 KB 428 KB +0.0%
gemma2 num_nodes 107 107 +0.0%
gpt2 model_size_bytes 388 KB 388 KB +0.0%
gpt2 num_nodes 53 53 +0.0%
llama model_size_bytes 425 KB 425 KB +0.0%
llama num_nodes 61 61 +0.0%
llama (static-cache) model_size_bytes 425 KB 425 KB +0.0%
llama (static-cache) num_nodes 58 58 +0.0%
mamba (ssm-text-generation) model_size_bytes 296 KB 296 KB +0.0%
mamba (ssm-text-generation) num_nodes 98 98 +0.0%
phi3 model_size_bytes 421 KB 421 KB +0.0%
phi3 num_nodes 59 59 +0.0%
phi3 (static-cache) model_size_bytes 421 KB 421 KB +0.0%
phi3 (static-cache) num_nodes 56 56 +0.0%
qwen2 model_size_bytes 425 KB 425 KB +0.0%
qwen2 num_nodes 61 61 +0.0%
qwen2 (static-cache) model_size_bytes 425 KB 425 KB +0.0%
qwen2 (static-cache) num_nodes 58 58 +0.0%
qwen3_5_moe (hybrid-text-generation) model_size_bytes 506 KB 506 KB +0.0%
qwen3_5_moe (hybrid-text-generation) num_nodes 275 275 +0.0%
qwen3_5_text (hybrid-text-generation) model_size_bytes 458 KB 458 KB +0.0%
qwen3_5_text (hybrid-text-generation) num_nodes 129 129 +0.0%
qwen3_5_vl (hybrid-qwen-vl) model_size_bytes 977 KB 977 KB +0.0%
qwen3_5_vl (hybrid-qwen-vl) num_nodes 408 408 +0.0%
t5 (seq2seq) model_size_bytes 836 KB 836 KB +0.0%
t5 (seq2seq) num_nodes 166 166 +0.0%
whisper (speech-to-text) model_size_bytes 1008 KB 1008 KB +0.0%
whisper (speech-to-text) num_nodes 128 128 +0.0%

No performance regressions.

@codecov

codecov Bot commented May 4, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 0% with 2 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
src/mobius/__main__.py 0.00% 1 Missing and 1 partial ⚠️

📢 Thoughts on this report? Let us know!

Comment thread src/mobius/__main__.py Fixed
…once'

Co-authored-by: Copilot Autofix powered by AI <223894421+github-code-quality[bot]@users.noreply.github.com>
Signed-off-by: Justin Chu <justinchuby@users.noreply.github.com>
@justinchuby
justinchuby merged commit 32854da into main May 4, 2026
20 of 23 checks passed
@justinchuby
justinchuby deleted the fix-safetensors-cuda-alignment branch May 4, 2026 23:21
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