import ocean c = ocean.arange(18, ocean.float, ocean.cpu).reshape([3, 6]) d = ocean.tensor([18], ocean.double, ocean.gpu[0]) c.byteswap() d.fill(2) print(c) print(d) print("--------------------------------------------------------------") d.copy(c) d.sync() print("--------------------------------------------------------------") print(c) print(d.storage) print(d)
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)
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) ocean.power(a, b, c) print(c) a **= ocean.asTensor([1, 1, 1, 0.5], 'r', ocean.float, device) print(a) failTest("pow(a,b,3)")
## Copying of tensor from GPU to CPU in compressed form import ocean a = ocean.arange(4, ocean.float, ocean.gpu[0]).broadcastTo([4, 3]) print(a) b = ocean.tensor([12], ocean.half, ocean.cpu) b.byteswapped = True b.copy(a) print(b.reshape([4, 3])) # --------------------------------------------------------------- b = ocean.tensor([12], ocean.double, ocean.cpu) b.byteswapped = True b.copy(a) print(b.reshape([4, 3])) # --------------------------------------------------------------- b = ocean.tensor([12], ocean.int8, ocean.cpu) b.copy(a) print(b.reshape([4, 3])) # --------------------------------------------------------------- b = ocean.tensor([12], ocean.double, ocean.cpu) b.copy(a) print(b.reshape([4, 3]))
import ocean A = ocean.arange(2 * 3 * 4, ocean.float, ocean.gpu[0]).reshape([2, 3, 4]) print(A) print(ocean.norm2(A, [0, 1])) A.fliplr(True) print(ocean.minimum(A, 0)) print(ocean.maximum(A, 2, True)) B = ocean.tensor([4], ocean.half, ocean.gpu[0]) ocean.norm(A, 3, [0, 1], B) print(B) print(ocean.norm(A, 3, [0, 1]))
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 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] 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 device = ocean.gpu[0] A = ocean.zeros([100], ocean.float, device) B = ocean.tensor(A.storage, 0, [15]) B.copy(range(15)) C = ocean.tensor(A.storage, 0, [5, 3], [3, 1]) print(A) print(B) print(C) print("========= Evaluating C += 100 =========") C += 100 print(C) print(A)
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)
## Copy an empty tensor from gpu to cpu import ocean a = ocean.tensor([0],ocean.gpu[0]) print(a)
import ocean a = ocean.tensor([2, 6], [0, 0], ocean.gpu[0]) b = ocean.tensor([2, 6], [0, 1], ocean.gpu[0]) c = ocean.tensor([2, 2, 3], [1, 0, 0], ocean.gpu[0]) d = ocean.tensor([2, 6], [0, 0], ocean.gpu[0]) a.fill(1) b.fill(2) c.fill(3) d.fill(4) print(a) print(b) print(c) print(d) print("\n--------- Copy ---------\n") b.copy(a) c.copy(a) d.copy(a) print(a) print(b) print(c) print(d) print("\n--------- Storage ---------\n") print(a.storage)
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)
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 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 a = ocean.arange(18, ocean.cpu).reshape([3, 6]) b = ocean.tensor([3, 6], ocean.cpu) c = ocean.tensor([3, 6], ocean.gpu[0]) a.byteswap() b.fill(1) c.fill(2) print(a) print(b) print(c) b.copy(a) c.copy(a) print("--------------------------------------------------------------") print(a) print(b) print(c) d = ocean.double(a) print(d) d = ocean.gpu[0](a) print(d)
import ocean a = ocean.tensor([3,6],[0,1],ocean.double, ocean.cpu) b = ocean.tensor([3,6],ocean.float, ocean.gpu[0]) a.fill(1) b.fill(2) print(a) print(b) print("--------------------------------------------------------------") b.copy(a) print("--------------------------------------------------------------") print(a) print(b)
import ocean a = ocean.asTensor([1, 2, 3], ocean.int8, ocean.cpu) b = ocean.tensor([3], ocean.int16, ocean.gpu[0]) ocean.square(a, b) print(b) a.byteswap() b.fill(0) ocean.square(a, b) print(b) a = ocean.asTensor([1, 2, 3], ocean.int8, ocean.gpu[0]) b = ocean.tensor([3], ocean.int16, ocean.cpu) ocean.square(a, b) print(b) b.fill(0) b.byteswap() ocean.square(a, b) print(b)
import ocean a = ocean.tensor([5, 6]) a.copy(range(a.nelem)) print("-----------------------------------") print("a") print("-----------------------------------") print(a) print("\n-----------------------------------") print("a.split(0,[ocean.cpu, ocean.gpu[0], ocean.cpu])") print("-----------------------------------") v = a.split(0, [ocean.cpu, ocean.gpu[0], ocean.cpu]) for tensor in v: print(tensor) print("\n-----------------------------------") print("a.split(1,[1,5,0])") print("-----------------------------------") v = a.split(1, [1, 5, 0]) for tensor in v: print(tensor) print("\n-----------------------------------") print("a.split(0,[ocean.gpu[0],ocean.cpu],[1,4])") print("-----------------------------------") v = a.split(0, [ocean.gpu[0], ocean.cpu], [1, 4]) for tensor in v: print(tensor)