Skip to content

Commit

Permalink
Weekly sync (#43)
Browse files Browse the repository at this point in the history
* repo-sync-2022-12-02T10:50:14+0800

* Fix merge

* Fix merge

* Fix yacl commit id.
  • Loading branch information
anakinxc authored Dec 2, 2022
1 parent 0d47bb8 commit f5c78de
Show file tree
Hide file tree
Showing 60 changed files with 2,508 additions and 2,349 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
## staging
> please add your unreleased change here.
- [Feature] ECDH-PSI supports white box interconnection mode
- [Feature] Various performance improvements
- [3p] Build with Tensorflow 2.11.0
- [bugfix] Fix various crashes

## 20221116
- [SPU] 0.3.0 release
Expand Down
3 changes: 2 additions & 1 deletion examples/cpp/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,9 @@ spu_cc_binary(
srcs = ["simple_pphlo.cc"],
deps = [
":utils",
"//spu/device:api",
"//spu/device:io",
"//spu/device/pphlo:executor",
"//spu/device/pphlo:pphlo_executor",
"@llvm-project//llvm:Support",
],
)
Expand Down
37 changes: 19 additions & 18 deletions examples/cpp/simple_pphlo.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,36 +21,37 @@
#include "examples/cpp/utils.h"
#include "spdlog/spdlog.h"

#include "spu/device/api.h"
#include "spu/device/io.h"
#include "spu/device/pphlo/executor.h"
#include "spu/device/pphlo/pphlo_executor.h"

// This example demostrates the basic compute functionality of spu vm.
void constant_add(spu::device::Executor* executor) {
void constant_add(spu::HalContext* hctx) {
// Write the assembly, this code simple add two numbers.
// - `%1` is a constant public integer, with dtype int32 and value 1.
// - `%2` is a constant public integer, with dtype int32 and value 2.
// - `%3` is the sum of two integers.
// - `dbg_print` print the value of `%3`
constexpr auto code = R"PPHlo(
func @main() -> () {
constexpr auto code = R"(
func.func @main() -> () {
%0 = "pphlo.constant"() {value = dense<1> : tensor<i32>} : () -> tensor<!pphlo.pub<i32>>
%1 = "pphlo.constant"() {value = dense<2> : tensor<i32>} : () -> tensor<!pphlo.pub<i32>>
%2 = "pphlo.add"(%0, %1) : (tensor<!pphlo.pub<i32>>, tensor<!pphlo.pub<i32>>) -> tensor<!pphlo.pub<i32>>
"pphlo.dbg_print"(%2) : (tensor<!pphlo.pub<i32>>) -> ()
return
}
)PPHlo";
})";

// Run it, with no input and output, (since the program does not contain IO)
spu::device::SymbolTable env;
executor->runWithEnv(code, {}, {}, &env);
spu::device::pphlo::PPHloExecutor executor;
spu::device::execute(&executor, hctx, code, {}, {}, &env);
}

