unaiverse 0.1.6__cp313-cp313-macosx_11_0_arm64.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.
Potentially problematic release.
This version of unaiverse might be problematic. Click here for more details.
- unaiverse/__init__.py +19 -0
- unaiverse/agent.py +2008 -0
- unaiverse/agent_basics.py +1846 -0
- unaiverse/clock.py +191 -0
- unaiverse/dataprops.py +1209 -0
- unaiverse/hsm.py +1880 -0
- unaiverse/modules/__init__.py +18 -0
- unaiverse/modules/cnu/__init__.py +17 -0
- unaiverse/modules/cnu/cnus.py +536 -0
- unaiverse/modules/cnu/layers.py +261 -0
- unaiverse/modules/cnu/psi.py +60 -0
- unaiverse/modules/hl/__init__.py +15 -0
- unaiverse/modules/hl/hl_utils.py +411 -0
- unaiverse/modules/networks.py +1509 -0
- unaiverse/modules/utils.py +680 -0
- unaiverse/networking/__init__.py +16 -0
- unaiverse/networking/node/__init__.py +18 -0
- unaiverse/networking/node/connpool.py +1261 -0
- unaiverse/networking/node/node.py +2223 -0
- unaiverse/networking/node/profile.py +446 -0
- unaiverse/networking/node/tokens.py +79 -0
- unaiverse/networking/p2p/__init__.py +198 -0
- unaiverse/networking/p2p/go.mod +127 -0
- unaiverse/networking/p2p/go.sum +548 -0
- unaiverse/networking/p2p/golibp2p.py +18 -0
- unaiverse/networking/p2p/golibp2p.pyi +135 -0
- unaiverse/networking/p2p/lib.go +2714 -0
- unaiverse/networking/p2p/lib.go.sha256 +1 -0
- unaiverse/networking/p2p/lib_types.py +312 -0
- unaiverse/networking/p2p/message_pb2.py +63 -0
- unaiverse/networking/p2p/messages.py +265 -0
- unaiverse/networking/p2p/mylogger.py +77 -0
- unaiverse/networking/p2p/p2p.py +929 -0
- unaiverse/networking/p2p/proto-go/message.pb.go +616 -0
- unaiverse/networking/p2p/unailib.cpython-313-darwin.so +0 -0
- unaiverse/streamlib/__init__.py +15 -0
- unaiverse/streamlib/streamlib.py +210 -0
- unaiverse/streams.py +770 -0
- unaiverse/utils/__init__.py +16 -0
- unaiverse/utils/ask_lone_wolf.json +27 -0
- unaiverse/utils/lone_wolf.json +19 -0
- unaiverse/utils/misc.py +305 -0
- unaiverse/utils/sandbox.py +293 -0
- unaiverse/utils/server.py +435 -0
- unaiverse/world.py +175 -0
- unaiverse-0.1.6.dist-info/METADATA +365 -0
- unaiverse-0.1.6.dist-info/RECORD +50 -0
- unaiverse-0.1.6.dist-info/WHEEL +6 -0
- unaiverse-0.1.6.dist-info/licenses/LICENSE +43 -0
- unaiverse-0.1.6.dist-info/top_level.txt +1 -0
unaiverse/clock.py
ADDED
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
"""
|
|
2
|
+
█████ █████ ██████ █████ █████ █████ █████ ██████████ ███████████ █████████ ██████████
|
|
3
|
+
░░███ ░░███ ░░██████ ░░███ ░░███ ░░███ ░░███ ░░███░░░░░█░░███░░░░░███ ███░░░░░███░░███░░░░░█
|
|
4
|
+
░███ ░███ ░███░███ ░███ ██████ ░███ ░███ ░███ ░███ █ ░ ░███ ░███ ░███ ░░░ ░███ █ ░
|
|
5
|
+
░███ ░███ ░███░░███░███ ░░░░░███ ░███ ░███ ░███ ░██████ ░██████████ ░░█████████ ░██████
|
|
6
|
+
░███ ░███ ░███ ░░██████ ███████ ░███ ░░███ ███ ░███░░█ ░███░░░░░███ ░░░░░░░░███ ░███░░█
|
|
7
|
+
░███ ░███ ░███ ░░█████ ███░░███ ░███ ░░░█████░ ░███ ░ █ ░███ ░███ ███ ░███ ░███ ░ █
|
|
8
|
+
░░████████ █████ ░░█████░░████████ █████ ░░███ ██████████ █████ █████░░█████████ ██████████
|
|
9
|
+
░░░░░░░░ ░░░░░ ░░░░░ ░░░░░░░░ ░░░░░ ░░░ ░░░░░░░░░░ ░░░░░ ░░░░░ ░░░░░░░░░ ░░░░░░░░░░
|
|
10
|
+
A Collectionless AI Project (https://collectionless.ai)
|
|
11
|
+
Registration/Login: https://unaiverse.io
|
|
12
|
+
Code Repositories: https://github.com/collectionlessai/
|
|
13
|
+
Main Developers: Stefano Melacci (Project Leader), Christian Di Maio, Tommaso Guidi
|
|
14
|
+
"""
|
|
15
|
+
import ntplib
|
|
16
|
+
import bisect
|
|
17
|
+
import socket
|
|
18
|
+
from ntplib import NTPException
|
|
19
|
+
from datetime import datetime, timezone
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
class Clock:
|
|
23
|
+
"""
|
|
24
|
+
A class for managing time cycles and converting between timestamp and cycle indices.
|
|
25
|
+
|
|
26
|
+
This class interacts with an NTP server to synchronize time and supports operations
|
|
27
|
+
to track cycles, manage timestamps, and calculate the time differences between cycles.
|
|
28
|
+
"""
|
|
29
|
+
|
|
30
|
+
def __init__(self, min_delta: float = -1):
|
|
31
|
+
"""Initialize a Clock instance.
|
|
32
|
+
|
|
33
|
+
Args:
|
|
34
|
+
min_delta (float): Minimum time (in seconds) between consecutive cycles.
|
|
35
|
+
If less than or equal to zero, the cycles will be real-time-based.
|
|
36
|
+
"""
|
|
37
|
+
self.min_delta = min_delta # Min-time passed between consecutive cycles (seconds) - if <=0, it is real-time
|
|
38
|
+
self.cycle = -1 # Internal index, not shared outside (the value -1 is only used at creation/reset time)
|
|
39
|
+
self.__servers = [
|
|
40
|
+
'pool.ntp.org',
|
|
41
|
+
'north-america.pool.ntp.org'
|
|
42
|
+
'asia.pool.ntp.org',
|
|
43
|
+
'europe.pool.ntp.org',
|
|
44
|
+
]
|
|
45
|
+
self.__global_initial_t = self.__get_time_from_server() # Real-time, wall-clock
|
|
46
|
+
if self.__global_initial_t == -1.:
|
|
47
|
+
raise ValueError("Unable to get the initial time (for synchronization purposes) from the NTP servers")
|
|
48
|
+
self.__local_initial_t = datetime.now(timezone.utc).timestamp() # Corresponding local time
|
|
49
|
+
self.__timestamps = [] # List to store timestamps for cycles
|
|
50
|
+
self.__time2cycle_cache = 0 # Cached cycle value for optimization
|
|
51
|
+
|
|
52
|
+
def __get_time_from_server(self) -> float:
|
|
53
|
+
"""Get the current time from an NTP server.
|
|
54
|
+
|
|
55
|
+
Returns:
|
|
56
|
+
float: The time returned by the NTP server, converted to a timestamp.
|
|
57
|
+
"""
|
|
58
|
+
c = ntplib.NTPClient()
|
|
59
|
+
response = None
|
|
60
|
+
for i in range(0, 10):
|
|
61
|
+
try:
|
|
62
|
+
server = self.__servers[i % len(self.__servers)]
|
|
63
|
+
response = c.request(server, version=3)
|
|
64
|
+
break
|
|
65
|
+
except (NTPException, socket.gaierror):
|
|
66
|
+
continue
|
|
67
|
+
if response is not None:
|
|
68
|
+
return datetime.fromtimestamp(response.tx_time, timezone.utc).timestamp()
|
|
69
|
+
else:
|
|
70
|
+
return -1.
|
|
71
|
+
|
|
72
|
+
def __add_timestamp(self, timestamp: float):
|
|
73
|
+
"""Add a timestamp to the list of timestamps for the clock cycles.
|
|
74
|
+
|
|
75
|
+
Args:
|
|
76
|
+
timestamp (float): The timestamp to be added to the list.
|
|
77
|
+
|
|
78
|
+
Raises:
|
|
79
|
+
ValueError: If the provided timestamp is not more recent than the last one.
|
|
80
|
+
"""
|
|
81
|
+
if len(self.__timestamps) == 0 or self.__timestamps[-1] < timestamp:
|
|
82
|
+
self.__timestamps.append(timestamp)
|
|
83
|
+
else:
|
|
84
|
+
raise ValueError("Cannot add a timestamp that is NOT more recent than the already added ones")
|
|
85
|
+
|
|
86
|
+
def time2cycle(self, timestamp: float, delta: float | None = None) -> int:
|
|
87
|
+
"""Convert a given timestamp to the corresponding cycle index.
|
|
88
|
+
|
|
89
|
+
Args:
|
|
90
|
+
timestamp (float): The timestamp to convert.
|
|
91
|
+
delta (float | None): The optional delta value for converting time to cycles.
|
|
92
|
+
|
|
93
|
+
Returns:
|
|
94
|
+
int: The cycle index corresponding to the given timestamp.
|
|
95
|
+
"""
|
|
96
|
+
if delta is not None and delta > 0:
|
|
97
|
+
passed = self.get_time() - timestamp # Precision: microseconds
|
|
98
|
+
return self.cycle - int(passed * delta)
|
|
99
|
+
else:
|
|
100
|
+
self.__time2cycle_cache = Clock.__search(self.__timestamps, timestamp, self.__time2cycle_cache)
|
|
101
|
+
return self.__time2cycle_cache
|
|
102
|
+
|
|
103
|
+
def cycle2time(self, cycle: int, delta: float | None = None) -> float:
|
|
104
|
+
"""Convert a cycle index to the corresponding timestamp.
|
|
105
|
+
|
|
106
|
+
Args:
|
|
107
|
+
cycle (int): The cycle index to convert.
|
|
108
|
+
delta (float | None): The optional delta value for converting cycles to time.
|
|
109
|
+
|
|
110
|
+
Returns:
|
|
111
|
+
float: The timestamp corresponding to the given cycle index.
|
|
112
|
+
"""
|
|
113
|
+
if delta is not None and delta > 0:
|
|
114
|
+
return cycle * delta
|
|
115
|
+
else:
|
|
116
|
+
return self.__timestamps[cycle] if cycle >= 0 else -1.
|
|
117
|
+
|
|
118
|
+
def get_time(self, passed: bool = False) -> float:
|
|
119
|
+
"""Get the current time based on the NTP server synchronization.
|
|
120
|
+
|
|
121
|
+
Returns:
|
|
122
|
+
float: The current synchronized time (in seconds since the Unix epoch).
|
|
123
|
+
"""
|
|
124
|
+
passed_since_beginning = datetime.now(timezone.utc).timestamp() - self.__local_initial_t
|
|
125
|
+
return self.__global_initial_t + passed_since_beginning if not passed else passed_since_beginning
|
|
126
|
+
|
|
127
|
+
def get_time_as_string(self) -> str:
|
|
128
|
+
"""Get the current time as a string (ISO format).
|
|
129
|
+
|
|
130
|
+
Returns:
|
|
131
|
+
str: A string representation of the current time (ISO format, UTC).
|
|
132
|
+
"""
|
|
133
|
+
dt_object = datetime.fromtimestamp(self.get_time(), tz=timezone.utc)
|
|
134
|
+
return dt_object.isoformat(timespec='milliseconds')
|
|
135
|
+
|
|
136
|
+
def get_cycle(self):
|
|
137
|
+
"""Get the current cycle index.
|
|
138
|
+
|
|
139
|
+
Returns:
|
|
140
|
+
int: The current cycle index.
|
|
141
|
+
"""
|
|
142
|
+
return self.cycle
|
|
143
|
+
|
|
144
|
+
def get_cycle_time(self):
|
|
145
|
+
"""Get the timestamp corresponding to the current cycle.
|
|
146
|
+
|
|
147
|
+
Returns:
|
|
148
|
+
float: The timestamp corresponding to the current cycle index.
|
|
149
|
+
"""
|
|
150
|
+
return self.cycle2time(self.cycle)
|
|
151
|
+
|
|
152
|
+
def next_cycle(self) -> bool:
|
|
153
|
+
"""Move to the next cycle if the minimum delta time has passed or if cycles are not constrained.
|
|
154
|
+
|
|
155
|
+
Returns:
|
|
156
|
+
bool: True if the cycle was successfully moved to the next one, False otherwise.
|
|
157
|
+
"""
|
|
158
|
+
if self.cycle >= 0 and (self.min_delta > 0 and len(self.__timestamps) > 0 and
|
|
159
|
+
(self.get_time() - self.__timestamps[-1]) < self.min_delta):
|
|
160
|
+
return False
|
|
161
|
+
else:
|
|
162
|
+
self.cycle += 1 # Increment the cycle index
|
|
163
|
+
self.__add_timestamp(self.get_time())
|
|
164
|
+
return True
|
|
165
|
+
|
|
166
|
+
@staticmethod
|
|
167
|
+
def __search(_list, _target, _last_pos):
|
|
168
|
+
"""Search for a target value in the list of timestamps and return the index of the corresponding cycle.
|
|
169
|
+
|
|
170
|
+
Args:
|
|
171
|
+
_list (list): The list of timestamps.
|
|
172
|
+
_target (float): The target timestamp to search for.
|
|
173
|
+
_last_pos (int): The last search position, used for optimization.
|
|
174
|
+
|
|
175
|
+
Returns:
|
|
176
|
+
int: The index of the found timestamp in the list, or -1 if not found.
|
|
177
|
+
"""
|
|
178
|
+
if len(_list) > 0 and _target > _list[-1]:
|
|
179
|
+
return len(_list)
|
|
180
|
+
if len(_list) > _last_pos and _list[_last_pos] == _target:
|
|
181
|
+
return _last_pos
|
|
182
|
+
elif len(_list) > (_last_pos + 1) and _list[_last_pos + 1] == _target:
|
|
183
|
+
return _last_pos + 1
|
|
184
|
+
elif len(_list) == 0:
|
|
185
|
+
return -1
|
|
186
|
+
else:
|
|
187
|
+
ret = bisect.bisect_left(_list, _target)
|
|
188
|
+
if _list[ret] == _target:
|
|
189
|
+
return ret
|
|
190
|
+
else:
|
|
191
|
+
return -1
|