File size: 11,154 Bytes
2b49274
 
 
8edbd2d
 
a45fa91
c088f6d
2347b7b
a66c947
98273a5
2081073
66a541b
 
f546acb
 
f3c8e5b
66a541b
 
 
 
 
 
 
 
702b6d2
66a541b
 
 
 
 
702b6d2
66a541b
 
 
 
 
fd68806
66a541b
 
4f8645a
 
 
a6871e6
4f8645a
 
fd68806
4f8645a
bafa720
fd68806
4f8645a
 
 
 
 
770b6d4
4f8645a
fd68806
 
4f8645a
44f38b5
 
4f8645a
 
 
2902e3a
 
702b6d2
2902e3a
44f38b5
 
2902e3a
44f38b5
 
2902e3a
 
 
4f8645a
 
66a541b
 
c6bfa86
 
 
f3c8e5b
79b5423
c42414a
2902e3a
269f6e6
429e164
2902e3a
3f81af5
954ef1e
2902e3a
2adc49b
2902e3a
 
a7fe167
2adc49b
 
2902e3a
139a326
2902e3a
 
 
 
 
 
a6871e6
ce5ec0c
2902e3a
3e754a2
ce5ec0c
2adc49b
 
 
2902e3a
 
5445d63
2902e3a
5445d63
ac16ead
2adc49b
04094ba
2222282
2902e3a
 
d6ea7aa
 
3f81af5
d6ea7aa
 
 
2902e3a
dd0c6e0
2902e3a
c088f6d
feaa544
e34a1af
 
04c779d
feaa544
2902e3a
8edbd2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
07de60f
 
8edbd2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42f4878
fdc5945
8edbd2d
 
 
 
 
 
 
 
 
 
0434080
297634e
3923b36
eae7893
516f72b
 
 
 
 
 
eae7893
 
 
feaa544
65d37a7
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
import streamlit as st
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image
import copy
import time
import requests
from time import strftime, localtime


sample_data = {
    "gpus" : {
          "Alice" : 16,
          "Tom" : 16,
          "ㅁㄴㅇㅇ" : 16,
    },
    "total" : {
          "total" : 28,
          "used" : 12
    },
    "chain" : [
      {
        'index': 1, 
        'timestamp': 1701025843.5186985, 
        'transactions': [], 
        'previous_hash': '1'
      }, 
      {
        'index': 2, 
        'timestamp': 1701037845.518921, 
        'transactions': [
            {'id': 'Alice', 'kind': 'add', 'data': '16'}, 
            {'id': 'bob', 'kind': 'add', 'data': '16'}, 
            {'id': 'Alice', 'kind': 'inference', 'data': 'Hello?'}, 
            {'id': 'Alice', 'kind': 'add', 'data': '16'}, 
            {'id': 'bob', 'kind': 'inference', 'data': '16'}
        ], 
        'previous_hash':'5199cc3018287cf3f5fffbb0d1ae3f949256774a2347401818bdc93d29c379e8'
        },
    {
        'index': 3, 
        'timestamp': 1701147846.535392, 
        'transactions': [
            {'id': 'Alice', 'kind': 'add', 'data': '16'}, 
            {'id': 'TOM', 'kind': 'add', 'data': '8'}, 
            {'id': 'Alice', 'kind': 'inference', 'data': 'Hello?'}, 
            {'id': 'Alice', 'kind': 'inference', 'data': '16'}, 
            {'id': 'bob', 'kind': 'inference', 'data': '0'}
        ], 
        'previous_hash':'5199cc3018287cf3f5fffbb0d1ae3f949256774a2347401818bdc93d29c379e8'
        },
    {
        'index': 4, 
        'timestamp': 1701157947.545325, 
        'transactions': [
            {'id': 'Alice', 'kind': 'inference', 'data': '16'}, 
            {'id': 'bob', 'kind': 'inference', 'data': '16'}, 
            {'id': 'Alice', 'kind': 'inference', 'data': 'Hello?'}, 
            {'id': 'Alice', 'kind': 'inference', 'data': '16'}, 
            {'id': 'Nee', 'kind': 'add', 'data': '16'}
        ], 
        'previous_hash':'5199cc3018287cf3f5fffbb0d1ae3f949256774a2347401818bdc93d29c379e8'
        },
    {
        'index': 5, 
        'timestamp': 1701057969.582371, 
        'transactions': [
            {'id': 'Alice', 'kind': 'add', 'data': '16'}, 
            {'id': 'bob', 'kind': 'inference', 'data': '16'}, 
            {'id': 'Alice', 'kind': 'inference', 'data': 'Hello?'}, 
            {'id': 'Alice', 'kind': 'inference', 'data': '16'}, 
            {'id': 'bob', 'kind': 'inference', 'data': '16'}
        ],
        'previous_hash':'5199cc3018287cf3f5fffbb0d1ae3f949256774a2347401818bdc93d29c379e8'
        },
        
    ]
} 

