Using Annotations to Construct and Execute a Graph

The following figure shows the process of using annotations to construct and execute a graph.

Figure 1 Using annotations to construct and execute a graph

The sample code is as follows:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import dataflow as df
import numpy as np

@df.pyflow
def add(in0, in1):
    return in0 + in1

# Initialize the system.
options = {
    "ge.exec.deviceId":"0",
    "ge.experiment.data_flow_deploy_info_path":"./data_flow_deploy_info.json",
    "ge.socVersion": "AscendXXX"  # Modify the version based on the environment.
}
df.init(options)

# Define FlowData.
data0 = df.FlowData()
data1 = df.FlowData()
# Generate a FlowNode by annotating the UDF.
add_node = add.fnode()

# Construct edge connections between FlowData and FlowNode.
add_out = add_node(data0, data1)

# Build a FlowGraph using FlowOut.
dag = df.FlowGraph([add_out])

# Automatically generate a deployment policy based on the graph. All nodes are deployed on the first device in numa_config.
# To make modifications, comment out the following line of code and modify the ./data_flow_deploy_info.json file.
df.utils.generate_deploy_template(dag, "./data_flow_deploy_info.json")

# Call FlowGraph.feed to feed inputs.
dag.feed({data0:np.array([[1, 2]], dtype=np.int32), data1:np.array([[2, 3]], dtype=np.int32)})

# Call FlowGraph.fetch to obtain outputs.
print("dataflow fetch result:", dag.fetch())
# Print: dataflow fetch result: ([array([[3, 5]], dtype=int32)], 0)

# Release system resources.
df.finalize()