Skip to content

Storage alignment is not preserved #607

@AnthonyBarbier

Description

@AnthonyBarbier

System Info

Version: 0.5.3

Information

  • The official example scripts
  • My own modified scripts

Reproduction

Some accelerators require the torch.Tensor storage to be 4 bytes aligned but because of the metadata at the start of the safetensors file and because tensors are serialised back to back without padding it happens that some fp32 values for example are only 2 bytes aligned (Which causes our HW to throw an exception).

from diffusers import StableDiffusionPipeline
import os

model_id = "stabilityai/stable-diffusion-2"
pipe = StableDiffusionPipeline.from_pretrained(
            model_id,
            use_safetensors=int(os.environ.get("USE_SAFETENSORS"))!= 0,
)
print(f"{pipe.text_encoder.text_model.encoder.layers[0].self_attn.q_proj.bias.data_ptr()/ 4.0}")
print(f"{pipe.text_encoder.text_model.encoder.layers[0].self_attn.q_proj.weight.data_ptr()/ 4.0}")

Note: because of a bug in diffusers you'll need to switch between two cache folders between runs or the model won't switch between safetensors / not-safetensors.

HF_HUB_CACHE=/tmp/safetensors USE_SAFETENSORS=1 TRANSFORMERS_VERBOSITY=debug python3 bug.py

Output:

loading weights file /tmp/safetensors/models--stabilityai--stable-diffusion-2/snapshots/1e128c8891e52218b74cde8f26dbfc701cb99d79/text_encoder/model.safetensors                                                                                                         
...
64712809483414.5
64712809484438.5

HF_HUB_CACHE=/tmp/no_safetensors USE_SAFETENSORS=0 TRANSFORMERS_VERBOSITY=debug python3 bug.py

Output:

loading weights file /tmp/no_safetensors/models--stabilityai--stable-diffusion-2/snapshots/1e128c8891e52218b74cde8f26dbfc701cb99d79/text_encoder/pytorch_model.bin
...
65466141378016.0
65466129834112.0

Expected behavior

All tensors alignments should match the original PyTorch tensor alignment (4 bytes).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions