You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
			
				
					102 lines
				
				3.0 KiB
			
		
		
			
		
	
	
					102 lines
				
				3.0 KiB
			| 
								 
											5 years ago
										 
									 | 
							
								// clang++ -O2 repro.cc && ./a.out
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 | 
							
								#include <sched.h>
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								#include <sys/types.h>
							 | 
						||
| 
								 | 
							
								#include <unistd.h>
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								#include <cstdint>
							 | 
						||
| 
								 | 
							
								#include <cstdio>
							 | 
						||
| 
								 | 
							
								#include <cstdlib>
							 | 
						||
| 
								 | 
							
								#include <cstring>
							 | 
						||
| 
								 | 
							
								#include <ctime>
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								static inline double millis_since_boot() {
							 | 
						||
| 
								 | 
							
								  struct timespec t;
							 | 
						||
| 
								 | 
							
								  clock_gettime(CLOCK_BOOTTIME, &t);
							 | 
						||
| 
								 | 
							
								  return t.tv_sec * 1000.0 + t.tv_nsec * 1e-6;
							 | 
						||
| 
								 | 
							
								}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								#define MODEL_WIDTH 320
							 | 
						||
| 
								 | 
							
								#define MODEL_HEIGHT 640
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								// null function still breaks it
							 | 
						||
| 
								 | 
							
								#define input_lambda(x) x
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								// this is copied from models/dmonitoring.cc, and is the code that triggers the issue
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								void inner(uint8_t *resized_buf, float *net_input_buf) {
							 | 
						||
| 
								 | 
							
								  int resized_width = MODEL_WIDTH;
							 | 
						||
| 
								 | 
							
								  int resized_height = MODEL_HEIGHT;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								  // one shot conversion, O(n) anyway
							 | 
						||
| 
								 | 
							
								  // yuvframe2tensor, normalize
							 | 
						||
| 
								 | 
							
								  for (int r = 0; r < MODEL_HEIGHT/2; r++) {
							 | 
						||
| 
								 | 
							
								    for (int c = 0; c < MODEL_WIDTH/2; c++) {
							 | 
						||
| 
								 | 
							
								      // Y_ul
							 | 
						||
| 
								 | 
							
								      net_input_buf[(c*MODEL_HEIGHT/2) + r] = input_lambda(resized_buf[(2*r*resized_width) + (2*c)]);
							 | 
						||
| 
								 | 
							
								      // Y_ur
							 | 
						||
| 
								 | 
							
								      net_input_buf[(c*MODEL_HEIGHT/2) + r + (2*(MODEL_WIDTH/2)*(MODEL_HEIGHT/2))] = input_lambda(resized_buf[(2*r*resized_width) + (2*c+1)]);
							 | 
						||
| 
								 | 
							
								      // Y_dl
							 | 
						||
| 
								 | 
							
								      net_input_buf[(c*MODEL_HEIGHT/2) + r + ((MODEL_WIDTH/2)*(MODEL_HEIGHT/2))] = input_lambda(resized_buf[(2*r*resized_width+1) + (2*c)]);
							 | 
						||
| 
								 | 
							
								      // Y_dr
							 | 
						||
| 
								 | 
							
								      net_input_buf[(c*MODEL_HEIGHT/2) + r + (3*(MODEL_WIDTH/2)*(MODEL_HEIGHT/2))] = input_lambda(resized_buf[(2*r*resized_width+1) + (2*c+1)]);
							 | 
						||
| 
								 | 
							
								      // U
							 | 
						||
| 
								 | 
							
								      net_input_buf[(c*MODEL_HEIGHT/2) + r + (4*(MODEL_WIDTH/2)*(MODEL_HEIGHT/2))] = input_lambda(resized_buf[(resized_width*resized_height) + (r*resized_width/2) + c]);
							 | 
						||
| 
								 | 
							
								      // V
							 | 
						||
| 
								 | 
							
								      net_input_buf[(c*MODEL_HEIGHT/2) + r + (5*(MODEL_WIDTH/2)*(MODEL_HEIGHT/2))] = input_lambda(resized_buf[(resized_width*resized_height) + ((resized_width/2)*(resized_height/2)) + (r*resized_width/2) + c]);
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								  }
							 | 
						||
| 
								 | 
							
								}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								float trial() {
							 | 
						||
| 
								 | 
							
								  int resized_width = MODEL_WIDTH;
							 | 
						||
| 
								 | 
							
								  int resized_height = MODEL_HEIGHT;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								  int yuv_buf_len = (MODEL_WIDTH/2) * (MODEL_HEIGHT/2) * 6; // Y|u|v -> y|y|y|y|u|v
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								  // allocate the buffers
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								  uint8_t *resized_buf = (uint8_t*)malloc(resized_width*resized_height*3/2);
							 | 
						||
| 
								 | 
							
								  float *net_input_buf = (float*)malloc(yuv_buf_len*sizeof(float));
							 | 
						||
| 
								 | 
							
								  printf("allocate -- %p 0x%x -- %p 0x%lx\n", resized_buf, resized_width*resized_height*3/2, net_input_buf, yuv_buf_len*sizeof(float));
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								  // test for bad buffers
							 | 
						||
| 
								 | 
							
								  static int CNT = 20;
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								  float avg = 0.0;
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								  for (int i = 0; i < CNT; i++) {
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								    double s4 = millis_since_boot();
							 | 
						||
| 
								 | 
							
								    inner(resized_buf, net_input_buf);
							 | 
						||
| 
								 | 
							
								    double s5 = millis_since_boot();
							 | 
						||
| 
								 | 
							
								    avg += s5-s4;
							 | 
						||
| 
								 | 
							
								  }
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								  avg /= CNT;
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								  // once it's bad, it's reliably bad
							 | 
						||
| 
								 | 
							
								  if (avg > 10) {
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								    printf("HIT %f\n", avg);
							 | 
						||
| 
								 | 
							
								    printf("BAD\n");
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    for (int i = 0; i < 200; i++) {
							 | 
						||
| 
								 | 
							
								      double s4 = millis_since_boot();
							 | 
						||
| 
								 | 
							
								      inner(resized_buf, net_input_buf);
							 | 
						||
| 
								 | 
							
								      double s5 = millis_since_boot();
							 | 
						||
| 
								 | 
							
								      printf("%.2f   ", s5-s4);
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								    printf("\n");
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    exit(0);
							 | 
						||
| 
								 | 
							
								  }
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								  // don't free so we get a different buffer each time
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								  //free(resized_buf);
							 | 
						||
| 
								 | 
							
								  //free(net_input_buf);
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								  return avg;
							 | 
						||
| 
								 | 
							
								}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								int main() {
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								  while (true) {
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								    float ret = trial();
							 | 
						||
| 
								 | 
							
								    printf("got %f\n", ret);
							 | 
						||
| 
								 | 
							
								  }
							 | 
						||
| 
								 | 
							
								}
							 | 
						||
| 
								 | 
							
								
							 |