Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ufunc and function dispatchers #354

Merged
merged 9 commits into from May 9, 2022
Merged

Conversation

mhostetter
Copy link
Owner

@mhostetter mhostetter commented May 3, 2022

This PR creates ufunc and function dispatchers. These are classes that wrap a Python implementation of the ufunc or function. Depending on the field being used, either a JIT-compiled or pure-Python ufunc/function is returned. All the necessary input/output conversion and verification is done.

This cleans up the class clutter. It will also make adding GPU support easier and more convenient.

Additionally, JIT functions now use the lookup ufuncs (if configured). This has a massive speed up for small fields that use lookup tables. Previously, only explicit calculation was used in JIT functions (eg, polynomial multiplication, matrix multiplication, etc). The difference is most notable in GF(p^m) fields.

Before

In [1]: import galois

In [2]: GF = galois.GF(7**5)

In [3]: f = galois.Poly.Random(100, seed=1, field=GF)

In [4]: g = galois.Poly.Random(10, seed=2, field=GF)

In [5]: f * g
Out[5]: Poly(3419x^110 + 11806x^109 + 16253x^108 + 11624x^107 + 7122x^106 + 10439x^105 + 9726x^104 + 16156x^103 + 14118x^102 + 11739x^101 + 5479x^100 + 9674x^99 
+ 14884x^98 + 13054x^97 + 13987x^96 + 14889x^95 + 528x^94 + 4033x^93 + 5662x^92 + 5326x^91 + 16504x^90 + 4999x^89 + 3070x^88 + 2773x^87 + 14723x^86 + 15849x^85 + 13785x^84 + 11438x^83 + 5109x^82 + 463x^81 + 1497x^80 + 6584x^79 + 11814x^78 + 7924x^77 + 7464x^76 + 7160x^75 + 11391x^74 + 12445x^73 + 5249x^72 + 742x^71 + 9192x^70 + 14644x^69 + 7194x^68 + 3017x^67 + 16424x^66 + 1683x^65 + 11817x^64 + 10766x^63 + 10693x^62 + 13717x^61 + 4815x^60 + 10120x^59 + 15956x^58 + 16740x^57 + 3723x^56 + 9490x^55 + 13519x^54 + 9782x^53 + 309x^52 + 11633x^51 + 8786x^50 + 3733x^49 + 7210x^48 + 10879x^47 + 7836x^46 + 4835x^45 + 15762x^44 + 6813x^43 + 15021x^42 + 5566x^41 + 6210x^40 + 4221x^39 + 11845x^38 + 6928x^37 + 5771x^36 + 6221x^35 + 2355x^34 + 1316x^33 + 11194x^32 + 14704x^31 + 3411x^30 + 272x^29 + 16736x^28 + 15618x^27 + 6033x^26 + 13613x^25 + 11378x^24 + 61x^23 + 11063x^22 + 603x^21 + 2506x^20 + 13488x^19 + 702x^18 + 1595x^17 + 6065x^16 + 5784x^15 + 202x^14 + 16275x^13 + 4924x^12 + 1080x^11 + 462x^10 + 11150x^9 + 1046x^8 + 13376x^7 + 15037x^6 + 10448x^5 + 12167x^4 + 5285x^3 + 2705x^2 + 3725x + 6317, GF(7^5))

In [6]: %timeit f * g
1.34 ms ± 3.17 µs per loop (mean ± std. dev. of 7 runs, 1,000 loops each)

In [7]: pow(f, 123456789, g)
Out[7]: Poly(4190x^9 + 806x^8 + 5109x^7 + 10008x^6 + 8421x^5 + 3671x^4 + 16697x^3 + 4503x^2 + 1386x + 12192, GF(7^5))

In [8]: %timeit pow(f, 123456789, g)
35.1 ms ± 102 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)

In [9]: A = GF.Random((100, 100), seed=1)

In [10]: B = GF.Random((100, 100), seed=2)

In [11]: A @ B
Out[11]: 
GF([[13407,  9380, 11364, ...,  9141,  1002,  4655],
    [ 8455,  1277, 13016, ...,  3880, 15000, 12969],
    [ 3576,  9945,  2328, ...,  6984,  4358,  2169],
    ...,
    [12030,  3755,  8130, ...,  9588,  3756, 14957],
    [ 9023,  2430, 10462, ...,  5847,    96, 14494],
    [11625, 16576,  5221, ...,  8900, 12055,  4516]], order=7^5)

In [12]: %timeit A @ B
1.1 s ± 13.4 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

After

In [6]: %timeit f * g
68 µs ± 131 ns per loop (mean ± std. dev. of 7 runs, 10,000 loops each)

In [8]: %timeit pow(f, 123456789, g)
378 µs ± 1.09 µs per loop (mean ± std. dev. of 7 runs, 1,000 loops each)

In [12]: %timeit A @ B
17.3 ms ± 56.7 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

@mhostetter mhostetter added the performance Affects speed/performance label May 3, 2022
This means ufuncs don't reference other JIT functions, but other specific ufuncs. This provides more flexibility, and runtime speed. This comes at a slight increase in JIT compilation time during the first invocation.

JIT functions also reference specific ufuncs rather than general JIT
arithmetic functions. This allows for using lookup tables in many common
JIT functions, such as dense polynomial arithmetic.
@codecov
Copy link

codecov bot commented May 3, 2022

Codecov Report

Merging #354 (1e19b27) into master (bcc8cba) will increase coverage by 0.59%.
The diff coverage is 91.34%.

@@            Coverage Diff             @@
##           master     #354      +/-   ##
==========================================
+ Coverage   92.93%   93.52%   +0.59%     
==========================================
  Files          42       41       -1     
  Lines        5404     5265     -139     
==========================================
- Hits         5022     4924      -98     
+ Misses        382      341      -41     
Impacted Files Coverage Δ
galois/_polys/_primitive.py 99.19% <ø> (ø)
galois/_fields/_gf2.py 68.00% <64.44%> (-32.00%) ⬇️
galois/_domains/_meta.py 81.48% <81.48%> (ø)
galois/_domains/_array.py 95.42% <82.60%> (+4.86%) ⬆️
galois/_polys/_poly.py 90.82% <84.00%> (ø)
galois/_polys/_dense.py 87.31% <86.80%> (-10.47%) ⬇️
galois/_domains/_ufunc.py 89.47% <89.09%> (-2.15%) ⬇️
galois/_domains/_function.py 83.88% <89.31%> (+0.07%) ⬆️
galois/_lfsr.py 89.60% <89.41%> (+5.23%) ⬆️
galois/_codes/_bch.py 97.60% <95.45%> (-0.46%) ⬇️
... and 13 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update bcc8cba...1e19b27. Read the comment docs.

@mhostetter mhostetter force-pushed the optimize-jit-functions branch 4 times, most recently from 7296a28 to f395465 Compare May 9, 2022 18:15
@mhostetter mhostetter changed the title Optimize JIT functions Add ufunc and function dispatchers May 9, 2022
@mhostetter mhostetter merged commit 2078aa9 into master May 9, 2022
@mhostetter mhostetter deleted the optimize-jit-functions branch May 9, 2022 20:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
performance Affects speed/performance
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant