aboutsummaryrefslogtreecommitdiff
path: root/mllib-local/src/test/scala/org/apache/spark/ml/linalg/BLASSuite.scala
blob: 6e72a5fff0a918326f81e78e1ccd6f3013bb1a2b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.spark.ml.linalg

import org.apache.spark.ml.SparkMLFunSuite
import org.apache.spark.ml.linalg.BLAS._
import org.apache.spark.ml.util.TestingUtils._

class BLASSuite extends SparkMLFunSuite {

  test("copy") {
    val sx = Vectors.sparse(4, Array(0, 2), Array(1.0, -2.0))
    val dx = Vectors.dense(1.0, 0.0, -2.0, 0.0)
    val sy = Vectors.sparse(4, Array(0, 1, 3), Array(2.0, 1.0, 1.0))
    val dy = Array(2.0, 1.0, 0.0, 1.0)

    val dy1 = Vectors.dense(dy.clone())
    copy(sx, dy1)
    assert(dy1 ~== dx absTol 1e-15)

    val dy2 = Vectors.dense(dy.clone())
    copy(dx, dy2)
    assert(dy2 ~== dx absTol 1e-15)

    intercept[IllegalArgumentException] {
      copy(sx, sy)
    }

    intercept[IllegalArgumentException] {
      copy(dx, sy)
    }

    withClue("vector sizes must match") {
      intercept[Exception] {
        copy(sx, Vectors.dense(0.0, 1.0, 2.0))
      }
    }
  }

  test("scal") {
    val a = 0.1
    val sx = Vectors.sparse(3, Array(0, 2), Array(1.0, -2.0))
    val dx = Vectors.dense(1.0, 0.0, -2.0)

    scal(a, sx)
    assert(sx ~== Vectors.sparse(3, Array(0, 2), Array(0.1, -0.2)) absTol 1e-15)

    scal(a, dx)
    assert(dx ~== Vectors.dense(0.1, 0.0, -0.2) absTol 1e-15)
  }

  test("axpy") {
    val alpha = 0.1
    val sx = Vectors.sparse(3, Array(0, 2), Array(1.0, -2.0))
    val dx = Vectors.dense(1.0, 0.0, -2.0)
    val dy = Array(2.0, 1.0, 0.0)
    val expected = Vectors.dense(2.1, 1.0, -0.2)

    val dy1 = Vectors.dense(dy.clone())
    axpy(alpha, sx, dy1)
    assert(dy1 ~== expected absTol 1e-15)

    val dy2 = Vectors.dense(dy.clone())
    axpy(alpha, dx, dy2)
    assert(dy2 ~== expected absTol 1e-15)

    val sy = Vectors.sparse(4, Array(0, 1), Array(2.0, 1.0))

    intercept[IllegalArgumentException] {
      axpy(alpha, sx, sy)
    }

    intercept[IllegalArgumentException] {
      axpy(alpha, dx, sy)
    }

    withClue("vector sizes must match") {
      intercept[Exception] {
        axpy(alpha, sx, Vectors.dense(1.0, 2.0))
      }
    }
  }

