import ocean device = ocean.gpu[0] dtypes = [ocean.bool, ocean.int8, ocean.uint8, ocean.uint32] for dtype in dtypes: print("\n========= Tensor %s (%s) =========" % (dtype.name, device.name)) a = ocean.asTensor([0, 1, 2, 9], 'r', dtype, device) b = ocean.asTensor([0, 1, 2, 4, 6, 8], ocean.uint8, device) r = ocean.tensor([6, 4], dtype, device) print(a) print(b) print(a << b) ocean.bitshiftLeft(a, b, r) print(r) a <<= 1 print(a)
import ocean a = ocean.arange(9, ocean.cfloat, ocean.gpu[0]).reshape([3, 3]) b = ocean.arange(9, ocean.cfloat, ocean.gpu[0]).reshape([3, 3]) print(a) alpha = ocean.asTensor([1.0, 2j, 1 + 2j], ocean.chalf, ocean.gpu[0]).reshape([1, 1, 3]) print(alpha) print(ocean.gemm(alpha, a, b))
import ocean a = ocean.asTensor([1, 1, 1, 1, 0, 1], ocean.gpu[0]) print(ocean.all(a))
import ocean import sys def exceptionMsg() : print("Expected error: %s" % str(sys.exc_info()[1])) def failTest(command) : try : eval(command) except : exceptionMsg() A = ocean.asTensor([[1+2j,3+1j],[1,2+2j]],ocean.gpu[0]) print(A) print(ocean.maximumAbs(A)) failTest("ocean.maximumAbs([[]])")
import ocean device = ocean.gpu[0] dtypes = [ocean.int8, ocean.int16, ocean.float, ocean.chalf, ocean.cdouble] otypes = [ocean.float, ocean.uint8, ocean.double, ocean.int32, ocean.chalf] print("\n========= fabs =========") for (dtype, otype) in zip(dtypes, otypes): a = ocean.asTensor([-3 - 4j], dtype, device) b = ocean.tensor([1], otype, device) ocean.fabs(a, b) print(b) print("\n========= absolute =========") for (dtype, otype) in zip(dtypes, otypes): a = ocean.asTensor([-3 - 4j], dtype, device) b = ocean.tensor([1], otype, device) ocean.absolute(a, b) print(b)
import ocean # Use GPU tensor to index CPU tensor v = ocean.gpu[0]([1, 2, 3]) idx = ocean.index[v] a = ocean.asTensor([5, 6, 7, 8, 9], ocean.double) print(a[idx]) print(a[v]) # Use byte-swapped CPU tensor to index GPU tensor a = ocean.asTensor([1, 2, 8], ocean.int32) a.byteswap() print(a) v = ocean.arange(10, ocean.int16, ocean.gpu[0]) idx = ocean.index[a]
import ocean a = ocean.asTensor([[0,1,2,3],[4,5,6,7],[8,9,10,11]], ocean.float, ocean.gpu[0]) b = ocean.asTensor([[12,13,14,15],[16,17,18,19],[20,21,22,23]], ocean.float, ocean.gpu[1]) print(a) print(b) b.flipAxis(1,True) print(b) a.copy(b) print(a.flipAxis(0))
import ocean print(ocean.sumAbs(ocean.gpu[0]([1, -2, 3]))) for dtype in [ocean.int8, ocean.int16, ocean.half, ocean.chalf, ocean.cdouble]: a = ocean.asTensor([1, -2, 5], dtype, ocean.gpu[0]) print(ocean.sumAbs(a))
import ocean device = ocean.gpu[0] print("\n========= Tensor and tensor =========") dtypes = [ocean.int8, ocean.int32, ocean.float, ocean.cfloat]; for dtype in dtypes : print("\n------ %s (%s) ------" % (dtype.name, device.name)) print(ocean.asTensor([1,2,3,4],'r',dtype,device) ** [0,1,2]) print(device([1,2,3]) ** ocean.asTensor([1,2,3,4],'r',dtype,device)) print(ocean.asTensor([1,2,3,4],'r',dtype,device) ** [0,0.5,1]) print(device([1.0,2.0,3.0]) ** ocean.asTensor([1,2,3,4],'r',dtype,device))
import ocean device = ocean.gpu[0] dtypes = [ocean.bool, ocean.int8, ocean.uint8, ocean.int32] v1 = [0, 2, 3, 7, 127] v2 = [0, 1, 2, 4, 8, 15] for dtype in dtypes: print("\n========= Tensor %s (%s) =========" % (dtype.name, device.name)) a1 = ocean.asTensor(v1, 'r', dtype, device) a2 = ocean.asTensor(v2, dtype, device).reshape([len(v2), 1]) r = ocean.tensor([len(v2), len(v1)], dtype, device) print(a1) print(a2) print("\n------ bitwiseOr(a1,a2), (a1 | a2), bitwiseOr(a1,a2,r) ------") print(ocean.bitwiseOr(a1, a2)) print(a1 | a2) ocean.bitwiseOr(a1, a2, r) print(r)
import ocean import numpy import ocean_numpy a = ocean.arange(64 * 65, ocean.double, ocean.gpu[0]).reshape(64, 65).T b = a.convertTo('numpy') print(a) print(ocean.norm(a, 2, 0)) print( ocean.asTensor( [float(numpy.linalg.norm(b[:, i], 2)) for i in range(b.shape[1])])) print(ocean.norm(a, 2, 1)) print( ocean.asTensor( [float(numpy.linalg.norm(b[i, :], 2)) for i in range(b.shape[0])])) print(ocean.norm(a, 2, [0, 1])) print(numpy.linalg.norm(range(64 * 65), 2))
import ocean for device in [ocean.cpu, ocean.gpu[0]]: print("\n# Boolean on %s" % (device.name)) a = ocean.asTensor([True, True, False], device) print(a) print(a / True) a /= [True, False, True] print(a) print("\n# Integer on %s" % (device.name)) a = ocean.asTensor([-10, -5, 0, 4, 6], ocean.int8, device) print(a) print(a / 3) a /= 3 print(a) print("\n# Float on %s" % (device.name)) a = ocean.asTensor([-10, -5, 0, 4, 6], ocean.float, device) print(a) print(a / 3.0) a /= 3 print(a)
import ocean device = ocean.gpu[0] dtypes = [ocean.int8, ocean.uint8, ocean.int32, ocean.float, ocean.chalf, ocean.cfloat] values = [-1,0,1,1+2j,0+2j, 0-2j, 0+0j] for dtype in dtypes : print("\n========= Tensor %s =========" % dtype.name) a = ocean.asTensor(values,dtype,device) b = ocean.tensor(len(values),ocean.int8,device) print(a) print(ocean.sign(a)) ocean.sign(a,b) print(b)
def exceptionMsg(): print("Expected error: %s" % str(sys.exc_info()[1])) def failTest(command): try: eval(command) except: exceptionMsg() device = ocean.gpu[0] print("\n========= Tensor and tensor =========") a = ocean.asTensor([1, 2, 3, 4], 'r', ocean.float, device) b = ocean.asTensor([0, 0.5, 1, 2], ocean.float, device) print(a**b) print(pow(a, b)) print(ocean.power(a, b)) c = ocean.tensor([4, 4], ocean.float, device) ocean.power(a, b, c) print(c) c = ocean.tensor([4, 4], ocean.cdouble, device) ocean.power(a, b, c) print(c) c = ocean.tensor([4, 4], ocean.int16, device)
import ocean device = ocean.gpu[0] dtypes = [ocean.bool, ocean.int32, ocean.uint32, ocean.float, ocean.cfloat] for dtype in dtypes : print("\n========= Tensor %s (%s) =========" % (dtype.name, device.name)) a1 = ocean.asTensor([0,0,1,1],dtype,device) a2 = ocean.asTensor([0,1,0,1],dtype,device) r1 = ocean.tensor([4],ocean.bool,device) r2 = ocean.tensor([4],ocean.int16,ocean.cpu) r2.byteswap() print(a1) print(a2) print("\n------ Logical AND ------") ocean.logicalAnd(a1,a2,r1) ocean.logicalAnd(a1,a2,r2) print(ocean.logicalAnd(a1,a2)) print(r1) print(r2) print("\n------ Logical OR ------") ocean.logicalOr(a1,a2,r1) ocean.logicalOr(a1,a2,r2) print(ocean.logicalOr(a1,a2)) print(r1) print(r2)
import ocean device = ocean.gpu[0] dtypes = [ocean.bool, ocean.int8, ocean.uint8] for dtype in dtypes: print("\n========= Tensor %s (%s) =========" % (dtype.name, device.name)) a = ocean.asTensor([0, 1, -1], dtype, device) print(a) print(ocean.bitwiseNot(a)) print(~a) print(ocean.logicalNot(a)) r = ocean.tensor(3, dtype, device) ocean.bitwiseNot(a, r) print(r) ocean.logicalNot(a, r) print(r)
import ocean t = ocean.tensor([2, 3], [0, 1], ocean.float) t.copy(ocean.asTensor([[1, 2, 3]], 'r')) print(t) print(t.storage) print(t.strides) print("\nClone") a = t.clone(ocean.gpu[0]) print(a.storage) print(a.strides) print("\nReplicate") b = t.replicate(ocean.gpu[0]) print(b.storage) print(b.strides)
import ocean for device in [ocean.cpu, ocean.gpu[0]]: print("\n# Boolean on %s" % (device.name)) a = ocean.asTensor([True, True, False], device) print(a) print(a * True) print(ocean.scale(a, [True, False, True])) a *= [True, False, True] print(a) print("\n# Integer on %s" % (device.name)) a = ocean.asTensor([-2, -1, 0, 1, 2], ocean.int8, device) print(a) print(a * 3) a *= 3 print(a) print("\n# Float on %s" % (device.name)) a = ocean.asTensor([-2, -1, 0, 1, 2], ocean.float, device) print(a) print(a * 3.0) a *= 3 print(a)
import ocean A = ocean.asTensor([1, 2, 3], ocean.float, ocean.gpu[0]) print(ocean.int8(A)) print(ocean.cdouble(A.storage)) print(ocean.half([[1, 2, 3], [4, 5, 6.]])) print(ocean.float(1.2))
import ocean device = ocean.gpu[0] print("\n========= Scalar and tensor =========") dtypes = [ocean.int8, ocean.int32, ocean.float, ocean.cfloat] for dtype in dtypes: print("\n------ %s (%s) ------" % (dtype.name, device.name)) print(ocean.asTensor([1, 2, 3, 4], dtype, device)**2) print(2**ocean.asTensor([1, 2, 3, 4], dtype, device)) print(ocean.asTensor([1, 2, 3, 4], dtype, device)**0.5) print(2.0**ocean.asTensor([1, 2, 3, 4], dtype, device))
import ocean a = ocean.asTensor([1, 2, 3]) v = a.clone([ocean.cpu, ocean.gpu[0]]) for tensor in v: print(tensor)
import ocean device = ocean.gpu[0] v1 = [1, 2, 3, 1, ocean.nan, 2 + 3j, 4 - 5j] v2 = [3, 2, 1, 1, 1, 2 + 2j, 4 - 6j] dtypes = [ocean.int8, ocean.uint8, ocean.float, ocean.chalf, ocean.cdouble] for dtype in dtypes: print("\n========= %s (%s) =========" % (dtype.name, device.name)) a1 = ocean.asTensor(v1, dtype, device) a2 = ocean.asTensor(v2, dtype, device) r1 = ocean.tensor([len(v1)], dtype, device) r2 = ocean.tensor([len(v1)], ocean.cfloat, device) print(a1) print(a2) print("\n------ min(a1,a2), min(a2,a1), max(a1,a2) ------") print(ocean.min(a1, a2)) print(ocean.min(a2, a1)) print(ocean.max(a1, a2)) print("\n------ fmin(a1,a2), fmin(a2,a1), fmax(a1,a2) ------") print(ocean.fmin(a1, a2)) print(ocean.fmin(a2, a1)) print(ocean.fmax(a1, a2)) print("\n------ fmin(a1,a2,r1), fmin(a1,a2,r2) ------") ocean.fmin(a1, a2, r1) ocean.fmin(a1, a2, r2) print(r1)
import ocean a = ocean.asTensor([ocean.nan, 3, 4, ocean.nan], ocean.float, ocean.gpu[0]) a.fillNaN(ocean.inf) print(a)