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) At = ocean.arange(2 * sizeA[0] * sizeA[1], dtype.basetype) At = ocean.tensor(At.storage, 0, [sizeA[0], sizeA[1]], dtype) A.copy(At) An = ocean.tensor(sizeA, dtype) An.copy(A) for orderB in ['F', 'C']: B = ocean.tensor(sizeB, orderB, dtype) Bt = ocean.arange(2 * sizeB[0] * sizeB[1], dtype.basetype) Bt = ocean.tensor(Bt.storage, 0, [sizeB[0], sizeB[1]], dtype) B.copy(Bt) 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)
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 a = ocean.asTensor([1 + 2j, 2 + 3j], 'r') b = ocean.asTensor([1, 2 + 1j]) print(a) print(ocean.conj(a)) print(b) print(ocean.gemm(1, a, b)) print(ocean.gemm(1, a.T, 'C', b)) print(ocean.gemm(1, a.T, 'C', b.T, 'C')) print(ocean.gemm(1, a, b.T, 'C'))
import pyOcean_cpu as ocean a = ocean.asTensor([[1, 2], [3, 4]], 'r', ocean.float) b = ocean.arange(6, ocean.float).reshape([2, 3]) print(a) print(b) print(ocean.gemm(ocean.float(3), a, b)) alpha = ocean.asTensor([1, 2, 3], ocean.float).reshape([1, 1, 3]) print(ocean.gemm(alpha, a, b)) print(ocean.gemm(ocean.float(1), a.T, b))
import pyOcean_cpu as ocean a = ocean.asTensor([[1 + 2j, 2 + 1j], [3 + 0j, 4 + 2j]], 'r', ocean.cfloat) b = ocean.asTensor([1 + 1j, 2 - 3j], ocean.cfloat) print(ocean.gemm(ocean.float(1), a, 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.asTensor([[0, 1, 2.], [3, 4, 5]]) b = ocean.arange(12, ocean.double).reshape([3, 4]) print(a.T) print(b) print(ocean.gemm(1, a, 'T', b)) print(ocean.gemm(1, a, a, 'T'))
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, 1, 3], ocean.float) ocean.gemm(ocean.float(1), a, b, 0, c) print(c)