  test("dot") {
    val sx = Vectors.sparse(3, Array(0, 2), Array(1.0, -2.0))
    val dx = Vectors.dense(1.0, 0.0, -2.0)
    val sy = Vectors.sparse(3, Array(0, 1), Array(2.0, 1.0))
    val dy = Vectors.dense(2.0, 1.0, 0.0)

    assert(dot(sx, sy) ~== 2.0 absTol 1e-15)
    assert(dot(sy, sx) ~== 2.0 absTol 1e-15)
    assert(dot(sx, dy) ~== 2.0 absTol 1e-15)
    assert(dot(dy, sx) ~== 2.0 absTol 1e-15)
    assert(dot(dx, dy) ~== 2.0 absTol 1e-15)
    assert(dot(dy, dx) ~== 2.0 absTol 1e-15)

    assert(dot(sx, sx) ~== 5.0 absTol 1e-15)
    assert(dot(dx, dx) ~== 5.0 absTol 1e-15)
    assert(dot(sx, dx) ~== 5.0 absTol 1e-15)
    assert(dot(dx, sx) ~== 5.0 absTol 1e-15)

    val sx1 = Vectors.sparse(10, Array(0, 3, 5, 7, 8), Array(1.0, 2.0, 3.0, 4.0, 5.0))
    val sx2 = Vectors.sparse(10, Array(1, 3, 6, 7, 9), Array(1.0, 2.0, 3.0, 4.0, 5.0))
    assert(dot(sx1, sx2) ~== 20.0 absTol 1e-15)
    assert(dot(sx2, sx1) ~== 20.0 absTol 1e-15)

    withClue("vector sizes must match") {
      intercept[Exception] {
        dot(sx, Vectors.dense(2.0, 1.0))
      }
    }
  }

  test("spr") {
    // test dense vector
    val alpha = 0.1
    val x = new DenseVector(Array(1.0, 2, 2.1, 4))
    val U = new DenseVector(Array(1.0, 2, 2, 3, 3, 3, 4, 4, 4, 4))
    val expected = new DenseVector(Array(1.1, 2.2, 2.4, 3.21, 3.42, 3.441, 4.4, 4.8, 4.84, 5.6))

    spr(alpha, x, U)
    assert(U ~== expected absTol 1e-9)

    val matrix33 = new DenseVector(Array(1.0, 2, 3, 4, 5))
    withClue("Size of vector must match the rank of matrix") {
      intercept[Exception] {
        spr(alpha, x, matrix33)
      }
    }

    // test sparse vector
    val sv = new SparseVector(4, Array(0, 3), Array(1.0, 2))
    val U2 = new DenseVector(Array(1.0, 2, 2, 3, 3, 3, 4, 4, 4, 4))
    spr(0.1, sv, U2)
    val expectedSparse = new DenseVector(Array(1.1, 2.0, 2.0, 3.0, 3.0, 3.0, 4.2, 4.0, 4.0, 4.4))
    assert(U2 ~== expectedSparse absTol 1e-15)
  }

  test("syr") {
    val dA = new DenseMatrix(4, 4,
      Array(0.0, 1.2, 2.2, 3.1, 1.2, 3.2, 5.3, 4.6, 2.2, 5.3, 1.8, 3.0, 3.1, 4.6, 3.0, 0.8))
    val x = new DenseVector(Array(0.0, 2.7, 3.5, 2.1))
    val alpha = 0.15

    val expected = new DenseMatrix(4, 4,
      Array(0.0, 1.2, 2.2, 3.1, 1.2, 4.2935, 6.7175, 5.4505, 2.2, 6.7175, 3.6375, 4.1025, 3.1,
        5.4505, 4.1025, 1.4615))

    syr(alpha, x, dA)

    assert(dA ~== expected absTol 1e-15)

    val dB =
      new DenseMatrix(3, 4, Array(0.0, 1.2, 2.2, 3.1, 1.2, 3.2, 5.3, 4.6, 2.2, 5.3, 1.8, 3.0))

    withClue("Matrix A must be a symmetric Matrix") {
      intercept[Exception] {
        syr(alpha, x, dB)
      }
    }

    val dC =
      new DenseMatrix(3, 3, Array(0.0, 1.2, 2.2, 1.2, 3.2, 5.3, 2.2, 5.3, 1.8))

    withClue("Size of vector must match the rank of matrix") {
      intercept[Exception] {
        syr(alpha, x, dC)
      }
    }

    val y = new DenseVector(Array(0.0, 2.7, 3.5, 2.1, 1.5))

    withClue("Size of vector must match the rank of matrix") {
      intercept[Exception] {
        syr(alpha, y, dA)
      }
    }

    val xSparse = new SparseVector(4, Array(0, 2, 3), Array(1.0, 3.0, 4.0))
    val dD = new DenseMatrix(4, 4,
      Array(0.0, 1.2, 2.2, 3.1, 1.2, 3.2, 5.3, 4.6, 2.2, 5.3, 1.8, 3.0, 3.1, 4.6, 3.0, 0.8))
    syr(0.1, xSparse, dD)
    val expectedSparse = new DenseMatrix(4, 4,
      Array(0.1, 1.2, 2.5, 3.5, 1.2, 3.2, 5.3, 4.6, 2.5, 5.3, 2.7, 4.2, 3.5, 4.6, 4.2, 2.4))
    assert(dD ~== expectedSparse absTol 1e-15)
  }