// This example demostrates how to pass parameters.
void parameters(spu::device::Executor* executor) {
void parameters(spu::HalContext* hctx) {
// In this example, data owner also participates the computation progress,
// which is called "colocated mode" in spu system.
spu::device::ColocatedIo cio(executor->getContext());
spu::device::ColocatedIo cio(hctx);

if (cio.getRank() == 0) {
// rank-0, set a float variable 3.14 as 'x' to the device.
Expand All @@ -72,29 +73,29 @@ void parameters(spu::device::Executor* executor) {
// - `%3` is the product of two values, it will do auto type promotion.
// - `dbg_print` print the value of `%3`
constexpr auto code = R"PPHlo(
func @main(%arg0: tensor<!pphlo.sec<i32>>, %arg1: tensor<!pphlo.sec<i32>>) -> () {
%0 = "pphlo.multiply"(%arg0, %arg1) : (tensor<!pphlo.sec<i32>>, tensor<!pphlo.sec<i32>>) -> tensor<!pphlo.sec<i32>>
"pphlo.dbg_print"(%0) : (tensor<!pphlo.sec<i32>>) -> ()
func.func @main(%arg0: tensor<!pphlo.sec<f32>>, %arg1: tensor<!pphlo.sec<i32>>) -> () {
%0 = "pphlo.multiply"(%arg0, %arg1) : (tensor<!pphlo.sec<f32>>, tensor<!pphlo.sec<i32>>) -> tensor<!pphlo.sec<f32>>
"pphlo.dbg_print"(%0) : (tensor<!pphlo.sec<f32>>) -> ()
return
}
)PPHlo";
})PPHlo";

// run the assembly, with
// - "x" binding to the first parameter (position 0).
// - "y" binding to the second parameter (position 1).
// - there is no output bindings.
executor->runWithEnv(code, {"x", "y"}, {}, &cio.deviceSymbols());
spu::device::pphlo::PPHloExecutor executor;
spu::device::execute(&executor, hctx, code, {"x", "y"}, {},
&cio.deviceSymbols());
}

int main(int argc, char** argv) {
llvm::cl::ParseCommandLineOptions(argc, argv);

auto hctx = MakeHalContext();
spu::device::pphlo::PPHloExecutor executor(hctx.get());

parameters(&executor);
parameters(hctx.get());

constant_add(&executor);
constant_add(hctx.get());

return 0;
}
2 changes: 1 addition & 1 deletion examples/python/millionare.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
# > bazel run -c opt //examples/python/utils:nodectl -- up
#
# Run this example script.
# > bazel run //examples/python:millionare
# > bazel run -c opt //examples/python:millionare


import argparse
Expand Down
2 changes: 1 addition & 1 deletion examples/python/ml/flax_mlp.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
# > bazel run -c opt //examples/python/utils:nodectl -- up
#
# Run this example script.
# > bazel run //examples/python/ml:flax_mlp
# > bazel run -c opt //examples/python/ml:flax_mlp

import argparse
import json
Expand Down
2 changes: 1 addition & 1 deletion examples/python/ml/jax_lr.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
# > bazel run -c opt //examples/python/utils:nodectl -- up
#
# Run this example script.
# > bazel run //examples/python/ml:jax_lr
# > bazel run -c opt //examples/python/ml:jax_lr


import argparse
Expand Down
2 changes: 1 addition & 1 deletion examples/python/ml/ss_lr.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
# > bazel run -c opt //examples/python/utils:nodectl -- up
#
# Run this example script.
# > bazel run //examples/python/ml:ss_lr
# > bazel run -c opt //examples/python/ml:ss_lr

import argparse
import json
Expand Down
2 changes: 1 addition & 1 deletion examples/python/ml/ss_xgb.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
# > bazel run -c opt //examples/python/utils:nodectl -- up
#
# Run this example script.
# > bazel run //examples/python/ml:ss_xgb
# > bazel run -c opt //examples/python/ml:ss_xgb

import argparse
import json
Expand Down
2 changes: 1 addition & 1 deletion examples/python/ml/stax_mnist_classifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
# > bazel run -c opt //examples/python/utils:nodectl -- up
#
# Run this example script.
# > bazel run //examples/python/ml:stax_mnist_classifier
# > bazel run -c opt //examples/python/ml:stax_mnist_classifier

import time
import itertools
Expand Down
12 changes: 6 additions & 6 deletions examples/python/ml/stax_nn.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,12 @@
import argparse

parser = argparse.ArgumentParser(description='distributed driver.')
parser.add_argument("--model", default='network_a')
parser.add_argument("-c", "--config", default="examples/python/conf/3pc.json")
parser.add_argument("-l", "--learning_rate", default=0.01)
parser.add_argument("-e", "--epoch", default=5)
parser.add_argument("-b", "--batch_size", default=128)
parser.add_argument("-o", "--optimizer", default="SGD")
parser.add_argument("--model", default='network_a', type=str)
parser.add_argument("-c", "--config", default="examples/python/conf/3pc.json", type=str)
parser.add_argument("-l", "--learning_rate", default=0.01, type=float)
parser.add_argument("-e", "--epoch", default=5, type=int)
parser.add_argument("-b", "--batch_size", default=128, type=int)
parser.add_argument("-o", "--optimizer", default="SGD", type=str)
args = parser.parse_args()

# Follows https://arxiv.org/pdf/2107.00501.pdf Appendix C.
Expand Down
2 changes: 1 addition & 1 deletion examples/python/ml/tf_experiment.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
# > bazel run -c opt //examples/python/utils:nodectl -- up
#
# Run this example script.
# > bazel run //examples/python/ml:tf_experiment
# > bazel run -c opt //examples/python/ml:tf_experiment
# This example is tf counterpart to //examples/python/ml:jax_lr


Expand Down
2 changes: 1 addition & 1 deletion examples/python/stats/corr.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
# > bazel run -c opt //examples/python/utils:nodectl -- up
#
# Run this example script.
# > bazel run //examples/python/stats:corr
# > bazel run -c opt //examples/python/stats:corr

import argparse
import json
Expand Down
2 changes: 1 addition & 1 deletion examples/python/stats/pvalue.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
# > bazel run -c opt //examples/python/utils:nodectl -- up
#
# Run this example script.
# > bazel run //examples/python/stats:pvalue
# > bazel run -c opt //examples/python/stats:pvalue


import argparse
Expand Down
2 changes: 1 addition & 1 deletion examples/python/stats/woe.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
# > bazel run -c opt //examples/python/utils:nodectl -- up
#
# Run this example script.
# > bazel run //examples/python/stats:woe
# > bazel run -c opt //examples/python/stats:woe


import argparse
Expand Down
2 changes: 1 addition & 1 deletion spu/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ load("//bazel:spu.bzl", "spu_version_file")

package(default_visibility = ["//visibility:public"])

SPU_VERSION = "0.3.1b0"
SPU_VERSION = "0.3.1b2"

proto_library(
name = "spu_proto",
Expand Down
3 changes: 2 additions & 1 deletion spu/binding/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,9 @@ pybind_extension(
":version_script.lds",
"//spu/compiler:compile",
"//spu/compiler/common:compilation_context",
"//spu/device:api",
"//spu/device:io",
"//spu/device/pphlo:executor",
"//spu/device/pphlo:pphlo_executor",
"//spu/psi:bucket_psi",
"//spu/psi:memory_psi",
"@yacl//yacl/link",
Expand Down
10 changes: 5 additions & 5 deletions spu/binding/_lib.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@
#include "spu/compiler/common/compilation_context.h"
#include "spu/compiler/compile.h"
#include "spu/core/type_util.h"
#include "spu/device/api.h"
#include "spu/device/io.h"
#include "spu/device/pphlo/executor.h"
#include "spu/device/pphlo/pphlo_executor.h"
#include "spu/kernel/context.h"
#include "spu/kernel/value.h"
#include "spu/psi/bucket_psi.h"
Expand Down Expand Up @@ -205,8 +206,7 @@ void BindLink(py::module& m) {
});
}

// Wrap Processor, it's workaround for protobuf pybind11/protoc conflict.

// Wrap Runtime, it's workaround for protobuf pybind11/protoc conflict.
class RuntimeWrapper {
std::unique_ptr<spu::HalContext> hctx_;

Expand All @@ -226,8 +226,8 @@ class RuntimeWrapper {
spu::ExecutableProto exec;
YACL_ENFORCE(exec.ParseFromString(exec_pb));

spu::device::pphlo::PPHloExecutor executor(hctx_.get());
executor.runWithEnv(exec, &env_);
spu::device::pphlo::PPHloExecutor executor;
spu::device::execute(&executor, hctx_.get(), exec, &env_);
}

void SetVar(const std::string& name, const py::bytes& value) {
Expand Down
12 changes: 4 additions & 8 deletions spu/binding/util/frontend_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,12 @@ def test_jax_compile(self):
self.assertEqual(executable.name, "add")
self.assertEqual(executable.input_names, ["in1", "in2"])
self.assertEqual(executable.output_names, ["test-out0"])
self.assertMultiLineEqual(
executable.code.decode(),
"module @xla_computation_add {\n"
self.assertTrue(
" func.func @main(%arg0: tensor<2x!pphlo.pub<i32>>,"
" %arg1: tensor<2x!pphlo.pub<i32>>) -> tensor<2x!pphlo.pub<i32>> {\n"
" %0 = \"pphlo.add\"(%arg0, %arg1) : (tensor<2x!pphlo.pub<i32>>,"
" tensor<2x!pphlo.pub<i32>>) -> tensor<2x!pphlo.pub<i32>>\n"
" return %0 : tensor<2x!pphlo.pub<i32>>\n }\n}\n",
" return %0 : tensor<2x!pphlo.pub<i32>>\n }" in executable.code.decode()
)
self.assertEqual(output.shape, (2,))
self.assertEqual(output.dtype, np.dtype("int32"))
Expand All @@ -62,14 +60,12 @@ def test_tf_compile(self):
self.assertEqual(executable.name, "add")
self.assertEqual(executable.input_names, ["in1", "in2"])
self.assertEqual(executable.output_names, ["test-out0"])
self.assertMultiLineEqual(
executable.code.decode(),
"module @a_inference_add_9__.9 {\n"
self.assertTrue(
" func.func @main(%arg0: tensor<2x!pphlo.pub<i64>>,"
" %arg1: tensor<2x!pphlo.pub<i64>>) -> tensor<2x!pphlo.pub<i64>> {\n"
" %0 = \"pphlo.add\"(%arg0, %arg1) : (tensor<2x!pphlo.pub<i64>>,"
" tensor<2x!pphlo.pub<i64>>) -> tensor<2x!pphlo.pub<i64>>\n"
" return %0 : tensor<2x!pphlo.pub<i64>>\n }\n}\n",
" return %0 : tensor<2x!pphlo.pub<i64>>\n }" in executable.code.decode()
)
self.assertEqual(output.shape, (2,))
self.assertEqual(output.dtype, np.dtype("int64"))
Expand Down
3 changes: 3 additions & 0 deletions spu/compiler/core/core.cc
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ void Core::buildPipeline(mlir::PassManager *pm) {
optPM.addPass(mlir::pphlo::createOptimizeMaxPoolingPass());
optPM.addPass(mlir::pphlo::createDecomposeComparisonPass());
optPM.addPass(mlir::pphlo::createDecomposeMinMaxPass());

optPM.addPass(mlir::createCSEPass());

optPM.addPass(mlir::pphlo::createReduceTruncationPass());
optPM.addPass(mlir::pphlo::createLowerMixedTypeOpPass());

Expand Down
Loading

0 comments on commit f5c78de

Please sign in to comment.