def gpu_context(): return device_context(device_type.gpu, 0)
def cpu_context(): return device_context(device_type.cpu, 0)
def read_csv(f, c, t=np.float64): return pandas.read_csv(f, usecols=c, delimiter=',', header=None, dtype=t) except ImportError: # fall back to numpy loadtxt def read_csv(f, c, t=np.float64): return np.loadtxt(f, usecols=c, delimiter=',', ndmin=2) try: from dpctx import device_context, device_type with device_context(device_type.gpu, 0): gpu_available = True except: try: from daal4py.oneapi import sycl_context with sycl_context('gpu'): gpu_available = True except: gpu_available = False # Commone code for both CPU and GPU computations def compute(data, nComponents): # configure a PCA object and perform PCA pca_algo = d4p.pca(isDeterministic=True, fptype='float',
def main(readcsv=read_csv, method='defaultDense'): nClasses = 5 nFeatures = 6 # read training data from file with 6 features per observation and 1 class label trainfile = os.path.join('..', 'data', 'batch', 'logreg_train.csv') train_data = readcsv(trainfile, range(nFeatures)) train_labels = readcsv(trainfile, range(nFeatures, nFeatures + 1)) # read testing data from file with 6 features per observation testfile = os.path.join('..', 'data', 'batch', 'logreg_test.csv') predict_data = readcsv(testfile, range(nFeatures)) # Using of the classic way (computations on CPU) result_classic, train_result = compute(train_data, train_labels, predict_data, nClasses) train_data = to_numpy(train_data) train_labels = to_numpy(train_labels) predict_data = to_numpy(predict_data) try: from dpctx import device_context, device_type gpu_context = lambda: device_context(device_type.gpu, 0) cpu_context = lambda: device_context(device_type.cpu, 0) except: from daal4py.oneapi import sycl_context gpu_context = lambda: sycl_context('gpu') cpu_context = lambda: sycl_context('cpu') # It is possible to specify to make the computations on GPU if gpu_available: with gpu_context(): sycl_train_data = sycl_buffer(train_data) sycl_train_labels = sycl_buffer(train_labels) sycl_predict_data = sycl_buffer(predict_data) result_gpu, _ = compute(sycl_train_data, sycl_train_labels, sycl_predict_data, nClasses) assert np.allclose(result_classic.prediction, result_gpu.prediction) assert np.allclose(result_classic.probabilities, result_gpu.probabilities, atol=1e-3) assert np.allclose(result_classic.logProbabilities, result_gpu.logProbabilities, atol=1e-2) # It is possible to specify to make the computations on CPU with cpu_context(): sycl_train_data = sycl_buffer(train_data) sycl_train_labels = sycl_buffer(train_labels) sycl_predict_data = sycl_buffer(predict_data) result_cpu, _ = compute(sycl_train_data, sycl_train_labels, sycl_predict_data, nClasses) # the prediction result provides prediction, probabilities and logProbabilities assert result_classic.probabilities.shape == (predict_data.shape[0], nClasses) assert result_classic.logProbabilities.shape == (predict_data.shape[0], nClasses) predict_labels = np.loadtxt(testfile, usecols=range(nFeatures, nFeatures + 1), delimiter=',', ndmin=2) assert np.count_nonzero(result_classic.prediction - predict_labels) / predict_labels.shape[0] < 0.025 assert np.allclose(result_classic.prediction, result_cpu.prediction) assert np.allclose(result_classic.probabilities, result_cpu.probabilities) assert np.allclose(result_classic.logProbabilities, result_cpu.logProbabilities) return (train_result, result_classic, predict_labels)
def get_context(device): if dpctx_available: return device_context(device, 0) if sycl_extention_available: return sycl_context(device) return None
def main(readcsv=read_csv, method='defaultDense'): # Input data set parameters train_file = os.path.join('..', 'data', 'batch', 'k_nearest_neighbors_train.csv') predict_file = os.path.join('..', 'data', 'batch', 'k_nearest_neighbors_test.csv') # Read data. Let's use 5 features per observation nFeatures = 5 nClasses = 5 train_data = readcsv(train_file, range(nFeatures), t=np.float32) train_labels = readcsv(train_file, range(nFeatures, nFeatures + 1), t=np.float32) predict_data = readcsv(predict_file, range(nFeatures), t=np.float32) predict_labels = readcsv(predict_file, range(nFeatures, nFeatures + 1), t=np.float32) predict_result_classic = compute(train_data, train_labels, predict_data, nClasses) # We expect less than 170 mispredicted values assert np.count_nonzero( predict_labels != predict_result_classic.prediction) < 170 train_data = to_numpy(train_data) train_labels = to_numpy(train_labels) predict_data = to_numpy(predict_data) try: from dpctx import device_context, device_type gpu_context = lambda: device_context(device_type.gpu, 0) cpu_context = lambda: device_context(device_type.cpu, 0) except: from daal4py.oneapi import sycl_context gpu_context = lambda: sycl_context('gpu') cpu_context = lambda: sycl_context('cpu') if gpu_available: with gpu_context(): sycl_train_data = sycl_buffer(train_data) sycl_train_labels = sycl_buffer(train_labels) sycl_predict_data = sycl_buffer(predict_data) predict_result_gpu = compute(sycl_train_data, sycl_train_labels, sycl_predict_data, nClasses) assert np.allclose(predict_result_gpu.prediction, predict_result_classic.prediction) with cpu_context(): sycl_train_data = sycl_buffer(train_data) sycl_train_labels = sycl_buffer(train_labels) sycl_predict_data = sycl_buffer(predict_data) predict_result_cpu = compute(sycl_train_data, sycl_train_labels, sycl_predict_data, nClasses) assert np.allclose(predict_result_cpu.prediction, predict_result_classic.prediction) return (predict_result_classic, predict_labels)
def main(readcsv=None, method='defaultDense'): # read data from file infile = os.path.join('..', 'data', 'batch', 'covcormoments_dense.csv') # Using of the classic way (computations on CPU) # Configure a low order moments object for streaming algo = d4p.low_order_moments(streaming=True, fptype='float') # get the generator (defined in stream.py)... rn = read_next(infile, 55, readcsv) # ... and iterate through chunks/stream for chunk in rn: algo.compute(chunk) # finalize computation result_classic = algo.finalize() try: from dpctx import device_context, device_type gpu_context = lambda: device_context(device_type.gpu, 0) cpu_context = lambda: device_context(device_type.cpu, 0) except: from daal4py.oneapi import sycl_context gpu_context = lambda: sycl_context('gpu') cpu_context = lambda: sycl_context('cpu') # It is possible to specify to make the computations on GPU try: with gpu_context(): # Configure a low order moments object for streaming algo = d4p.low_order_moments(streaming=True, fptype='float') # get the generator (defined in stream.py)... rn = read_next(infile, 55, readcsv) # ... and iterate through chunks/stream for chunk in rn: sycl_chunk = sycl_buffer(to_numpy(chunk)) algo.compute(sycl_chunk) # finalize computation result_gpu = algo.finalize() for name in [ 'minimum', 'maximum', 'sum', 'sumSquares', 'sumSquaresCentered', 'mean', 'secondOrderRawMoment', 'variance', 'standardDeviation', 'variation' ]: assert np.allclose(getattr(result_classic, name), getattr(result_gpu, name)) except RuntimeError: pass # It is possible to specify to make the computations on CPU with cpu_context(): # Configure a low order moments object for streaming algo = d4p.low_order_moments(streaming=True, fptype='float') # get the generator (defined in stream.py)... rn = read_next(infile, 55, readcsv) # ... and iterate through chunks/stream for chunk in rn: sycl_chunk = sycl_buffer(to_numpy(chunk)) algo.compute(sycl_chunk) # finalize computation result_cpu = algo.finalize() # result provides minimum, maximum, sum, sumSquares, sumSquaresCentered, # mean, secondOrderRawMoment, variance, standardDeviation, variation for name in [ 'minimum', 'maximum', 'sum', 'sumSquares', 'sumSquaresCentered', 'mean', 'secondOrderRawMoment', 'variance', 'standardDeviation', 'variation' ]: assert np.allclose(getattr(result_classic, name), getattr(result_cpu, name)) return result_classic