mantatech-sdk 0.5b0.dev65__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (54) hide show
  1. manta/__init__.light.py +22 -0
  2. manta/__init__.py +83 -0
  3. manta/__main__.py +21 -0
  4. manta/apis/__init__.py +7 -0
  5. manta/apis/async_user_api.py +6458 -0
  6. manta/apis/graph.py +498 -0
  7. manta/apis/module.py +316 -0
  8. manta/apis/results.py +251 -0
  9. manta/apis/swarm.py +206 -0
  10. manta/apis/user_api.py +1016 -0
  11. manta/cli/__init__.py +1 -0
  12. manta/cli/commands/__init__.py +1 -0
  13. manta/cli/commands/base_handler.py +229 -0
  14. manta/cli/commands/doc.py +192 -0
  15. manta/cli/commands/install.py +346 -0
  16. manta/cli/commands/sdk.py +9 -0
  17. manta/cli/commands/sdk_cluster.py +211 -0
  18. manta/cli/commands/sdk_config.py +347 -0
  19. manta/cli/commands/sdk_globals.py +280 -0
  20. manta/cli/commands/sdk_logs.py +174 -0
  21. manta/cli/commands/sdk_main.py +167 -0
  22. manta/cli/commands/sdk_module.py +516 -0
  23. manta/cli/commands/sdk_nodes.py +168 -0
  24. manta/cli/commands/sdk_original.py +3873 -0
  25. manta/cli/commands/sdk_results.py +265 -0
  26. manta/cli/commands/sdk_swarm.py +454 -0
  27. manta/cli/commands/sdk_user.py +234 -0
  28. manta/cli/commands/status.py +292 -0
  29. manta/cli/component_detector.py +112 -0
  30. manta/cli/config_manager.py +445 -0
  31. manta/cli/main.py +265 -0
  32. manta/cli/utils/__init__.py +27 -0
  33. manta/cli/utils/converters.py +140 -0
  34. manta/clients/cluster_management_client.py +486 -0
  35. manta/clients/local_client.py +149 -0
  36. manta/clients/module_management_client.py +217 -0
  37. manta/clients/swarm_management_client.py +562 -0
  38. manta/clients/user_management_client.py +395 -0
  39. manta/clients/world_client.py +195 -0
  40. manta/light/__init__.py +31 -0
  41. manta/light/globals.py +245 -0
  42. manta/light/local.py +407 -0
  43. manta/light/logging_config.py +39 -0
  44. manta/light/path.py +116 -0
  45. manta/light/results.py +236 -0
  46. manta/light/task.py +100 -0
  47. manta/light/utils.py +217 -0
  48. manta/light/world.py +177 -0
  49. mantatech_sdk-0.5b0.dev65.dist-info/METADATA +1039 -0
  50. mantatech_sdk-0.5b0.dev65.dist-info/RECORD +54 -0
  51. mantatech_sdk-0.5b0.dev65.dist-info/WHEEL +5 -0
  52. mantatech_sdk-0.5b0.dev65.dist-info/entry_points.txt +2 -0
  53. mantatech_sdk-0.5b0.dev65.dist-info/licenses/LICENSE +683 -0
  54. mantatech_sdk-0.5b0.dev65.dist-info/top_level.txt +1 -0
