Could Vintage Computers Do AI?

The now-famous 2017 paper “Attention Is All You Need” kicked off the Large Language Model revolution by describing a very effective algorithm for Artificial Intelligence. With it (and a lot of fast hardware), we have developed language models that can do things that, just five or ten years ago, we would never have imagined computers doing. We are now able to ask these models, in plain human language, to create programs for us — and they do, far faster than we humans could even type the code in.

A huge piece of the puzzle, of course, is the raw speed at which modern computers can do calculations. Specifically, matrix multiplications. Phenomena like reasoning and natural-language processing can require trillions upon trillions of floating-point calculations to be made — and if we want to get any results before the heat death of the Universe, the computers doing these calculations had better be fast.

But could we, with a lot of patience, have run language models decades ago, if only we knew the algorithms?

As a test, I decided to compare the speed of my very first computer — a Timex/Sinclair 1000 — against the speed of Scientia (my current Core i9 workstation) at matrix multiplications. While there are far too many variables to make this a proper apples-to-apples comparison (for one, the Sinclair is running interpreted BASIC instead of compiled C), the difference in raw speed is striking.

For reasonable accuracy, I chose as the benchmark for each machine a matrix size that would take it at least a few minutes to compute. Additionally, I tried to keep the display output to a minimum, since the result I’m interested in is calculation speed. For the Sinclair, I chose the largest matrix multiplication that I could easily fit into 16KB of memory — a size of 30×30. In order to multiply two 30×30 matrices, a total of 30x30x30 multiplications and the same number of additions must be made. This is a total of 54,000 floating-point operations (half of them multiplications and half of them additions.)

The Sinclair (or, rather, a 1x-speed emulation of one) completed this in roughly twelve and a half minutes, for a grand total of 72 FLOPS (floating operations per second). Even this is blazingly fast compared to truly old-school machines like ENIAC or the Mark I relay computer.

…But how much faster is a fairly modest, modern (2020-era) workstation?

Running the same 30×30 matrix multiplication on Scientia took about 34 milliseconds — some 22,000 times faster. But a lot of that is simply the overhead of compiling the code, displaying the window, and so forth. At 3000×3000, it took about three minutes. Since the computational complexity of naïve square matrix multiplication goes up by the third power of the size of the matrices, this task is not 100 times harder, but rather one million times harder.

Scientia (using a single CPU core, a fraction of the available memory, and not using the GPU) was able to finish in 180 seconds, for a resulting speed of about 300MFLOPS (300 million floating operations per second.) That’s 4.167 million times faster!

And that’s not even the whole picture. AI models, with their heavy reliance on matrix multiplications, naturally work best when one or more GPUs are available to handle the calculations. GPUs use thousands of (simpler) compute cores, compared to the handful of more complex cores available on a CPU. Matrix multiplication naturally lends itself to such parallelization, whether across multiple CPU cores (Scientia’s CPU, representative of a typical modern computer, has eight cores capable of sixteen threads) or across the thousands of specialized cores available in a GPU (Scientia’s RTX4070 has nearly six thousand such cores.)

To be fair, the Sinclair is handicapped (by perhaps a factor of 10x to 20x) due to running interpreted BASIC rather than compiled code. Even in “fast mode” where the display is turned off so the Z80 processor can focus on computation, the process is very inefficient (basically doing everything in translation instead of using efficiently-compiled machine code.)

But running a single-CPU-core matrix multiplication test on a modern PC is also making it work without the vast majority of its compute capability, too. The RTX4070 GPU is theoretically capable of some 29 teraflops — 29 trillion floating-point operations per second(!)

That’s about 400 billion times faster than the Sinclair. That’s how far we’ve come in 40-some years. And that (plus terabytes and terabytes of training data) is what makes the AI magic work. Even if the Sinclair could fit a trained LLM into the maximum 64KB of memory it can access (it can’t, by many orders of magnitude), a response that takes a modern workstation ten seconds to produce would take some twelve hundred years, running on Sinclair BASIC.

I love living in the future.

Posted in Algorithms, BASIC, C | Leave a comment

Stupid Op-Amp Tricks

The op amp (“operational amplifier“) has rightly been called the Swiss Army Knife of analog electronics. These integrated circuits are active devices with a deceptively simple description: high-input-impedance difference amplifiers with very high gain. Especially when connected in negative feedback, op amps can perform lots of useful functions on electronic signals.

The schematic symbol for a generic op amp (Wikipedia)

Vs+ and Vs- are typically connected to +15VDC and -15VDC supply rails.

