liangsu9988 commited on
Commit
dc7e967
·
verified ·
1 Parent(s): 0e2012f

Uploaded using `kernel-builder`.

Browse files
benchmarks/benchmark.py ADDED
@@ -0,0 +1,90 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ """Higgs delayed-codebook benchmark against eager/compile and raw op."""
3
+
4
+ from __future__ import annotations
5
+
6
+ import argparse
7
+ import importlib
8
+ import sys
9
+ from pathlib import Path
10
+
11
+ import torch
12
+
13
+
14
+ def elapsed_us(fn, warmup, iterations):
15
+ for _ in range(warmup):
16
+ fn()
17
+ torch.cuda.synchronize()
18
+ start = torch.cuda.Event(enable_timing=True)
19
+ end = torch.cuda.Event(enable_timing=True)
20
+ start.record()
21
+ for _ in range(iterations):
22
+ fn()
23
+ end.record()
24
+ end.synchronize()
25
+ return start.elapsed_time(end) * 1000.0 / iterations
26
+
27
+
28
+ def main():
29
+ parser = argparse.ArgumentParser()
30
+ parser.add_argument("--backend", choices=("source", "installed"), default="source")
31
+ parser.add_argument("--artifact")
32
+ parser.add_argument("--warmup", type=int, default=50)
33
+ parser.add_argument("--iterations", type=int, default=500)
34
+ args = parser.parse_args()
35
+ if args.backend == "source":
36
+ tests = Path(__file__).resolve().parents[1] / "tests"
37
+ sys.path.insert(0, str(tests))
38
+ from test_audio_codebook_primitives import load_source_ops
39
+ ops = load_source_ops()
40
+ else:
41
+ if args.artifact:
42
+ sys.path.insert(0, args.artifact)
43
+ ops = importlib.import_module("audio_codebook_primitives")
44
+
45
+ c, v, h, delay, boc = 8, 1026, 1024, 7, 1024
46
+ logits = torch.randn((c, v), device="cuda", dtype=torch.bfloat16)
47
+ codebook = torch.randn((c, v, h), device="cuda", dtype=torch.bfloat16)
48
+ index = torch.arange(c, device="cuda")
49
+ active = index <= delay
50
+ boc_tensor = torch.full((c,), boc, device="cuda", dtype=torch.int64)
51
+
52
+ def eager():
53
+ codes = torch.where(active, logits.argmax(dim=1), boc_tensor)
54
+ embedding = codebook[index, codes].float().sum(dim=0).bfloat16()
55
+ return codes, embedding
56
+
57
+ compiled = torch.compile(eager, fullgraph=True)
58
+ codes = torch.empty(c, device="cuda", dtype=torch.int64)
59
+ embedding = torch.empty(h, device="cuda", dtype=torch.bfloat16)
60
+
61
+ def wrapper():
62
+ return ops.delayed_codebook_argmax_embed_bf16(
63
+ logits, codebook, delay=delay, boc=boc,
64
+ codes=codes, embedding=embedding
65
+ )
66
+
67
+ namespace = ops.ops
68
+
69
+ def raw():
70
+ namespace.delayed_codebook_argmax_embed_bf16(
71
+ logits, codebook, delay, boc, codes, embedding
72
+ )
73
+
74
+ expected = eager()
75
+ actual = wrapper()
76
+ torch.testing.assert_close(actual[0], expected[0], rtol=0, atol=0)
77
+ torch.testing.assert_close(actual[1], expected[1], rtol=0, atol=0)
78
+
79
+ rows = {
80
+ "torch_eager_us": elapsed_us(eager, args.warmup, args.iterations),
81
+ "torch_compile_us": elapsed_us(compiled, args.warmup, args.iterations),
82
+ "hub_wrapper_us": elapsed_us(wrapper, args.warmup, args.iterations),
83
+ "raw_native_op_us": elapsed_us(raw, args.warmup, args.iterations),
84
+ }
85
+ for name, value in rows.items():
86
+ print(f"{name}={value:.3f}")
87
+
88
+
89
+ if __name__ == "__main__":
90
+ main()
build/torch211-cxx11-cu130-x86_64-linux/__init__.py ADDED
@@ -0,0 +1,101 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """FlashRT delayed-codebook selection and embedding kernels."""
2
+
3
+ from __future__ import annotations
4
+
5
+ from typing import Optional
6
+
7
+ import torch
8
+
9
+ from ._ops import add_op_namespace_prefix, ops
10
+
11
+
12
+ def _check(logits, codebook, delay: int, boc: int, codes, embedding) -> None:
13
+ if logits.dim() != 2:
14
+ raise RuntimeError("logits must have shape (num_codebooks, codebook_vocab)")
15
+ if codebook.dim() != 3 or codebook.shape[:2] != logits.shape:
16
+ raise RuntimeError("codebook must have shape (num_codebooks, codebook_vocab, hidden)")
17
+ if codes.shape != (logits.shape[0],):
18
+ raise RuntimeError("codes must have shape (num_codebooks,)")
19
+ if embedding.shape != (codebook.shape[2],):
20
+ raise RuntimeError("embedding must have shape (hidden,)")
21
+ if not 0 <= delay <= logits.shape[0]:
22
+ raise RuntimeError("delay must be in [0, num_codebooks]")
23
+ if not 0 <= boc < logits.shape[1]:
24
+ raise RuntimeError("boc must be a valid codebook index")
25
+
26
+
27
+ @torch.library.register_fake(
28
+ add_op_namespace_prefix("delayed_codebook_argmax_embed_bf16")
29
+ )
30
+ def _argmax_fake(logits, codebook, delay: int, boc: int, codes, embedding) -> None:
31
+ _check(logits, codebook, delay, boc, codes, embedding)
32
+ return None
33
+
34
+
35
+ @torch.library.register_fake(
36
+ add_op_namespace_prefix("delayed_codebook_sample_embed_bf16")
37
+ )
38
+ def _sample_fake(
39
+ logits, codebook, delay: int, boc: int, temperature: float,
40
+ seed: int, step: int, codes, embedding
41
+ ) -> None:
42
+ _check(logits, codebook, delay, boc, codes, embedding)
43
+ if temperature <= 0:
44
+ raise RuntimeError("temperature must be strictly positive")
45
+ if step < 0:
46
+ raise RuntimeError("step must be non-negative")
47
+ return None
48
+
49
+
50
+ def _outputs(logits, codebook, codes, embedding):
51
+ if codes is None:
52
+ codes = torch.empty(
53
+ (logits.shape[0],), device=logits.device, dtype=torch.int64
54
+ )
55
+ if embedding is None:
56
+ embedding = torch.empty(
57
+ (codebook.shape[2],), device=codebook.device, dtype=torch.bfloat16
58
+ )
59
+ return codes, embedding
60
+
61
+
62
+ def delayed_codebook_argmax_embed_bf16(
63
+ logits: torch.Tensor,
64
+ codebook: torch.Tensor,
65
+ *,
66
+ delay: int,
67
+ boc: int,
68
+ codes: Optional[torch.Tensor] = None,
69
+ embedding: Optional[torch.Tensor] = None,
70
+ ) -> tuple[torch.Tensor, torch.Tensor]:
71
+ codes, embedding = _outputs(logits, codebook, codes, embedding)
72
+ ops.delayed_codebook_argmax_embed_bf16(
73
+ logits, codebook, int(delay), int(boc), codes, embedding
74
+ )
75
+ return codes, embedding
76
+
77
+
78
+ def delayed_codebook_sample_embed_bf16(
79
+ logits: torch.Tensor,
80
+ codebook: torch.Tensor,
81
+ *,
82
+ delay: int,
83
+ boc: int,
84
+ temperature: float,
85
+ seed: int,
86
+ step: int,
87
+ codes: Optional[torch.Tensor] = None,
88
+ embedding: Optional[torch.Tensor] = None,
89
+ ) -> tuple[torch.Tensor, torch.Tensor]:
90
+ codes, embedding = _outputs(logits, codebook, codes, embedding)
91
+ ops.delayed_codebook_sample_embed_bf16(
92
+ logits, codebook, int(delay), int(boc), float(temperature),
93
+ int(seed), int(step), codes, embedding
94
+ )
95
+ return codes, embedding
96
+
97
+
98
+ __all__ = [
99
+ "delayed_codebook_argmax_embed_bf16",
100
+ "delayed_codebook_sample_embed_bf16",
101
+ ]
build/torch211-cxx11-cu130-x86_64-linux/_audio_codebook_primitives_cuda_bcb0782.abi3.so ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:6ad6d97d9bda4b81bf8e78f10167f7de84cfed2c25045dda31fe95503f57aae3
3
+ size 315320
build/torch211-cxx11-cu130-x86_64-linux/_ops.py ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ from . import _audio_codebook_primitives_cuda_bcb0782
3
+ ops = torch.ops._audio_codebook_primitives_cuda_bcb0782
4
+
5
+ def add_op_namespace_prefix(op_name: str):
6
+ """
7
+ Prefix op by namespace.
8
+ """
9
+ return f"_audio_codebook_primitives_cuda_bcb0782::{op_name}"
build/torch211-cxx11-cu130-x86_64-linux/audio_codebook_primitives/__init__.py ADDED
@@ -0,0 +1,26 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import ctypes
2
+ import importlib.util
3
+ import sys
4
+ from pathlib import Path
5
+ from types import ModuleType
6
+
7
+
8
+ def _import_from_path(file_path: Path) -> ModuleType:
9
+ # We cannot use the module name as-is, after adding it to `sys.modules`,
10
+ # it would also be used for other imports. So, we make a module name that
11
+ # depends on the path for it to be unique using the hex-encoded hash of
12
+ # the path.
13
+ path_hash = "{:x}".format(ctypes.c_size_t(hash(file_path.absolute())).value)
14
+ module_name = path_hash
15
+ spec = importlib.util.spec_from_file_location(module_name, file_path)
16
+ if spec is None:
17
+ raise ImportError(f"Cannot load spec for {module_name} from {file_path}")
18
+ module = importlib.util.module_from_spec(spec)
19
+ if module is None:
20
+ raise ImportError(f"Cannot load module {module_name} from spec")
21
+ sys.modules[module_name] = module
22
+ spec.loader.exec_module(module) # type: ignore
23
+ return module
24
+
25
+
26
+ globals().update(vars(_import_from_path(Path(__file__).parent.parent / "__init__.py")))
build/torch211-cxx11-cu130-x86_64-linux/metadata.json ADDED
@@ -0,0 +1,20 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "name": "audio-codebook-primitives",
3
+ "id": "_audio_codebook_primitives_cuda_bcb0782",
4
+ "version": 1,
5
+ "license": "Apache-2.0",
6
+ "python-depends": [],
7
+ "backend": {
8
+ "type": "cuda",
9
+ "archs": [
10
+ "10.0",
11
+ "11.0",
12
+ "12.0",
13
+ "12.1",
14
+ "8.0",
15
+ "8.6",
16
+ "8.9",
17
+ "9.0"
18
+ ]
19
+ }
20
+ }
build/torch212-cxx11-cu130-x86_64-linux/__init__.py ADDED
@@ -0,0 +1,101 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """FlashRT delayed-codebook selection and embedding kernels."""
2
+
3
+ from __future__ import annotations
4
+
5
+ from typing import Optional
6
+
7
+ import torch
8
+
9
+ from ._ops import add_op_namespace_prefix, ops
10
+
11
+
12
+ def _check(logits, codebook, delay: int, boc: int, codes, embedding) -> None:
13
+ if logits.dim() != 2:
14
+ raise RuntimeError("logits must have shape (num_codebooks, codebook_vocab)")
15
+ if codebook.dim() != 3 or codebook.shape[:2] != logits.shape:
16
+ raise RuntimeError("codebook must have shape (num_codebooks, codebook_vocab, hidden)")
17
+ if codes.shape != (logits.shape[0],):
18
+ raise RuntimeError("codes must have shape (num_codebooks,)")
19
+ if embedding.shape != (codebook.shape[2],):
20
+ raise RuntimeError("embedding must have shape (hidden,)")
21
+ if not 0 <= delay <= logits.shape[0]:
22
+ raise RuntimeError("delay must be in [0, num_codebooks]")
23
+ if not 0 <= boc < logits.shape[1]:
24
+ raise RuntimeError("boc must be a valid codebook index")
25
+
26
+
27
+ @torch.library.register_fake(
28
+ add_op_namespace_prefix("delayed_codebook_argmax_embed_bf16")
29
+ )
30
+ def _argmax_fake(logits, codebook, delay: int, boc: int, codes, embedding) -> None:
31
+ _check(logits, codebook, delay, boc, codes, embedding)
32
+ return None
33
+
34
+
35
+ @torch.library.register_fake(
36
+ add_op_namespace_prefix("delayed_codebook_sample_embed_bf16")
37
+ )
38
+ def _sample_fake(
39
+ logits, codebook, delay: int, boc: int, temperature: float,
40
+ seed: int, step: int, codes, embedding
41
+ ) -> None:
42
+ _check(logits, codebook, delay, boc, codes, embedding)
43
+ if temperature <= 0:
44
+ raise RuntimeError("temperature must be strictly positive")
45
+ if step < 0:
46
+ raise RuntimeError("step must be non-negative")
47
+ return None
48
+
49
+
50
+ def _outputs(logits, codebook, codes, embedding):
51
+ if codes is None:
52
+ codes = torch.empty(
53
+ (logits.shape[0],), device=logits.device, dtype=torch.int64
54
+ )
55
+ if embedding is None:
56
+ embedding = torch.empty(
57
+ (codebook.shape[2],), device=codebook.device, dtype=torch.bfloat16
58
+ )
59
+ return codes, embedding
60
+
61
+
62
+ def delayed_codebook_argmax_embed_bf16(
63
+ logits: torch.Tensor,
64
+ codebook: torch.Tensor,
65
+ *,
66
+ delay: int,
67
+ boc: int,
68
+ codes: Optional[torch.Tensor] = None,
69
+ embedding: Optional[torch.Tensor] = None,
70
+ ) -> tuple[torch.Tensor, torch.Tensor]:
71
+ codes, embedding = _outputs(logits, codebook, codes, embedding)
72
+ ops.delayed_codebook_argmax_embed_bf16(
73
+ logits, codebook, int(delay), int(boc), codes, embedding
74
+ )
75
+ return codes, embedding
76
+
77
+
78
+ def delayed_codebook_sample_embed_bf16(
79
+ logits: torch.Tensor,
80
+ codebook: torch.Tensor,
81
+ *,
82
+ delay: int,
83
+ boc: int,
84
+ temperature: float,
85
+ seed: int,
86
+ step: int,
87
+ codes: Optional[torch.Tensor] = None,
88
+ embedding: Optional[torch.Tensor] = None,
89
+ ) -> tuple[torch.Tensor, torch.Tensor]:
90
+ codes, embedding = _outputs(logits, codebook, codes, embedding)
91
+ ops.delayed_codebook_sample_embed_bf16(
92
+ logits, codebook, int(delay), int(boc), float(temperature),
93
+ int(seed), int(step), codes, embedding
94
+ )
95
+ return codes, embedding
96
+
97
+
98
+ __all__ = [
99
+ "delayed_codebook_argmax_embed_bf16",
100
+ "delayed_codebook_sample_embed_bf16",
101
+ ]
build/torch212-cxx11-cu130-x86_64-linux/_audio_codebook_primitives_cuda_bcb0782.abi3.so ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:5d2365e676c2962d167dccbf1d71fc1e0da4a243cafc36d866f12362d1af2020
3
+ size 321408
build/torch212-cxx11-cu130-x86_64-linux/_ops.py ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ from . import _audio_codebook_primitives_cuda_bcb0782
3
+ ops = torch.ops._audio_codebook_primitives_cuda_bcb0782
4
+
5
+ def add_op_namespace_prefix(op_name: str):
6
+ """
7
+ Prefix op by namespace.
8
+ """
9
+ return f"_audio_codebook_primitives_cuda_bcb0782::{op_name}"
build/torch212-cxx11-cu130-x86_64-linux/audio_codebook_primitives/__init__.py ADDED
@@ -0,0 +1,26 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import ctypes
2
+ import importlib.util
3
+ import sys
4
+ from pathlib import Path
5
+ from types import ModuleType
6
+
7
+
8
+ def _import_from_path(file_path: Path) -> ModuleType:
9
+ # We cannot use the module name as-is, after adding it to `sys.modules`,
10
+ # it would also be used for other imports. So, we make a module name that
11
+ # depends on the path for it to be unique using the hex-encoded hash of
12
+ # the path.
13
+ path_hash = "{:x}".format(ctypes.c_size_t(hash(file_path.absolute())).value)
14
+ module_name = path_hash
15
+ spec = importlib.util.spec_from_file_location(module_name, file_path)
16
+ if spec is None:
17
+ raise ImportError(f"Cannot load spec for {module_name} from {file_path}")
18
+ module = importlib.util.module_from_spec(spec)
19
+ if module is None:
20
+ raise ImportError(f"Cannot load module {module_name} from spec")
21
+ sys.modules[module_name] = module
22
+ spec.loader.exec_module(module) # type: ignore
23
+ return module
24
+
25
+
26
+ globals().update(vars(_import_from_path(Path(__file__).parent.parent / "__init__.py")))
build/torch212-cxx11-cu130-x86_64-linux/metadata.json ADDED
@@ -0,0 +1,20 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "name": "audio-codebook-primitives",
3
+ "id": "_audio_codebook_primitives_cuda_bcb0782",
4
+ "version": 1,
5
+ "license": "Apache-2.0",
6
+ "python-depends": [],
7
+ "backend": {
8
+ "type": "cuda",
9
+ "archs": [
10
+ "10.0",
11
+ "11.0",
12
+ "12.0",
13
+ "12.1",
14
+ "8.0",
15
+ "8.6",
16
+ "8.9",
17
+ "9.0"
18
+ ]
19
+ }
20
+ }