Skip to content

feat: read PyTorch parameters, bf16 tensors, and uint8 tensors for bytes - #106

Merged
cursor[bot] merged 5 commits into
mainfrom
akshey/pytorch-inputs-fefb
Sep 28, 2026
Merged

cursor[bot] merged 5 commits into
mainfrom
akshey/pytorch-inputs-fefb

Conversation

@aksheyd

@aksheyd aksheyd commented Sep 28, 2026

Copy link
Copy Markdown
Owner

Stacked on #101, which adds the Quantized(data) constructor that this also changes. Merge #101 first. CI only runs on PRs into main, so update this branch once #101 merges.

The first calls a PyTorch user makes failed with PyTorch's errors:

  • quantize(linear.weight) raised RuntimeError: Can't call numpy() on Tensor that requires grad, since every module parameter requires grad and numpy.asarray refuses those. feat: accept anything np.asarray reads, like PyTorch tensors #64 let this through on purpose, but it's a RuntimeError, so except ValueError missed it
  • a bf16 weight, the way most LLM checkpoints load, raised TypeError: Got unsupported ScalarType BFloat16, since NumPy has no bfloat16
  • Quantized.from_bytes rejected the uint8 tensor that safetensors.torch.load_file returns, and from_parts(codes=...) did too, though scales= took one

Now:

  • a PyTorch tensor is detached, and a floating-point one read as float32, before numpy.asarray reads it. bf16 to float32 is exact, and fp16 and fp64 tensors give the same codes as before. Complex tensors are still rejected
  • this covers every argument that reads real numbers: quantize, matmul, dot, refine, alternate, and from_parts' scales
  • torch is looked up in sys.modules, never imported, since only a program that imported it can pass a tensor. NumPy arrays skip even that, so they cost what they did: a dot on 32 values takes 1.05 µs before and after
  • from_bytes, Quantized(data), and from_parts' codes also take any 1-D uint8 array that numpy.asarray reads. Anything else raises TypeError: data must be bytes, like to_bytes returns, or a 1-D uint8 array

Checked with torch 2.14 (CPU), with warnings raised as errors: a Linear weight, and its bf16, fp16, fp64, int, and bool versions, quantize like their NumPy versions. matmul takes a batch that requires grad, refine and adaptive.quantize take the parameter, and from_bytes and from_parts take uint8 tensors. The parameter itself is left as it was. The tests use a stand-in for PyTorch's tensor, so CI doesn't need torch.

just lint, just test, and just python pass.

Open in Web Open in Cursor 

torch.load defaults to weights_only=True, which refuses any global it doesn't trust. Pickles called the static method from_bytes, which pickles as builtins.getattr, so add_safe_globals([Quantized]) didn't help, and allowing getattr would defeat weights_only. Quantized(data) now loads the bytes that to_bytes saved, like from_bytes, and pickles call it, so allowing the class is enough. Pickles made through from_bytes still load, since from_bytes stays.
quantize(linear.weight) raised PyTorch's RuntimeError, since numpy.asarray refuses a tensor that requires grad, and a bf16 weight raised "Got unsupported ScalarType BFloat16", since NumPy has no bfloat16. A PyTorch tensor is now detached, and a floating-point one read as float32, which is exact for bf16, before numpy.asarray reads it. torch is looked up in sys.modules, never imported, and NumPy arrays skip the lookup, so they cost what they did. from_bytes, Quantized(data), and from_parts' codes also take any 1-D uint8 array that numpy.asarray reads, like the tensor that safetensors.torch loads.
@cursor
cursor Bot changed the base branch from akshey/torch-load-pickles-fefb to main September 28, 2026 04:47
@aksheyd
aksheyd marked this pull request as ready for review September 28, 2026 05:00
@cursor
cursor Bot merged commit 9926f30 into main Sep 28, 2026
10 checks passed
@cursor
cursor Bot deleted the akshey/pytorch-inputs-fefb branch September 28, 2026 05:00
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