manta/apis/swarm.py ADDED
@@ -0,0 +1,206 @@
1
+ import io
2
+ from collections.abc import Sequence
3
+ from typing import Any, Dict, Iterable, List, Union
4
+
5
+ from manta_common.build.common.results import Global
6
+ from manta_common.build.common.system import Network, Networks
7
+ from manta_common.const import CHUNK_SIZE
8
+ from manta_common.conversions import ID, dict_to_bytes
9
+ from .graph import Driver, Task, fold
10
+
11
+
12
+ class Swarm:
13
+ """
14
+ This class defines a decentralized algorithm which must be sent
15
+ to a cluster
16
+
17
+ Attributes
18
+ ----------
19
+ globals : Dict[str, Any]
20
+ Initial global values
21
+ networks : Dict[str, str]
22
+ Networks used for peer-to-peer communications
23
+ """
24
+
25
+ name: str = "Swarm"
26
+
27
+ def __init__(self):
28
+ self.globals = {}
29
+ self.networks = {}
30
+
31
+ def execute(self) -> Union[Task, Sequence[Task]]:
32
+ """
33
+ This method must be defined by the User
34
+
35
+ Returns
36
+ -------
37
+ Task
38
+ Task which is the last Task executed in the graph
39
+ before iterating
40
+
41
+ Examples
42
+ --------
43
+
44
+
45
+ >>> def execute(self):
46
+ ... \"\"\"
47
+ ... Generation of the task graph
48
+ ...
49
+ ... +--------+ +------------+ +-----------+ if has_converged
50
+ ... | Worker | --> | Aggregator | --> | Scheduler | ----------------> END PROGRAM
51
+ ... +--------+ +------------+ +-----------+
52
+ ... | | else
53
+ ... +--<<<----------<<<----------<<<----+
54
+ ... \"\"\"
55
+ ... m = self.worker()
56
+ ... m = self.aggregator(m)
57
+ ... return self.scheduler(m)
58
+ """
59
+ raise NotImplementedError(
60
+ f'Swarm [{type(self).__name__}] is missing the required "Swarm" function'
61
+ )
62
+
63
+ def set_global(self, tag: str, value: Union[dict, bytes]):
64
+ """
65
+ Set a global value in the swarm
66
+
67
+ Examples
68
+ --------
69
+
70
+ >>> self.set_global(
71
+ ... "hyperparameters",
72
+ ... {
73
+ ... "epochs": 1,
74
+ ... "batch_size": 32,
75
+ ... "loss": "CrossEntropyLoss",
76
+ ... "loss_params": {},
77
+ ... "optimizer": "SGD",
78
+ ... "optimizer_params": {"lr": 0.01, "momentum": 0.9},
79
+ ... },
80
+ ... )
81
+ """
82
+ self.globals[tag] = value
83
+
84
+ def add_network(self, name: str, driver: Driver = Driver.OVERLAY):
85
+ """
86
+ Add network in the swarm
87
+
88
+ Parameters
89
+ ----------
90
+ name : str
91
+ Name of the network
92
+ driver : Driver
93
+ Driver of the network
94
+
95
+ Raises
96
+ ------
97
+ KeyError
98
+ Name of network must be unique
99
+
100
+ Examples
101
+ --------
102
+
103
+ >>> self.add_network(
104
+ ... "docker_network",
105
+ ... Driver.BRIDGE,
106
+ ... )
107
+ """
108
+ if name in self.networks:
109
+ driver = self.networks[name]
110
+ raise KeyError(f"Name of network must be unique (found {name} => {driver})")
111
+ self.networks[name] = driver.value
112
+
113
+ def prepare_graph(self) -> List[Dict[str, Any]]:
114
+ """
115
+ Prepares the graph of the Swarm to a protobuf object
116
+
117
+ Returns
118
+ -------
119
+ List[Dict[str, Any]]
120
+ List of tasks
121
+ """
122
+ output = self.execute()
123
+ if isinstance(output, Sequence) and all(
124
+ map(lambda obj: isinstance(obj, Task), output)
125
+ ):
126
+ graph = fold([module.graph for module in output])
127
+ elif isinstance(output, Task):
128
+ graph = output.graph
129
+ else:
130
+ raise ValueError(
131
+ "Invalid output type. It must be 'Union[Task, Sequence[Task]]'."
132
+ )
133
+ graph.check_networks(self.networks)
134
+ return graph.to_proto()
135
+
136
+ def prepare_globals(self, swarm_id: str) -> Iterable[Iterable[Global]]:
137
+ """
138
+ Generates an iterable of asynchronous generators where each generator
139
+ yields global chunks of a global value.
140
+
141
+ Parameters
142
+ ----------
143
+ swarm_id : str
144
+ Swarm ID
145
+ Iterable[Iterable[Global]]
146
+ Iterable of asynchronous generators of global chunks
147
+ """
148
+ for tag, values in self.globals.items():
149
+ buffer = io.BytesIO(dict_to_bytes(values))
150
+
151
+ def generator():
152
+ while chunk := buffer.read(CHUNK_SIZE):
153
+ yield Global(ID(swarm_id).oid, tag=tag, data=chunk)
154
+
155
+ yield generator()
156
+
157
+ def prepare_networks(self, swarm_id: str) -> Networks:
158
+ """
159
+ Casts networks attribute as protobuf object
160
+
161
+ Parameters
162
+ ----------
163
+ swarm_id : str
164
+ Swarm ID
165
+
166
+ Returns
167
+ -------
168
+ Networks
169
+ Networks protobuf object
170
+ """
171
+ return Networks(
172
+ ID(swarm_id).oid,
173
+ [Network(name, driver) for name, driver in self.networks.items()],
174
+ )
175
+
176
+ def __str__(self) -> str:
177
+ """
178
+ Convert the Swarm to string
179
+
180
+ Returns
181
+ -------
182
+ str
183
+ Swarm formatted into string
184
+ """
185
+ # TODO: use pprint
186
+ try:
187
+ execution = self.execute()
188
+ if isinstance(execution, Task):
189
+ graph = execution.graph
190
+ else:
191
+ graph = fold([task.graph for task in execution])
192
+ string = (
193
+ "Swarm(\n "
194
+ + "\n ".join(str(graph).split("\n"))
195
+ + f",\n Networks({self.networks}),\n"
196
+ + f" Globals(keys: {list(self.globals.keys())}),\n"
197
+ + ")"
198
+ )
199
+ except NotImplementedError:
200
+ string = (
201
+ "Swarm(\n"
202
+ + f" Networks({self.networks}),\n"
203
+ + f" Globals(keys: {list(self.globals.keys())}),\n"
204
+ + ")"
205
+ )
206
+ return string