Example.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import os.path
  2. import numpy as np
  3. import itertools
  4. # This python module is containing definitions useful for
  5. # generating test patterns.
  6. import Tools
  7. # This function is generating patterns for an addition
  8. def writeTests(config,format):
  9. # In this test, we can use the same inputs for several tests.
  10. # For instance, we could test an addition on 3 samples and an addition
  11. # on 9 samples to be sure that the vectorized code with tail is
  12. # working.
  13. # So, we generate two long patterns and in the different tests we may load
  14. # only a subset of the samples.
  15. NBSAMPLES=256
  16. # Two random arrays with gaussian distribution
  17. data1=np.random.randn(NBSAMPLES)
  18. data2=np.random.randn(NBSAMPLES)
  19. # We normalize the data to ensure that the q31, q15 and q7 patterns won't
  20. # be already saturated.
  21. data1 = Tools.normalize(data1)
  22. data2 = Tools.normalize(data2)
  23. # The input patterns are written. The writeInput function of the config object is
  24. # doing a lot:
  25. # It is converting the float data to the right format (float, q31, q15 or q7)
  26. # depending on the config object.
  27. # It is generating a text file with the right format as recognized by the test framework
  28. # It is naming the file using the PATTERNDIR (defined below), the id (1 or 2 in this example)
  29. # and using "Input".
  30. #
  31. # So first file is named "Input1_f32.txt"
  32. config.writeInput(1, data1)
  33. config.writeInput(2, data2)
  34. # We compute the reference pattern
  35. ref = data1 + data2
  36. # Write reference is similar to writeInput.
  37. # The created file will be named "Reference1_f32.txt"
  38. config.writeReference(1, ref)
  39. # This function is generating patterns for all the types
  40. def generatePatterns():
  41. # We define the path to the patterns.
  42. # This path must be compatible with the folder directives used in the desc.txt
  43. # test description file.
  44. # By default, the root folder for pattern is Patterns and the root one for
  45. # Parameters is Parameters.
  46. # So both path defines in desc.txt are relative to those root folders.
  47. #
  48. # The last folder will be completed with the type.
  49. # So for instance we will get ExampleCategoryF32, ExampleCategoryQ31 ...
  50. PATTERNDIR = os.path.join("Patterns","Example","ExampleCategory","ExampleCategory")
  51. PARAMDIR = os.path.join("Parameters","Example","ExampleCategory","ExampleCategory")
  52. # config object for each type are created
  53. configf32=Tools.Config(PATTERNDIR,PARAMDIR,"f32")
  54. configq31=Tools.Config(PATTERNDIR,PARAMDIR,"q31")
  55. configq15=Tools.Config(PATTERNDIR,PARAMDIR,"q15")
  56. configq7=Tools.Config(PATTERNDIR,PARAMDIR,"q7")
  57. # Test patterns for each config are generated.
  58. # Second argument may be used to vary the content fo files
  59. # depending on the type.
  60. #
  61. # For instance, in Tools there is Tools.loopnb which can be used
  62. # like Tools.loopnb(format,Tools.TAILONLY)
  63. # It is giving a number of iterations corresponding to the case (Tail only, body only, body and tail)
  64. # Since the number of lanes depends on the type, testing vectorized code is requiring the use of
  65. # different lengths according to the type.
  66. writeTests(configf32,0)
  67. writeTests(configq31,31)
  68. writeTests(configq15,15)
  69. writeTests(configq7,7)
  70. # Useful to be able to use this file as a script or to import it from another script
  71. # and use the generatePatterns function
  72. if __name__ == '__main__':
  73. generatePatterns()