![]() ![]() ![]() However, functionality like this can’t work magic: there are fundamental limitations on what’s possible with Python based on how the language itself is designed. Libraries like PyTorch have been developing increasingly ingenious ways to deal with these performance problems, with the newly released PyTorch 2 even including a compile() function that uses a sophisticated compilation backend to create high performance implementations of Python code. There are some clunky ways to write parallel code which uses more than one core, but they either have to work on totally separate memory (and have a lot of overhead to start up) or they have to take it in turns to access memory (the dreaded “global interpreter lock” which often makes parallel code actually slower than single-threaded code!) Nowadays we all have computers with lots of cores, but Python generally will just use one at a time. We also have to deal with the lack of effective parallel processing in Python. This means there’s a lot more library functions to implement and remember, and you’re out of luck if you’re doing anything even slightly non-standard because there won’t be a fused version for you. So instead we have to write special “fused” versions of common combinations of functions (such as a linear layer followed by a rectified linear layer in a neural net), and call these fused versions from Python. One major issue is the lack of “fusion” – that is, calling a bunch of compiled functions in a row leads to a lot of overhead, as data is converted to and from python formats, and the cost of switching from python to C and back repeatedly must be paid. There are also unavoidable performance problems, even when a faster compiled implementation language is used for a library. The two-language problem means that the tools that Python programmers are familiar with no longer apply as soon as we find ourselves jumping into the backend implementation language. The same problem occurs when trying to debug code or find and resolve performance problems. So difficulties learning and problems for experienced devs just as much as it is for students starting out. All coders are learners (or at least, they should be) because the field constantly develops, and no-one can understand it all. Instead of being able to step into the implementation of an algorithm while your code runs, or jump to the definition of a method of interest, instead you find yourself deep in the weeds of C libraries and binary blobs. The two-language problem gets in the way of learning. It’s very hard to profile or debug the deployment version of the code, and there’s no guarantee it will even run identically to the python version. But these deployment approaches can’t support all of Python’s features, so Python programmers have to learn to use a subset of the language that matches their deployment target. For instance, AI models often have to be converted from Python into a faster implementation, such as ONNX or torchscript. Nearly all AI models today are developed in Python, thanks to the flexible and elegant programming language, fantastic tools and ecosystem, and high performance compiled libraries.īut this “two-language” approach has serious downsides. Libraries like Numpy and PyTorch provide “pythonic” interfaces to high performance code, allowing Python programmers to feel right at home, even as they’re using highly optimised numeric libraries. So Python programmers learn to avoid using Python for the implementation of performance-critical sections, instead using Python wrappers over C, FORTRAN, Rust, etc code. However, Python has a trick up its sleeve: it can call out to code written in fast languages. This makes it impractical to use Python for the performance-sensitive parts of code – the inner loops where performance is critical. But Python is many thousands of times slower than languages like C++. But it comes with a downside: performance.Ī few percent here or there doesn’t matter. This approach means that Python can (and does) do absolutely anything. It has an elegant core, on which everything else is built. Python is the language that I have used for nearly all my work over the last few years. Before I explain why I’m so excited about Mojo, I first need to say a few things about Python.
0 Comments
Leave a Reply. |