-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add static inference support for rawc and ggml backends
- Loading branch information
1 parent
048686e
commit 9b2f289
Showing
8 changed files
with
508 additions
and
12 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
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
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,53 @@ | ||
import ctypes | ||
import os | ||
from ....cores.c.array import PyArray | ||
from ....cores.c.raw_c.array import ( | ||
Array, | ||
zeros, | ||
lib | ||
) | ||
from ....backends.with_manualgrad.c_backend.backend import array | ||
from ....backends.with_manualgrad.c_backend.utils import from_numpy | ||
from ....cores.c.ggml.ggml_core import ggml_struct | ||
import numpy as np | ||
|
||
__all__ = [ | ||
"add", | ||
"multiplication" | ||
] | ||
|
||
def convert_to_c_array( | ||
input: PyArray | ||
) -> PyArray: | ||
input_np = np.array(input.data, dtype=input.dtype) | ||
return from_numpy(input_np) | ||
|
||
def add( | ||
left: PyArray, | ||
right: PyArray | ||
) -> PyArray: | ||
# In C backend, output is given as first input | ||
output = zeros(left.shape) | ||
left_c = convert_to_c_array(left) | ||
right_c = convert_to_c_array(right) | ||
lib.add(ctypes.byref(output.arr), ctypes.byref(left_c.arr), ctypes.byref(right_c.arr)) | ||
_shape = output.shape | ||
data_ptr = ctypes.cast(output.arr.data, ctypes.c_void_p) | ||
return PyArray(ggml_struct(data=data_ptr), _shape) | ||
|
||
def multiplication( | ||
left: PyArray, | ||
right: PyArray | ||
) -> PyArray: | ||
# In C backend, output is given as first input | ||
output = zeros(left.shape) | ||
left_c = convert_to_c_array(left) | ||
right_c = convert_to_c_array(right) | ||
lib.multiplication(ctypes.byref(output.arr), ctypes.byref(left_c.arr), ctypes.byref(right_c.arr)) | ||
_shape = output.shape | ||
data_ptr = ctypes.cast(output.arr.data, ctypes.c_void_p) | ||
return PyArray(ggml_struct(data=data_ptr), _shape) | ||
|
||
|
||
|
||
primitive_func_dict = {key: fn for key, fn in globals().items() if callable(fn)} |
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,32 @@ | ||
import ctypes | ||
import os | ||
from ....cores.c.array import PyArray | ||
from ....cores.c.raw_c.array import ( | ||
Array, | ||
zeros, | ||
lib | ||
) | ||
__all__ = [ | ||
"add", | ||
"multiplication" | ||
] | ||
|
||
def add( | ||
left: PyArray, | ||
right: PyArray | ||
) -> PyArray: | ||
# In C backend, output is given as first input | ||
output = zeros(left.shape) | ||
lib.add(ctypes.byref(output.arr), ctypes.byref(left.arr), ctypes.byref(right.arr)) | ||
return output | ||
|
||
def multiplication( | ||
left: PyArray, | ||
right: PyArray | ||
) -> PyArray: | ||
# In C backend, output is given as first input | ||
output = zeros(left.shape) | ||
lib.multiplication(ctypes.byref(output.arr), ctypes.byref(left.arr), ctypes.byref(right.arr)) | ||
return output | ||
|
||
primitive_func_dict = {key: fn for key, fn in globals().items() if callable(fn)} |
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
Oops, something went wrong.