|
| 1 | +/* |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * This source code is licensed under the BSD-style license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. |
| 7 | + */ |
| 8 | +package org.pytorch.executorch |
| 9 | + |
| 10 | +import android.Manifest |
| 11 | +import android.util.Log |
| 12 | +import androidx.test.ext.junit.runners.AndroidJUnit4 |
| 13 | +import androidx.test.rule.GrantPermissionRule |
| 14 | +import java.io.File |
| 15 | +import java.io.IOException |
| 16 | +import java.net.URISyntaxException |
| 17 | +import org.apache.commons.io.FileUtils |
| 18 | +import org.junit.Assert |
| 19 | +import org.junit.Rule |
| 20 | +import org.junit.Test |
| 21 | +import org.junit.runner.RunWith |
| 22 | +import org.pytorch.executorch.TestFileUtils.getTestFilePath |
| 23 | +import kotlin.random.Random |
| 24 | +import kotlin.test.assertContains |
| 25 | + |
| 26 | +/** Unit tests for [TrainingModule]. */ |
| 27 | +@RunWith(AndroidJUnit4::class) |
| 28 | +class TrainingModuleE2ETest { |
| 29 | + @get:Rule |
| 30 | + var runtimePermissionRule: GrantPermissionRule = |
| 31 | + GrantPermissionRule.grant(Manifest.permission.READ_EXTERNAL_STORAGE) |
| 32 | + |
| 33 | + @Test |
| 34 | + @Throws(IOException::class, URISyntaxException::class) |
| 35 | + fun testTrainXOR() { |
| 36 | + val pteFilePath = "/xor.pte" |
| 37 | + val ptdFilePath = "/xor.ptd" |
| 38 | + |
| 39 | + val pteFile = File(getTestFilePath(pteFilePath)) |
| 40 | + val pteInputStream = javaClass.getResourceAsStream(pteFilePath) |
| 41 | + FileUtils.copyInputStreamToFile(pteInputStream, pteFile) |
| 42 | + pteInputStream.close() |
| 43 | + |
| 44 | + val ptdFile = File(getTestFilePath(ptdFilePath)) |
| 45 | + val ptdInputStream = javaClass.getResourceAsStream(ptdFilePath) |
| 46 | + FileUtils.copyInputStreamToFile(ptdInputStream, ptdFile) |
| 47 | + ptdInputStream.close() |
| 48 | + |
| 49 | + val module = TrainingModule.load(getTestFilePath(pteFilePath), getTestFilePath(ptdFilePath)) |
| 50 | + val params = module.namedParameters("forward") |
| 51 | + |
| 52 | + Assert.assertEquals(4, params.size) |
| 53 | + assertContains(params, LIN_WEIGHT) |
| 54 | + assertContains(params, LIN_BIAS) |
| 55 | + assertContains(params, LIN2_WEIGHT) |
| 56 | + assertContains(params, LIN2_BIAS) |
| 57 | + |
| 58 | + val sgd = SGD.create(params, 0.5); |
| 59 | + val dataset = listOf<Tensor>( |
| 60 | + Tensor.fromBlob(floatArrayOf(1.0f, 1.0f), longArrayOf(1, 2)), |
| 61 | + Tensor.fromBlob(longArrayOf(0), longArrayOf(1)), |
| 62 | + Tensor.fromBlob(floatArrayOf(0.0f, 0.0f), longArrayOf(1, 2)), |
| 63 | + Tensor.fromBlob(longArrayOf(0), longArrayOf(1)), |
| 64 | + Tensor.fromBlob(floatArrayOf(1.0f, 0.0f), longArrayOf(1, 2)), |
| 65 | + Tensor.fromBlob(longArrayOf(1), longArrayOf(1)), |
| 66 | + Tensor.fromBlob(floatArrayOf(0.0f, 1.0f), longArrayOf(1, 2)), |
| 67 | + Tensor.fromBlob(longArrayOf(1), longArrayOf(1)), |
| 68 | + ) |
| 69 | + |
| 70 | + val numEpochs = 5000; |
| 71 | + var finalLoss = Float.MAX_VALUE |
| 72 | + |
| 73 | + for (i in 0 until numEpochs) { |
| 74 | + val inputDex = 2 * Random.nextInt(dataset.size / 2) |
| 75 | + val targetDex = inputDex + 1 |
| 76 | + val input = dataset.get(inputDex) |
| 77 | + val target = dataset.get(targetDex) |
| 78 | + val out = module.executeForwardBackward("forward", EValue.from(input), EValue.from(target)) |
| 79 | + val gradients = module.namedGradients("forward") |
| 80 | + |
| 81 | + if (i == 0) { |
| 82 | + Assert.assertEquals(4, gradients.size) |
| 83 | + assertContains(gradients, LIN_WEIGHT) |
| 84 | + assertContains(gradients, LIN_BIAS) |
| 85 | + assertContains(gradients, LIN2_WEIGHT) |
| 86 | + assertContains(gradients, LIN2_BIAS) |
| 87 | + } |
| 88 | + |
| 89 | + if (i % 500 == 0 || i == numEpochs - 1) { |
| 90 | + Log.i( |
| 91 | + "testTrainXOR", |
| 92 | + String.format( |
| 93 | + "Step %d, Loss %f, Input [%.0f, %.0f], Prediction %d, Label %d", |
| 94 | + i, |
| 95 | + out[0].toTensor().getDataAsFloatArray()[0], |
| 96 | + input.getDataAsFloatArray()[0], |
| 97 | + input.getDataAsFloatArray()[1], |
| 98 | + out[1].toTensor().getDataAsLongArray()[0], |
| 99 | + target.getDataAsLongArray()[0])); |
| 100 | + } |
| 101 | + |
| 102 | + sgd.step(gradients) |
| 103 | + |
| 104 | + if (i == numEpochs - 1) { |
| 105 | + finalLoss = out[0].toTensor().dataAsFloatArray[0] |
| 106 | + } |
| 107 | + } |
| 108 | + Assert.assertTrue(finalLoss < 0.1f) |
| 109 | + } |
| 110 | + |
| 111 | + @Test |
| 112 | + @Throws(IOException::class, URISyntaxException::class) |
| 113 | + fun testTrainXOR_PTEOnly() { |
| 114 | + val pteFilePath = "/xor_full.pte" |
| 115 | + |
| 116 | + val pteFile = File(getTestFilePath(pteFilePath)) |
| 117 | + val pteInputStream = javaClass.getResourceAsStream(pteFilePath) |
| 118 | + FileUtils.copyInputStreamToFile(pteInputStream, pteFile) |
| 119 | + pteInputStream.close() |
| 120 | + |
| 121 | + val module = TrainingModule.load(getTestFilePath(pteFilePath)); |
| 122 | + val params = module.namedParameters("forward") |
| 123 | + |
| 124 | + Assert.assertEquals(4, params.size) |
| 125 | + assertContains(params, LIN_WEIGHT) |
| 126 | + assertContains(params, LIN_BIAS) |
| 127 | + assertContains(params, LIN2_WEIGHT) |
| 128 | + assertContains(params, LIN2_BIAS) |
| 129 | + |
| 130 | + val sgd = SGD.create(params, 0.5); |
| 131 | + val dataset = listOf<Tensor>( |
| 132 | + Tensor.fromBlob(floatArrayOf(1.0f, 1.0f), longArrayOf(1, 2)), |
| 133 | + Tensor.fromBlob(longArrayOf(0), longArrayOf(1)), |
| 134 | + Tensor.fromBlob(floatArrayOf(0.0f, 0.0f), longArrayOf(1, 2)), |
| 135 | + Tensor.fromBlob(longArrayOf(0), longArrayOf(1)), |
| 136 | + Tensor.fromBlob(floatArrayOf(1.0f, 0.0f), longArrayOf(1, 2)), |
| 137 | + Tensor.fromBlob(longArrayOf(1), longArrayOf(1)), |
| 138 | + Tensor.fromBlob(floatArrayOf(0.0f, 1.0f), longArrayOf(1, 2)), |
| 139 | + Tensor.fromBlob(longArrayOf(1), longArrayOf(1)), |
| 140 | + ) |
| 141 | + |
| 142 | + val numEpochs = 5000; |
| 143 | + var finalLoss = Float.MAX_VALUE |
| 144 | + |
| 145 | + for (i in 0 until numEpochs) { |
| 146 | + val inputDex = 2 * Random.nextInt(dataset.size / 2) |
| 147 | + val targetDex = inputDex + 1 |
| 148 | + val input = dataset.get(inputDex) |
| 149 | + val target = dataset.get(targetDex) |
| 150 | + val out = module.executeForwardBackward("forward", EValue.from(input), EValue.from(target)) |
| 151 | + val gradients = module.namedGradients("forward") |
| 152 | + |
| 153 | + if (i == 0) { |
| 154 | + Assert.assertEquals(4, gradients.size) |
| 155 | + assertContains(gradients, LIN_WEIGHT) |
| 156 | + assertContains(gradients, LIN_BIAS) |
| 157 | + assertContains(gradients, LIN2_WEIGHT) |
| 158 | + assertContains(gradients, LIN2_BIAS) |
| 159 | + } |
| 160 | + |
| 161 | + if (i % 500 == 0 || i == numEpochs - 1) { |
| 162 | + Log.i( |
| 163 | + "testTrainXOR_PTEOnly", |
| 164 | + String.format( |
| 165 | + "Step %d, Loss %f, Input [%.0f, %.0f], Prediction %d, Label %d", |
| 166 | + i, |
| 167 | + out[0].toTensor().getDataAsFloatArray()[0], |
| 168 | + input.getDataAsFloatArray()[0], |
| 169 | + input.getDataAsFloatArray()[1], |
| 170 | + out[1].toTensor().getDataAsLongArray()[0], |
| 171 | + target.getDataAsLongArray()[0])); |
| 172 | + } |
| 173 | + |
| 174 | + sgd.step(gradients) |
| 175 | + |
| 176 | + if (i == numEpochs - 1) { |
| 177 | + finalLoss = out[0].toTensor().dataAsFloatArray[0] |
| 178 | + } |
| 179 | + } |
| 180 | + Assert.assertTrue(finalLoss < 0.1f) |
| 181 | + } |
| 182 | + |
| 183 | + companion object { |
| 184 | + private const val LIN_WEIGHT = "net.linear.weight" |
| 185 | + private const val LIN_BIAS = "net.linear.bias" |
| 186 | + private const val LIN2_WEIGHT = "net.linear2.weight" |
| 187 | + private const val LIN2_BIAS = "net.linear2.bias" |
| 188 | + } |
| 189 | +} |
0 commit comments