response = requests.post("https://ldhldh-api-for-unity.hf.space/run/predict_7", json={
      "data": [
    ]}).json()
#sample_data = eval(response["data"][0])

print(sample_data)

user_num = len(sample_data['gpus'])

total_gpu = sample_data['total']['total']
used_gpu = min(sample_data['total']['used'],total_gpu*0.87)
timestamp_list = []


name_list = []
timestamp_gpu_data = []
timestamp_inference_data = [0, 0]
timestamp_list_date = []

for block in sample_data['chain']:
    timestamp_list.append(block['timestamp'] )
    for t in block['transactions']:
        if t['kind'] == "add":
            if not t['id'] in name_list:
                name_list.append(t['id'])

timestamp_gpu_data = [[0 for _ in range(len(name_list))]]
timestamp_list_date = [1701115843.5186985, sample_data['chain'][0]['timestamp']]

for block in sample_data['chain']:
    temp = timestamp_gpu_data[int(block['index']) - 1]
    if strftime('%Y-%m-%d', localtime( timestamp_list_date[-1] )) != strftime('%Y-%m-%d', localtime( block['timestamp'] )):
        timestamp_list_date.append(block['timestamp'])
        timestamp_inference_data.append(0)
    
    for t in block['transactions']:
        if t['kind'] == "add":
            temp[name_list.index(t['id'])] += int(t['data'])
        elif t['kind'] == "out":
            temp[name_list.index(t['id'])] = 0
        else:
            timestamp_inference_data[-1] += 1
    timestamp_gpu_data.append(copy.deepcopy(temp))
timestamp_gpu_data = timestamp_gpu_data[0:-1]
col1, col2, col3 = st.columns(3)
col1.metric("Total GPU", f"{total_gpu} GB", f"{user_num} users are sharing now")
if total_gpu != 0:
    col2.metric("Used", f"{used_gpu} GB")
    col3.metric("Percent", f"{round(100 * used_gpu/total_gpu, 2)} %",)
else:
    col2.metric("Used", f"0 GB")
    col3.metric("Percent", f"- %",)

chart_data = pd.DataFrame(timestamp_gpu_data, columns = name_list, index = timestamp_list)

last_timestamp = strftime('%Y-%m-%d %I:%M:%S %p', localtime( sample_data['chain'][-1]['timestamp'] ))
st.title('Gpu pool timeline')
st.area_chart(chart_data)
st.caption(f'last updated in timestamp {last_timestamp}.')



