Main Point: Matrix multiplication combines rows of inputs with columns of weights. Its operation count describes numerical work; data movement, software and other task stages determine elapsed time.
Key Points
- A 2 × 3 input matrix multiplied by a 3 × 2 weight matrix produces four outputs in a 2 × 2 matrix.
- Each output uses three matching input–weight pairs.
- State the counting convention before comparing FLOP figures.
1. One accelerator, two tasks
- An umbrella retailer uses the same accelerator for two jobs. Overnight, it runs a large batch of numerical forecasts.
- During the day, a live customer question may need a small model calculation, a record from another system, and a reply across the network. The chip specification is the same in both cases.
- Yet the time and cost to complete each useful job can be very different.
2. Build the matrices
- A weighted sum took input values, multiplied each by a weight, and added the products. For example, three inputs can be combined with three weights to make one output.
- Matrix multiplication organizes many related combinations of that kind.
- A matrix is a rectangular arrangement of numbers in rows and columns. For our retailer, imagine two store records, each with three numerical inputs.
- We will use deliberately simple whole numbers so you can follow the arithmetic. The first store's row is two, one, three.
- The second store's row is one, four, two. This input matrix has two rows and three columns, so we call its shape two by three.
- Now suppose the model has two sets of weights, each meant to form a different intermediate output score. The first weight column is one, two, two.
- The second is two, one, three. Placing those columns side by side makes a weight matrix with three rows and two columns: three weights for each of two outputs.
- These numbers are invented for the exercise. The scores will show the calculation, not a validated demand forecast or an automatic business decision.
- The inner sizes match: three input values per store and three weights per output. For every store row, we can combine the values with each weight column.
- Two store rows times two output columns gives four output positions. The product therefore has two rows and two columns.
- Neural networks repeatedly use related numerical transformations like this, alongside other kinds of work. The repeated structure is one reason specialized hardware can help.
- It does not mean every operation in a neural network, much less the entire application, is a matrix multiplication.
3. Calculate all four outputs
The input rows are [2, 1, 3] and [1, 4, 2]. The weight columns are [1, 2, 2] and [2, 1, 3]. Each output combines one input row with one weight column.
| Output | Matching Products | Sum |
|---|---|---|
| Store 1, score 1 | 2 × 1 + 1 × 2 + 3 × 2 | 10 |
| Store 1, score 2 | 2 × 2 + 1 × 1 + 3 × 3 | 14 |
| Store 2, score 1 | 1 × 1 + 4 × 2 + 2 × 2 | 13 |
| Store 2, score 2 | 1 × 2 + 4 × 1 + 2 × 3 | 12 |
- Output matrix: [[10, 14], [13, 12]], with shape 2 × 2.
- Four outputs use 12 input–weight pairs. These are invented intermediate scores for the exercise; business interpretation still needs validation.
4. Instructions and counted operations
- A multiply-accumulate takes a pair of values, multiplies them, and adds the product into a running total. In our calculation of ten, we multiplied three pairs and accumulated their contributions.
- Some hardware can fuse a multiplication and addition into one instruction, called a fused multiply-add. An instruction is a command the processor executes.
- Performance reports often use a different counting unit: a FLOP, meaning a floating-point operation. Floating point is one way computers represent numerical values.
- Under a common multiply-add convention, each pair contributes two counted floating-point operations: one multiplication and one addition.
- A fused instruction can therefore be one instruction while still counting as two operations under that convention.
- We had four outputs, with three pairs for each output. Four times three is twelve pairs.
- Two counted operations per pair gives twenty-four FLOPs. The common high-level formula says the same thing for larger matrices.
- If the first matrix is m rows by k columns and the second is k rows by n columns, the approximate count is two times m times n times k. Here, m is two, n is two, and k is three.
- Two times two times two times three is twenty-four.
- There is a counting detail worth keeping straight. If you write each output as three products and add only those three products together, you can count three multiplications and two additions per output.
- Across four outputs, that is twelve multiplications and eight additions, or twenty scalar steps. The standard twenty-four-FLOP convention instead counts a multiply and an accumulation for each of the three pairs, including adding the first product to a starting total of zero.
- The answer values are unchanged. The convention differs.
- Before comparing performance figures, check that the operation counts use the same convention.
- Whole numbers keep the example arithmetic simple. The FLOP convention describes a corresponding floating-point computation.
- The count describes numerical work. Estimating time also requires a relevant measured execution rate.
5. Why elapsed time differs
- Larger matrix dimensions can expose more independent combinations and let hardware reuse values. In our small example, each store row is used with two weight columns, and each weight column is used with two store rows.
- At larger scale, arranging such reuse can matter because repeatedly moving the same values may be expensive.
- But a tiny matrix operation can finish so little arithmetic that setting it up takes a noticeable share of the total. Software must launch and coordinate work.
- If a model consists of many small operations, that overhead can keep it far below an advertised peak matrix rate.
- Shape is not the only qualifier. The numerical representation and the exact dimensions can determine whether the device has a fast supported path.
- A framework, meaning software that selects and runs model operations, may choose a fast kernel for one shape or data type and a slower kernel for another. A kernel is a program launched on the accelerator.
- Both versions may produce the correct output, while their performance differs.
- This creates a portability problem. Moving a model to another device can require software and data-layout work to preserve efficiency, even when the model runs correctly there.
- Test the actual shapes, formats and software path. A vendor's peak rate does not certify the retailer's particular matrix operation.
6. Follow the whole task
- Consider the retailer's two jobs. The overnight forecast uses large matrix operations on a batch of store data.
- If that data can be reused and the software selects efficient operations, the accelerator may spend much of its time on suitable numerical work.
- The live customer question can have a very different path. The service may fetch a purchase record, wait for a network response, perform a small model operation, and send an answer.
- Retrieval and network time are part of what the customer waits for. The small calculation may also pay launch and coordination overhead.
- Both applications use the same accelerator, but the accelerator's numerical count describes only part of the live task.
- We also have to be careful with cost per task. A large batch may use the device efficiently but still consume time and energy.
- A live request may use little arithmetic but occupy a service that must meet a response deadline. The useful comparison needs the full task, the required quality, elapsed time and resources paid for.
- It cannot be settled by operation count alone.
- If two versions of the retailer's model have the same approximate operation count, could they still take different time? Yes.
- One may run many operations at once while another has dependencies. One may find its values nearby while another must move them.
- One may use a supported fast kernel, while another falls back to a slower path. Starting and coordinating small jobs adds overhead.
- Some stages must synchronize: wait for other stages to finish before continuing. Actual clock behavior can also vary.
- These are separate effects on elapsed time. The operation count tells us how much of a specified kind of arithmetic the workload describes under our chosen convention.
- It does not say how efficiently a device executes it or how much waiting surrounds it.
- For the retailer, we would record matrix shapes and number formats, identify the software kernel, measure data movement and launches, and time the whole service. That turns an impressive hardware claim into a testable diagnosis.
- It also keeps the nightly batch and the customer-facing request from being mistaken for the same workload.