def test_define_dynamic(self): # Define an element-wise blaze function f = blaze_func("test_func", dshape("(A... * T, A... * T) -> A... * T"), elementwise=True) # Define implementation of element-wise blaze function # use implementation category 'funky' signature1 = T.Function(*[dshape("float64")] * 3) kernel1 = lambda a, b: a * b kernel(f, 'funky', kernel1, signature1) signature2 = T.Function(*[dshape("Axes... * float64")] * 3) kernel2 = lambda a, b: a * b kernel(f, 'funky', kernel2, signature2) # See that we can find the right 'funky' implementation overload = f.best_match('funky', T.Tuple([dshape("float32"), dshape("float64")])) self.assertEqual(overload.resolved_sig, signature1) self.assertEqual(overload.func, kernel1) overload = f.best_match('funky', T.Tuple([dshape("10 * 10 * float32"), dshape("10 * 10 * float64")])) self.assertEqual(overload.resolved_sig, dshape("(10 * 10 * float64, 10 * 10 * float64) -> 10 * 10 * float64")[0]) self.assertEqual(overload.func, kernel2)
def test_define_dynamic_nargs(self): # Define an element-wise blaze function f = blaze_func_from_nargs("test_func2", 2) # Define implementation of element-wise blaze function # use implementation category 'funky' signature = T.Function(*[dshape("float64")] * 3) kernel(f, 'funky', lambda a, b: a * b, signature) # See that we can find the right 'funky' implementation overload = f.best_match('funky', [dshape("float32"), dshape("float64")]) self.assertEqual(overload.resolved_sig, signature)
def test_define_dynamic(self): # Define an element-wise blaze function f = blaze_func("test_func", dshape("a -> a -> a"), elementwise=True) # Define implementation of element-wise blaze function # use implementation category 'funky' signature1 = T.Function(*[dshape("float64")] * 3) kernel(f, 'funky', lambda a, b: a * b, signature1) signature2 = T.Function(*[dshape("axes..., float64")] * 3) kernel(f, 'funky', lambda a, b: a * b, signature2) # See that we can find the right 'funky' implementation overload = f.best_match('funky', [dshape("float32"), dshape("float64")]) self.assertEqual(overload.resolved_sig, signature1) overload = f.best_match('funky', [dshape("10, 10, float32"), dshape("10, 10, float64")]) self.assertEqual(overload.resolved_sig, dshape("10, 10, float64 -> 10, 10, float64 -> 10, 10, float64"))
def create_overloaded_add(): # Create an overloaded blaze func, populate it with # some ckernel implementations extracted from numpy, # and test some calls on it. #d = blaze.overloading.Dispatcher() @function('A -> A -> A') def myfunc(x, y): raise NotImplementedError # overload int32 -> np.add ckd = _lowlevel.ckernel_deferred_from_ufunc(np.add, (np.int32, np.int32, np.int32), False) kernel(myfunc, "ckernel", ckd, "A..., int32 -> A..., int32 -> A..., int32") # overload int16 -> np.subtract (so we can see the difference) ckd = _lowlevel.ckernel_deferred_from_ufunc(np.subtract, (np.int16, np.int16, np.int16), False) kernel(myfunc, "ckernel", ckd, "A..., int16 -> A..., int16 -> A..., int16") return myfunc
def scidb_kernel(blaze_func, kern, signature, **metadata): """Define a scidb kernel implementation for the given blaze function""" kernel(blaze_func, AFL, kern, signature, **metadata)