  test("gemm") {
    val dA =
      new DenseMatrix(4, 3, Array(0.0, 1.0, 0.0, 0.0, 2.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 3.0))
    val sA = new SparseMatrix(4, 3, Array(0, 1, 3, 4), Array(1, 0, 2, 3), Array(1.0, 2.0, 1.0, 3.0))

    val B = new DenseMatrix(3, 2, Array(1.0, 0.0, 0.0, 0.0, 2.0, 1.0))
    val expected = new DenseMatrix(4, 2, Array(0.0, 1.0, 0.0, 0.0, 4.0, 0.0, 2.0, 3.0))
    val BTman = new DenseMatrix(2, 3, Array(1.0, 0.0, 0.0, 2.0, 0.0, 1.0))
    val BT = B.transpose

    assert(dA.multiply(B) ~== expected absTol 1e-15)
    assert(sA.multiply(B) ~== expected absTol 1e-15)

    val C1 = new DenseMatrix(4, 2, Array(1.0, 0.0, 2.0, 1.0, 0.0, 0.0, 1.0, 0.0))
    val C2 = C1.copy
    val C3 = C1.copy
    val C4 = C1.copy
    val C5 = C1.copy
    val C6 = C1.copy
    val C7 = C1.copy
    val C8 = C1.copy
    val C9 = C1.copy
    val C10 = C1.copy
    val C11 = C1.copy
    val C12 = C1.copy
    val C13 = C1.copy
    val C14 = C1.copy
    val C15 = C1.copy
    val C16 = C1.copy
    val C17 = C1.copy
    val expected2 = new DenseMatrix(4, 2, Array(2.0, 1.0, 4.0, 2.0, 4.0, 0.0, 4.0, 3.0))
    val expected3 = new DenseMatrix(4, 2, Array(2.0, 2.0, 4.0, 2.0, 8.0, 0.0, 6.0, 6.0))
    val expected4 = new DenseMatrix(4, 2, Array(5.0, 0.0, 10.0, 5.0, 0.0, 0.0, 5.0, 0.0))
    val expected5 = C1.copy

    gemm(1.0, dA, B, 2.0, C1)
    gemm(1.0, sA, B, 2.0, C2)
    gemm(2.0, dA, B, 2.0, C3)
    gemm(2.0, sA, B, 2.0, C4)
    assert(C1 ~== expected2 absTol 1e-15)
    assert(C2 ~== expected2 absTol 1e-15)
    assert(C3 ~== expected3 absTol 1e-15)
    assert(C4 ~== expected3 absTol 1e-15)
    gemm(1.0, dA, B, 0.0, C17)
    assert(C17 ~== expected absTol 1e-15)
    gemm(1.0, sA, B, 0.0, C17)
    assert(C17 ~== expected absTol 1e-15)

    withClue("columns of A don't match the rows of B") {
      intercept[Exception] {
        gemm(1.0, dA.transpose, B, 2.0, C1)
      }
    }

    val dATman =
      new DenseMatrix(3, 4, Array(0.0, 2.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 3.0))
    val sATman =
      new SparseMatrix(3, 4, Array(0, 1, 2, 3, 4), Array(1, 0, 1, 2), Array(2.0, 1.0, 1.0, 3.0))

    val dATT = dATman.transpose
    val sATT = sATman.transpose
    val BTT = BTman.transpose.asInstanceOf[DenseMatrix]

    assert(dATT.multiply(B) ~== expected absTol 1e-15)
    assert(sATT.multiply(B) ~== expected absTol 1e-15)
    assert(dATT.multiply(BTT) ~== expected absTol 1e-15)
    assert(sATT.multiply(BTT) ~== expected absTol 1e-15)

    gemm(1.0, dATT, BTT, 2.0, C5)
    gemm(1.0, sATT, BTT, 2.0, C6)
    gemm(2.0, dATT, BTT, 2.0, C7)
    gemm(2.0, sATT, BTT, 2.0, C8)
    gemm(1.0, dA, BTT, 2.0, C9)
    gemm(1.0, sA, BTT, 2.0, C10)
    gemm(2.0, dA, BTT, 2.0, C11)
    gemm(2.0, sA, BTT, 2.0, C12)
    assert(C5 ~== expected2 absTol 1e-15)
    assert(C6 ~== expected2 absTol 1e-15)
    assert(C7 ~== expected3 absTol 1e-15)
    assert(C8 ~== expected3 absTol 1e-15)
    assert(C9 ~== expected2 absTol 1e-15)
    assert(C10 ~== expected2 absTol 1e-15)
    assert(C11 ~== expected3 absTol 1e-15)
    assert(C12 ~== expected3 absTol 1e-15)

    gemm(0, dA, B, 5, C13)
    gemm(0, sA, B, 5, C14)
    gemm(0, dA, B, 1, C15)
    gemm(0, sA, B, 1, C16)
    assert(C13 ~== expected4 absTol 1e-15)
    assert(C14 ~== expected4 absTol 1e-15)
    assert(C15 ~== expected5 absTol 1e-15)
    assert(C16 ~== expected5 absTol 1e-15)

  }

