forked from modular/modular
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaddition.py
More file actions
executable file
·43 lines (33 loc) · 1.52 KB
/
addition.py
File metadata and controls
executable file
·43 lines (33 loc) · 1.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# ===----------------------------------------------------------------------=== #
# Copyright (c) 2026, Modular Inc. All rights reserved.
#
# Licensed under the Apache License v2.0 with LLVM Exceptions:
# https://llvm.org/LICENSE.txt
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ===----------------------------------------------------------------------=== #
from pathlib import Path
import torch
from max.torch import CustomOpLibrary
# Load the Mojo custom operations from the `operations` directory.
mojo_kernels = Path(__file__).parent / "operations"
op_library = CustomOpLibrary(mojo_kernels)
# Register a custom operation that adds a constant value of 10 to a tensor.
# The `value` parameter is a compile-time constant that we specify when
# registering this operation
add_const_op = op_library.add_constant_custom[{"value": 10}]
def add_const(x: torch.Tensor) -> torch.Tensor:
"""A wrapper PyTorch function that calls the Mojo custom operation."""
result = torch.zeros_like(x)
add_const_op(result, x)
return result
if __name__ == "__main__":
# Initialize a tensor of ones.
device = "cuda" if torch.cuda.is_available() else "cpu"
x = torch.ones(10, device=device)
# Call the custom operation, and print the result.
print(add_const(x))