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 # 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]
## 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 def run(cmd): print("====== %s ======" % cmd) print(eval(cmd)) a = ocean.arange(25, ocean.gpu[0]).reshape(5, 5) print(a) run("a[[1,2,1]]") run("a[:,[0,2,1]]") run("a[[2,1],:]") run("a[:,[1,2]]") run("a[[2,1],[1,2,2,2],None]") run("a[[[1,2],[2,3],[3,4]]]") b = (a <= a.T) run("b") f = ocean.find(b) run("f") run("a[None,b]") run("a[None,f]") run("a[[True,False,True,False,True],[True,True,False,False,True]]")
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 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 import sys def exceptionMsg() : print("Expected error: %s" % str(sys.exc_info()[1])) def failTest(command) : try : eval(command) except : exceptionMsg() s = ocean.arange(10,ocean.int8,ocean.gpu[0]).storage print(s) failTest("ocean.tensor(s,1,[1],[2],1, ocean.int16)")
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 A = ocean.arange([15], ocean.float, ocean.gpu[0]).reshape([3, 5]) print(A) print(A.T) print(A.T.strides) X = ocean.cpu(A.T) print(X) print(X.strides) B = A.flipAxis(0) print(B) print(B.T) B = A.swapAxes(0, 1) print(B) print(B.T) print("\n========= Copy B internally =========") print(B) print(B.strides) C = ocean.zeros(B.size, B.dtype, B.device) print(C.strides) C.copy(B) print(C) print(C.T)