def checkDType(dtype): nError = 0 for transA in ['N', 'T', 'C']: if (transA == 'N'): sizeA = [2, 3] else: sizeA = [3, 2] for transB in ['N', 'T', 'C']: if (transB == 'N'): sizeB = [3, 4] else: sizeB = [4, 3] for orderA in ['F', 'C']: A = ocean.tensor(sizeA, orderA, dtype) A.copy(ocean.arange(sizeA[0] * sizeA[1], dtype).reshape(sizeA)) An = ocean.tensor(sizeA, dtype) An.copy(A) for orderB in ['F', 'C']: B = ocean.tensor(sizeB, orderB, dtype) B.copy( ocean.arange(sizeB[0] * sizeB[1], dtype).reshape(sizeB)) Bn = ocean.tensor(sizeB, dtype) Bn.copy(B) for orderC in ['F', 'C']: C = ocean.tensor([2, 4], orderC, dtype) print( "Setting: transA='%s', transB='%s', orderA='%s', orderB='%s', orderC='%s'\n" % (transA, transB, orderA, orderB, orderC)) ocean.gemm(1, A, transA, B, transB, 0, C) Cn = ocean.gemm(1, An, transA, Bn, transB) if (not ocean.all(C == Cn)): expectedStr = str(C).replace('\n', '\n ') obtainedStr = str(Cn).replace('\n', '\n ') print("Expected : %s" % expectedStr) print("Obtained : %s" % obtainedStr) nError += 1 else: expectedStr = str(C).replace('\n', '\n ') print("Success : %s" % expectedStr) if (nError == 0): print("All checks passed for data type %s" % dtype.name)
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========= Scalar %s =========" % dtype.name) print([dtype(value) for value in values]) print([ocean.sign(dtype(value)) for value in values]) for dtype in dtypes: print("\n========= Tensor %s =========" % dtype.name) a = ocean.asTensor(values, dtype) b = ocean.tensor(len(values), ocean.int8) print(a) print(ocean.sign(a)) ocean.sign(a, b) print(b) print("\n========= Exceptions =========") s = ocean.nan print(s) print(ocean.sign(s)) print(ocean.sign([s])) s = ocean.cfloat(0) s.imag = ocean.nan print(s) print(ocean.sign(s))
import pyOcean_cpu as ocean device = ocean.cpu 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 pyOcean_cpu as ocean A = ocean.asTensor([[0,1],[2,3]],'r',ocean.float) b = ocean.asTensor([1,2],ocean.float) B = b.reshape([2,1]); c = ocean.tensor([2],ocean.float); C = ocean.tensor([2,1],ocean.float); print(A) print(b) print(B) print("\n====== A*b ======"); print(ocean.gemm(1,A,b)) print("\n====== A*B ======"); print(ocean.gemm(1,A,B)) print("\n====== c = A*b ======"); ocean.gemm(1,A,b,0,c); print(c) print("\n====== C = A*b ======"); ocean.gemm(1,A,b,0,C); print(C) print("\n====== c = A*B ======"); ocean.gemm(1,A,B,0,c); print(c) print("\n====== C = A*B ======"); ocean.gemm(1,A,B,0,C); print(C)
import pyOcean_cpu as 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,2,3],ocean.int8) b = ocean.tensor([3],ocean.uint16) ocean.square(a,b) print(b) b.fill(0) b.byteswap() ocean.square(a,b) print(b) a.byteswap() b.fill(0) ocean.square(a,b) print(b) b.byteswap() b.fill(0) ocean.square(a,b)
import pyOcean_cpu as ocean device = ocean.cpu 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 pyOcean_cpu as ocean import sys def exceptionMsg(): print("Expected error: %s" % str(sys.exc_info()[1])) def failTest(command): try: eval(command) except: exceptionMsg() a = ocean.tensor([2, 3, 4, 5]) failTest("a.permuteAxes(range(100))") failTest("a.permuteAxes([1,2,3,4,5])") failTest("a.permuteAxes([-1,1,2,3])") failTest("a.permuteAxes([1,2,3,20])") failTest("a.permuteAxes([1,2,3,1])")
import pyOcean_cpu as ocean a = ocean.asTensor([1, 2, 3]) b = ocean.tensor(a.size, a.dtype) a.byteswap() b.copy(a) print(a) print(b)
import pyOcean_cpu as ocean a = ocean.asTensor([[0,1],[2,3]],'r',ocean.float) b = ocean.asTensor([1,2],ocean.float) c = ocean.tensor([2],[128],ocean.float); print(a) print(b) ocean.gemm(1,a,b,0,c) print(c) print("\n----------------------------------------") a = ocean.asTensor([1.,2,3]).reshape([3,1]); b = ocean.asTensor([2.,3,4],'r'); c = ocean.tensor([3,3],a.dtype); print(a) print(b) ocean.gemm(1,a,b,0,c); print(c) #print(ocean.gemm(1,a,b))
import pyOcean_cpu as ocean a = ocean.tensor([0]) a.fill(3) print(a)
import pyOcean_cpu as ocean # Fortran-style strides a = ocean.tensor([3, 4], 'F') a.copy(range(a.nelem)) print(a) print(a.strides) b = a.reshape([2, 3, 2]) print(b) print(b.storage.obj == a.storage.obj) a.reshape([2, 6], True) print(a) # C-style strides a = ocean.tensor([3, 4], 'C') a.copy(range(a.nelem)) print(a) print(a.strides) b = a.reshape([2, 3, 2]) print(b) print(b.storage.obj == a.storage.obj) a.reshape([2, 6], True) print(a)
def exceptionMsg(): print("Expected error: %s" % str(sys.exc_info()[1])) def failTest(command): try: eval(command) except: exceptionMsg() # Boolean a = ocean.asTensor([True, False, False]) print(-a) # Unsigned integer (8) a = ocean.arange(5, ocean.uint8) print(-a) # Different types types = [ocean.int16, ocean.half, ocean.float, ocean.cdouble] for t in types: a = ocean.linspace(-5, 5, 11, t) print(-a) # Result in unsigned integer a = ocean.arange(-5, 5, ocean.int8) b = ocean.tensor(a.size, ocean.uint8) failTest("ocean.negative(a,b)")
import pyOcean_cpu as ocean device = ocean.cpu 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 pyOcean_cpu as ocean a = ocean.tensor([5, 5]) a.fill(0) print(a) a.diag().fill(3) a.fliplr().diag().fill(5) print(a)
import pyOcean_cpu as ocean print("------ Original matrix ------") A = ocean.tensor([1, 4, 1, 3]) A.copy(range(A.nelem)) print("\n------ Squeeze all axes ------") print(A.squeeze()) print("\n------ Squeeze only axis 0 ------") print(A.squeeze(0)) print("\n------ In-place squeezing of axis 2 ------") A.squeeze(2, True) print(A) print("\n------ In-place squeezing of all axes ------") A.squeeze(True) print(A)
import pyOcean_cpu as ocean import sys def fail(command): try: eval(command) print("%s: SUCCEEDED BUT SHOULD HAVE FAILED!" % command) except: print("%s: Expected error: %s" % (command, str(sys.exc_info()[1]))) a = ocean.tensor([3, 4]) b = ocean.tensor([3]) c = ocean.tensor([3, 1]) fail("ocean.sum(a,[])") fail("ocean.sum(a,0,b)") fail("ocean.sum(a,1,True,b)") fail("ocean.sum(a,[1,1])") fail("ocean.sum(a,[-3])") fail("ocean.sum(a,[2])") fail("ocean.sum(a,[1,-1])")
## Conversion from storage to tensor import pyOcean_cpu as ocean s = ocean.storage(14) s.dtype = ocean.cfloat s.asTensor().fill(1 + 2j) print(s) s.dtype = ocean.float print(s) t = ocean.tensor(s, 0, [3, 4]) print(t) t = ocean.tensor(s, 4, [3, 4], 'F', 1) print(t) t = ocean.tensor(s, 4, [3, 2], 'F', 1, ocean.cfloat) print(t)
import pyOcean_cpu as ocean device = ocean.cpu dtypes = [ocean.bool, ocean.int8, ocean.uint8, ocean.int32] v1 = [1,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------ bitwiseAnd(a1,a2), (a1 & a2), bitwiseAnd(a1,a2,r) ------") print(ocean.bitwiseAnd(a1,a2)) print(a1 & a2) ocean.bitwiseAnd(a1,a2,r) print(r)
import pyOcean_cpu as ocean a = ocean.tensor([5, 6]) a.T.copy(range(a.nelem)) print(a) v = a.split(0, 2) print(v) v = a.split(1, 2) print(v)
import pyOcean_cpu as 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, 2, 0, 0, 3], [0, 4, 5, 0, 0]], 'R') print(a) print(ocean.find(a)) a = ocean.tensor([]) failTest("ocean.find(a)")
print(a) v = a.split(0,[0,2,3,1]) for i in range(4) : print("\n------ Slice %d ------" % (i+1)) print(v[i]) b = ocean.merge(v,0) print(b) print("\n------ Allowed case #1 ------") ocean.merge(v,0,a) print(a) print("\n------ Allowed case #2 ------") b = ocean.tensor(a.size, a.strides, a.dtype) b.fill(0) ocean.merge(v,0,b) print(b) print("\n------ Non-allowed case #1 ------") v[-1] = v[-1].clone() try : ocean.merge(v,0,a) except : print("Expected error: merging along zero-strided dimension") print("\n------ Non-allowed case #2 ------") try : ocean.merge(ocean.tensor(b.size),0,b)
## Square-root operation with overlap import pyOcean_cpu as ocean a = ocean.asTensor([4, 9, 16, 25]) a1 = ocean.tensor(a.storage, 0, [3]) a2 = ocean.tensor(a.storage, 1, [3]) print(a1) print(a2) ocean.sqrt(a1, a2) print(a1) print(a2)
import pyOcean_cpu as ocean device = ocean.cpu 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)
except: exceptionMsg() device = ocean.cpu 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)")
import pyOcean_cpu as ocean import sys def exceptionMsg(): print("Expected error: %s" % str(sys.exc_info()[1])) def failTest(command): try: eval(command) except: exceptionMsg() a = ocean.tensor([2]) b = ocean.tensor([2, 3]) a.fill(10) c = a.broadcastLike(b) print(a) print(c) print(c.strides) c.fill(9) print(a) print(c) print(c.strides) d = a.shallowCopy()
import pyOcean_cpu as ocean print(ocean.tensor([]).isScalar()) print(ocean.tensor([2]).isScalar()) print(ocean.tensor([1]).isScalar())
import pyOcean_cpu as ocean a = ocean.asTensor(range(24)) b = a.reshape([2, 3, 4]) print(b) a = ocean.tensor([4, 6], [7, 1]) a.copy(range(a.nelem)) b = a.reshape([2, 3, 4]) print(a) print(b) b = a.reshape([6, 4]) print(b) a.reshape([6, 4], True) print(a)
## Basic tensor properties import pyOcean_cpu as ocean T = ocean.tensor([2, 3], ocean.float) T.storage.zero() print(T) print(T.storage) print(T.device) print(T.dtype) print(T.ndims) print(T.size) print(T.strides) print(T.offset) print(T.device) print(T.elemsize) print(T.refcount) print(T.footer)
import pyOcean_cpu as ocean a = ocean.tensor([2, 3], [0, ocean.float.size], ocean.float) a.T.copy([1, 2, 3]) print(a)
import pyOcean_cpu as ocean a = ocean.tensor([2, 6], [0, 0], ocean.cpu) b = ocean.tensor([2, 6], [0, 1], ocean.cpu) c = ocean.tensor([2, 6, 2], [0, 0, 1], ocean.cpu) d = ocean.tensor([2, 6], [0, 0], ocean.cpu) 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) print(b.storage)