def test_dense(): tf.random.set_seed(0) model = tf.keras.Sequential() # default linear, which is what we're implementing # no bias input_shape = 16 output_shape = 8 model.add( tf.keras.layers.Dense(output_shape, input_shape=(input_shape, ), dtype="float32", bias_initializer="random_uniform")) layer = Dense((1, input_shape, 1), "float32", output_shape) weights = model.weights[0].numpy().reshape(layer.weights.shape) bias = model.weights[1].numpy().reshape(layer.bias.shape) weights_t = tensor(weights, dtype="float32") bias_t = tensor(bias, dtype="float32") layer.load_weights(weights_t) layer.load_bias(bias_t) input_tensor = np.ones((1, input_shape), dtype="float32") tf_out = model.predict(input_tensor) # reshape it input_tensor = input_tensor.reshape((1, input_shape, 1)) layer.forward(input_tensor) out = np.reshape(layer.out, (1, output_shape)) assert np.isclose(out, tf_out).all()
def bench_conv(N=1000, size=5, dtype="int8", useTF=False): tf.random.set_seed(0) input_size = size num_filter = size kernel_size = int(size/2) input_channel = 1 input_shape = (1, input_size, input_size, input_channel) model = tf.keras.Sequential() conv = tf.keras.layers.Conv2D(num_filter, (kernel_size, kernel_size), dtype=dtype, bias_initializer="random_uniform") model.add(conv) model.build(input_shape) weights = model.weights[0].numpy() layer = udnn.Conv2D((input_size, input_size, input_channel), dtype, kernel_size, num_filter) weights_t = udnn.tensor(weights, dtype=dtype) bias = model.weights[1].numpy().reshape(layer.bias.shape) bias_t = udnn.tensor(bias, dtype=dtype) layer.load_weights(weights_t) layer.load_bias(bias_t) total_num = input_size * input_size * input_channel data = [i for i in range(total_num)] input_tensor = np.array(data, dtype=dtype).reshape((1, input_size, input_size, input_channel)) input_tensor_udnn = np.reshape(input_tensor, (input_size, input_size, input_channel)) start = time.time() if useTF: for _ in range(N): tf_out = model.predict(input_tensor) else: for _ in range(N): layer.forward(input_tensor_udnn) elapsed = time.time() - start avg = elapsed / N print(f'Ran conv {N} times for {size}x{size}x{size} input: took {elapsed} seconds') print(f'Avg: {avg} seconds / op') return avg
def test_buffer_convert(dtype): data = [i for i in range(4 * 4 * 3)] a = np.array(data, dtype=dtype) a = a.reshape((4, 4, 3, 1)) b = tensor(a, dtype=dtype) d = np.array(b, dtype=dtype) for x in range(4): for y in range(4): for c in range(3): assert b[x, y, c] == a[x, y, c] assert d[x, y, c] == a[x, y, c]
def test_conv(): tf.random.set_seed(0) input_size = 4 num_filter = 10 kernel_size = 3 input_channel = 1 input_shape = (1, input_size, input_size, input_channel) model = tf.keras.Sequential() conv = tf.keras.layers.Conv2D(num_filter, (kernel_size, kernel_size), dtype="float32", bias_initializer="random_uniform") model.add(conv) model.build(input_shape) weights = model.weights[0].numpy() layer = Conv2D((input_size, input_size, input_channel), "float32", kernel_size, num_filter) weights_t = tensor(weights, dtype="float32") bias = model.weights[1].numpy().reshape(layer.bias.shape) bias_t = tensor(bias, dtype="float32") layer.load_weights(weights_t) layer.load_bias(bias_t) total_num = input_size * input_size * input_channel data = [i for i in range(total_num)] input_tensor = np.array(data, dtype="float32").reshape( (1, input_size, input_size, input_channel)) tf_out = model.predict(input_tensor) tf_out = tf_out.reshape((input_size - kernel_size + 1, input_size - kernel_size + 1, num_filter)) input_tensor = np.reshape(input_tensor, (input_size, input_size, input_channel)) layer.forward(input_tensor) out = np.array(layer.out).reshape( (input_size - kernel_size + 1, input_size - kernel_size + 1, num_filter)) assert np.isclose(out, tf_out).all()
def bench_dense(N=1000, size=5, dtype="int8", useTF=False): tf.random.set_seed(0) model = tf.keras.Sequential() input_shape = size output_shape = int(size/2) model.add(tf.keras.layers.Dense(output_shape, input_shape=(input_shape,), dtype=dtype, bias_initializer="random_uniform")) layer = udnn.Dense((1, input_shape, 1), dtype, output_shape) weights = model.weights[0].numpy().reshape(layer.weights.shape) bias = model.weights[1].numpy().reshape(layer.bias.shape) weights_t = udnn.tensor(weights, dtype=dtype) bias_t = udnn.tensor(bias, dtype=dtype) layer.load_weights(weights_t) layer.load_bias(bias_t) input_tensor = np.reshape( np.array([random.randint(-127, 127) for i in range(input_shape)], dtype=dtype), (1, input_shape)) input_tensor_udnn = input_tensor.reshape((1, input_shape, 1)) start = time.time() if useTF: for _ in range(N): tf_out = model.predict(input_tensor) else: for _ in range(N): layer.forward(input_tensor_udnn) elapsed = time.time() - start avg = elapsed / N print(f'Ran dense {N} times for {size}x{size}x{size} input: took {elapsed} seconds') print(f'Avg: {avg} seconds / op') return avg
def test_shape(): shape = (1, 2, 3) a = np.ones(shape=shape, dtype="int8") b = tensor(shape, dtype="int8") assert b.shape != a.shape assert b.shape == (1, 2, 3, 1)