add xlib.avecl

This commit is contained in:
iperov 2021-09-30 18:21:30 +04:00
commit 0058474da7
56 changed files with 5569 additions and 0 deletions

View file

@ -0,0 +1,89 @@
import numpy as np
from ..backend import Kernel
from ..HKernel import HKernel
from ..SCacheton import SCacheton
from ..Tensor import Tensor
from .Initializer import Initializer
class InitCoords2DArange(Initializer):
def __init__(self, h_start, h_stop, w_start, w_stop):
"""
Initialize (...,H,W,D) tensor with coords arange
D == 2(x,y) or 3 (x,y,1)
arguments
h_start float height start value (inclusive)
h_stop float height stop value (inclusive)
w_start float width start value (inclusive)
w_stop float width stop value (inclusive)
"""
super().__init__()
self._h_start = h_start
self._h_stop = h_stop
self._w_start = w_start
self._w_stop = w_stop
def initialize_tensor(self, tensor : Tensor):
shape = tensor.shape
dtype = tensor.dtype
if shape.ndim < 3:
raise ValueError(f'tensor.shape.ndim must be >= 3 (...,H,W,D)')
OH,OW,OD = shape[-3:]
if OD not in [2,3]:
raise ValueError(f'last dim D {OD} must == 2 or 3')
if OH > 1:
h_step = (self._h_stop-self._h_start) / (OH-1)
else:
h_step = 0
if OW > 1:
w_step = (self._w_stop-self._w_start) / (OW-1)
else:
w_step = 0
key = (InitCoords2DArange, dtype)
kernel = SCacheton.get_var(key)
if kernel is None:
kernel = Kernel(kernel_text=f"""
{HKernel.define_tensor_type('O', tensor.dtype)}
__kernel void impl(__global O_PTR_TYPE* O_PTR_NAME , float h_start, float h_step
, float w_start, float w_step,
uint O0, uint O1, uint O2)
{{
size_t gid = get_global_id(0);
{HKernel.decompose_idx_to_axes_idxs('gid', 'O', 3)}
O_TYPE v;
if (o2 == 0)
v = w_start+o1*w_step;
else
if (o2 == 1)
v = h_start+o0*h_step;
else
v = 1;
O_GLOBAL_STORE(gid, v);
}}
""")
SCacheton.set_var(key, kernel)
tensor.get_device().run_kernel( kernel, tensor.get_buffer(),
np.float32(self._h_start), np.float32(h_step),
np.float32(self._w_start), np.float32(w_step),
np.uint32(OH), np.uint32(OW), np.uint32(OD),
global_shape=(shape.size,) )
def __str__(self): return f'CoordsArange'

View file

@ -0,0 +1,62 @@
import numpy as np
from ..backend import Kernel
from ..HKernel import HKernel
from ..SCacheton import SCacheton
from ..Tensor import Tensor
from .Initializer import Initializer
class InitRandomUniform(Initializer):
def __init__(self, low=0, high=1):
"""
arguments
low(0) low value
high(1) high value (exclusive)
"""
super().__init__()
self._low = low
self._high = high
def initialize_tensor(self, tensor : Tensor):
key = (InitRandomUniform, self._low, self._high, tensor.dtype)
kernel = SCacheton.get_var(key)
if kernel is None:
hl = self._high-self._low
l = self._low
if tensor.dtype in [np.bool_,np.int8, np.uint8, np.int16, np.uint16, np.int32, np.uint32]:
gen_expression = f'hash_uint_from_uint(gid+seed32) % {int(hl)} + {int(l)}'
elif tensor.dtype in [np.int64]:
gen_expression = f'hash_ulong_from_ulong(gid+seed64) % {int(hl)} + {int(l)}'
elif tensor.dtype in [np.uint64]:
gen_expression = f'hash_ulong_from_ulong(gid+seed64) % {int(hl)} + {int(l)}'
elif tensor.dtype in [np.float16, np.float32]:
gen_expression = f'hash_float_from_uint(gid+seed32)*{hl} + {l}'
elif tensor.dtype in [np.float64]:
gen_expression = f'hash_double_from_ulong(gid+seed64)*{hl} + {l}'
kernel = Kernel(kernel_text=f"""
{HKernel.include_hash()}
{HKernel.define_tensor('O', (tensor.shape.size,), tensor.dtype )}
__kernel void impl(__global O_PTR_TYPE* O_PTR_NAME, uint seed32, ulong seed64)
{{
size_t gid = get_global_id(0);
O_GLOBAL_STORE(gid, {gen_expression} );
}}
""")
SCacheton.set_var(key, kernel)
tensor.get_device().run_kernel( kernel, tensor.get_buffer(),
np.uint32(np.random.randint(np.iinfo(np.uint32).max, dtype=np.uint32)),
np.uint64(np.random.randint(np.iinfo(np.uint64).max, dtype=np.uint64)),
global_shape=(tensor.shape.size,) )
def __str__(self): return f'InitRandomUniform low={self._low}, high={self._high}'

View file

@ -0,0 +1,17 @@
from ..Tensor import Tensor
class Initializer:
"""
Base class for tensor inititalizers
"""
def initialize_tensor(self, tensor : Tensor):
"""
Implement initialization of tensor
You can compute the data using python and then call buffer.set(numpy_value)
or call kernel.run( tensor.get_device(), tensor.get_buffer, ...) to initialize the data using OpenCL
"""
raise NotImplementedError()
def __str__(self): return 'Initializer'
def __repr__(self): return self.__str__()

View file

@ -0,0 +1,3 @@
from .InitCoords2DArange import InitCoords2DArange
from .Initializer import Initializer
from .InitRandomUniform import InitRandomUniform