You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@flink.apache.org by GitBox <gi...@apache.org> on 2022/06/15 03:00:57 UTC
[GitHub] [flink-ml] yunfengzhou-hub opened a new pull request, #110: [FLINK-27096] Optimize KMeans performance
yunfengzhou-hub opened a new pull request, #110:
URL: https://github.com/apache/flink-ml/pull/110
This PR optimizes KMeans Algorithm's performance by reducing data transmission and per-round operator creation overhead.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [flink-ml] lindong28 commented on a diff in pull request #110: [FLINK-27096] Optimize KMeans performance
Posted by GitBox <gi...@apache.org>.
lindong28 commented on code in PR #110:
URL: https://github.com/apache/flink-ml/pull/110#discussion_r901606296
##########
flink-ml-core/src/main/java/org/apache/flink/ml/linalg/VectorWithNorm.java:
##########
@@ -0,0 +1,48 @@
+/*
+ * 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.flink.ml.linalg;
+
+import org.apache.flink.api.common.typeinfo.TypeInfo;
+import org.apache.flink.ml.linalg.typeinfo.VectorWithNormTypeInfoFactory;
+
+/** A vector with its norm. */
+@TypeInfo(VectorWithNormTypeInfoFactory.class)
+public class VectorWithNorm {
+ private final Vector vector;
+
+ public double l2Norm;
Review Comment:
This variable can be final.
##########
flink-ml-core/src/main/java/org/apache/flink/ml/linalg/typeinfo/VectorWithNormSerializer.java:
##########
@@ -0,0 +1,189 @@
+/*
+ * 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.flink.ml.linalg.typeinfo;
+
+import org.apache.flink.api.common.typeutils.SimpleTypeSerializerSnapshot;
+import org.apache.flink.api.common.typeutils.TypeSerializer;
+import org.apache.flink.api.common.typeutils.TypeSerializerSnapshot;
+import org.apache.flink.core.memory.DataInputView;
+import org.apache.flink.core.memory.DataOutputView;
+import org.apache.flink.ml.linalg.DenseVector;
+import org.apache.flink.ml.linalg.SparseVector;
+import org.apache.flink.ml.linalg.Vector;
+import org.apache.flink.ml.linalg.VectorWithNorm;
+
+import java.io.IOException;
+import java.io.UnsupportedEncodingException;
+
+/** Specialized serializer for {@link VectorWithNorm}. */
+public class VectorWithNormSerializer extends TypeSerializer<VectorWithNorm> {
+ private final DenseVectorSerializer denseVectorSerializer = new DenseVectorSerializer();
+
+ private static final SparseVectorSerializer sparseVectorSerializer =
+ SparseVectorSerializer.INSTANCE;
+
+ private static final long serialVersionUID = 1L;
+
+ private static final double[] EMPTY = new double[0];
+
+ @Override
+ public boolean isImmutableType() {
+ return false;
+ }
+
+ @Override
+ public TypeSerializer<VectorWithNorm> duplicate() {
+ return new VectorWithNormSerializer();
+ }
+
+ @Override
+ public VectorWithNorm createInstance() {
+ return new VectorWithNorm(new DenseVector(EMPTY));
+ }
+
+ @Override
+ public VectorWithNorm copy(VectorWithNorm from) {
+ Vector vector;
+ if (from.getVector() instanceof DenseVector) {
+ vector = denseVectorSerializer.copy((DenseVector) from.getVector());
+ } else {
+ vector = sparseVectorSerializer.copy((SparseVector) from.getVector());
+ }
+
+ return new VectorWithNorm(vector, from.getL2Norm());
+ }
+
+ @Override
+ public VectorWithNorm copy(VectorWithNorm from, VectorWithNorm reuse) {
+ Vector vector;
+ if (from.getVector() instanceof DenseVector && reuse.getVector() instanceof DenseVector) {
+ vector =
+ denseVectorSerializer.copy(
+ (DenseVector) from.getVector(), (DenseVector) reuse.getVector());
+ } else if (from.getVector() instanceof SparseVector
+ && reuse.getVector() instanceof SparseVector) {
+ vector =
+ sparseVectorSerializer.copy(
+ (SparseVector) from.getVector(), (SparseVector) reuse.getVector());
+ } else {
+ vector = from.getVector().clone();
+ }
+
+ if (vector == reuse.getVector()) {
+ reuse.l2Norm = from.l2Norm;
Review Comment:
Suppose we have already re-used the vector inside the `vectorWithNorm`, I am not sure we can get an extra non-trivial performance improvement by re-using the `vectorWithNorm` object itself.
It might be better to keep the code simpler (e.g. let `l2Norm` by public final and remove the `if` statement here) if re-using the `vectorWithNorm` have a negligible impact on performance.
##########
flink-ml-core/src/main/java/org/apache/flink/ml/linalg/typeinfo/VectorWithNormTypeInfo.java:
##########
@@ -0,0 +1,83 @@
+/*
+ * 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.flink.ml.linalg.typeinfo;
+
+import org.apache.flink.api.common.ExecutionConfig;
+import org.apache.flink.api.common.typeinfo.TypeInformation;
+import org.apache.flink.api.common.typeutils.TypeSerializer;
+import org.apache.flink.ml.linalg.VectorWithNorm;
+
+/** A {@link TypeInformation} for the {@link VectorWithNorm} type. */
+public class VectorWithNormTypeInfo extends TypeInformation<VectorWithNorm> {
+ @Override
+ public boolean isBasicType() {
+ return false;
+ }
+
+ @Override
+ public boolean isTupleType() {
+ return false;
+ }
+
+ @Override
+ public int getArity() {
+ return 2;
+ }
+
+ @Override
+ public int getTotalFields() {
+ return 2;
+ }
+
+ @Override
+ public Class<VectorWithNorm> getTypeClass() {
+ return VectorWithNorm.class;
+ }
+
+ @Override
+ public boolean isKeyType() {
+ return false;
+ }
+
+ @Override
+ public TypeSerializer<VectorWithNorm> createSerializer(ExecutionConfig executionConfig) {
+ return new VectorWithNormSerializer();
+ }
+
+ @Override
+ public String toString() {
+ return "DenseVectorWithNormType";
Review Comment:
DenseVectorWithNormType -> VectorWithNormTypeInfo
##########
flink-ml-core/src/main/java/org/apache/flink/ml/linalg/VectorWithNorm.java:
##########
@@ -0,0 +1,48 @@
+/*
+ * 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.flink.ml.linalg;
+
+import org.apache.flink.api.common.typeinfo.TypeInfo;
+import org.apache.flink.ml.linalg.typeinfo.VectorWithNormTypeInfoFactory;
+
+/** A vector with its norm. */
+@TypeInfo(VectorWithNormTypeInfoFactory.class)
+public class VectorWithNorm {
+ private final Vector vector;
+
+ public double l2Norm;
+
+ public VectorWithNorm(Vector vector) {
+ this(vector, BLAS.norm2(vector));
+ }
+
+ public VectorWithNorm(Vector vector, double l2Norm) {
+ this.vector = vector;
+ this.l2Norm = l2Norm;
+ }
+
+ public Vector getVector() {
Review Comment:
It seems simpler to make both `vector` and `l2Norm` public final and remove the accessor methods.
##########
flink-ml-core/src/main/java/org/apache/flink/ml/linalg/typeinfo/VectorWithNormSerializer.java:
##########
@@ -0,0 +1,189 @@
+/*
+ * 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.flink.ml.linalg.typeinfo;
+
+import org.apache.flink.api.common.typeutils.SimpleTypeSerializerSnapshot;
+import org.apache.flink.api.common.typeutils.TypeSerializer;
+import org.apache.flink.api.common.typeutils.TypeSerializerSnapshot;
+import org.apache.flink.core.memory.DataInputView;
+import org.apache.flink.core.memory.DataOutputView;
+import org.apache.flink.ml.linalg.DenseVector;
+import org.apache.flink.ml.linalg.SparseVector;
+import org.apache.flink.ml.linalg.Vector;
+import org.apache.flink.ml.linalg.VectorWithNorm;
+
+import java.io.IOException;
+import java.io.UnsupportedEncodingException;
+
+/** Specialized serializer for {@link VectorWithNorm}. */
+public class VectorWithNormSerializer extends TypeSerializer<VectorWithNorm> {
+ private final DenseVectorSerializer denseVectorSerializer = new DenseVectorSerializer();
Review Comment:
Could we re-use `VectorSerializer` instead of dealing with `DenseVectorSerializer` and `SparseVectorSerializer` in this class?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [flink-ml] yunfengzhou-hub commented on a diff in pull request #110: [FLINK-27096] Optimize KMeans performance
Posted by GitBox <gi...@apache.org>.
yunfengzhou-hub commented on code in PR #110:
URL: https://github.com/apache/flink-ml/pull/110#discussion_r902069622
##########
flink-ml-core/src/main/java/org/apache/flink/ml/linalg/VectorWithNorm.java:
##########
@@ -0,0 +1,48 @@
+/*
+ * 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.flink.ml.linalg;
+
+import org.apache.flink.api.common.typeinfo.TypeInfo;
+import org.apache.flink.ml.linalg.typeinfo.VectorWithNormTypeInfoFactory;
+
+/** A vector with its norm. */
+@TypeInfo(VectorWithNormTypeInfoFactory.class)
+public class VectorWithNorm {
+ private final Vector vector;
+
+ public double l2Norm;
Review Comment:
`l2Norm` needs to be re-written in `reuse` object in its serializer's `deserialize` and `copy` method. Thus it might not be final.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [flink-ml] zhipeng93 commented on a diff in pull request #110: [FLINK-27096] Optimize KMeans performance
Posted by GitBox <gi...@apache.org>.
zhipeng93 commented on code in PR #110:
URL: https://github.com/apache/flink-ml/pull/110#discussion_r902112959
##########
flink-ml-core/src/main/java/org/apache/flink/ml/linalg/BLAS.java:
##########
@@ -113,11 +113,22 @@ private static double dot(SparseVector x, SparseVector y) {
return dotValue;
}
+ public static double norm2(Vector x) {
+ if (x instanceof DenseVector) {
+ return norm2((DenseVector) x);
+ }
+ return norm2((SparseVector) x);
+ }
+
/** \sqrt(\sum_i x_i * x_i) . */
public static double norm2(DenseVector x) {
return JAVA_BLAS.dnrm2(x.size(), x.values, 1);
}
+ public static double norm2(SparseVector x) {
Review Comment:
nits: norm2(SparseVector) and norm2(DenseVector) could be private.
##########
flink-ml-core/src/main/java/org/apache/flink/ml/linalg/typeinfo/DenseVectorSerializer.java:
##########
@@ -84,7 +84,7 @@ public void serialize(DenseVector vector, DataOutputView target) throws IOExcept
target.writeInt(len);
for (int i = 0; i < len; i++) {
- Bits.putDouble(buf, i << 3, vector.values[i]);
+ Bits.putDouble(buf, (i & 127) << 3, vector.values[i]);
Review Comment:
nit: could you update also the source.read() -> source.readFully() in Line#107?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [flink-ml] lindong28 commented on a diff in pull request #110: [FLINK-27096] Optimize KMeans performance
Posted by GitBox <gi...@apache.org>.
lindong28 commented on code in PR #110:
URL: https://github.com/apache/flink-ml/pull/110#discussion_r899170104
##########
flink-ml-lib/src/main/java/org/apache/flink/ml/clustering/kmeans/KMeans.java:
##########
@@ -307,24 +266,41 @@ public void processElement2(StreamRecord<DenseVector[]> streamRecord) throws Exc
@Override
public void onEpochWatermarkIncremented(
- int epochWatermark, Context context, Collector<Tuple2<Integer, DenseVector>> out)
+ int epochWatermark,
+ Context context,
+ Collector<Tuple2<Integer[], DenseVector[]>> out)
throws Exception {
DenseVector[] centroidValues =
Objects.requireNonNull(
OperatorStateUtils.getUniqueElement(centroids, "centroids")
.orElse(null));
+
+ DenseVector[] newCentroids = new DenseVector[centroidValues.length];
+ int[] counts = new int[centroidValues.length];
+ for (int i = 0; i < centroidValues.length; i++) {
+ newCentroids[i] = new DenseVector(centroidValues[i].size());
+ }
+
for (DenseVector point : points.get()) {
int closestCentroidId =
findClosestCentroidId(centroidValues, point, distanceMeasure);
- output.collect(new StreamRecord<>(Tuple2.of(closestCentroidId, point)));
+
+ BLAS.axpy(1.0, point, newCentroids[closestCentroidId]);
+ counts[closestCentroidId]++;
}
+ output.collect(
+ new StreamRecord<>(
+ Tuple2.of(
+ Arrays.stream(counts).boxed().toArray(Integer[]::new),
Review Comment:
`Arrays.stream(...)` is generally much slower than an iterative approach as of now [1]. If the iterative approach is not too complex to write, how about we use the iterative approach here?
[1] https://stackoverflow.com/questions/27925954/is-arrays-streamarray-name-sum-slower-than-iterative-approach
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [flink-ml] zhipeng93 commented on a diff in pull request #110: [FLINK-27096] Optimize KMeans performance
Posted by GitBox <gi...@apache.org>.
zhipeng93 commented on code in PR #110:
URL: https://github.com/apache/flink-ml/pull/110#discussion_r898854960
##########
flink-ml-lib/src/main/java/org/apache/flink/ml/clustering/kmeans/KMeans.java:
##########
@@ -153,39 +149,24 @@ public IterationBodyResult process(
DataStream<Integer> terminationCriteria =
centroids.flatMap(new TerminateOnMaxIter(maxIterationNum));
- DataStream<Tuple2<Integer, DenseVector>> centroidIdAndPoints =
+ DataStream<Tuple2<Integer[], DenseVector[]>> centroidIdAndPoints =
points.connect(centroids.broadcast())
.transform(
- "SelectNearestCentroid",
+ "CentroidsUpdateAccumulator",
new TupleTypeInfo<>(
- BasicTypeInfo.INT_TYPE_INFO,
- DenseVectorTypeInfo.INSTANCE),
- new SelectNearestCentroidOperator(distanceMeasure));
+ BasicArrayTypeInfo.INT_ARRAY_TYPE_INFO,
+ ObjectArrayTypeInfo.getInfoFor(
+ DenseVectorTypeInfo.INSTANCE)),
+ new CentroidsUpdateAccumulator(distanceMeasure));
Review Comment:
One optimization from algorithm side could be pre-compute the norm of each data point and thus accelerate the computing of `findClosestCentroidId`.
A similar solution could be found [1] [2].
[1] https://github.com/alibaba/Alink/blob/9ae07329a9fb77e39c334c09c79e5016b62f59ee/core/src/main/java/com/alibaba/alink/operator/common/distance/FastDistance.java
[2] https://github.com/apache/spark/blob/2fa98a6a0971cafeb98a2148a962cf8d79381842/mllib/src/main/scala/org/apache/spark/mllib/clustering/KMeans.scala#L222
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [flink-ml] zhipeng93 merged pull request #110: [FLINK-27096] Optimize KMeans performance
Posted by GitBox <gi...@apache.org>.
zhipeng93 merged PR #110:
URL: https://github.com/apache/flink-ml/pull/110
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org