[PR #483] fix(utils): enhance tensor view contiguity, squeeze safety, and scale indexing #483

Open
opened 2026-10-26 21:18:24 +00:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/deepseek-ai/DeepGEMM/pull/483
Author: @reginaldalfret
Created: 10/25/2026
Status: 🔄 Open

Base: main ← Head: fix/math-utils-contiguous-and-shape-safety


📝 Commits (1)

  • b696727 fix(utils): enhance tensor view contiguity, squeeze safety, and scale indexing

📊 Changes

3 files changed (+113 additions, -6 deletions)

View changed files

📝 deep_gemm/locality_domain.py (+1 -0)
📝 deep_gemm/utils/math.py (+10 -6)
➕ tests/test_math_utils.py (+102 -0)

📄 Description

Problem

  1. View Contiguity in UE8M0 Packing: In deep_gemm/utils/math.py, pack_ue8m0_to_int() and unpack_ue8m0_from_int() called .view(torch.int) directly on x_int and (x_int >> 23).to(torch.uint8). In PyTorch, if input tensors or intermediate slices are non-contiguous, .view() raises RuntimeError: view size is not compatible with input tensor's size and stride.
  2. Indiscriminate Squeeze in Custom Dims Cast: In per_custom_dims_cast_to_fp8(), sf.squeeze() squeezed all dimensions of size 1. If any dimension specified in dims had size 1 (e.g. batch dimension of 1 or head count 1), sf.squeeze() inadvertently collapsed it to a scalar or lower-rank tensor.
  3. 1D Scale Tensor Support: cast_back_from_fp8() and cast_back_from_fp4() indexed sf[:, group_idx], raising IndexError when sf is a 1D tensor.
  4. Zero-Division in Locality Domain: In deep_gemm/locality_domain.py, localize() executed dim % t.dim() without checking t.dim() > 0, raising ZeroDivisionError on 0-D scalar inputs.

Solution

  1. Ensure tensors are .contiguous() before .view(torch.int) in pack_ue8m0_to_int() and unpack_ue8m0_from_int().
  2. Squeeze only excluded_dims in per_custom_dims_cast_to_fp8() (sf.squeeze(dim=excluded_dims)), preserving requested dimensions of size 1.
  3. Add 1D scale tensor support in cast_back_from_fp8() and cast_back_from_fp4().
  4. Add assertion t.dim() > 0 in localize().
  5. Add unit test suite in tests/test_math_utils.py verifying all edge cases.

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/deepseek-ai/DeepGEMM/pull/483 **Author:** [@reginaldalfret](https://github.com/reginaldalfret) **Created:** 10/25/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `fix/math-utils-contiguous-and-shape-safety` --- ### 📝 Commits (1) - [`b696727`](https://github.com/deepseek-ai/DeepGEMM/commit/b696727e02e18552f2a6d0a53a1e5bee25778855) fix(utils): enhance tensor view contiguity, squeeze safety, and scale indexing ### 📊 Changes **3 files changed** (+113 additions, -6 deletions) <details> <summary>View changed files</summary> 📝 `deep_gemm/locality_domain.py` (+1 -0) 📝 `deep_gemm/utils/math.py` (+10 -6) ➕ `tests/test_math_utils.py` (+102 -0) </details> ### 📄 Description ### Problem 1. **View Contiguity in UE8M0 Packing**: In `deep_gemm/utils/math.py`, `pack_ue8m0_to_int()` and `unpack_ue8m0_from_int()` called `.view(torch.int)` directly on `x_int` and `(x_int >> 23).to(torch.uint8)`. In PyTorch, if input tensors or intermediate slices are non-contiguous, `.view()` raises `RuntimeError: view size is not compatible with input tensor's size and stride`. 2. **Indiscriminate Squeeze in Custom Dims Cast**: In `per_custom_dims_cast_to_fp8()`, `sf.squeeze()` squeezed all dimensions of size 1. If any dimension specified in `dims` had size 1 (e.g. batch dimension of 1 or head count 1), `sf.squeeze()` inadvertently collapsed it to a scalar or lower-rank tensor. 3. **1D Scale Tensor Support**: `cast_back_from_fp8()` and `cast_back_from_fp4()` indexed `sf[:, group_idx]`, raising `IndexError` when `sf` is a 1D tensor. 4. **Zero-Division in Locality Domain**: In `deep_gemm/locality_domain.py`, `localize()` executed `dim % t.dim()` without checking `t.dim() > 0`, raising `ZeroDivisionError` on 0-D scalar inputs. ### Solution 1. Ensure tensors are `.contiguous()` before `.view(torch.int)` in `pack_ue8m0_to_int()` and `unpack_ue8m0_from_int()`. 2. Squeeze only `excluded_dims` in `per_custom_dims_cast_to_fp8()` (`sf.squeeze(dim=excluded_dims)`), preserving requested dimensions of size 1. 3. Add 1D scale tensor support in `cast_back_from_fp8()` and `cast_back_from_fp4()`. 4. Add assertion `t.dim() > 0` in `localize()`. 5. Add unit test suite in `tests/test_math_utils.py` verifying all edge cases. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
Sign in to join this conversation.
No labels
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
deepseek-ai/DeepGEMM#483
No description provided.