In the previous blog article, we have seen how to use the AMD DSP Library to quickly implement an FFT on the AMD Versal AIE-ML architecture. As with any project, we might want to simulate the generated graph to ensure we are getting the correct behavior.
In this article, we are showing how we can use Python to generate an input text file to feed the AI Engine simulator.
Creating a Python input text file generator scriptWe have seen in previous article that the AI Engine simulator can work from input text files to feed the PLIOs of the graph under test. One convenient way to create these input text files is to use Python.
You can find the python script I have created to generate input stimuli for our FFT here:
https://github.com/xflorentw/AI_Engine_Basic/tree/main/02_FFT_AIE-ML/python/fft_gen.py
In this file, I am basically creating an input signal with 2 tones:
# ------------------------------------------------------------
# Generate Simulus I/O
# ------------------------------------------------------------
#Parameters
Iterations = 1
Input_shift = 15;
N_Taps = 1024
F1_MHz = 50
F2_MHz = 150
Fs_MHz = 400
# Number of sample points
N_Samp = N_Taps * Iterations
# sample spacing
T = 1.0 / Fs_MHz
A1 = 0.2
A2 = 0.4;
# Generate Input Signal
x = np.linspace(0.0, N_Samp*T, N_Samp, endpoint=False)
tone1 = A1 * np.exp( 1.j * 2.0*np.pi*F1_MHz*x)
tone2 = A2 * np.exp( 1.j * 2.0*np.pi*F2_MHz*x)
sig_i = tone1 + tone2;Then I am converting the input data to 16-bit complex fixed point to match my input on my AI Engine graph:
sig_i_cplx = Fxp(sig_i, dtype='S1.15')
sig_i_cplx = sig_i_cplx.astype(complex)
sig_i_cplx16 = sig_i_cplx*2**Input_shiftThen I am writing the samples in an output text file (input.txt), with 2 complex numbers per line (i.e. 4 numbers per lines) as my PLIOs are configured as 64-bit:
# Write the input to a file
with open("../aie/data/input.txt", "w") as f:
for i in range(0,int(N_Samp),2):
f.write(str(int(sig_i_cplx16[i].real))+" "+str(int(sig_i_cplx16[i].imag))+" "+str(int(sig_i_cplx16[i+1].real))+" "+str(int(sig_i_cplx16[i+1].imag))+"\n")Finally, I am running an FFT using scipy and writing the numbers as integers into another text file (output_ref.txt)
# ------------------------------------------------------------
# FFT
# ------------------------------------------------------------
# Process each frame with Python
for i in range(0, Iterations):
input_data = sig_i_cplx[i*1024:(i+1)*1024]
# Run Pyton FFT
golden_fft_out[i*1024:(i+1)*1024] = fft(input_data)
golden_fft_out = Fxp(golden_fft_out, dtype='S10.5')
golden_fft_out = golden_fft_out.astype(complex)
golden_fft_out = golden_fft_out*2**5
# Write the output reference to a file
with open("../aie/data/output_ref.txt", "w") as f:
for i in range(0,int(N_Samp),2):
f.write(str(int(golden_fft_out[i].real))+" "+str(int(golden_fft_out[i].imag))+" "+str(int(golden_fft_out[i+1].real))+" "+str(int(golden_fft_out[i+1].imag))+"\n")Running the simulationOnce we have generated the text files containing our input stimuli and the golden result we can run the AI Engine simulation.
We first need to import the input.txt file into our AI Engine component in Vitis.
Note: You can recreate the workspace from last week by running make all in AI_Engine_Basic/02_FFT_AIE-MLTo import the file, right cling on Sources under the fft_1024 AI Engine component and click Import > Folders...
Then select the folder AI_Engine_Basic/02_FFT_AIE-ML/aie/data which was created by the previous Python script
We can now run the AI Engine simulation.
The simulation should complete meaning that we have at least generated enough input samples to run one iteration of the graph.
Creating a Python output checkerWe could compare the file generated by the simulator to the golden reference manually. But you can see that there are minor (1 LSB) differences that we could ignore.
So I have created a another Python script which will read the golden and AI Engine simulator output text files and will compare the value:
https://github.com/xflorentw/AI_Engine_Basic/tree/main/02_FFT_AIE-ML/python/verify_fft_out.py
You can give the simulator as argument:
python3 verify_fft_out.py x86simor
python3 verify_fft_out.py hwWhen running the python script you should see the message "AI Engine FFT matches the Python FFT within 1 LSB" meaning that the FFT implemented inside the AIE-ML matches our golden reference.
SummaryIn this tutorial, I have shown how to use Python to generate input text file to feed the AI Engine in simulation and verify the output. This required multiple steps. There is one feature from AMD which allows calling the AI Engine simulator directly from a Python script which is called VitisFunctionalSimulation which provide a greater ease of use. I will show how we can use it for our graph testing in the next tutorial.
Disclaimers- AMD, Versal, and Vitis are trademarks or registered trademarks of Advanced Micro Devices, Inc.
- Other product names used in this publication are for identification purposes only and may be trademarks of their respective companies.






Comments