forked from intel-analytics/ipex-llm
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge ckks in FLServer LR Aggregator (intel-analytics#7)
* update ckks in FLServer LR Aggregator * some changes * support ckks at scala FL Server * add * add * fix and add ut * fix and add ut * add support for customized client module and divide example to 2 parties * add comments
- Loading branch information
1 parent
f39805b
commit a948a81
Showing
20 changed files
with
806 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
...c/main/scala/com/intel/analytics/bigdl/ppml/fl/algorithms/VFLLogisticRegressionCkks.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
* Copyright 2016 The BigDL Authors. | ||
* | ||
* Licensed 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 com.intel.analytics.bigdl.ppml.fl.algorithms | ||
|
||
import com.intel.analytics.bigdl.Module | ||
import com.intel.analytics.bigdl.dllib.nn.{Linear, Sequential} | ||
import com.intel.analytics.bigdl.dllib.optim.Adam | ||
import com.intel.analytics.bigdl.dllib.utils.Log4Error | ||
import com.intel.analytics.bigdl.ppml.fl.NNModel | ||
import com.intel.analytics.bigdl.ppml.fl.nn.VFLNNEstimator | ||
import com.intel.analytics.bigdl.ppml.fl.utils.FLClientClosable | ||
|
||
/** | ||
* VFL Logistic Regression | ||
* @param featureNum | ||
* @param learningRate | ||
*/ | ||
class VFLLogisticRegressionCkks(featureNum: Int = -1, | ||
learningRate: Float = 0.005f, | ||
customModel: Module[Float] = null) extends NNModel () { | ||
Log4Error.invalidInputError (featureNum != -1 || customModel != null, | ||
"Either featureNum or customModel should be provided") | ||
val clientModule = if (customModel == null) { | ||
Linear[Float] (featureNum, 1) | ||
} else customModel | ||
val model = Sequential[Float]().add(Linear(featureNum, 1)) | ||
override val estimator = new VFLNNEstimator( | ||
"vfl_logistic_regression_ckks", model, new Adam(learningRate)) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
scala/ppml/src/main/scala/com/intel/analytics/bigdl/ppml/fl/example/ckks/Client.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package com.intel.analytics.bigdl.ppml.fl.example.ckks | ||
|
||
import com.intel.analytics.bigdl.dllib.NNContext | ||
import com.intel.analytics.bigdl.dllib.nn.SparseLinear | ||
import com.intel.analytics.bigdl.dllib.utils.{Log4Error, RandomGenerator} | ||
import com.intel.analytics.bigdl.ppml.fl.NNModel | ||
import com.intel.analytics.bigdl.ppml.fl.algorithms.{VFLLogisticRegression, VFLLogisticRegressionCkks} | ||
import com.intel.analytics.bigdl.ppml.fl.utils.FlContextForTest | ||
import org.apache.spark.sql.SparkSession | ||
|
||
class Client(trainDataPath: String, | ||
testDataPath: String, | ||
clientId: Int, | ||
appName: String) extends Thread { | ||
override def run(): Unit = { | ||
val testFlContext = new FlContextForTest() | ||
testFlContext.initFLContext(clientId.toString, "localhost:8980") | ||
val sqlContext = SparkSession.builder().getOrCreate() | ||
val pre = new DataPreprocessing(sqlContext, trainDataPath, testDataPath, clientId) | ||
val (trainDataset, validationDataset) = pre.loadCensusData() | ||
|
||
val numFeature = 3049 | ||
|
||
RandomGenerator.RNG.setSeed(2L) | ||
val linear = SparseLinear[Float](numFeature, 1) | ||
|
||
val lr: NNModel = appName match { | ||
case "dllib" => new VFLLogisticRegression(learningRate = 0.001f, customModel = linear) | ||
case "ckks" => new VFLLogisticRegressionCkks(learningRate = 0.001f, customModel = linear) | ||
case _ => throw new Error() | ||
} | ||
|
||
|
||
val epochNum = 20 | ||
var accTime: Long = 0 | ||
(0 until epochNum).foreach { epoch => | ||
trainDataset.shuffle() | ||
val trainData = trainDataset.toLocal().data(false) | ||
while (trainData.hasNext) { | ||
val miniBatch = trainData.next() | ||
val input = miniBatch.getInput() | ||
val currentBs = input.toTensor[Float].size(1) | ||
val target = miniBatch.getTarget() | ||
val dllibStart = System.nanoTime() | ||
lr.trainStep(input, target) | ||
accTime += System.nanoTime() - dllibStart | ||
} | ||
println(s"$appName Time: " + accTime / 1e9) | ||
} | ||
|
||
linear.evaluate() | ||
val evalData = validationDataset.toLocal().data(false) | ||
var accDllib = 0 | ||
while (evalData.hasNext) { | ||
val miniBatch = evalData.next() | ||
val input = miniBatch.getInput() | ||
val currentBs = input.toTensor[Float].size(1) | ||
val target = miniBatch.getTarget().toTensor[Float] | ||
val predict = lr.predictStep(input) | ||
println(s"Predicting $appName") | ||
(0 until currentBs).foreach { i => | ||
val dllibPre = predict.toTensor[Float].valueAt(i) | ||
val t = target.valueAt(i + 1, 1) | ||
if (t == 0) { | ||
if (dllibPre <= 0.5) { | ||
accDllib += 1 | ||
} | ||
} else { | ||
if (dllibPre > 0.5) { | ||
accDllib += 1 | ||
} | ||
} | ||
// println(t + " " + dllibPre + " " + ckksPre) | ||
} | ||
} | ||
println(s"$appName predict correct: $accDllib") | ||
|
||
} | ||
} |
Oops, something went wrong.