-
Notifications
You must be signed in to change notification settings - Fork 217
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ef3568b
commit 9103233
Showing
7 changed files
with
257 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
docs/source/python_tutorials/ragged/code/basics/dtype-device.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
#!/usr/bin/env python3 | ||
|
||
import k2 | ||
import torch | ||
|
||
a = k2.RaggedTensor([[1, 2], [1]]) | ||
b = k2.RaggedTensor([[1, 2], [1]], dtype=torch.int32) | ||
c = k2.RaggedTensor([[1, 2], [1.5]]) | ||
d = k2.RaggedTensor([[1, 2], [1.5]], dtype=torch.float32) | ||
e = k2.RaggedTensor([[1, 2], [1.5]], dtype=torch.float64) | ||
f = k2.RaggedTensor([[1, 2], [1]], dtype=torch.float32, device=torch.device("cuda", 0)) | ||
g = k2.RaggedTensor([[1, 2], [1]], device="cuda:0", dtype=torch.float64) | ||
print(f"a:\n{a}") | ||
print(f"b:\n{b}") | ||
print(f"c:\n{c}") | ||
print(f"d:\n{d}") | ||
print(f"e:\n{e}") | ||
print(f"f:\n{f}") | ||
print(f"g:\n{g}") | ||
print(f"g.to_str_simple():\n{g.to_str_simple()}") | ||
print(f"a.dtype: {a.dtype}, g.device: {g.device}") | ||
print(f"a.to(g.device).device: {a.to(g.device).device}") | ||
print(f"a.to(g.dtype).dtype: {a.to(g.dtype).dtype}") | ||
""" | ||
a: | ||
RaggedTensor([[1, 2], | ||
[1]], dtype=torch.int32) | ||
b: | ||
RaggedTensor([[1, 2], | ||
[1]], dtype=torch.int32) | ||
c: | ||
RaggedTensor([[1, 2], | ||
[1.5]], dtype=torch.float32) | ||
d: | ||
RaggedTensor([[1, 2], | ||
[1.5]], dtype=torch.float32) | ||
e: | ||
RaggedTensor([[1, 2], | ||
[1.5]], dtype=torch.float64) | ||
f: | ||
RaggedTensor([[1, 2], | ||
[1]], device='cuda:0', dtype=torch.float32) | ||
g: | ||
RaggedTensor([[1, 2], | ||
[1]], device='cuda:0', dtype=torch.float64) | ||
g.to_str_simple(): | ||
RaggedTensor([[1, 2], [1]], device='cuda:0', dtype=torch.float64) | ||
a.dtype: torch.int32, g.device: cuda:0 | ||
a.to(g.device).device: cuda:0 | ||
a.to(g.dtype).dtype: torch.float64 | ||
""" |
53 changes: 53 additions & 0 deletions
53
docs/source/python_tutorials/ragged/code/basics/images/simple-fsa.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 16 additions & 0 deletions
16
docs/source/python_tutorials/ragged/code/basics/ragged_shape_1.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
#!/usr/bin/env python3 | ||
|
||
import k2 | ||
import torch | ||
|
||
shape = k2.ragged.create_ragged_shape2( | ||
row_splits=torch.tensor([0, 2, 3, 3], dtype=torch.int32), | ||
) | ||
print(type(shape)) | ||
print(shape) | ||
""" | ||
<class '_k2.ragged.RaggedShape'> | ||
[ [ x x ] [ x ] [ ] ] | ||
""" | ||
print("num_states:", shape.dim0) | ||
print("num_arcs:", shape.numel()) |
23 changes: 23 additions & 0 deletions
23
docs/source/python_tutorials/ragged/code/basics/single-fsa.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#!/usr/bin/env python3 | ||
import k2 | ||
|
||
s = """ | ||
0 1 1 0.1 | ||
0 1 2 0.2 | ||
1 2 -1 0.3 | ||
2 | ||
""" | ||
fsa = k2.Fsa.from_str(s) | ||
print(fsa.arcs) | ||
""" | ||
[ [ 0 1 1 0.1 0 1 2 0.2 ] [ 1 2 -1 0.3 ] [ ] ] | ||
""" | ||
|
||
sym_str = """ | ||
a 1 | ||
b 2 | ||
""" | ||
|
||
# fsa.labels_sym = k2.SymbolTable.from_str(sym_str) | ||
# fsa.draw("images/simple-fsa.svg") | ||
# print(k2.to_dot(fsa)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters