Live data from Hacker News

Multiplying Matrices Without Multiplying

arxiv.org

101–110 of 124 posts

Re: Multiplying Matrices Without Multiplying

#101

Primary author here. Happy to answer questions! Also, feel free to email me at the address in the paper if you're interested in talking about it in more detail. E.g., I've already heard from some hardware folks looking at expanding on this work.

One of our graduate students is thrilled about this paper, although he tends to do that with any new CS advance that seems sensational and that we barely understand (we do bioinformatics). He said that it stood to reason that if it can mmult 100GB/s/core, then we could matrix multiply 12TB in a minute!

Could you translate into practitioner-level language what are the practical limitations of this method; specifically, what would the error rates induced by approximation be under some practical scenarios, when would it make sense and not make sense to use it, etc? There is a complex equation in the paper describing the theoretical error bounds, but I have no idea whether in some practical scenario multiplying some normally distributed variables, whether that would mean a 0.1%, 1%, 5%, 10% error.

Personally I think it only makes sense to use this kind of method in some real-time algorithm where speed is of the essence, the downstream results of the mmult are themselves used in some other approximation (like many ML applications), and emphatically not to make the process of drawing biological conclusions from painstakingly derived data a few minutes faster for the analyst.

I fear that you have made an impressive, but dangerous, tool to people who don't know what they're doing.

Re: Multiplying Matrices Without Multiplying

#102
post #81

Primary author here. Happy to answer questions! Also, feel free to email me at the address in the paper if you're interested in talking about it in more detail. E.g., I've already heard from some hardware folks looking at expanding on this work.

