summaryrefstree
path: root/kernel/drivers/vision/npu/npu-util-comparator.c
blob: 16e9154932234b9741c78cc9bee36e99e778f26d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/*
 * Samsung Exynos SoC series NPU driver
 *
 * Copyright (c) 2017 Samsung Electronics Co., Ltd.
 *              http://www.samsung.com/
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 */

#include <linux/kernel.h>
#include <linux/types.h>
#include "npu-util-comparator.h"
#include "npu-log.h"

int simple_compare(u32 flags, const u8 *golden, const u8 *target, struct npu_fmap_dim dim, struct npu_golden_compare_result *result)
{
	size_t c, h, w;

	const u8 *pgolden = golden;
	const u8 *ptarget = target;

	int diff_cnt = 0;
	int match_cnt = 0;

	BUG_ON(!pgolden);
	BUG_ON(!ptarget);

	for (c = 0; c < dim.axis1; c++) {
		for (h = 0; h < dim.axis2; h++) {
			for (w = 0; w < dim.axis3; w++) {
				if (*pgolden != *ptarget) {
					add_compare_msg(result, " (%lu %lu %lu %u %u)",
						c, h, w, *pgolden, *ptarget);
					diff_cnt++;
				} else {
					match_cnt++;
				}
				pgolden++;
				ptarget++;
			}
		}
	}
	return diff_cnt;
}

#define ROUNDUP_FACTOR(N, B) ((((N) + (B)-1)) / (B))
#define ROUNDUP(N, B) (ROUNDUP_FACTOR((N), (B)) * (B))
#define CELL_W 4
#define CELL_H 4

static u8 atBinFile08(const u8 *addr, struct npu_fmap_dim dim, size_t channel, size_t row, size_t col)
{
	const size_t channelSize = dim.axis2 * dim.axis3;
	const size_t rowSize = dim.axis3;

	const size_t idx = channel * channelSize + row * rowSize + col;

	return *(addr + idx);
}

__attribute__((unused)) static u16 atBinFile16(const u8 *addr, struct npu_fmap_dim dim, size_t channel, size_t row, size_t col)
{
	return 0;
}

static u8 atNPUDump08(const u8 *addr, struct npu_fmap_dim dim, size_t channel, size_t row, size_t col)
{
	const size_t channelIdx = channel;
	const size_t channelStride =
		ROUNDUP(dim.axis3, CELL_W) * ROUNDUP(dim.axis2, CELL_H);

	const size_t rowIdx = row / CELL_H;
	const size_t rowStride = CELL_H * ROUNDUP(dim.axis3, CELL_W);

	const size_t cellIdx = col / CELL_W;
	const size_t cellStride = CELL_W * CELL_H;

	const size_t cellrow = row % CELL_H;
	const size_t cellcol = col % CELL_W;

	size_t offset = 0;
	u8 value = 0;

	offset += channelIdx * channelStride;
	offset += rowIdx * rowStride;
	offset += cellIdx * cellStride;
	offset += (cellrow * CELL_W + cellcol);

	return value = *(addr + offset);
}

__attribute__((unused)) static u16 atNPUDump16(const u8 *addr, struct npu_fmap_dim dim, size_t channel, size_t row, size_t col)
{
	return 0;
}

int reordered_compare(u32 flags, const u8 *golden, const u8 *target, struct npu_fmap_dim dim, struct npu_golden_compare_result *result)
{
	size_t ch, row, col;

	int diff_cnt = 0;
	int pass_cnt = 0;

	for (ch = 0; ch < dim.axis1; ch++) {
		for (row = 0; row < dim.axis2; row++) {
			for (col = 0; col < dim.axis3; col++) {
				const u8 golden_value = atBinFile08(golden, dim, ch, row, col);
				const u8 target_value = atNPUDump08(target, dim, ch, row, col);

				/*
				if(golden_value != target_value) {
					diff_cnt++;
				}
				*/

				if (golden_value != target_value) {
					add_compare_msg(result, " (%lu %lu %lu %u %u)",
						ch, row, col, golden_value, target_value);
					diff_cnt++;
				} else
					pass_cnt++;

			}
		}
	}
	return diff_cnt;
}

/* Return 1 if check was success, 0 if failed */
int is_av_valid_simple(const u32 golden_size, const u32 target_size, struct npu_golden_compare_result *result)
{
	BUG_ON(!result);

	if (golden_size > target_size) {
		add_compare_msg(result, "Size mismatch - Golden(%uB) / Target(%uB):Golden size should be smaller or equal to target. ",
			golden_size, target_size);
		return 0;
	}
	return 1;
}

int is_av_valid_reordered(const u32 golden_size, const u32 target_size, struct npu_golden_compare_result *result)
{
	BUG_ON(!result);

	if (golden_size > target_size) {
		add_compare_msg(result, "Size mismatch - Golden(%uB) / Target(%uB):Golden size should be smaller or equal to target. ",
			golden_size, target_size);
		return 0;
	}
	return 1;
}