V+ and V- are high-impedance inputs.

Vout is typically the difference in voltage between V+ and V-, multiplied by a large gain (of perhaps a hundred thousand times or more.)

When connected in negative feedback, two “golden rules of op amps” govern their basic behavior in electronic circuits:

  • Little or no measurable current flows in or out of V+ and V- ; and
  • Unless driven to saturation, the op amp will do what is needed to make its inputs equal.

That’s it. That’s what they do. The magic is in just how many ways this is useful:

A voltage follower using an op amp

The voltage follower is the simplest closed-loop op amp circuit. It feeds the output back into the input. If the input voltage is even a little higher than the output, the output voltage rises. If the input voltage is lower, the output voltage drops. Either way, it reaches equilibrium when the output voltage equals the input voltage.

How is this useful? As an impedance converter. An op amp can take a signal with a very high impedance (that cannot source much current at all) and copy it as a stronger signal, capable of driving lower-impedance loads. And that’s just the beginning.

We can modify the simple voltage-follower so that the output voltage is sent through a voltage divider. Since the input node sees a fraction of the output voltage, the output voltage has to be (R1+R2)/R2 times the input voltage. So gain is 1+R1/R2.

A non-inverting amplifier, with gain G=(R2+R1)/R1

Similarly, we can make an inverting amplifier, with gain G=-R2/R1.

How does this work? Follow the rules: No current flows in or out of + and -, and A and B are at the same voltage. Since B is grounded, A is also at zero volts. So any input voltage applied will cause a current through R1. (Almost) no current flows into the op amp input, so this current has to also flow through R2. This means that the output voltage is inverted with respect to the input voltage: Vout is negative Vin, times R2/R1. Use equal resistors, and gain is G=-1.

An inverting amplifier, with gain G=-R2/R1.

We can make an inverting, summing amplifier as follows:

An inverting, summing amplifier

How does this one work? Point X is at ground due to how the amplifier works. Given that, the input impedance of each input is simply the value of its input resistor (10k for all inputs, in this case.) Therefore, a current of I= (V1/R1)+(V2/R2)+(V3/R3) flows past point X. For point X to be held at zero volts, the op amp must output a voltage of Vout=-I*Rf.

Since op amps are inherently difference amplifiers, they can be used to make difference amplifiers with modest gain, as well as their open-loop behavior:

A difference amplifier with gain Vout=(V2-V1)*(R2/R1).
(If R2=R1, output voltage is equal to the difference in input voltages.)

These circuits are just the beginning — op amps can be used to make current sources (useful for LED strings), “ideal diode” circuits, integrators, differentiators, and lots more.

(I guess if they turned out to be useless, they’d be non-operational amplifiers!)

Posted in Analog, Components, Electronics | Leave a comment

Components: LM386 audio amplifier

You sometimes hear electronics types talk about “jellybean” components. They’re not necessarily referring to the shape, so much as that such parts are likely to be stocked on hand in reasonable quantity in even a modest electronics lab, often in bins to be dispensed as needed like candy. The 2N2222 transistor, 555 timer, and ‘741 op amp are other such parts.

The LM386 audio amplifier is one such “jellybean” part. Although as a digital partisan, when I hear “386,” I think of the Intel 80386, the LM386 audio amplifier chip is a very useful part for projects that include a speaker.

Pinout of the LM386. (Source: TI datasheet)

The LM386 is straightforward enough to use: Provide ground, suitable supply voltage (5-12VDC or so), and a low-level audio signal on the non-inverting input. Leave the gain pins disconnected for the default gain of 20, and connect a speaker via a 250uF series cap, with a 500nF+10R series pair to ground from the output, and you’re good to go. The input is nice and high impedance, so it can be driven by pretty much anything.

Posted in Analog, Audio, Building blocks, Components, Electronics | Leave a comment

Kilowatt Counter Cleverness

I hope the designer got recognized for this one, because it’s really clever.

The readout dials of a Westinghouse Type CA watt-hour meter

On a recent trip to the frozen Northlands, I came across a Westinghouse kilowatt-hour meter and picked it up to use in classroom discussions about power and energy.

Looking more closely at it reveals a neat design quirk — the 2nd and 4th indicators move counterclockwise, and so (with reversed scales) can share two digits with each neighbor.

There seems to be enough space in the meter that this wouldn’t be necessary — but perhaps the design was originally from a smaller meter. Or maybe this was just an engineer having fun.

Posted in Design, Power, User Interface Design | Leave a comment