So, do I understand this correctly: 0. The problem you're really looking at is quickly computing a'B for many different vectors a (of length D) which are conceptually random. 1. To do that, you approximate AB, where A is very long and thin (NxD), and B (DxM). (Note: this problem has complexity O(NDM) naively, or, if N=D=M, O(N^3), and there are smarter algorithms that reduce the exponent 3 down to 2.8 (Strassen) or e…

Yes, basically correct. A couple notes/clarifications for other readers:

- The rows a of A are "random," but in the sense of being drawn from some distribution for which we have a training set--not in the sense of, e.g., "assume everything is gaussian" or some other idealized case.

- The training takes a few seconds to maybe a couple minutes, depending on your training set size. But it does have to be done ahead of time--you can't train after receiving the matrices and still get a speedup. Unless maybe the matrices are unrealistically large.

- The error in the approximation is provably not much larger than the error on the training set with high probability (under standard ML assumptions--finite variance, training and test distributions match), but there's no hard limit on it in general. If you know the range of values in your individual vectors, you could obtain a hard limit, although it would be much looser than what you see in practice.

- Correct that many ML applications tolerate large losses in precision. But caveat is that we won't get the right answer to a full 16 bits in each element (this would be ~99.999% accuracy), but instead more like 90-99.9%, depending on the speed-quality tradeoff chosen. So we can't claim it will be sufficiently accurate in all cases

Re: Multiplying Matrices Without Multiplying

#103

An interesting work, with some to-be-addressed questions: 1.The paper only covers the GEMM part with small-scale experiments(CIFAR-10/100), not covering convolution, not covering GEMM part in more popular network such as Transformer/BERT, etc. 2. It is still an approximating method, meaning potential accuracy loss. So I think this method is less attractive to training acceleration scenario, maybe potentially as a com…

> I think this method is less attractive to training acceleration scenario The proposed hash based encoding function is not differentiable, so it doesn’t appear this method can be used for training at all. I’m not aware of any hash functions that are analytically differentiable, so to support efficient back-propagation I suspect that some fundamental changes to this method would be necessary.

You could still optimize the prototypes, so fine-tuning with this in place would be possible (see, e.g., [1]). But we don't yet have data on how well this would work using our exact method, how early in training you could do the op replacement, etc.

[1] http://openaccess.thecvf.com/content_ECCV_2018/html/Sanghyun...

Re: Multiplying Matrices Without Multiplying

#104
post #90

Lots of discussion here about higher dimensional matrices. The argument is that the dot product is an operation on two arguments, therefore any multidimensional matrix multiplication can be broken down into multiple two dimensional operations. The image offerred is that two vectors define a plane so any two dimensional operation is valid. But how about curved space? Suppose I have two vectorsin curved space-they look…

I'm actually not quite sure what you mean by breaking down into two-dimensional operations. We use operations on pairs of vectors, but nothing is assumed to be two-dimensional, and I don't think we suggest imagining anything as a plane? The vectors can be arbitrary and there's no assumption about curvature. Happy to clarify if I can help.

Re: Multiplying Matrices Without Multiplying

#105

Method 1: computer, use MUL to multiply X and Y. Method 2: computer, remember this number. When I give you X and Y, tell me what that number was. This requires no multiplication! Let us tell SCIENCE about our discovery!

So this misses a few aspects of why the method works:

- You can't actually get a speedup from the proposed approach. You'd need a lookup table of size b^2 to multiply two b-bit numbers, which will be much slower than just doing the multiplication.

- Most of what we do is perform fewer operations. We're actually slower per op than multiply-adds because those are supported better in hardware. We just compress the matrices by so much that it's worth it. Relatedly, for sufficiently few codebooks, we're actually sublinear in the input size--i.e., we don't even read it all.

- We have some lightweight machine learning in there to make the sublinearity and other approximations not suck. Getting the ML fast enough to beat a BLAS GEMM is far from trivial.

Re: Multiplying Matrices Without Multiplying

#106

Earlier quoted context omitted.

There is an intuitive version of this. Volume in n dimensions is C*r^n (C is some constant) and surface is the first derivative, leading to a ratio of n/r (the C constant cancels out). Hmm... Maybe not that intuitive

But the former formula already tells you that most of the volume is near the high range of r : that which was to be shown. The surface area to volume concept adds nothing. Because the volume of a sphere is proportional to r cubed, you know there is much more volume between r in [0.9, 1.0] than in the same sized interval of r [0.0, 0.1]. You can find the break-even point almost in your head. At what r value is half th…

The surface area to volume ratio is just a limit of the shell volume to total volume ratio as the shell thickness goes to zero. So both should asymptotically scale with higher dimensions in the same way.

Re: Multiplying Matrices Without Multiplying

#107
post #7

Earlier quoted context omitted.

Probably, but hill climbing will avoid loss in the long run. The speed boost is probably more than worth it. I wouldn't be surprised if we started using lossier math for faster state space search. Once you find a peak, you could swap out the maths to be more exact.

Suppose you have a set membership question. If you approximate yes, you do a more complex inquiry to grab details. If you approximate no, you tell the user no, don't know that face [answer, command, etc]. So this is ripe for approximate methods as you can tune to get very few false negatives (respond no when it's really yes), but allow a healthy dose of false positives.

