|
| 1 | +/* eslint-disable @typescript-eslint/no-explicit-any */ |
| 2 | +import type { ComponentSpec, Value, IOType } from '@/components/component-form'; |
| 3 | + |
| 4 | +const clusterConfig = { |
| 5 | + public_config: { |
| 6 | + ray_fed_config: { |
| 7 | + parties: ['alice', 'bob'], |
| 8 | + addresses: ['alice:8000', 'bob:8000'], |
| 9 | + }, |
| 10 | + spu_configs: [ |
| 11 | + { |
| 12 | + name: 'spu', |
| 13 | + parties: ['alice', 'bob'], |
| 14 | + addresses: ['alice:8001', 'bob:8001'], |
| 15 | + }, |
| 16 | + ], |
| 17 | + }, |
| 18 | + private_config: { self_party: '{self_party}', ray_head_addr: '127.0.0.1:6379' }, |
| 19 | + desc: { |
| 20 | + parties: ['alice', 'bob'], |
| 21 | + devices: [ |
| 22 | + { |
| 23 | + name: 'spu', |
| 24 | + type: 'spu', |
| 25 | + parties: ['alice', 'bob'], |
| 26 | + config: |
| 27 | + '{"runtime_config":{"protocol":"REF2K","field":"FM64"},"link_desc":{"connect_retry_times":60,"connect_retry_interval_ms":1000,"brpc_channel_protocol":"http","brpc_channel_connection_type":"pooled","recv_timeout_ms":1200000,"http_timeout_ms":1200000}}', |
| 28 | + }, |
| 29 | + ], |
| 30 | + }, |
| 31 | +}; |
| 32 | + |
| 33 | +const getAttrValue = (component: ComponentSpec, key: string, value: any) => { |
| 34 | + const commonAttr = (component.attrs || []).find((attr) => attr.name === key); |
| 35 | + if (commonAttr) { |
| 36 | + const type = commonAttr.type; |
| 37 | + switch (type) { |
| 38 | + case 'AT_INT': |
| 39 | + return { i64: value }; |
| 40 | + case 'AT_BOOL': |
| 41 | + return { b: value }; |
| 42 | + case 'AT_STRING': |
| 43 | + return { s: value }; |
| 44 | + default: |
| 45 | + return { s: value }; |
| 46 | + } |
| 47 | + } else { |
| 48 | + // input has no marked data type in the attr, so it can only be fixed |
| 49 | + return { ss: [value] }; |
| 50 | + } |
| 51 | +}; |
| 52 | + |
| 53 | +const getIOMetaType = (type: IOType) => { |
| 54 | + if (type === 'sf.table.individual') { |
| 55 | + return 'type.googleapis.com/secretflow.spec.v1.IndividualTable'; |
| 56 | + } else if (type === 'sf.table.vertical_table') { |
| 57 | + return 'type.googleapis.com/secretflow.spec.v1.VerticalTable'; |
| 58 | + } |
| 59 | + return ''; |
| 60 | +}; |
| 61 | + |
| 62 | +const generateComponentCellCode = (component: ComponentSpec, config: Value) => { |
| 63 | + if (!(component && config)) { |
| 64 | + return ''; |
| 65 | + } |
| 66 | + |
| 67 | + const componentConfig: any = { |
| 68 | + domain: component.domain, |
| 69 | + name: component.name, |
| 70 | + version: component.version, |
| 71 | + attr_paths: [], |
| 72 | + attrs: [], |
| 73 | + inputs: [], |
| 74 | + output_uris: [], |
| 75 | + }; |
| 76 | + |
| 77 | + const { input, output, ...others } = config; |
| 78 | + |
| 79 | + // attr |
| 80 | + Object.entries(others).forEach(([key, value]) => { |
| 81 | + componentConfig.attr_paths.push(key); |
| 82 | + const attrValue = getAttrValue(component, key, value); |
| 83 | + componentConfig.attrs.push(attrValue); |
| 84 | + }); |
| 85 | + |
| 86 | + // input |
| 87 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 88 | + Object.entries(input).forEach(([key, value]: [string, any]) => { |
| 89 | + const { type, tables } = value; |
| 90 | + |
| 91 | + if (type && tables) { |
| 92 | + componentConfig.inputs.push({ |
| 93 | + name: key, |
| 94 | + type: type, |
| 95 | + data_refs: tables.data_ref.map((ref: any) => ({ |
| 96 | + uri: ref.uri, |
| 97 | + party: ref.party, |
| 98 | + format: 'csv', |
| 99 | + })), |
| 100 | + meta: { |
| 101 | + '@type': getIOMetaType(type), |
| 102 | + schema: tables.schema, |
| 103 | + line_count: -1, |
| 104 | + }, |
| 105 | + }); |
| 106 | + } |
| 107 | + }); |
| 108 | + |
| 109 | + // output |
| 110 | + Object.entries(output).forEach(([, value]) => { |
| 111 | + componentConfig.output_uris.push(value); |
| 112 | + }); |
| 113 | + |
| 114 | + return ` |
| 115 | + from secretflow.spec.v1.evaluation_pb2 import NodeEvalParam |
| 116 | + from secretflow.spec.extend.cluster_pb2 import SFClusterConfig |
| 117 | + from secretflow.component.entry import comp_eval |
| 118 | + from google.protobuf.json_format import Parse |
| 119 | + from secretflow.spec.v1.data_pb2 import StorageConfig |
| 120 | + import os |
| 121 | +
|
| 122 | + cluster_config_str = r""" |
| 123 | + ${JSON.stringify(clusterConfig)} |
| 124 | + """ |
| 125 | +
|
| 126 | +
|
| 127 | + component_config_str = r""" |
| 128 | + ${JSON.stringify(componentConfig)} |
| 129 | + """ |
| 130 | +
|
| 131 | + self_party = os.getenv("SELF_PARTY", "alice") |
| 132 | + cluster_config = SFClusterConfig() |
| 133 | + Parse(cluster_config_str.replace('{self_party}', self_party), cluster_config) |
| 134 | +
|
| 135 | + node_eval_config = NodeEvalParam() |
| 136 | + Parse(component_config_str, node_eval_config) |
| 137 | +
|
| 138 | + storage_config = StorageConfig( |
| 139 | + type="local_fs", |
| 140 | + local_fs=StorageConfig.LocalFSConfig(wd="/home/vscode/examples"), |
| 141 | + ) |
| 142 | +
|
| 143 | + res = comp_eval(node_eval_config, storage_config, cluster_config) |
| 144 | +
|
| 145 | + print(f"The execution is complete and the result is: \\n{res}") |
| 146 | + `; |
| 147 | +}; |
| 148 | + |
| 149 | +export { generateComponentCellCode }; |
0 commit comments