GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Fix dtype-independent analytical alias checks · cslht11/diffct@1d69187

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • diffct
    • streaming.py
  • tests
    • test_analytical_input_alias_bfloat16.py
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎diffct/streaming.py‎

Copy file name to clipboardExpand all lines: diffct/streaming.py
+3-5Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,9 @@ def _tensor_span(tensor):
7474

7575

7676
def _tensor_metadata(tensor, base=None, offset=0):
77-
if tensor.device.type == "cpu":
78-
return tensor.detach().numpy()
79-
# These read-only virtual views are only passed to shares_memory, which
80-
# examines addresses/strides without dereferencing data. One byte models
81-
# CUDA address differences; no tensor data is copied or volume allocated.
77+
# These read-only byte views are only passed to shares_memory, which
78+
# examines addresses/strides without dereferencing data. They model each
79+
# element's bytes regardless of dtype or device; no tensor data is copied.
8280
if base is None:
8381
base = np.empty(1, dtype=np.uint8)
8482
head = np.lib.stride_tricks.as_strided(
Collapse file

‎tests/test_analytical_input_alias_bfloat16.py‎

Copy file name to clipboard
+121Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
"""Analytical output admission must compare logical bytes across tensor dtypes."""
2+
3+
import itertools
4+
import math
5+
6+
import pytest
7+
import torch
8+
9+
from .test_analytical_input_alias import (
10+
ALIAS_ERROR,
11+
BEAMS,
12+
WEIGHTS,
13+
_ObservedStore,
14+
_case,
15+
_reconstruct,
16+
_weight_shape,
17+
)
18+
19+
import diffct
20+
21+
22+
def _logical_bytes(tensor):
23+
"""Enumerate byte addresses for these small fixtures without a NumPy cast."""
24+
size = tensor.element_size()
25+
start = tensor.data_ptr()
26+
return {
27+
start + sum(index * stride for index, stride in zip(indices, tensor.stride())) * size + byte
28+
for indices in itertools.product(*(range(length) for length in tensor.shape))
29+
for byte in range(size)
30+
}
31+
32+
33+
def _shared_views(shape, weight_shape, halfword_offset):
34+
count = max(math.prod(shape), math.prod(weight_shape))
35+
backing = torch.full((3 * count,), -19., dtype=torch.float32)
36+
output = backing[::3][:math.prod(shape)].reshape(shape)
37+
weight = backing.view(torch.bfloat16)[halfword_offset::6][:math.prod(weight_shape)]
38+
return backing, output, weight.reshape(weight_shape)
39+
40+
41+
@pytest.mark.parametrize("beam", BEAMS)
42+
@pytest.mark.parametrize("name", WEIGHTS)
43+
@pytest.mark.parametrize("layout", ["independent", "strided-shared"])
44+
@pytest.mark.parametrize("delegated", [False, True])
45+
def test_into_accepts_bfloat16_weights_with_float32_output(beam, name, layout, delegated):
46+
data, trajectory, shape, angles, options = _case(beam)
47+
weight_shape = _weight_shape(name, data)
48+
if layout == "independent":
49+
output_tensor = torch.full(shape, -19., dtype=torch.float32)
50+
weight = torch.empty(weight_shape, dtype=torch.bfloat16)
51+
assert output_tensor.untyped_storage().data_ptr() != weight.untyped_storage().data_ptr()
52+
else:
53+
# Each 12-byte slot contains four output bytes and a separate two-byte weight.
54+
backing, output_tensor, weight = _shared_views(shape, weight_shape, 2)
55+
assert output_tensor.untyped_storage().data_ptr() == weight.untyped_storage().data_ptr()
56+
assert not output_tensor.is_contiguous() and not weight.is_contiguous()
57+
assert backing.dtype == torch.float32
58+
weight.copy_(torch.linspace(.625, 1.25, weight.numel()).reshape(weight_shape))
59+
output_bytes, weight_bytes = _logical_bytes(output_tensor), _logical_bytes(weight)
60+
assert output_bytes.isdisjoint(weight_bytes)
61+
if layout == "strided-shared":
62+
assert max(min(output_bytes), min(weight_bytes)) <= min(max(output_bytes), max(weight_bytes))
63+
assert weight.dtype == torch.bfloat16 and weight.device.type == "cpu"
64+
assert options["precision"] == "float32" and options["backend"] == "torch"
65+
saved_weight, saved_data = weight.clone(), data.clone()
66+
expected = _reconstruct(
67+
beam, data, trajectory, shape, angles, {**options, name: weight.clone()},
68+
)
69+
assert expected.dtype == torch.float32
70+
assert torch.isfinite(expected).all() and expected.abs().max() > 0
71+
source = _ObservedStore(diffct.TensorStore(data))
72+
output = (
73+
_ObservedStore(_ObservedStore(diffct.TensorStore(output_tensor)))
74+
if delegated else output_tensor
75+
)
76+
77+
result = _reconstruct(
78+
beam, source, trajectory, shape, angles, {**options, name: weight}, output=output,
79+
)
80+
81+
assert result is output and source.reads
82+
assert source.writes == [] and source.flushes == 0
83+
if delegated:
84+
for store in (output, output.store):
85+
assert store.writes and store.flushes == 1
86+
assert output_tensor.dtype == torch.float32
87+
torch.testing.assert_close(output_tensor, expected, rtol=3e-6, atol=3e-7)
88+
torch.testing.assert_close(weight, saved_weight, rtol=0, atol=0)
89+
torch.testing.assert_close(data, saved_data, rtol=0, atol=0)
90+
91+
92+
@pytest.mark.parametrize("beam", BEAMS)
93+
@pytest.mark.parametrize("name", WEIGHTS)
94+
@pytest.mark.parametrize("halfword_offset", [0, 1], ids=["same-start", "upper-half"])
95+
def test_into_rejects_true_bfloat16_byte_overlap_before_numerical_io(
96+
beam, name, halfword_offset,
97+
):
98+
data, trajectory, shape, angles, options = _case(beam)
99+
backing, output_tensor, weight = _shared_views(shape, _weight_shape(name, data), halfword_offset)
100+
weight.copy_(torch.linspace(.625, 1.25, weight.numel()).reshape(weight.shape))
101+
assert output_tensor.dtype == torch.float32 and weight.dtype == torch.bfloat16
102+
assert not output_tensor.is_contiguous() and not weight.is_contiguous()
103+
assert output_tensor.untyped_storage().data_ptr() == weight.untyped_storage().data_ptr()
104+
assert _logical_bytes(output_tensor) & _logical_bytes(weight)
105+
if halfword_offset == 1:
106+
assert weight.data_ptr() == output_tensor.data_ptr() + weight.element_size()
107+
saved_bytes = backing.view(torch.uint8).clone()
108+
saved_weight, saved_data = weight.clone(), data.clone()
109+
source = _ObservedStore(diffct.TensorStore(data))
110+
output = _ObservedStore(_ObservedStore(diffct.TensorStore(output_tensor)))
111+
112+
with pytest.raises(ValueError, match=ALIAS_ERROR):
113+
_reconstruct(
114+
beam, source, trajectory, shape, angles, {**options, name: weight}, output=output,
115+
)
116+
117+
for store in (source, output, output.store):
118+
assert store.reads == [] and store.writes == [] and store.flushes == 0
119+
torch.testing.assert_close(backing.view(torch.uint8), saved_bytes, rtol=0, atol=0)
120+
torch.testing.assert_close(weight, saved_weight, rtol=0, atol=0)
121+
torch.testing.assert_close(data, saved_data, rtol=0, atol=0)

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.