  test("gemv") {

    val dA =
      new DenseMatrix(4, 3, Array(0.0, 1.0, 0.0, 0.0, 2.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 3.0))
    val sA = new SparseMatrix(4, 3, Array(0, 1, 3, 4), Array(1, 0, 2, 3), Array(1.0, 2.0, 1.0, 3.0))

    val dA2 =
      new DenseMatrix(4, 3, Array(0.0, 2.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 3.0), true)
    val sA2 =
      new SparseMatrix(4, 3, Array(0, 1, 2, 3, 4), Array(1, 0, 1, 2), Array(2.0, 1.0, 1.0, 3.0),
        true)

    val dx = new DenseVector(Array(1.0, 2.0, 3.0))
    val sx = dx.toSparse
    val expected = new DenseVector(Array(4.0, 1.0, 2.0, 9.0))

    assert(dA.multiply(dx) ~== expected absTol 1e-15)
    assert(sA.multiply(dx) ~== expected absTol 1e-15)
    assert(dA.multiply(sx) ~== expected absTol 1e-15)
    assert(sA.multiply(sx) ~== expected absTol 1e-15)

    val y1 = new DenseVector(Array(1.0, 3.0, 1.0, 0.0))
    val y2 = y1.copy
    val y3 = y1.copy
    val y4 = y1.copy
    val y5 = y1.copy
    val y6 = y1.copy
    val y7 = y1.copy
    val y8 = y1.copy
    val y9 = y1.copy
    val y10 = y1.copy
    val y11 = y1.copy
    val y12 = y1.copy
    val y13 = y1.copy
    val y14 = y1.copy
    val y15 = y1.copy
    val y16 = y1.copy

    val expected2 = new DenseVector(Array(6.0, 7.0, 4.0, 9.0))
    val expected3 = new DenseVector(Array(10.0, 8.0, 6.0, 18.0))

    gemv(1.0, dA, dx, 2.0, y1)
    gemv(1.0, sA, dx, 2.0, y2)
    gemv(1.0, dA, sx, 2.0, y3)
    gemv(1.0, sA, sx, 2.0, y4)

    gemv(1.0, dA2, dx, 2.0, y5)
    gemv(1.0, sA2, dx, 2.0, y6)
    gemv(1.0, dA2, sx, 2.0, y7)
    gemv(1.0, sA2, sx, 2.0, y8)

    gemv(2.0, dA, dx, 2.0, y9)
    gemv(2.0, sA, dx, 2.0, y10)
    gemv(2.0, dA, sx, 2.0, y11)
    gemv(2.0, sA, sx, 2.0, y12)

    gemv(2.0, dA2, dx, 2.0, y13)
    gemv(2.0, sA2, dx, 2.0, y14)
    gemv(2.0, dA2, sx, 2.0, y15)
    gemv(2.0, sA2, sx, 2.0, y16)

    assert(y1 ~== expected2 absTol 1e-15)
    assert(y2 ~== expected2 absTol 1e-15)
    assert(y3 ~== expected2 absTol 1e-15)
    assert(y4 ~== expected2 absTol 1e-15)

    assert(y5 ~== expected2 absTol 1e-15)
    assert(y6 ~== expected2 absTol 1e-15)
    assert(y7 ~== expected2 absTol 1e-15)
    assert(y8 ~== expected2 absTol 1e-15)

    assert(y9 ~== expected3 absTol 1e-15)
    assert(y10 ~== expected3 absTol 1e-15)
    assert(y11 ~== expected3 absTol 1e-15)
    assert(y12 ~== expected3 absTol 1e-15)

    assert(y13 ~== expected3 absTol 1e-15)
    assert(y14 ~== expected3 absTol 1e-15)
    assert(y15 ~== expected3 absTol 1e-15)
    assert(y16 ~== expected3 absTol 1e-15)

    withClue("columns of A don't match the rows of B") {
      intercept[Exception] {
        gemv(1.0, dA.transpose, dx, 2.0, y1)
      }
      intercept[Exception] {
        gemv(1.0, sA.transpose, dx, 2.0, y1)
      }
      intercept[Exception] {
        gemv(1.0, dA.transpose, sx, 2.0, y1)
      }
      intercept[Exception] {
        gemv(1.0, sA.transpose, sx, 2.0, y1)
      }
    }

    val y17 = new DenseVector(Array(0.0, 0.0))
    val y18 = y17.copy

    val sA3 = new SparseMatrix(3, 2, Array(0, 2, 4), Array(1, 2, 0, 1), Array(2.0, 1.0, 1.0, 2.0))
      .transpose
    val sA4 =
      new SparseMatrix(2, 3, Array(0, 1, 3, 4), Array(1, 0, 1, 0), Array(1.0, 2.0, 2.0, 1.0))
    val sx3 = new SparseVector(3, Array(1, 2), Array(2.0, 1.0))

    val expected4 = new DenseVector(Array(5.0, 4.0))

    gemv(1.0, sA3, sx3, 0.0, y17)
    gemv(1.0, sA4, sx3, 0.0, y18)

    assert(y17 ~== expected4 absTol 1e-15)
    assert(y18 ~== expected4 absTol 1e-15)

    val dAT =
      new DenseMatrix(3, 4, Array(0.0, 2.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 3.0))
    val sAT =
      new SparseMatrix(3, 4, Array(0, 1, 2, 3, 4), Array(1, 0, 1, 2), Array(2.0, 1.0, 1.0, 3.0))

    val dATT = dAT.transpose
    val sATT = sAT.transpose

    assert(dATT.multiply(dx) ~== expected absTol 1e-15)
    assert(sATT.multiply(dx) ~== expected absTol 1e-15)
    assert(dATT.multiply(sx) ~== expected absTol 1e-15)
    assert(sATT.multiply(sx) ~== expected absTol 1e-15)
  }
}