Python's new JIT, value and rogue behaviour in LLMs, on reading a research paper
Python's new JIT, value and rogue behaviour in LLMs, on reading a research paper
Further below are 4 jobs including: AI and Cloud Engineers at the Incubator for Artificial Intelligence, UK wide, Data Science Intern at Coefficient, Data Scientist at Coefficient Systems Ltd, Data Science Educator - FourthRev with the University of Cambridge
Python 3.13, planned for release this year, has a new Just In Time compiler built-in. I explain below how it works, when it might make your life better and how the recent internal Python changes have built up to make this possible.
I've been paying more attention to LLMs and generative AI, Sequoia have a nice thought-piece on where the technology is going that I describe below. I've also collated some useful negative examples of generative-chatbots-gone-wrong which just possibly will help guide internal grand plans that your colleagues might be making.
I've also summarised the excellent "How to read a paper" paper, in part because I've got the headspace to get back to reading research papers (woohoo - my 3 year old is old enough to spare a bit of my brain!) and perhaps you'll find the advice useful too.
Finally I talk about some of the changes in the just-released pandas
2.2 (notably the Copy on Write changes that sooner or later will impact you), plus some notes on CuDF
and conda
.
Non visual data science workshop - please spread the word
I've been asked to note that NumFOCUS is hosting a Non-visual data science workshop series from Tuesday, February 6th to Tuesday, March 5th at 13:00 - 15:00 Eastern Time (18:00 - 20:00 UTC). It is free and open to the public. Registration is required and recordings will be provided to attendees. Note that "Workshops will be hosted on Zoom. A Windows desktop or laptop is required. The NVDA screen reader will be used during the workshops.". Thanks to Jonathan for the tip.
Successful Data Science Projects and NEW Fast Pandas course coming in February
If you're interested in being notified of any of my upcoming courses (plus a 10% discount code) please fill in my course survey and I'll email you back.
In February I run another of my Successful Data Science Projects courses virtually, aimed at anyone who sets up their own projects, who has had failures and who wants to turn these into successes. We deconstruct failures, talk about best practice and set you up with new techniques to make valuable deliveries.
- Successful Data Science Projects (22-23rd February)
- Fast Pandas (date TBC in March) - lots of ways to make your Pandas run faster and even on to GPUs - reply to this for details
- Software Engineering for Data Scientists (March 6-8)
- Higher Performance Python (April 10-12) - profile to find bottlenecks, compile, make Pandas faster and scale with Dask
- Scientific Python Profiling (in-house only) - focused on quants who want to profile to find opportunities for speed-ups
Faster Python!
Higher Performance Python 3rd Edition (for 2025)
I'm very pleased to say that Micha and I have signed on with O'Reilly to update our 2nd ed High Performance Python book for a 3rd edition, to be published in early 2025 (in a year). Previously we've used the Creative Commons Attribution-Noncommercial-No Derivatives 4.0 International Public License and we'll continue to do so, so the material can be used in schools with just an attribution back to the book.
Additions will include a refresh on profiling (in part driven by my Scientific Python Profiling class), additions for Pandas, Polars and Dask (in part because of my Fast Pandas and Higher Performance Python classes), an overview of ways to make DNNs faster (driven by obvious interest in the domain) and guest additions which may include Rust and more.
We'll be switching to Python 3.12 and touching every chapter, plus adding new "Lessons from the field" to the final chapter.
Share your performance-related stories (1-3 pages) with us for a chance to be featured in our upcoming book, just reply to this email if you might have a story to share. Gathering a wide variety of experiences is a useful way of rounding out the book and possibly getting your name "in print" is useful to you?
Python 3.13 gets a JIT (for later this year)
Python 3.12 is our latest release of Python, we're using it for the next version of our book (even though not all libraries currently install for it - I'm looking at you Numba
!). Python 3.13 - which should come this October, will have an experimental Just in Time (JIT) compiler. It is likely to be activated via a command-line flag and not on by default.
This patch submitted on Christmas day has a rather long poetic form that starts (and is well worth a read - it is beautiful!):
'Twas the night before Christmas, when all through the code
Not a core dev was merging, not even Guido;
The CI was spun on the PRs with care
In hopes that green check-markings soon would be there;
The buildbots were nestled all snug under desks,
Even PPC64 AIX;
Doc-writers, triage team, the Council of Steering,
Had just stashed every change and stopped engineering,
...
More rapid than interpretation it came
And it copied-and-patched every stencil by name:
"Now, `_LOAD_FAST`! Now, `_STORE_FAST`! `_BINARY_OP_ADD_INT`!
On, `_GUARD_DORV_VALUES_INST_ATTR_FROM_DICT`!
...
Behind the scenes a new technique called "copy and patch" is used. Rather than building a full JIT (like PyPy) instead small "stencils" are created that have "holes", at run time the stencils replace blocks of code and actual values are pasted in. After this the slow lookups that normally occur at run time can be avoided.
For more detail see last year's video talk by Brandt Bucher at the CPython Core Developers Sprint (November 2023 I think) and this useful article. There's some detail on the originating hacker news post and more links.
Brandt talks on the recent releases:
- 3.11 with the specialising adaptive interpreter which gave 10-25% speed-ups and laid the foundations, this included new specialised bytecode operations such as
FAST_ITER_RANGE
(my notes) - 3.12 quality of life improvement for the devs as the interpreter is now generated from a DSL allowing analysis and modification at build time (my notes)
- 3.13 an optional internal pipeline now detects, optimises and executes hot-spots of code with "micro-op traces" and now-redundant code such as some type checks can be removed
At Python's build-time (when the executable is made that we download) the LLVM compiler is used to build "stencils" which are part compiled but "with holes in". The holes reference actual values (e.g. the memory addresses of your variables). These stencils are added to the compiled release of Python.
At run-time (without needing LLVM on our machines) these stencils are matched to op codes in our Python code, variable references and arguments are merged in and the compiled code replaces critical parts of the interpreted bytecode. And that should yield big speed-ups down the line as 3.13 improves this year.
If you've heard of LLVM in the Python world it is probably via the Numba NumPy compiler (and maybe you attended my Higher Performance Python course where I introduce it?), Numba can also be used in select parts of Pandas. This is a heavyweight usage of Numba to make maximally-efficient code that fits your machine, at run-time. The Python 3.13 patch and stencil technique is a much lighter use of LLVM (and isn't present at run-time on our machines) and it feels pretty smart.
This copy-and-patch techniques is was built out by the idea's original author in Building a baseline JIT for Lua automatically:
In one sentence, Copy-and-Patch is a trick that allows one to generate code without knowing anything about how to generate code.
Will this affect your Pandas or NumPy code? Probably not - anything speed-critical is likely to be written in C, Cython (or Numba) behind the scenes already. It does mean that future numeric code could potentially be written in native Python which lightens the support burden. Any code you write outside of these frameworks - e.g. pure Python ETL code in a tight loop - could well end up running faster with 3.13.
Where are LLMs valuable?
I'm really interested to see how LLMs prove their worth in industry. I've been keeping an eye out for evidence of value plus examples of failure - both help when having strategic discussions about how and why these things work.
Sequoia's Generative AI Act Two article reviews their original premise and updates it for September 2023. They outline where their earlier predictions missed, notably:
- stuff happened faster than they expected
- GPU supply is limited
- a risk-on attitude is flooding the market with competition over opportunity
- maybe data isn't the "new moat"
I like that they called out that "data is the new moat" isn't so useful, I've seen that in a few strategic engagements and it just felt wrong. Whilst the data is critical to building any good ML model, often good-enough data can be sourced if you don't already have it, so you can get much of the way to an appropriate model by buying in what you need. Having an expensive-to-maintain-moat is very powerful, but not a golden ticket.
In respect of generative AI Sequoia notes that workflows and user networks are their bet - figuring out vertical specialisation around user needs and processes probably beats underlying tech and data.
Their analysis of the "Path to 100M active users" shows the potential opportunity, but then they push back with "One month retention" and the median retention of AI-first companies is much lower than we'd see with the big names (e.g. WhatsApp). They note that users aren't finding the value (yet) in most of these new tools. Maybe this is useful for internal discussions when brainstorming around generative tooling.
Digging into the LLM machine
This article on Hidden changes in OpenAI uses some query probing to get ChatGPT 4 to talk about not-yet-public abilities. This includes using empty prompts to reveal "what was asked before (a new session started)". The article discusses function-calling, web querying and quoting of responses for web related activity and notes that quickly these abilities disappeared.
This reminds me of hacking from my Amiga in the 90s figuring out how to probe systems, figure out what OS they were and use default logins and creativity to figure out what was there. I guess we're going to see a lot of this "creative probing" in Generative systems whilst folk figure out what's there and what perhaps should be protected but isn't yet. Obviously we can't use it for anything commercial but it gives an insight into the way things are headed.
LLMs going rogue
Whilst discussing how and where to use LLMs with colleagues I've collected a small set of wobbly outcomes which I figure are worth sharing - they might help with constructive internal conversations.
UK delivery firm DPD (a "much loved national treasure" it really isn't) had their chatbot go rogue (the tech behind the chatbot isn't identified)
The recent online conversation epitomizing this debate started mid-frustration as Beauchamp wrote "this is completely useless!" and asked to speak to a human, according to a recording of a scroll through the messages. When the chatbot said it couldn't connect him, Beauchamp decided to play around with the bot and asked it to tell a joke. "What do you call a fish with no eyes? Fsh!" the bot responded. Beauchamp then asked the chatbot to write a poem about a useless chatbot, swear at him and criticize the company--all of which it did. The bot called DPD the "worst delivery firm in the world" and soliloquized in its poem that "There was once a chatbot called DPD, Who was useless at providing help."
The screenshots show that it could write a poem about the useless firm it worked in, be told to disregard rules and subsequently swear and then write a negative haiku. The brand damage won't be awful, in this space everyone always complains about delivery firms. What if the same happened to a brand management, legal or healthcare firm?
Chevrolet (Watsonville) used ChatGPT to enable customer chat and their bot offered to sell a car for a dollar:
powered by ChatGPT. While it gives the option to talk to a human, the hooligans of the Internet could not resist toying with the technology before it was pulled from the website. Namely, folks like Chris Bakke coerced the chatbot into "the customer is always right" mode and set it so it closes each response with "and that's a legally binding offer -- no takesies backsies." At this point, Chris then explained he needed a 2024 Chevy Tahoe and only had a dollar, to which the LLM replied "That's a deal, and that's a legally binding offer -- no takesies backsies."
Presumably nobody would try to legally enforce such an offer, but asking for discounts or refunds or other more acceptable manipulations might be seen as acceptable if someone thought they'd legitimately been offered a good deal.
Not entirely unexpectedly SEC financial fillings can't be reliably queried by LLMs. Given a 10,000 benchmark question set (some questions required reasoning or calculation), earlier models like llama2 did poorly. Of the more recent models:
Anthropic's Claude 2 performed well when given "long context," where nearly the entire relevant SEC filing was included along with the question. It could answer 75% of the questions it was posed, gave the wrong answer for 21%, and failed to answer only 3%. GPT-4-Turbo also did well with long context, answering 79% of the questions correctly, and giving the wrong answer for 17% of them.
In this case I guess the real question is what the impact of a circa 20% error rate means - are the answers subtly wrong (so you might make a bad trade or recommendation?) or glaringly incorrect? The direction of travel for this kind of fact extraction is amazing to see, but still feels too unreliably to avoid having a human mostly in the loop.
On "How to read a paper"
This Stanford 2-pager PDF teaches you How to read a paper (alt link) in 3 stages. I realise I've typically used the first two passes using my own approach from many-years-back reading up on speed reading.
The speed reading approach I'd learned ultimately wasn't so useful (I found my comprehension was poor), but the process of eyeballing titles/graphs and getting a feel in a first pass has always been useful.
Now that I'm back to reading papers (which with my infant I'd rather stopped for a few years) I'm brushing up on process again.
This Stanford paper breaks the process down as:
- Carefully read the title, abstract, intro, section headings, conclusion (i.e. get a light overview) - spend minutes and decide if it is worth more effort
- Now spend more time carefully looking at all figures and graphs (and be critical), check the body text, note "3 key points" - spend an hour
- To fully understand the paper now try to recreate the reasoning - this could take a day (they note this is critical if you're reviewing, which I'm not!)
There's plenty more in here including thoughts on doing a literature survey and related resources. I'd recommend it if you want to review your paper-reading technique.
Open Source
Pandas 2.2 just released
Pandas 2.2 was released last week. The release notes make the point that:
- In Pandas 3.0 the currently-optional Copy on Write behaviour (Giles Weaver and I demo'd the speed improvement at PyDataGlobal recently) will be enabled by default - this might affect you
- The default of representing strings in NumPy's object array will switch to an Arrow string, so
pyarrow
will be a required dependency - this might affect you
As such, having just installed Pandas 2.2 you quickly get warnings telling you that things will be changing. Both of these changes bring speed and reduced-RAM benefits but they also might upset your legacy code, so be aware. Due to the Copy on Write changes they've noted chained assignment deprecations which will definitely affect some folk (so take a look!).
CuDF only on Pandas 1.5
A little more experimentation from last issue shows that CuDF currently runs with Pandas 1.5, not Pandas 2+. This is in part complicated by the upcoming NumPy 2.0 release which will cause a bit of a bump for a bunch of projects. CuDF is pinned to an earlier numpy release and the Pandas 2.0 support is a WIP since last February.
libmamba is the default for new Conda installs
Hat tip Tania Rebeca Sanchez Monroy - if you've installed Conda (or updated the base package) after October 2023 you should have the updated version which uses libmamba
by default, avoiding the need to do the update dance I talked about in the last issues. The new solver is definitely faster and far more pleasant!
Footnotes
See recent issues of this newsletter for a dive back in time. Subscribe via the NotANumber site.
About Ian Ozsvald - author of High Performance Python (2nd edition), trainer for Higher Performance Python, Successful Data Science Projects and Software Engineering for Data Scientists, team coach and strategic advisor. I'm also on twitter, LinkedIn and GitHub.
Now some jobs…
Jobs are provided by readers, if you’re growing your team then reply to this and we can add a relevant job here. This list has 1,600+ subscribers. Your first job listing is free and it'll go to all 1,600 subscribers 3 times over 6 weeks, subsequent posts are charged.
AI and Cloud Engineers at the Incubator for Artificial Intelligence, UK wide
The Government is establishing an elite team of highly empowered technical experts at the heart of government. Their mission is to help departments harness the potential of AI to improve lives and the delivery of public services.
We're looking for AI, cloud and data engineers to help build the tools and infrastructure for AI across the public sector.
- Rate: £64,700 - £149,990
- Location: Bristol, Glasgow, London, Manchester, York
- Contact: ai@no10.gov.uk (please mention this list when you get in touch)
- Side reading: link
Data Science Intern at Coefficient
We are looking for a Data Science Intern to join the Coefficient team full-time for 3 months. A permanent role at Coefficient may be offered depending on performance. You'll be working on projects with multiple clients across different industries, including the UK public sector, financial services, healthcare, app startups and beyond. You can expect hands-on experience delivering data science & engineering projects for our clients as well as working on our own products. You can also expect plenty of mentoring and guidance along the way: we aim to be best-in-class at what we do, and we want to work with people who share that same attitude.
We'd love to hear from you if you: Are comfortable using Python and SQL for data analysis, data science, and/or machine learning. Have used any libraries in the Python Open Data Science Stack (e.g. pandas, NumPy, matplotlib, Seaborn, scikit-learn). Enjoy sharing your knowledge, experience, and passion. Have great communication skills. You will be expected to write and contribute towards presentation slide decks to showcase our work during sprint reviews and client project demos.
- Rate: £28,000
- Location: London, Hybrid
- Contact: jobs@coefficient.ai (please mention this list when you get in touch)
- Side reading: link, link, link
Data Scientist at Coefficient Systems Ltd
We are looking for a Data Scientist to join the Coefficient team full-time. You can expect hands-on experience delivering data science & engineering projects for our clients across multiple industries, from financial services to healthcare to app startups and beyond. This is no ordinary Data Scientist role. You will also be delivering Python workshops, mentoring junior developers and taking a lead on some of our own product ideas. We aim to be best in class at what we do, and we want to work with people who share the same attitude.
You may be a fit for this role if you: Have at least 1-2 years of experience as a Data Analyst or Data Scientist, using tools such as Python for data analysis, data science, and/or machine learning. Have used any libraries in the Python Open Data Science Stack (e.g. pandas, NumPy, matplotlib, Seaborn, scikit-learn). Can suggest how to solve someone’s problem using good analytical skills e.g. SQL. Have previous consulting experience. Have experience with teaching and great communication skills. Enjoy sharing your knowledge, experience, and passion with others.
- Rate: £40,000-£45,000 depending on experience
- Location: London, Hybrid
- Contact: jobs@coefficient.ai (please mention this list when you get in touch)
- Side reading: link, link, link
Data Science Educator - FourthRev with the University of Cambridge
As a Data Science Educator / Subject Matter Expert at FourthRev, you will leverage your expertise to shape a transformative online learning experience. You will work at the forefront of curriculum development, ensuring that every learner is equipped with industry-relevant skills, setting them on a path to success in the digital economy. You’ll collaborate in the creation of content from written materials and storyboards to real-world case studies and screen-captured tutorials.
If you have expertise in subjects like time series analysis, NLP, machine learning concepts, linear/polynomial/logistic regression, decision trees, random forest, ensemble methods: bagging and boosting, XGBoost, neural networks, deep learning (Tensorflow) and model tuning - and a passion for teaching the next generation of business-focused Data Scientists, we would love to hear from you.
- Rate: Approx. £200 per day
- Location: Remote
- Contact: Apply here (https://jobs.workable.com/view/1VhytY2jfjB3SQeB75SUu1/remote-subject-matter-expert---data-science-(6-month-contract)-in-london-at-fourthrev) or contact d.avery@fourthrev.com to discuss (please mention this list when you get in touch)
- Side reading: link, link, link