|
| 1 | +# SPDX-FileCopyrightText: 2022 - 2023 Intel Corporation |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: Apache-2.0 |
| 4 | + |
| 5 | +from math import ceil, sqrt |
| 6 | + |
| 7 | +import cupy as cp |
| 8 | +from numba import cuda |
| 9 | + |
| 10 | + |
| 11 | +@cuda.jit |
| 12 | +def _knn_kernel( # noqa: C901: TODO: can we simplify logic? |
| 13 | + train, |
| 14 | + train_labels, |
| 15 | + test, |
| 16 | + k, |
| 17 | + classes_num, |
| 18 | + train_size, |
| 19 | + predictions, |
| 20 | + votes_to_classes_lst, |
| 21 | + data_dim, |
| 22 | +): |
| 23 | + dtype = train.dtype |
| 24 | + |
| 25 | + i = cuda.grid(1) |
| 26 | + |
| 27 | + # here k has to be 5 in order to match with numpy |
| 28 | + queue_neighbors = cuda.local.array(shape=(5, 2), dtype=dtype) |
| 29 | + |
| 30 | + for j in range(k): |
| 31 | + x1 = train[j] |
| 32 | + x2 = test[i] |
| 33 | + |
| 34 | + distance = dtype.type(0.0) |
| 35 | + for jj in range(data_dim): |
| 36 | + diff = x1[jj] - x2[jj] |
| 37 | + distance += diff * diff |
| 38 | + dist = sqrt(distance) |
| 39 | + |
| 40 | + queue_neighbors[j, 0] = dist |
| 41 | + queue_neighbors[j, 1] = train_labels[j] |
| 42 | + |
| 43 | + for j in range(k): |
| 44 | + new_distance = queue_neighbors[j, 0] |
| 45 | + new_neighbor_label = queue_neighbors[j, 1] |
| 46 | + index = j |
| 47 | + |
| 48 | + while index > 0 and new_distance < queue_neighbors[index - 1, 0]: |
| 49 | + queue_neighbors[index, 0] = queue_neighbors[index - 1, 0] |
| 50 | + queue_neighbors[index, 1] = queue_neighbors[index - 1, 1] |
| 51 | + |
| 52 | + index = index - 1 |
| 53 | + |
| 54 | + queue_neighbors[index, 0] = new_distance |
| 55 | + queue_neighbors[index, 1] = new_neighbor_label |
| 56 | + |
| 57 | + for j in range(k, train_size): |
| 58 | + x1 = train[j] |
| 59 | + x2 = test[i] |
| 60 | + |
| 61 | + distance = dtype.type(0.0) |
| 62 | + for jj in range(data_dim): |
| 63 | + diff = x1[jj] - x2[jj] |
| 64 | + distance += diff * diff |
| 65 | + dist = sqrt(distance) |
| 66 | + |
| 67 | + if dist < queue_neighbors[k - 1][0]: |
| 68 | + queue_neighbors[k - 1][0] = dist |
| 69 | + queue_neighbors[k - 1][1] = train_labels[j] |
| 70 | + new_distance = queue_neighbors[k - 1, 0] |
| 71 | + new_neighbor_label = queue_neighbors[k - 1, 1] |
| 72 | + index = k - 1 |
| 73 | + |
| 74 | + while index > 0 and new_distance < queue_neighbors[index - 1, 0]: |
| 75 | + queue_neighbors[index, 0] = queue_neighbors[index - 1, 0] |
| 76 | + queue_neighbors[index, 1] = queue_neighbors[index - 1, 1] |
| 77 | + |
| 78 | + index = index - 1 |
| 79 | + |
| 80 | + queue_neighbors[index, 0] = new_distance |
| 81 | + queue_neighbors[index, 1] = new_neighbor_label |
| 82 | + |
| 83 | + votes_to_classes = votes_to_classes_lst[i] |
| 84 | + |
| 85 | + for j in range(len(queue_neighbors)): |
| 86 | + votes_to_classes[int(queue_neighbors[j, 1])] += 1 |
| 87 | + |
| 88 | + max_ind = 0 |
| 89 | + max_value = dtype.type(0) |
| 90 | + |
| 91 | + for j in range(classes_num): |
| 92 | + if votes_to_classes[j] > max_value: |
| 93 | + max_value = votes_to_classes[j] |
| 94 | + max_ind = j |
| 95 | + |
| 96 | + predictions[i] = max_ind |
| 97 | + |
| 98 | + |
| 99 | +def knn( |
| 100 | + x_train, |
| 101 | + y_train, |
| 102 | + x_test, |
| 103 | + k, |
| 104 | + classes_num, |
| 105 | + test_size, |
| 106 | + train_size, |
| 107 | + predictions, |
| 108 | + votes_to_classes, |
| 109 | + data_dim, |
| 110 | +): |
| 111 | + nthreads = 256 |
| 112 | + nblocks = ceil(test_size // nthreads) |
| 113 | + |
| 114 | + _knn_kernel[nblocks, nthreads]( |
| 115 | + x_train, |
| 116 | + y_train, |
| 117 | + x_test, |
| 118 | + k, |
| 119 | + classes_num, |
| 120 | + train_size, |
| 121 | + predictions, |
| 122 | + votes_to_classes, |
| 123 | + data_dim, |
| 124 | + ) |
0 commit comments