This reminds me of the mechanism of a bloom filter (https://en.wikipedia.org/wiki/Bloom_filter).

Re: Multiplying Matrices Without Multiplying

#108
post #31

Every element of a matrix multiplication is a dot-product of two vectors. The dot-product of two vectors quantifies their similarity -- in fact, we call it "dot-product similarity" in a nearest-neighbors context: If the dot product > 0, the vectors point in similar directions; if It's not too hard to imagine that it might be possible to learn representative K-means clusters of training vectors and then, at run-time,…

Please see this thread started by one of the authors, which I would rather see at the top of the page, above my comment:

https://news.ycombinator.com/item?id=28376439

Re: Multiplying Matrices Without Multiplying

#109
post #37

Earlier quoted context omitted.

The dot-product of two vectors quantifies their similarity -- in fact, we call it "dot-product similarity" in a nearest-neighbors context: If the dot product > 0, the vectors point in similar directions; if To make it more explicit, dot product of two vectors is just cosine of the angle between them, multiplied by their lengths.

The angle and cosine start to lose their geometric intuition when we go beyond 3D. The concept of correlation has no issue with additional components. The concept of similarity of two 17-element vectors is clear. In fact correlation intuitively scales to "infinite component vectors": the dot product becomes multiplying two functions together and then taking an integral. The Fourier transform of a periodic signal is b…

Not really, for example, in physics, lines in 4D are just as meaningful as they are in 3D, more even (they are called geodesics). So are the angles between them. The real problem is that we just don't have good intuitions of higher dimensions in general.

Re: Multiplying Matrices Without Multiplying

#110
post #71

Earlier quoted context omitted.

Ah that's useful to know that it was conventional up until then, thanks. It was my first exposure, personally :)

Checking Google Scholar, I found this 1962 paper by King titled "Table Look-Up Procedures in Data Processing" - https://dl.acm.org/doi/abs/10.1145/800198.806120 > It is not generally known that to provide seven- decimal accuracy of the sine function, allowing third-order interpolation, only 15 entries are required [*] ... The following commentary is interesting. > In spite of these developments in table construction,…

> It is not generally known that to provide seven- decimal accuracy of the sine function, allowing third-order interpolation, only 15 entries are required [*] ...

That got me thinking: is that so? how do you do that?

Luckily, scipy has all sorts of interpolation tools, so one can just experiment a little.

Third-order interpolation -> that means cubic splines. How many points do you need for a cubic spline approximation of the sine function to have 7 decimal place accuracy (in other words the error should be about 0.5e-8? Well, the sine function being anti-symmetric and periodic, you need to only approximate it on the interval [0, pi/2]. It turns out you need 42 points, not including the ends of the interval (where we don't need to tabulate the values of sine; it is simply 0 at 0 and 1 at pi/2).

     import numpy as np
     from scipy.interpolate import CubicSpline
     xs = np.linspace(0,np.pi/2,44,endpoint=True)
     sines = np.sin(xs)
     spline=CubicSpline(xs, sines)
     ts = np.linspace(0,1,10000,endpoint=True)\*np.pi/2
     print("Max Error is ", np.max(np.abs(spline(ts)-np.sin(ts))))
     >> Max Error is  5.02852541828247e-08
But, we can do better. If we tabulate values of sin, then we have for free the values of the cosine at the complementary angles ( cos(x) = sin(pi/2-x). But the derivative of sine is the cosine, so we can use a cubic spline that matches both the values and the derivatives at a number of points. This is called a Cubic Hermite Spline. Turns out we need 23 points.

     from scipy.interpolate import CubicHermiteSpline
     xs = np.linspace(0,np.pi/2,25,endpoint=True)
     sines = np.sin(xs)
     cosines = sines[::-1] #note that we reuse the sines, 
                           #we don't invoke np.cos
     spline=CubicHermiteSpline(xs, sines, dydx=cosines)
     ts = np.linspace(0,1,10000,endpoint=True)*np.pi/2
     print("Max Error is ", np.max(np.abs(spline(ts)-np.sin(ts))))

     >> Max Error is  4.775709550042251e-08
Now that we got to do Hermite interpolation, why stop here? The second derivative of the sine function is the sine with the sign flipped (oh, man, how do you avoid this pun?). So, you don't need to tabulate any new values. In scipy you can use BPoly to get this higher order spline. Now, you only need to tabulate 4 values of the sine function.

     xs = np.linspace(0,np.pi/2,6,endpoint=True)
     sines = np.sin(xs)
     cosines = sines[::-1]
     spline = BPoly.from_derivatives(xs, np.vstack((sines,cosines,-sines)).T)
     ts =np.linspace(0,np.pi/2,10000) 
     print("Max Error is ", np.max(np.abs(spline(ts)- 
                         np.sin(ts))))


     >> Max Error is  2.057940906574629e-08
Well, we can go all the way now. If we only use the values of the sine function and its first 4 derivatives only at the end of the [0,pi/2] interval, we can find a 9th degree polynomial that matches them.

     spline =  BPoly.from_derivatives([0,np.pi/2], 
        [[0,1,0,-1,0], [1,0,-1,0,1]])
     PSpline = PPoly.from_bernstein_basis(spline)
     P = Polynomial(PSpline.c.T[0][::-1])
     print("Max Error is ", np.max(np.abs(P(ts)- 
       np.sin(ts))))

     >>Max Error is  1.700470619869776e-08
And here's that 9th degree polynomial, if you are curious:

x −0.16666666666666785 x^3+8.881784197001252e-15 x^4+0.008331651428065356 x^5+5.167900831715144e-06 x^6−0.000204626778274708 x^7+3.5525642158307225e-06 x^8+1.8946044087406189e-06 x^9

Post reply on HN