11 ms·
Accelerate Python code by importing Taichi
- NotYourLawyer 4y agoSounds kind of like the old psyco, but with GPU support.
- sitkack 4y agoOr JAX
- 19h 4y agoPretty sure isPrime can't simply be replaced with k % 2 == 0 ;)
- MobiusHorizons 4y agoWhat makes you think that it was?
- cyphar 4y agoThe indentation is broken, so one possible way to interpret it is: def is_prime(n: int): result = True for k in range(2, int(n ** 0.5) + 1): if n % k == 0: result = False break return result Which is just a more complicated (n % 2 != 0). Obviously the break should be in the if block.
- deleted 4y ago[deleted]
- coldpie 4y agoGreat, now you tell me. * Tosses doctoral thesis in the trash.
- graton 4y agoWouldn't also need to change the function name to `isEven`?
- deleted 4y ago[deleted]
- tzot 4y agoYes, the identation is incorrect in the blog post (running the code would throw an IndentationError anyway); it's correct in the github code though.
- speps 4y agoI thought it was a parsing issue in Python when doing "import taichi as ti" vs "import taichi". No it's just presenting Taichi, a Python package to do parallel computation. EDIT: title of the thread was "Accelerate Python code 100x by import taichi as ti" like TFA
- rjh29 4y agoMe too - it wouldn't be unheard of in a language where referencing multiple.levels.of.variable in a loop is orders of magnitude slower than doing "a = multiple.levels.of.variable" outside the loop and referencing a inside of it. *may have been fixed in recent versions of Python - I heard of this many years ago!
- stingraycharles 4y agoIsn’t that expected behaviour, as you’re only looking up “a” once when you do it outside the loop, while doing it every time when inside the loop? Because any reference in the whole hierarchy could change during the looping (e.g. one could say “multiple.levels = {}” at some point), the interpreter really would need to check it every time unless it can somehow “prove” that these changes will never happen / haven’t happened. Just keeping a reference to “a” is semantically very different, and I’d consider that a normal optimisation.
- 20after4 4y agoThe issue is just how slow python is, it takes a very long time to resolve those references. So it's expected that the multiple.levels.of.dereference would be slower but it's perhaps orders of magnitude slower where in another language it might be much less of a performance hit.
- stingraycharles 4y agoMaybe, but then the loop has nothing to do with it, and the problem is rather the slow lookups.
- 4y ago
- okasaki 4y agoWould be even more interesting if the examples were also implemented in C.
- v3ss0n 4y agoHow many time this has been posted.
- bragr 4y ago6 times: https://news.ycombinator.com/from?site=taichi-lang.org https://news.ycombinator.com/from?site=taichi-lang.org
- forgotpwd16 4y agoThis shows everything from the domain. The specific article was posted once but went unnoticed.
- kburman 4y ago
- bragr 4y ago> I am a loyal C++/Fortran user but would like to try out Python as it is gaining increasing popularity. However, rewriting code in Python is a nightmare - I feel the performance must be more than 100x slower than before! I think I found your problem. TBH you might like Julia more than Python and you won't have to invent a new DSL in the process.
- stackbutterflow 4y agoPython is rising in popularity? I can't remember the last time I reached to Python for something. Not that it says anything about the language but it kinda disappeared from my bubble.
- azinman2 4y agoIt’s the first thing I’ll pick for most problems.
- ShamelessC 4y agoPython is extremely popular, yes.
- BerislavLopac 4y agoPython is pretty much the second best available language for everything.
- mountainriver 4y agoEver heard of machine learning?
- visarga 4y agoDoes it support dicts? I guess not
- ipunchghosts 4y agoHow does this differ from numba?
- garyrob 4y agoTaichi vs. Numba: As its name indicates, Numba is tailored for Numpy. Numba is recommended if your functions involve vectorization of Numpy arrays. Compared with Numba, Taichi enjoys the following advantages: Taichi supports multiple data types, including struct, dataclass, quant, and sparse, and allows you to adjust memory layout flexibly. This feature is extremely desirable when a program handles massive amounts of data. However, Numba only performs best when dealing with dense NumPy arrays. Taichi can call different GPU backends for computation, making large-scale parallel programming (such as particle simulation or rendering) as easy as winking. But it would be hard even to imagine writing a renderer in Numba.
- make3 4y agodid they show benchmark to support that it does better
- brrrrrm 4y agohow does this compare to a normal JIT compiled language? Hacking on performance to Python seems really brittle
- brrrrrm 4y agoTo answer the question, JavaScript is 31x faster out of the box on size 1000000 (compared to the 6x claimed in the post). 71x on 10M. bwasti@bwasti-mbp code % time python3 prime.py 78498 python3 prime.py 3.86s user 0.02s system 98% cpu 3.938 total bwasti@bwasti-mbp code % time bun prime.js 78498 bun prime.js 0.07s user 0.02s system 74% cpu 0.125 total
- zeugmasyllepsis 4y agoI was curious about node/jitless node versus python/pypy: src time node primes.js 78498 node primes.js 1.75s user 0.05s system 102% cpu 1.761 total src time node --jitless primes.js 78498 node --jitless primes.js 5.06s user 0.04s system 100% cpu 5.088 total src time python primes.py 78498 python primes.py 4.27s user 0.03s system 100% cpu 4.276 total src asdf shell python pypy3.9-7.3.9 src time python primes.py 78498 python primes.py 0.91s user 0.07s system 100% cpu 0.970 total Actually relatively on par with one another.
- jokoon 4y agoquestion to OP: how faster can the code in those SO answers be? https://stackoverflow.com/questions/73473074/speed-up-set-partition-generation-by-skipping-ones-with-subsets-smaller-or-large/73549333#73549333 https://stackoverflow.com/questions/73473074/speed-up-set-pa... This code is recursive and generate set partitions for large N values (N larger than 12), it essentially works by skipping small partitions and small subsets to target desirable set partitions. Solutions that don't skip those suffer from "combinatory explosion". I did not write this code, I want to test it later with taichi, but I'm curious if taichi can run this faster.
- jesuslop 4y agoI now get that Reaction-Diffusion business. 1) "Diffussion" is species vs time equals species spatial laplacian. 2) The "reaction" equations are non-painfully derived from Baez stochastic Petri nets/chemical reaction networks in [1] (species vs time = multivariate polynomial in species, "space dependant rate equation") So Reaction-Diffusion is just adding up. Species vs time = species spatial laplacian plus multivariate polynomial in species. One more for the toolbox! [1] https://arxiv.org/abs/1209.3632 https://arxiv.org/abs/1209.3632
- astroalex 4y agoI'm interested in understanding this comment but I don't know where to start. I love the way Reaction Diffusion simulations look and I've coded it up a few times. But I don't understand what you mean by "species vs time". (Some of the other technical language seems more Google-able, but "species vs time" isn't turning up anything obvious.)
- soVeryTired 4y agoI think GP is referring to the heat equation. "species" is the concentration of the "stuff" that you're describing mathematically. Call that u(x), where is a spatial coordinate and u is a real valued function. Then the diffusive part says du(x, t)/dt = \nabla_x u(x, t). The \nabla term is the laplacian: a multivariate form of the second derivative. The equation says that a short time from now, u(x, t) will change in proportion to the average value of u, calculated over a small ball surrounding the point x, minus the value of u at the point x itself. If there's less "stuff" in the points that neighbour x than at x itself, the function will decrease over time. Similarly if there's more stuff at the neighbours of x, u(x, t) will increase. This is the basis of diffusive behaviour. (Edit: I think the equation in the article is wrong, unless I've misunderstood something: they have a delta (first derivative) when they should have a nabla (laplacian))
- xkevio 4y agoI think you might have gotten your symbols mixed up a litte. From what I understand, just \nabla(f) describes the gradient of a function f -- meaning all the first order partial derivatives (of a certain point) in a vector. \nabla * f describes the divergence of the function f -- meaning a scalar field of the quantity of the vector field's sources at each point. \nabla * \nabla(f), divergence of gradient, then describes the Laplace operator. Also written as \nabla^2(f) or \Delta(f) -- note that it's an uppercase delta.
- tomaszsobota 4y agoI enjoyed learning about Reaction-Diffusion from this article more than actually finding out I can run them 93475x faster with Taichi ;D
- salty_biscuits 4y agoInterestingly enough, Alan Turing is originator of the study of reaction diffusion equations in biology https://www.semanticscholar.org/paper/The-chemical-basis-of-morphogenesis-Turing/d635e2843c6fb034e9126aa73ef9c2e4e2c4714d https://www.semanticscholar.org/paper/The-chemical-basis-of-...
- tomthe 4y agoUnfortunately, phytran is missing in the comparison. Phytran works in a lot of cases and it easy to use by just using python types. I would like to see a comparison with taichi, as taichi also seems to be interesting.
- mkl 4y agoI'm pretty sure you mean Pythran. That's been disappointing in my experiments with it. Nuitka is another one that's missing.
- cycomanic 4y agoWhat do you mean disappointing? I have consistently been getting the best results with pythran. That said it is strongly focused on numerical code, so your milage might vary for other code. You also should add compiler optimisation flags to get the best performance. Regarding Nutika AFAIK its goal is not a speed up and performance gains are pretty modest in most cases.
- mkl 4y agoMaybe I didn't have appropriate compiler flags. I didn't devote much time to it, as it didn't seem promising from my initial attempts and I found it easier to get faster performance with other methods (see reply to sibling comment). This was integer numerical code filling in a 2D array with lots of individual comparisons and backtracking (no whole-array operations). Terrible for plain Python, but Cython and C ate it up.
- tomthe 4y agoYes, I mean Pythran ( https://github.com/serge-sans-paille/pythran https://github.com/serge-sans-paille/pythran ). Thank you. Was Nuitka better? Pythran is quite simple to install and use in Jupyter.
- mkl 4y agoFor the most recent application I tried Pythran on, significantly annotated Cython was the winner (compared to plain Python, Numba, Pythran, and more readable Cython). Plain Python was the slowest, and Pythran was much slower than the others. I didn't try Nuitka for it. I ended up rewriting the key code in C anyway, which was faster still. This was integer numerical code filling in a 2D array with lots of comparisons and backtracking.
- IanCal 4y agoI thought I recognized the name. Taichi also is linked with differentiable programming https://docs.taichi-lang.org/docs/differentiable_programming https://docs.taichi-lang.org/docs/differentiable_programming An extremely interesting area. I keep wanting to use it for something but haven't had a good use case yet, nor frankly do I think I really understand it.
- jack_pp 4y agoI'll take this opportunity to point out that if you're doing anything numpy related that seems too slow you should run numba on it, in my case we were doing a lot of cosine distance calculations and our inference time sped up 10x by simply running the cosine distance function from numpy through numba and it's as easy as adding a decorator.
- krastanov 4y agoDo jax/tensorflow/pytorch work with numba? I.e. can you pass one of their arrays through a numba function and have it (a) not crash (b) support backprop?
- p1esk 4y agoSeems like pytorch does: https://stackoverflow.com/questions/63169760/how-can-i-use-numba-for-pytorch-tensors https://stackoverflow.com/questions/63169760/how-can-i-use-n...
- krastanov 4y agoIt seems it would not crash if you use some interface function, but it does not seem to support autodiff.
- Siira 4y agoJax already has its own tracing compiler which produces optimized code.
- learndeeply 4y agoThis is mentioned directly in the article: Taichi vs. Numba: As its name indicates, Numba is tailored for Numpy. Numba is recommended if your functions involve vectorization of Numpy arrays. Compared with Numba, Taichi enjoys the following advantages: Taichi supports multiple data types, including struct, dataclass, quant, and sparse, and allows you to adjust memory layout flexibly. This feature is extremely desirable when a program handles massive amounts of data. However, Numba only performs best when dealing with dense NumPy arrays. Taichi can call different GPU backends for computation, making large-scale parallel programming (such as particle simulation or rendering) as easy as winking. But it would be hard even to imagine writing a renderer in Numba.
- skykooler 4y agoThis page is unreadable if your system theme is dark mode - dark grey text on a black background.
- insane_dreamer 4y agoImpressive performance gains. any Taichi v Julia benchmarks?
- ChrisRackauckas 4y agoThe only ones that I know of are in https://arxiv.org/abs/2012.06684 https://arxiv.org/abs/2012.06684 (with Julia DifferentialEquations.jl and DiffTaichi), but those are more algorithmic. There, Julia does extremely well, but the conclusion that I would draw from it is more: use a programming language with robust and differentiable differential equation solvers rather than writing simple Euler loops by hand (as this article does).
- stevenhuang 4y agoIs this doing automatic memoization or actually emitting optimized JIT?
- cyber_kinetist 4y agoIt’s an AOT compiler that compiles a Python DSL to assembly via LLVM.
- windsignaling 4y agoSlightly off topic but the choice of name is interesting given that Tai chi is well-known for its slow movements and being practiced by the elderly at the park.
- user5678 4y ago
- tiagod 4y agoI think it might have to do with making your code faster without touching it
- wiz21c 4y agoI practice tai chi and I'm not elderly (though not exactly young either :-) ) tai chi is actually very hard to do well because it requires a lot of flexibility, I mean a lot).
- Bimos 4y agoYou should see what Taichi lessons in Chinese colleges look like: full with students who like no kinds of sports but must choose a PE class, and yes, I was one of them.
- xiaodai 4y agoObligatory metnion of Julia. BAE
- catlifeonmars 4y ago> Yet its [python] easy readability comes at the cost of performance. This does not sound correct. Why would there be a tradeoff between readability and performance?
- KronisLV 4y agoThis probably just refers to the current reality of using Python in many cases. If you want it's readability, you'll (most likely) need to deal with it's relatively slow runtime. That said, the readability itself shouldn't have much to do with the performance, as many reimplementations of the Python runtime already show.
- Retr0id 4y agoIn python, and many other languages, there are plenty of performance hacks you can apply that might make your code faster, but simultaneously less readable - often by stripping away levels of abstraction, or restructuring your code/data in unintuitive ways. For example, the prime-counting example in the article could've been optimised slightly by inlining the is_prime function into the body of the count_primes function (thus removing the overhead of calling a function).
- BerislavLopac 4y ago"Readability" is solely a function of familiarity.
- klibertp 4y agoIn most part, but not only. There are biological constraints on how the eye can move, and there are cognitive barriers you cannot overcome (how many "things" you can hold in your working memory at once). The code can be extremely familiar, but still hard to read, if it's too dense, too sparse, too long, involves too many concepts, and so on. You should still optimize the code you write along these axes to make it readable. It's true, however, that most of what people call "readability" is actually just familiarity - whether you should optimize the code for those unfamiliar with the language/domain/idiom is a separate question (the answer being: it depends).
- thyrsus 4y agoMy ansible-pull runs are horribly slow (git pull is fast, running a few hundred tasks on the local system is not). I suspect the problem is in unnecessary copies of large dicts and lots of them. How would I confirm or refute that? If that is the problem, what techniques are available to address it?
- bigbogpytgob 4y ago
- bigbogpytgob 4y ago
- niemandhier 4y agoI am missing the 'about us' part of the website. Which entity is behind this? Usually you have some details on the genesis of projects like this. I will never get approval to use this on the HPC systems of my org if its developed by an unknown entity in China ( as the wechat link might indicate ), which is sad since it looks useful.
- niemandhier 4y agoI found https://taichi.graphics/ https://taichi.graphics/ which is the related company. On their website there is also no obvious info about where they are incorporated. Crunchbase lists their headquarter as Beijing.