danieldk HF Staff commited on
Commit
540dcc9
·
verified ·
1 Parent(s): 534e6a6

Build uploaded using `kernels`.

Browse files
build/torch-universal/__init__.py ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+
3
+ from ._ops import ops
4
+ from .silu_and_mul import _silu_and_mul
5
+
6
+
7
+ def silu_and_mul(x: torch.Tensor) -> torch.Tensor:
8
+ return ops.silu_and_mul(x)
9
+
10
+
11
+ __all__ = ["silu_and_mul"]
build/torch-universal/_ops.py ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ ops = torch.ops._flattened_build_19700101000000
3
+
4
+ def add_op_namespace_prefix(op_name: str):
5
+ """
6
+ Prefix op by namespace.
7
+ """
8
+ return f"_flattened_build_19700101000000::{op_name}"
build/torch-universal/flattened_build/__init__.py ADDED
@@ -0,0 +1,26 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import ctypes
2
+ import sys
3
+
4
+ import importlib
5
+ from pathlib import Path
6
+ from types import ModuleType
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/torch-universal/flattened_build/__pycache__/__init__.cpython-313.pyc ADDED
Binary file (1.58 kB). View file
 
build/torch-universal/silu_and_mul.py ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import torch.nn.functional as F
3
+
4
+ from ._ops import add_op_namespace_prefix
5
+
6
+
7
+ @torch.library.custom_op(add_op_namespace_prefix("silu_and_mul"), mutates_args=())
8
+ def _silu_and_mul(x: torch.Tensor) -> torch.Tensor:
9
+ d = x.shape[-1] // 2
10
+ return F.silu(x[..., :d]) * x[..., d:]
11
+
12
+
13
+ def backward(ctx, grad_output):
14
+ x = ctx.saved_tensors[0]
15
+ d = x.shape[-1] // 2
16
+ x1, x2 = x[..., :d], x[..., d:]
17
+ sigmoid_x1 = torch.sigmoid(x1)
18
+ silu_x1 = F.silu(x1)
19
+ dsilu_dx1 = sigmoid_x1 + silu_x1 * (1 - sigmoid_x1)
20
+ dx1 = grad_output * x2 * dsilu_dx1
21
+ dx2 = grad_output * silu_x1
22
+ return torch.cat([dx1, dx2], dim=-1)
23
+
24
+
25
+ def setup_context(ctx, inputs, output):
26
+ (x,) = inputs
27
+ ctx.save_for_backward(x)
28
+
29
+
30
+ _silu_and_mul.register_autograd(backward, setup_context=setup_context)
31
+
32
+
33
+ @_silu_and_mul.register_fake
34
+ def _(x: torch.Tensor) -> torch.Tensor:
35
+ return x.new_empty(x.shape[0], x.shape[1] // 2)