class BubbleChart:
    def __init__(self, data, bubble_spacing=0, n_bins=5):

        unity_ids = list(data.keys())
        share_values = list(data.values())

        area = np.asarray(share_values)
        r = np.sqrt(area / np.pi)

        self.bubble_spacing = bubble_spacing
        self.bubbles = np.ones((len(area), 4))
        self.bubbles[:, 2] = r
        self.bubbles[:, 3] = area
        self.maxstep = 2 * self.bubbles[:, 2].max() + self.bubble_spacing
        self.step_dist = self.maxstep / 2

        length = np.ceil(np.sqrt(len(self.bubbles)))
        grid = np.arange(length) * self.maxstep
        gx, gy = np.meshgrid(grid, grid)
        self.bubbles[:, 0] = gx.flatten()[:len(self.bubbles)]
        self.bubbles[:, 1] = gy.flatten()[:len(self.bubbles)]

        min_size = np.min(area)
        max_size = np.max(area)
        if min_size == max_size:
            min_size = max_size//2

        self.colors = self.quantize_colors(area, min_size, max_size, n_bins)

        self.com = self.center_of_mass()

    def quantize_colors(self, sizes, min_size, max_size, n_bins):

        size_range = max_size - min_size
        bin_size = size_range / n_bins

        quantized_colors = []
        for size in sizes:
            bin_index = min(int((size - min_size) / bin_size), n_bins - 1)
            quantized_colors.append(self.get_color_from_bin(bin_index, n_bins))
            print(f"Size: {size}, Bin Index: {bin_index}, Color: {self.get_color_from_bin(bin_index, n_bins)}")

        return quantized_colors

    def get_color_from_bin(self, bin_index, n_bins):
        # colors = ['#9a9fad', '#848dad', '#7080b5', '#5269b3', '#3654b5']
        colors = ['#42c5ac', '#61d87d', '#a8eb86', '#d3f59a', '#f8ffaf']
        return colors[bin_index]

    def center_of_mass(self):
        return np.average(
            self.bubbles[:, :2], axis=0, weights=self.bubbles[:, 3]
        )

    def center_distance(self, bubble, bubbles):
        return np.hypot(bubble[0] - bubbles[:, 0],
                        bubble[1] - bubbles[:, 1])

    def outline_distance(self, bubble, bubbles):
        center_distance = self.center_distance(bubble, bubbles)
        return center_distance - bubble[2] - \
            bubbles[:, 2] - self.bubble_spacing

    def check_collisions(self, bubble, bubbles):
        distance = self.outline_distance(bubble, bubbles)
        return len(distance[distance < 0])

    def collides_with(self, bubble, bubbles):
        distance = self.outline_distance(bubble, bubbles)
        idx_min = np.argmin(distance)
        return idx_min if type(idx_min) == np.ndarray else [idx_min]

    def collapse(self, n_iterations=50):
        for _i in range(n_iterations):
            moves = 0
            for i in range(len(self.bubbles)):
                rest_bub = np.delete(self.bubbles, i, 0)

                dir_vec = self.com - self.bubbles[i, :2]
                dir_vec = dir_vec / np.sqrt(dir_vec.dot(dir_vec))

                new_point = self.bubbles[i, :2] + dir_vec * self.step_dist
                new_bubble = np.append(new_point, self.bubbles[i, 2:4])

                if not self.check_collisions(new_bubble, rest_bub):
                    self.bubbles[i, :] = new_bubble
                    self.com = self.center_of_mass()
                    moves += 1
                else:
                    for colliding in self.collides_with(new_bubble, rest_bub):
                        dir_vec = rest_bub[colliding, :2] - self.bubbles[i, :2]
                        dir_vec = dir_vec / np.sqrt(dir_vec.dot(dir_vec))
                        orth = np.array([dir_vec[1], -dir_vec[0]])
                        new_point1 = (self.bubbles[i, :2] + orth *
                                      self.step_dist)
                        new_point2 = (self.bubbles[i, :2] - orth *
                                      self.step_dist)
                        dist1 = self.center_distance(
                            self.com, np.array([new_point1]))
                        dist2 = self.center_distance(
                            self.com, np.array([new_point2]))
                        new_point = new_point1 if dist1 < dist2 else new_point2
                        new_bubble = np.append(new_point, self.bubbles[i, 2:4])
                        if not self.check_collisions(new_bubble, rest_bub):
                            self.bubbles[i, :] = new_bubble
                            self.com = self.center_of_mass()

            if moves / len(self.bubbles) < 0.1:
                self.step_dist = self.step_dist / 2

    def plot(self, ax, labels, alpha, edge_alpha = 1):
        for i in range(len(self.bubbles)):
            circ = plt.Circle(
                self.bubbles[i, :2], self.bubbles[i, 2], 
                color=self.colors[i], alpha= alpha)
            ax.add_patch(circ)
            ax.text(*self.bubbles[i, :2], labels[i],
                    horizontalalignment='center', verticalalignment='center')
            

class GenerateChart:
    def __init__(self, data, bubble_spacing=0, n_bins=5):
        unity_ids = list(data.keys())
        share_values = list(data.values())

        simulation = BubbleChart(data, bubble_spacing=bubble_spacing, n_bins=n_bins)

        simulation.collapse()

        fig, ax = plt.subplots(subplot_kw=dict(aspect="equal"))
        alpha_value = 1
        simulation.plot(ax, unity_ids, alpha=alpha_value)
        ax.axis("off")
        ax.relim()
        ax.autoscale_view()
        ax.set_title('p2p share', color = "white")

        fig = plt.gcf()
        img = fig2img(fig)
        st.title('Current GPU contributors')
        st.image(img, caption='Bubble chart of gpu pool of each Users')

def fig2img(fig):
    """Convert a Matplotlib figure to a PIL Image and return it"""
    import io
    buf = io.BytesIO()
    fig.savefig(buf)
    buf.seek(0)
    img = Image.open(buf)
    return img

if len(sample_data['gpus'])> 1:
    chart_generator = GenerateChart(sample_data.get("gpus", {}), bubble_spacing=0.1, n_bins=5)


#st.title('Inference call')
#timestamp_list_date_str = []
#for d in timestamp_list_date:
#    timestamp_list_date_str.append(strftime('%Y-%m-%d', localtime( d )))
#call_data = pd.DataFrame(timestamp_inference_data, columns = ["inference call"], index = timestamp_list_date_str)
#st.bar_chart(call_data)



st.title('Previous Blocks')
st.json(sample_data['chain'])