Skip to content

Commit

Permalink
PYNQ: Added loading and allocating from binary file into buffer (exam…
Browse files Browse the repository at this point in the history
…ple in kernel_v).
  • Loading branch information
ribesstefano committed Sep 5, 2021
1 parent 1607747 commit 4f3fa97
Show file tree
Hide file tree
Showing 2 changed files with 194 additions and 18 deletions.
Binary file added pynq/kernel_v/binfile_example.bin
Binary file not shown.
212 changes: 194 additions & 18 deletions pynq/kernel_v/kernel_v.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@
},
{
"cell_type": "code",
"execution_count": 34,
"execution_count": 51,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -378,25 +378,17 @@
"output_size = 256 # % H\n",
"# NOTE: Working with (R, N, out) == (16, 2, 8) \n",
"\n",
"xus = np.random.randn(R, N, G).astype(dtype=data_t)\n",
"v = np.random.randn(R, output_size // Tv, G, Tv).astype(dtype=data_t)\n",
"y = np.zeros((output_size // Tv, N, Tv, G)).astype(dtype=data_t)\n",
"\n",
"xus_buffer = pynq.allocate(shape=(R, N, G), dtype=data_t)\n",
"v_buffer = pynq.allocate(shape=(R, output_size // Tv, G, Tv), dtype=data_t)\n",
"y_buffer = pynq.allocate(shape=(output_size // Tv, N, Tv, G), dtype=data_t)\n",
"\n",
"# for i in range(N):\n",
"# for j in range(I):\n",
"# # for ii in range(R):\n",
"# x_buffer[i, j] = data_t(np.random.uniform(low=-2**15, high=2**15))\n",
"\n",
"# for i in range(R):\n",
"# for j in range(I // Tu):\n",
"# for k in range(G):\n",
"# for ii in range(Tu):\n",
"# u_buffer[i, j, k, ii] = data_t(np.random.uniform(low=-2**15, high=2**15))\n",
"\n",
"# for i in range(R):\n",
"# for j in range(G):\n",
"# for k in range(N):\n",
"# xu_buffer[i, j, k] = 0\n",
"np.copyto(xus_buffer, xus, casting='no')\n",
"np.copyto(v_buffer, v, casting='no')\n",
"np.copyto(y_buffer, y, casting='no')\n",
"\n",
"print('Buffers setup completed.')\n",
"print(f'xus_buffer.shape: {xus_buffer.shape} - Bytes: {xus_buffer.nbytes}')\n",
Expand All @@ -413,14 +405,14 @@
},
{
"cell_type": "code",
"execution_count": 35,
"execution_count": 52,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0x4\n",
"0xa\n",
"0x1\n",
"Starting transfer:\n",
"Wait xus...DONE.\n",
Expand Down Expand Up @@ -586,6 +578,190 @@
"print('fpga[0]: ', xu_buffer[0])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Loading File into PYNQ Buffer"
]
},
{
"cell_type": "code",
"execution_count": 53,
"metadata": {},
"outputs": [],
"source": [
"tmp = np.random.randn(R, N, G).astype(dtype=data_t)\n",
"tmp.tofile('binfile_example.bin')"
]
},
{
"cell_type": "code",
"execution_count": 59,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"PynqBuffer([[[0, 0, 0, 0]],\n",
"\n",
" [[0, 0, 0, 0]],\n",
"\n",
" [[0, 0, 0, 0]],\n",
"\n",
" [[0, 0, 0, 0]],\n",
"\n",
" [[0, 0, 0, 0]],\n",
"\n",
" [[0, 0, 0, 0]],\n",
"\n",
" [[0, 0, 0, 0]],\n",
"\n",
" [[0, 0, 0, 0]],\n",
"\n",
" [[0, 0, 0, 0]],\n",
"\n",
" [[0, 0, 0, 0]],\n",
"\n",
" [[0, 0, 0, 0]],\n",
"\n",
" [[0, 0, 0, 0]],\n",
"\n",
" [[0, 0, 0, 0]],\n",
"\n",
" [[0, 0, 0, 0]],\n",
"\n",
" [[0, 0, 0, 0]],\n",
"\n",
" [[0, 0, 0, 0]]], dtype=int16)"
]
},
"execution_count": 59,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"xus_buffer = pynq.allocate(shape=(R, N, G), dtype=data_t)\n",
"xus_buffer"
]
},
{
"cell_type": "code",
"execution_count": 61,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"PynqBuffer([[[-1, 0, -1, 0]],\n",
"\n",
" [[ 0, -2, 0, -1]],\n",
"\n",
" [[ 0, 0, 2, 0]],\n",
"\n",
" [[ 0, -1, 0, 0]],\n",
"\n",
" [[ 0, 0, 0, 0]],\n",
"\n",
" [[ 0, 1, 0, 0]],\n",
"\n",
" [[ 0, 0, 0, 0]],\n",
"\n",
" [[-1, 0, 0, 0]],\n",
"\n",
" [[ 0, 0, -1, 0]],\n",
"\n",
" [[ 0, -1, 0, 0]],\n",
"\n",
" [[ 1, 0, 0, 0]],\n",
"\n",
" [[ 0, 0, -1, 0]],\n",
"\n",
" [[-1, 0, -1, 0]],\n",
"\n",
" [[ 0, 0, 0, 0]],\n",
"\n",
" [[ 0, -1, -1, -1]],\n",
"\n",
" [[-1, 0, 0, 0]]], dtype=int16)"
]
},
"execution_count": 61,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"tmp = np.fromfile('binfile_example.bin', dtype=data_t).reshape(xus_buffer.shape)\n",
"np.copyto(xus_buffer, tmp, casting='no')\n",
"xus_buffer"
]
},
{
"cell_type": "code",
"execution_count": 65,
"metadata": {},
"outputs": [],
"source": [
"def load_from_bin(binfile, shape, dtype):\n",
" tmp_buffer = pynq.allocate(shape=shape, dtype=dtype)\n",
" tmp = np.fromfile(binfile, dtype=data_t).reshape(tmp_buffer.shape)\n",
" np.copyto(tmp_buffer, tmp, casting='no')\n",
" return tmp_buffer"
]
},
{
"cell_type": "code",
"execution_count": 66,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"PynqBuffer([[[-1, 0, -1, 0]],\n",
"\n",
" [[ 0, -2, 0, -1]],\n",
"\n",
" [[ 0, 0, 2, 0]],\n",
"\n",
" [[ 0, -1, 0, 0]],\n",
"\n",
" [[ 0, 0, 0, 0]],\n",
"\n",
" [[ 0, 1, 0, 0]],\n",
"\n",
" [[ 0, 0, 0, 0]],\n",
"\n",
" [[-1, 0, 0, 0]],\n",
"\n",
" [[ 0, 0, -1, 0]],\n",
"\n",
" [[ 0, -1, 0, 0]],\n",
"\n",
" [[ 1, 0, 0, 0]],\n",
"\n",
" [[ 0, 0, -1, 0]],\n",
"\n",
" [[-1, 0, -1, 0]],\n",
"\n",
" [[ 0, 0, 0, 0]],\n",
"\n",
" [[ 0, -1, -1, -1]],\n",
"\n",
" [[-1, 0, 0, 0]]], dtype=int16)"
]
},
"execution_count": 66,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"xus_buffer = load_from_bin('binfile_example.bin', shape=(R, N, G), dtype=data_t)\n",
"xus_buffer"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand Down

0 comments on commit 4f3fa97

Please sign in to comment.