Skip to content

Commit bc7281a

Browse files
Greatly limits how the dtype and device can be used in Tensor
Previously, setting `dtype` or `device` in the Tensor constructor would incur a cast or copy operation respectively if the input data were not already of the correct data type or on the right device. These would be immediately evaluated, which could lead to extremely long tracing times. Additionally, the implicit behavior is not in line with the rest of Tripy's API design. With this change, users must explicitly call `cast` or `copy`. This makes it clear *when* such an operation is actually happening.
1 parent 64aed72 commit bc7281a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+155
-215
lines changed

tripy/docs/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ def relu(input: "nvtripy.Tensor") -> "nvtripy.Tensor":
7373
.. code-block:: python
7474
:linenos:
7575
76-
input = tp.Tensor([1., 2., 3., 4.], dtype=tp.float32)
76+
input = tp.Tensor([1., 2., 3., 4.])
7777
output = tp.relu(input)
7878
7979
t = torch.tensor([1, 2, 3, 4], dtype=torch.float32) # doc: omit

tripy/docs/pre0_user_guides/01-compiler.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ fast_geglu = tp.compile(layer, args=[inp_info])
7474
out0 = fast_geglu(inp)
7575

7676
# Now use an input with a different shape: (2, 2):
77-
inp1 = tp.Tensor([[1., 2.], [2., 3.]], dtype=tp.float32).eval()
77+
inp1 = tp.Tensor([[1., 2.], [2., 3.]]).eval()
7878
out1 = fast_geglu(inp1)
7979
```
8080

tripy/examples/nanogpt/example.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ def main():
132132
# Append sampled index to the running sequence and continue
133133
idx = torch.from_dlpack(idx).to(torch.int32)
134134
idx = torch.concat([idx, idx_next], dim=1).to(torch.int32)
135-
idx = tp.Tensor(idx, device=tp.device("gpu"))
135+
idx = tp.Tensor(idx)
136136

137137
response = encoder.decode(torch.from_dlpack(idx[0, :]).tolist())
138138
end_time = time.perf_counter()

tripy/examples/segment-anything-model-v2/sam2/modeling/position_encoding.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,7 @@ def __init__(self, num_pos_feats: int = 64, scale: Optional[float] = None) -> No
113113
if scale is None or scale <= 0.0:
114114
scale = 1.0
115115
self.positional_encoding_gaussian_matrix = tp.Tensor(
116-
scale * np.random.randn(2, num_pos_feats).astype(np.float32),
117-
dtype=tp.float32,
116+
scale * np.random.randn(2, num_pos_feats).astype(np.float32)
118117
)
119118

120119
def _pe_encoding(self, coords: tp.Tensor) -> tp.Tensor:

tripy/examples/segment-anything-model-v2/sam2/modeling/sam/mask_decoder.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ def _get_stability_scores(self, mask_logits):
332332
stability_delta = self.dynamic_multimask_stability_delta
333333
area_i = tp.cast(tp.sum(tp.cast(mask_logits > stability_delta, tp.float32), dim=-1), self.dtype)
334334
area_u = tp.cast(tp.sum(tp.cast(mask_logits > -stability_delta, tp.float32), dim=-1), self.dtype)
335-
stability_scores = tp.where(area_u > 0, area_i / area_u, tp.Tensor(1.0, dtype=self.dtype))
335+
stability_scores = tp.where(area_u > 0, area_i / area_u, 1.0)
336336
return stability_scores
337337

338338
def _dynamic_multimask_via_stability(self, all_mask_logits, all_iou_scores):

tripy/notebooks/resnet50.ipynb

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -460,11 +460,7 @@
460460
"\n",
461461
" # Preprocess the image for inference\n",
462462
" processed_image = processor(image, return_tensors=\"np\")[\"pixel_values\"]\n",
463-
" tp_image = tp.Tensor(\n",
464-
" processed_image,\n",
465-
" dtype=tp.float32,\n",
466-
" device=tp.device(\"gpu\"),\n",
467-
" )\n",
463+
" tp_image = tp.copy(tp.Tensor(processed_image), tp.device(\"gpu\"))\n",
468464
"\n",
469465
" # Run inference with compiled model and get predicted label\n",
470466
" label_idx = compiled_resnet_classifier(tp_image)\n",

tripy/nvtripy/frontend/dimension_size.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def __init__(self, data: int, name: Optional[str] = None) -> None:
3434
data: The value of the DimensionSize, which should be a scalar integer.
3535
name: An optional name.
3636
"""
37-
super().__init__(data=data, dtype=int32, name=name)
37+
super().__init__(data=data, name=name)
3838

3939
def __int__(self) -> int:
4040
return self.tolist()

tripy/nvtripy/frontend/module/embedding.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ def __init__(self, num_embeddings: int, embedding_dim: int, dtype: datatype.dtyp
5353
5454
embedding.weight = tp.iota(embedding.weight.shape)
5555
56-
input = tp.Tensor([0, 2], dtype=tp.int32)
56+
input = tp.Tensor([0, 2])
5757
output = embedding(input)
5858
5959
assert np.array_equal(cp.from_dlpack(output).get(), cp.from_dlpack(embedding.weight).get()[[0,2], :])

tripy/nvtripy/frontend/module/module.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,14 +118,14 @@ class Module:
118118
class AddBias(tp.Module):
119119
def __init__(self):
120120
super().__init__()
121-
self.bias = tp.Tensor([1.0, 1.0], dtype=tp.float32)
121+
self.bias = tp.Tensor([1.0, 1.0])
122122
123123
def forward(self, x):
124124
return x + self.bias
125125
126126
add_bias = AddBias()
127127
128-
input = tp.Tensor([1.0, 1.0], dtype=tp.float32)
128+
input = tp.Tensor([1.0, 1.0])
129129
output = add_bias(input)
130130
131131
assert np.array_equal(cp.from_dlpack(output).get(), np.array([2.0, 2.0]))

tripy/nvtripy/frontend/ops/cast.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def cast(input: "nvtripy.Tensor", dtype: "nvtripy.dtype") -> "nvtripy.Tensor":
6161
.. code-block:: python
6262
:linenos:
6363
64-
input = tp.Tensor([1, 2], dtype=tp.int32)
64+
input = tp.Tensor([1, 2])
6565
output = tp.cast(input, tp.float32)
6666
6767
assert np.array_equal(cp.from_dlpack(output).get(), np.array([1, 2], dtype=np.float32))

0 commit comments

Comments
 (0)