Skip to content

Commit b4da026

Browse files
committed
Keep string length bounds out of compiled metadata
1 parent a0c5143 commit b4da026

3 files changed

Lines changed: 12 additions & 6 deletions

File tree

‎sdm/tensor/string.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,9 @@ class StringTensor(VarLenTensor):
3535
stride: The stride of the tensor.
3636
storage_offset: The offset into the logical ``offset`` storage.
3737
max_length: Upper bound on the byte length of every stored string,
38-
or :obj:`None` if unknown. Lets compiled operations size buffers
38+
or :obj:`None` if unknown. Lets eager operations size buffers
3939
without reading lengths from the device. Must not underestimate.
40+
Compiled tensor operations see :obj:`None` and never recompile.
4041
"""
4142

4243
# NOTE Assume that `data` stores valid UTF-8 bytes and do not validate it.

‎sdm/tensor/var_len.py‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,9 @@ class VarLenTensor(Tensor):
6363
stride: The stride of the tensor.
6464
storage_offset: The offset into the logical ``offset`` storage.
6565
max_length: Upper bound on the length of every stored entry, or
66-
:obj:`None` if unknown. Lets compiled operations size buffers
66+
:obj:`None` if unknown. Lets eager operations size buffers
6767
without reading lengths from the device. Must not underestimate.
68+
Compiled tensor operations see :obj:`None` and never recompile.
6869
"""
6970

7071
ALLOWED_DTYPES: ClassVar[tuple[torch.dtype, ...] | None] = None
@@ -576,7 +577,9 @@ def __tensor_flatten__(self) -> tuple[list[str], tuple[Any, ...]]:
576577
attrs = ["_offset", "_data", "_layout"]
577578
if self._valid is not None:
578579
attrs.append("_valid")
579-
return attrs, (self.__class__, self.storage_offset(), self._max_length)
580+
# The length bound stays eager: compiled operations see ``None``.
581+
# As context, each new query bound would compile another graph.
582+
return attrs, (self.__class__, self.storage_offset())
580583

581584
@staticmethod
582585
def __tensor_unflatten__(
@@ -585,7 +588,7 @@ def __tensor_unflatten__(
585588
outer_size: tuple[int, ...],
586589
outer_stride: tuple[int, ...],
587590
) -> VarLenTensor:
588-
cls, storage_offset, max_length = ctx
591+
cls, storage_offset = ctx
589592
layout = inner_tensors["_layout"]
590593
out = cls(
591594
data=inner_tensors["_data"],
@@ -594,7 +597,6 @@ def __tensor_unflatten__(
594597
size=layout.size()[:-1],
595598
stride=layout.stride()[:-1],
596599
storage_offset=storage_offset,
597-
max_length=max_length,
598600
)
599601
out._layout = layout
600602
return out

‎test/tensor/test_string.py‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -450,12 +450,15 @@ def test_bound_provenance(
450450
(values.clone(), texts),
451451
(values.detach(), texts),
452452
(values.to(device, copy=True), texts),
453-
(compiled(values), texts),
454453
(pickle.loads(pickle.dumps(values)), texts),
455454
):
456455
copied = cast(StringTensor, copied)
457456
assert copied._max_length == bound
458457
assert copied.tolist() == expected
458+
# Compiled outputs carry no bound, so new bounds do not recompile.
459+
compiled_values = cast(StringTensor, compiled(values))
460+
assert compiled_values._max_length is None
461+
assert compiled_values.tolist() == texts
459462
for suffix in ("", "x" * 16):
460463
extra = StringTensor.from_list([suffix], device=device)
461464
joined = cast(StringTensor, torch.cat((values, extra)))

0 commit comments

Comments
 (0)