Skip to content

Commit 0b955f4

Browse files
authored
Merge pull request #9250 from lassewesth/frp3
migrate FastRP stream
2 parents 9558d30 + 1702b1e commit 0b955f4

File tree

12 files changed

+193
-109
lines changed

12 files changed

+193
-109
lines changed

algo/src/main/java/org/neo4j/gds/algorithms/embeddings/NodeEmbeddingsAlgorithmStreamBusinessFacade.java

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@
2121

2222
import org.neo4j.gds.algorithms.AlgorithmComputationResult;
2323
import org.neo4j.gds.algorithms.StreamComputationResult;
24-
import org.neo4j.gds.embeddings.fastrp.FastRPResult;
25-
import org.neo4j.gds.embeddings.fastrp.FastRPStreamConfig;
2624
import org.neo4j.gds.embeddings.graphsage.algo.GraphSageResult;
2725
import org.neo4j.gds.embeddings.graphsage.algo.GraphSageStreamConfig;
2826
import org.neo4j.gds.embeddings.hashgnn.HashGNNResult;
@@ -61,18 +59,6 @@ public StreamComputationResult<GraphSageResult> graphSage(
6159
return createStreamComputationResult(result);
6260
}
6361

64-
public StreamComputationResult<FastRPResult> fastRP(
65-
String graphName,
66-
FastRPStreamConfig config
67-
) {
68-
var result = this.nodeEmbeddingsAlgorithmsFacade.fastRP(
69-
graphName,
70-
config
71-
);
72-
73-
return createStreamComputationResult(result);
74-
}
75-
7662
public StreamComputationResult<HashGNNResult> hashGNN(
7763
String graphName,
7864
HashGNNStreamConfig config
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Copyright (c) "Neo4j"
3+
* Neo4j Sweden AB [http://neo4j.com]
4+
*
5+
* This file is part of Neo4j.
6+
*
7+
* Neo4j is free software: you can redistribute it and/or modify
8+
* it under the terms of the GNU General Public License as published by
9+
* the Free Software Foundation, either version 3 of the License, or
10+
* (at your option) any later version.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Public License
18+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
19+
*/
20+
package org.neo4j.gds.applications.algorithms.embeddings;
21+
22+
import org.neo4j.gds.api.GraphName;
23+
import org.neo4j.gds.applications.algorithms.machinery.AlgorithmProcessingTemplate;
24+
import org.neo4j.gds.applications.algorithms.machinery.ResultBuilder;
25+
import org.neo4j.gds.embeddings.fastrp.FastRPResult;
26+
import org.neo4j.gds.embeddings.fastrp.FastRPStreamConfig;
27+
28+
import java.util.Optional;
29+
30+
import static org.neo4j.gds.applications.algorithms.metadata.LabelForProgressTracking.FastRP;
31+
32+
public class NodeEmbeddingAlgorithmsStreamModeBusinessFacade {
33+
private final NodeEmbeddingAlgorithmsEstimationModeBusinessFacade estimationFacade;
34+
private final NodeEmbeddingAlgorithms algorithms;
35+
private final AlgorithmProcessingTemplate algorithmProcessingTemplate;
36+
37+
public NodeEmbeddingAlgorithmsStreamModeBusinessFacade(
38+
NodeEmbeddingAlgorithmsEstimationModeBusinessFacade estimationFacade,
39+
NodeEmbeddingAlgorithms algorithms,
40+
AlgorithmProcessingTemplate algorithmProcessingTemplate
41+
) {
42+
this.estimationFacade = estimationFacade;
43+
this.algorithms = algorithms;
44+
this.algorithmProcessingTemplate = algorithmProcessingTemplate;
45+
}
46+
47+
public <RESULT> RESULT fastRP(
48+
GraphName graphName,
49+
FastRPStreamConfig configuration,
50+
ResultBuilder<FastRPStreamConfig, FastRPResult, RESULT, Void> resultBuilder
51+
) {
52+
return algorithmProcessingTemplate.processAlgorithm(
53+
graphName,
54+
configuration,
55+
FastRP,
56+
() -> estimationFacade.fastRP(configuration),
57+
graph -> algorithms.fastRP(graph, configuration),
58+
Optional.empty(),
59+
resultBuilder
60+
);
61+
}
62+
}

applications/facade/src/main/java/org/neo4j/gds/applications/NodeEmbeddingApplications.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.neo4j.gds.applications.algorithms.embeddings.NodeEmbeddingAlgorithmsEstimationModeBusinessFacade;
2424
import org.neo4j.gds.applications.algorithms.embeddings.NodeEmbeddingAlgorithmsMutateModeBusinessFacade;
2525
import org.neo4j.gds.applications.algorithms.embeddings.NodeEmbeddingAlgorithmsStatsModeBusinessFacade;
26+
import org.neo4j.gds.applications.algorithms.embeddings.NodeEmbeddingAlgorithmsStreamModeBusinessFacade;
2627
import org.neo4j.gds.applications.algorithms.machinery.AlgorithmEstimationTemplate;
2728
import org.neo4j.gds.applications.algorithms.machinery.AlgorithmProcessingTemplate;
2829
import org.neo4j.gds.applications.algorithms.machinery.MutateNodeProperty;
@@ -33,15 +34,18 @@ public final class NodeEmbeddingApplications {
3334
private final NodeEmbeddingAlgorithmsEstimationModeBusinessFacade estimationMode;
3435
private final NodeEmbeddingAlgorithmsMutateModeBusinessFacade mutateMode;
3536
private final NodeEmbeddingAlgorithmsStatsModeBusinessFacade statsMode;
37+
private final NodeEmbeddingAlgorithmsStreamModeBusinessFacade streamMode;
3638

3739
private NodeEmbeddingApplications(
3840
NodeEmbeddingAlgorithmsEstimationModeBusinessFacade estimationMode,
3941
NodeEmbeddingAlgorithmsMutateModeBusinessFacade mutateMode,
40-
NodeEmbeddingAlgorithmsStatsModeBusinessFacade statsMode
42+
NodeEmbeddingAlgorithmsStatsModeBusinessFacade statsMode,
43+
NodeEmbeddingAlgorithmsStreamModeBusinessFacade streamMode
4144
) {
4245
this.estimationMode = estimationMode;
4346
this.mutateMode = mutateMode;
4447
this.statsMode = statsMode;
48+
this.streamMode = streamMode;
4549
}
4650

4751
static NodeEmbeddingApplications create(
@@ -68,8 +72,13 @@ static NodeEmbeddingApplications create(
6872
algorithms,
6973
algorithmProcessingTemplate
7074
);
75+
var streamMode = new NodeEmbeddingAlgorithmsStreamModeBusinessFacade(
76+
estimationMode,
77+
algorithms,
78+
algorithmProcessingTemplate
79+
);
7180

72-
return new NodeEmbeddingApplications(estimationMode, mutateMode, statsMode);
81+
return new NodeEmbeddingApplications(estimationMode, mutateMode, statsMode, streamMode);
7382
}
7483

7584
public NodeEmbeddingAlgorithmsEstimationModeBusinessFacade estimate() {
@@ -83,4 +92,8 @@ public NodeEmbeddingAlgorithmsMutateModeBusinessFacade mutate() {
8392
public NodeEmbeddingAlgorithmsStatsModeBusinessFacade stats() {
8493
return statsMode;
8594
}
95+
96+
public NodeEmbeddingAlgorithmsStreamModeBusinessFacade stream() {
97+
return streamMode;
98+
}
8699
}

proc/embeddings/src/main/java/org/neo4j/gds/embeddings/fastrp/FastRPStreamProc.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
import org.neo4j.gds.applications.algorithms.machinery.MemoryEstimateResult;
2323
import org.neo4j.gds.procedures.GraphDataScienceProcedures;
24-
import org.neo4j.gds.procedures.embeddings.fastrp.FastRPStreamResult;
24+
import org.neo4j.gds.procedures.algorithms.embeddings.FastRPStreamResult;
2525
import org.neo4j.procedure.Context;
2626
import org.neo4j.procedure.Description;
2727
import org.neo4j.procedure.Name;
@@ -44,7 +44,7 @@ public Stream<FastRPStreamResult> stream(
4444
@Name(value = "graphName") String graphName,
4545
@Name(value = "configuration", defaultValue = "{}") Map<String, Object> configuration
4646
) {
47-
return facade.oldNodeEmbeddings().fastRP().stream(graphName, configuration);
47+
return facade.algorithms().nodeEmbeddings().fastRPStream(graphName, configuration);
4848
}
4949

5050
@Procedure(value = "gds.fastRP.stream.estimate", mode = READ)
@@ -53,6 +53,6 @@ public Stream<MemoryEstimateResult> estimate(
5353
@Name(value = "graphNameOrConfiguration") Object graphNameOrConfiguration,
5454
@Name(value = "algoConfiguration") Map<String, Object> algoConfiguration
5555
) {
56-
return facade.oldNodeEmbeddings().fastRP().streamEstimate(graphNameOrConfiguration, algoConfiguration);
56+
return facade.algorithms().nodeEmbeddings().fastRPStreamEstimate(graphNameOrConfiguration, algoConfiguration);
5757
}
5858
}

proc/embeddings/src/main/java/org/neo4j/gds/embeddings/fastrp/FastRPStreamSpec.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
import org.neo4j.gds.executor.ExecutionContext;
2626
import org.neo4j.gds.executor.GdsCallable;
2727
import org.neo4j.gds.procedures.algorithms.configuration.NewConfigFunction;
28-
import org.neo4j.gds.procedures.embeddings.fastrp.FastRPStreamResult;
28+
import org.neo4j.gds.procedures.algorithms.embeddings.FastRPStreamResult;
2929

3030
import java.util.stream.Stream;
3131

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Copyright (c) "Neo4j"
3+
* Neo4j Sweden AB [http://neo4j.com]
4+
*
5+
* This file is part of Neo4j.
6+
*
7+
* Neo4j is free software: you can redistribute it and/or modify
8+
* it under the terms of the GNU General Public License as published by
9+
* the Free Software Foundation, either version 3 of the License, or
10+
* (at your option) any later version.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Public License
18+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
19+
*/
20+
package org.neo4j.gds.procedures.algorithms.embeddings;
21+
22+
import org.neo4j.gds.api.Graph;
23+
import org.neo4j.gds.api.GraphStore;
24+
import org.neo4j.gds.api.IdMap;
25+
import org.neo4j.gds.api.properties.nodes.NodePropertyValuesAdapter;
26+
import org.neo4j.gds.applications.algorithms.machinery.AlgorithmProcessingTimings;
27+
import org.neo4j.gds.applications.algorithms.machinery.ResultBuilder;
28+
import org.neo4j.gds.embeddings.fastrp.FastRPResult;
29+
import org.neo4j.gds.embeddings.fastrp.FastRPStreamConfig;
30+
31+
import java.util.Optional;
32+
import java.util.stream.LongStream;
33+
import java.util.stream.Stream;
34+
35+
class FastRPResultBuilderForStreamMode implements ResultBuilder<FastRPStreamConfig, FastRPResult, Stream<FastRPStreamResult>, Void> {
36+
@Override
37+
public Stream<FastRPStreamResult> build(
38+
Graph graph,
39+
GraphStore graphStore,
40+
FastRPStreamConfig configuration,
41+
Optional<FastRPResult> result,
42+
AlgorithmProcessingTimings timings,
43+
Optional<Void> unused
44+
) {
45+
if (result.isEmpty()) return Stream.empty();
46+
47+
var fastRPResult = result.get();
48+
49+
var nodePropertyValues = NodePropertyValuesAdapter.adapt(fastRPResult.embeddings());
50+
return LongStream
51+
.range(IdMap.START_NODE_ID, nodePropertyValues.nodeCount())
52+
.filter(nodePropertyValues::hasValue)
53+
.mapToObj(nodeId -> FastRPStreamResult.create(
54+
graph.toOriginalNodeId(nodeId),
55+
nodePropertyValues.floatArrayValue(nodeId)
56+
));
57+
}
58+
}

procedures/facade/src/main/java/org/neo4j/gds/procedures/embeddings/fastrp/FastRPStreamResult.java renamed to procedures/algorithms-facade/src/main/java/org/neo4j/gds/procedures/algorithms/embeddings/FastRPStreamResult.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
* You should have received a copy of the GNU General Public License
1818
* along with this program. If not, see <http://www.gnu.org/licenses/>.
1919
*/
20-
package org.neo4j.gds.procedures.embeddings.fastrp;
20+
package org.neo4j.gds.procedures.algorithms.embeddings;
2121

2222
import java.util.ArrayList;
2323
import java.util.List;
@@ -26,11 +26,16 @@ public final class FastRPStreamResult {
2626
public final long nodeId;
2727
public final List<Double> embedding;
2828

29-
public FastRPStreamResult(long nodeId, float[] embedding) {
29+
private FastRPStreamResult(long nodeId, List<Double> embedding) {
3030
this.nodeId = nodeId;
31-
this.embedding = new ArrayList<>(embedding.length);
32-
for (var f : embedding) {
33-
this.embedding.add((double) f);
31+
this.embedding = embedding;
32+
}
33+
34+
static FastRPStreamResult create(long nodeId, float[] embeddingAsArray) {
35+
var embedding = new ArrayList<Double>(embeddingAsArray.length);
36+
for (var f : embeddingAsArray) {
37+
embedding.add((double) f);
3438
}
39+
return new FastRPStreamResult(nodeId, embedding);
3540
}
3641
}

procedures/algorithms-facade/src/main/java/org/neo4j/gds/procedures/algorithms/embeddings/NodeEmbeddingsProcedureFacade.java

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,10 @@
2222
import org.neo4j.gds.applications.ApplicationsFacade;
2323
import org.neo4j.gds.applications.algorithms.embeddings.NodeEmbeddingAlgorithmsEstimationModeBusinessFacade;
2424
import org.neo4j.gds.applications.algorithms.embeddings.NodeEmbeddingAlgorithmsStatsModeBusinessFacade;
25+
import org.neo4j.gds.applications.algorithms.embeddings.NodeEmbeddingAlgorithmsStreamModeBusinessFacade;
2526
import org.neo4j.gds.applications.algorithms.machinery.MemoryEstimateResult;
2627
import org.neo4j.gds.embeddings.fastrp.FastRPStatsConfig;
28+
import org.neo4j.gds.embeddings.fastrp.FastRPStreamConfig;
2729
import org.neo4j.gds.procedures.algorithms.embeddings.stubs.FastRPMutateStub;
2830
import org.neo4j.gds.procedures.algorithms.runners.AlgorithmExecutionScaffolding;
2931
import org.neo4j.gds.procedures.algorithms.runners.EstimationModeRunner;
@@ -39,32 +41,37 @@ public final class NodeEmbeddingsProcedureFacade {
3941

4042
private final EstimationModeRunner estimationMode;
4143
private final AlgorithmExecutionScaffolding algorithmExecutionScaffolding;
44+
private final AlgorithmExecutionScaffolding algorithmExecutionScaffoldingForStreamMode;
4245

4346
private NodeEmbeddingsProcedureFacade(
4447
FastRPMutateStub fastRPMutateStub,
4548
ApplicationsFacade applicationsFacade,
4649
EstimationModeRunner estimationMode,
47-
AlgorithmExecutionScaffolding algorithmExecutionScaffolding
50+
AlgorithmExecutionScaffolding algorithmExecutionScaffolding,
51+
AlgorithmExecutionScaffolding algorithmExecutionScaffoldingForStreamMode
4852
) {
4953
this.fastRPMutateStub = fastRPMutateStub;
5054
this.applicationsFacade = applicationsFacade;
5155
this.estimationMode = estimationMode;
5256
this.algorithmExecutionScaffolding = algorithmExecutionScaffolding;
57+
this.algorithmExecutionScaffoldingForStreamMode = algorithmExecutionScaffoldingForStreamMode;
5358
}
5459

5560
public static NodeEmbeddingsProcedureFacade create(
5661
GenericStub genericStub,
5762
ApplicationsFacade applicationsFacade,
5863
EstimationModeRunner estimationModeRunner,
59-
AlgorithmExecutionScaffolding algorithmExecutionScaffolding
64+
AlgorithmExecutionScaffolding algorithmExecutionScaffolding,
65+
AlgorithmExecutionScaffolding algorithmExecutionScaffoldingForStreamMode
6066
) {
6167
var fastRPMutateStub = new FastRPMutateStub(genericStub, applicationsFacade);
6268

6369
return new NodeEmbeddingsProcedureFacade(
6470
fastRPMutateStub,
6571
applicationsFacade,
6672
estimationModeRunner,
67-
algorithmExecutionScaffolding
73+
algorithmExecutionScaffolding,
74+
algorithmExecutionScaffoldingForStreamMode
6875
);
6976
}
7077

@@ -100,11 +107,43 @@ public Stream<MemoryEstimateResult> fastRPStatsEstimate(
100107
return Stream.of(result);
101108
}
102109

110+
public Stream<FastRPStreamResult> fastRPStream(
111+
String graphName,
112+
Map<String, Object> configuration
113+
) {
114+
var resultBuilder = new FastRPResultBuilderForStreamMode();
115+
116+
return algorithmExecutionScaffoldingForStreamMode.runAlgorithm(
117+
graphName,
118+
configuration,
119+
FastRPStreamConfig::of,
120+
streamMode()::fastRP,
121+
resultBuilder
122+
);
123+
}
124+
125+
public Stream<MemoryEstimateResult> fastRPStreamEstimate(
126+
Object graphNameOrConfiguration,
127+
Map<String, Object> algorithmConfiguration
128+
) {
129+
var result = estimationMode.runEstimation(
130+
algorithmConfiguration,
131+
FastRPStreamConfig::of,
132+
configuration -> estimationMode().fastRP(configuration, graphNameOrConfiguration)
133+
);
134+
135+
return Stream.of(result);
136+
}
137+
103138
private NodeEmbeddingAlgorithmsEstimationModeBusinessFacade estimationMode() {
104139
return applicationsFacade.nodeEmbeddings().estimate();
105140
}
106141

107142
private NodeEmbeddingAlgorithmsStatsModeBusinessFacade statsMode() {
108143
return applicationsFacade.nodeEmbeddings().stats();
109144
}
145+
146+
private NodeEmbeddingAlgorithmsStreamModeBusinessFacade streamMode() {
147+
return applicationsFacade.nodeEmbeddings().stream();
148+
}
110149
}

procedures/facade/src/main/java/org/neo4j/gds/procedures/AlgorithmProcedureFacadeBuilder.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,8 @@ NodeEmbeddingsProcedureFacade createNodeEmbeddingsProcedureFacade() {
158158
genericStub,
159159
applicationsFacade,
160160
estimationModeRunner,
161-
algorithmExecutionScaffolding
161+
algorithmExecutionScaffolding,
162+
algorithmExecutionScaffoldingForStreamMode
162163
);
163164
}
164165

0 commit comments

Comments
 (0)