summaryrefslogtreecommitdiff
path: root/python-bittensor.spec
blob: bd3a8451fe3200cdb99dce59e7fa07304207fedc (plain)
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
%global _empty_manifest_terminate_build 0
Name:		python-bittensor
Version:	4.0.1
Release:	1
Summary:	bittensor
License:	MIT
URL:		https://github.com/opentensor/bittensor
Source0:	https://mirrors.nju.edu.cn/pypi/web/packages/b2/b7/edd639c895875c16cc864ebc383a7c660fc849cb7bb0551c89b252ac1398/bittensor-4.0.1.tar.gz
BuildArch:	noarch

Requires:	python3-ansible-vault
Requires:	python3-argparse
Requires:	python3-backoff
Requires:	python3-base58
Requires:	python3-certifi
Requires:	python3-cryptography
Requires:	python3-fuzzywuzzy
Requires:	python3-google-api-python-client
Requires:	python3-grpcio-tools
Requires:	python3-grpcio
Requires:	python3-hypothesis
Requires:	python3-idna
Requires:	python3-jinja2
Requires:	python3-jsonschema[format-nongpl]
Requires:	python3-loguru
Requires:	python3-markupsafe
Requires:	python3-miniupnpc
Requires:	python3-msgpack-numpy
Requires:	python3-msgpack
Requires:	python3-munch
Requires:	python3-nest-asyncio
Requires:	python3-netaddr
Requires:	python3-numpy
Requires:	python3-pandas
Requires:	python3-password-strength
Requires:	python3-prometheus-client
Requires:	python3-protobuf
Requires:	python3-psutil
Requires:	python3-py-bip39-bindings
Requires:	python3-py-ed25519-bindings
Requires:	python3-py-sr25519-bindings
Requires:	python3-pycryptodome
Requires:	python3-levenshtein
Requires:	python3-pyyaml
Requires:	python3-qqdm
Requires:	python3-requests
Requires:	python3-retry
Requires:	python3-rich
Requires:	python3-scalecodec
Requires:	python3-sentencepiece
Requires:	python3-substrate-interface
Requires:	python3-termcolor
Requires:	python3-torch
Requires:	python3-tqdm
Requires:	python3-transformers
Requires:	python3-wandb
Requires:	python3-wheel
Requires:	python3-coveralls
Requires:	python3-ddt
Requires:	python3-pytest-cov
Requires:	python3-pytest-rerunfailures
Requires:	python3-pytest-split
Requires:	python3-pytest-xdist
Requires:	python3-pytest

%description
### Internet-scale Neural Networks <!-- omit in toc -->
[Discord](https://discord.gg/bittensor) • [Docs](https://docs.bittensor.com/) • [Network](https://www.bittensor.com/network) • [Research](https://drive.google.com/file/d/1VnsobL6lIAAqcA1_Tbm8AYIQscfJV4KU) • [Code](https://github.com/opentensor/BitTensor)
</div>
This repository contains Bittensor's Python API, which can be used for the following purposes:
1. Querying the Bittensor network as a [client](https://github.com/opentensor/bittensor#31-client).
2. Running and building Bittensor miners and validators for [mining TAO](https://github.com/opentensor/bittensor#43-running-a-template-miner).
3. Pulling network [state information](https://github.com/opentensor/bittensor#3-using-bittensor).
4. Managing [TAO wallets](https://github.com/opentensor/bittensor#41-cli), balances, transfers, etc.
Bittensor is a mining network, similar to Bitcoin, that includes built-in incentives designed to encourage miners to provide value by hosting trained or training machine learning models. These models can be queried by clients seeking inference over inputs, such as token-based text generations or numerical embeddings from a large foundation model like GPT-NeoX-20B.
Token-based incentives are designed to drive the network's growth and distribute the value generated by the network directly to the individuals producing that value, without intermediaries. The network is open to all participants, and no individual or group has full control over what is learned, who can profit from it, or who can access it.
To learn more about Bittensor, please read our [paper](https://drive.google.com/file/d/1VnsobL6lIAAqcA1_Tbm8AYIQscfJV4KU/view).
- [1. Documentation](#1-documentation)
- [2. Install](#2-install)
- [3. Using Bittensor](#3-using-bittensor)
  - [3.1. Client](#31-client)
  - [3.2. Server](#32-server)
  - [3.3. Validator](#33-validator)
- [4. Features](#4-features)
  - [4.1. Using the CLI](#41-cli)
  - [4.2. Selecting the network to join](#42-selecting-the-network-to-join)
  - [4.3. Running a template miner](#43-running-a-template-miner)
  - [4.4. Running a template server](#44-running-a-template-server)
  - [4.5. Syncing with the chain/ Finding the ranks/stake/uids of other nodes](#46-syncing-with-the-chain-finding-the-ranksstakeuids-of-other-nodes)
  - [4.6. Finding and creating the endpoints for other nodes in the network](#47-finding-and-creating-the-endpoints-for-other-nodes-in-the-network)
  - [4.7. Querying others in the network](#48-querying-others-in-the-network)
- [5. Release](#5-release)
- [6. License](#6-license)
- [7. Acknowledgments](#7-acknowledgments)
## 1. Documentation
https://docs.bittensor.com/
## 2. Install
Three ways to install Bittensor
1. Through the installer:
```
$ /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/opentensor/bittensor/master/scripts/install.sh)"
```
2. With pip:
```bash
$ pip3 install bittensor
```
3. From source:
```
$ git clone https://github.com/opentensor/bittensor.git
$ python3 -m pip install -e bittensor/
```
## 3. Using Bittensor
The following examples showcase how to use the Bittensor API for 3 separate purposes.
### 3.1. Client 
Querying the network for generations.
```python
import bittensor
wallet = bittensor.wallet().create_if_non_existent()
graph = bittensor.metagraph().sync()
print ( bittensor.dendrite( wallet = wallet ).generate
        ( 
            endpoints = graph.endpoints[graph.incentive.sort()[1][-1]],  // The highest ranked peer.
            prompt = "The quick brown fox jumped over the lazy dog", 
            num_to_generate = 20
        )
)
```
Querying the network for representations.
```python
import bittensor
wallet = bittensor.wallet().create_if_non_existent()
graph = bittensor.metagraph().sync()
print ( bittensor.dendrite( wallet = wallet ).text_last_hidden_state
        (
            endpoints = graph.endpoints[graph.incentive.sort()[1][-1]],  // The highest ranked peer.
            inputs = "The quick brown fox jumped over the lazy dog"
        )
)
// Apply model. 
loss.backward() // Accumulate gradients on endpoints.
```
### 3.2. Server
Serving a custom model.
```python
import bittensor
import torch
from transformers import GPT2Model, GPT2Config
model = GPT2Model( GPT2Config(vocab_size = bittensor.__vocab_size__, n_embd = bittensor.__network_dim__ , n_head = 8))
optimizer = torch.optim.SGD( [ {"params": model.parameters()} ], lr = 0.01 )
def forward_text( pubkey, inputs_x ):
    return model( inputs_x )
def backward_text( pubkey, inputs_x, grads_dy ):
    with torch.enable_grad():
        outputs_y = model( inputs_x.to(device) ).last_hidden_state
        torch.autograd.backward (
            tensors = [ outputs_y.to(device) ],
            grad_tensors = [ grads_dy.to(device) ]
        )
        optimizer.step()
        optimizer.zero_grad() 
wallet = bittensor.wallet().create().register()
axon = bittensor.axon (
    wallet = wallet,
    forward_text = forward_text,
    backward_text = backward_text
).start().serve()
```
### 3.3. Validator 
Validating models by setting weights.
```python
import bittensor
import torch
graph = bittensor.metagraph().sync()
dataset = bittensor.dataset()
chain_weights = torch.ones( [graph.n.item()], dtype = torch.float32 )
for batch in dataset.dataloader( 10 ):
    // Train chain_weights.
bittensor.subtensor().set_weights (
    weights = chain_weights,
    uids = graph.uids,
    wait_for_inclusion = True,
    wallet = bittensor.wallet(),
)
```
## 4. Features
### 4.1. CLI
Creating a new wallet.
```bash
$ btcli new_coldkey
$ btcli new_hotkey
```
Listing your wallets
```bash
$ btcli list
```
Registering a wallet
```bash
$ btcli register
```
Running a miner
```bash
$ btcli run
```
Checking balances
```bash
$ btcli overview
```
Checking the incentive mechanism.
```bash
$ btcli metagraph
```
Transfering funds
```bash
$ btcli transfer
```
Staking/Unstaking from a hotkey
```bash
$ btcli stake
$ btcli unstake
```
### 4.2. Selecting the network to join 
There are two open Bittensor networks: staging (Nobunaga) and main (Nakamoto, Local).
- Nobunaga (staging)
- Nakamoto (main)
- Local (localhost, mirrors nakamoto)
```bash
$ export NETWORK=local 
$ python (..) --subtensor.network $NETWORK
or
>> btcli run --subtensor.network $NETWORK
```
### 4.3. Running a template miner
The following command will run Bittensor's template miner
```bash
$ cd bittensor
$ python ./bittensor/_neuron/text/template_miner/main.py
```
or 
```python3
>> import bittensor
>> bittensor.neurons.text.template_miner.neuron().run()
```
OR with customized settings
```bash
$ cd bittensor
$ python3 ./bittensor/_neuron/text/template_miner/main.py --wallet.name <WALLET NAME> --wallet.hotkey <HOTKEY NAME>
```
For the full list of settings, please run
```bash
$ python3 ~/.bittensor/bittensor/bittensor/_neuron/neurons/text/template_miner/main.py --help
```
### 4.4. Running a template server
The template server follows a similar structure as the template miner. 
```bash
$ cd bittensor
$ python3 ./bittensor/_neuron/text/core_server/main.py --wallet.name <WALLET NAME> --wallet.hotkey <HOTKEY NAME>
```
or 
```python3
>> import bittensor
>> bittensor.neurons.text.core_server.neuron().run()
```
For the full list of settings, please run
```bash
$ cd bittensor
$ python3 ./bittensor/_neuron/text/core_server/main.py --help
```
###  4.5. Serving an endpoint on the network
Endpoints are served to the bittensor network through the axon. The axon is instantiated via a wallet which holds an account on the Bittensor network.
```python
import bittensor
wallet = bittensor.wallet().create().register()
axon = bittensor.axon (
    wallet = wallet,
    forward_text = forward_text,
    backward_text = backward_text
).start().serve()
```
### 4.6. Syncing with the chain/ Finding the ranks/stake/uids of other nodes
Information from the chain is collected/formated by the metagraph.
```bash
btcli metagraph
```
and
```python
import bittensor
meta = bittensor.metagraph()
meta.sync()
# --- uid ---
print(meta.uids)
# --- hotkeys ---
print(meta.hotkeys)
# --- ranks ---
print(meta.R)
# --- stake ---
print(meta.S)
```
### 4.7. Finding and creating the endpoints for other nodes in the network
```python
import bittensor
meta = bittensor.metagraph()
meta.sync()
### Address for the node uid 0
endpoint_as_tensor = meta.endpoints[0]
endpoint_as_object = meta.endpoint_objs[0]
```
### 4.8. Querying others in the network
```python
import bittensor
meta = bittensor.metagraph()
meta.sync()
### Address for the node uid 0
endpoint_0 = meta.endpoints[0]
### Creating the wallet, and dendrite
wallet = bittensor.wallet().create().register()
den = bittensor.dendrite(wallet = wallet)
representations, _, _ = den.forward_text (
    endpoints = endpoint_0,
    inputs = "Hello World"
)
```
## 5. Release
The release manager should follow the instructions of the [RELEASE_GUIDELINES.md](./RELEASE_GUIDELINES.md) document.
## 6. License
The MIT License (MIT)
Copyright © 2021 Yuma Rao
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
## 7. Acknowledgments
**learning-at-home/hivemind**

%package -n python3-bittensor
Summary:	bittensor
Provides:	python-bittensor
BuildRequires:	python3-devel
BuildRequires:	python3-setuptools
BuildRequires:	python3-pip
%description -n python3-bittensor
### Internet-scale Neural Networks <!-- omit in toc -->
[Discord](https://discord.gg/bittensor) • [Docs](https://docs.bittensor.com/) • [Network](https://www.bittensor.com/network) • [Research](https://drive.google.com/file/d/1VnsobL6lIAAqcA1_Tbm8AYIQscfJV4KU) • [Code](https://github.com/opentensor/BitTensor)
</div>
This repository contains Bittensor's Python API, which can be used for the following purposes:
1. Querying the Bittensor network as a [client](https://github.com/opentensor/bittensor#31-client).
2. Running and building Bittensor miners and validators for [mining TAO](https://github.com/opentensor/bittensor#43-running-a-template-miner).
3. Pulling network [state information](https://github.com/opentensor/bittensor#3-using-bittensor).
4. Managing [TAO wallets](https://github.com/opentensor/bittensor#41-cli), balances, transfers, etc.
Bittensor is a mining network, similar to Bitcoin, that includes built-in incentives designed to encourage miners to provide value by hosting trained or training machine learning models. These models can be queried by clients seeking inference over inputs, such as token-based text generations or numerical embeddings from a large foundation model like GPT-NeoX-20B.
Token-based incentives are designed to drive the network's growth and distribute the value generated by the network directly to the individuals producing that value, without intermediaries. The network is open to all participants, and no individual or group has full control over what is learned, who can profit from it, or who can access it.
To learn more about Bittensor, please read our [paper](https://drive.google.com/file/d/1VnsobL6lIAAqcA1_Tbm8AYIQscfJV4KU/view).
- [1. Documentation](#1-documentation)
- [2. Install](#2-install)
- [3. Using Bittensor](#3-using-bittensor)
  - [3.1. Client](#31-client)
  - [3.2. Server](#32-server)
  - [3.3. Validator](#33-validator)
- [4. Features](#4-features)
  - [4.1. Using the CLI](#41-cli)
  - [4.2. Selecting the network to join](#42-selecting-the-network-to-join)
  - [4.3. Running a template miner](#43-running-a-template-miner)
  - [4.4. Running a template server](#44-running-a-template-server)
  - [4.5. Syncing with the chain/ Finding the ranks/stake/uids of other nodes](#46-syncing-with-the-chain-finding-the-ranksstakeuids-of-other-nodes)
  - [4.6. Finding and creating the endpoints for other nodes in the network](#47-finding-and-creating-the-endpoints-for-other-nodes-in-the-network)
  - [4.7. Querying others in the network](#48-querying-others-in-the-network)
- [5. Release](#5-release)
- [6. License](#6-license)
- [7. Acknowledgments](#7-acknowledgments)
## 1. Documentation
https://docs.bittensor.com/
## 2. Install
Three ways to install Bittensor
1. Through the installer:
```
$ /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/opentensor/bittensor/master/scripts/install.sh)"
```
2. With pip:
```bash
$ pip3 install bittensor
```
3. From source:
```
$ git clone https://github.com/opentensor/bittensor.git
$ python3 -m pip install -e bittensor/
```
## 3. Using Bittensor
The following examples showcase how to use the Bittensor API for 3 separate purposes.
### 3.1. Client 
Querying the network for generations.
```python
import bittensor
wallet = bittensor.wallet().create_if_non_existent()
graph = bittensor.metagraph().sync()
print ( bittensor.dendrite( wallet = wallet ).generate
        ( 
            endpoints = graph.endpoints[graph.incentive.sort()[1][-1]],  // The highest ranked peer.
            prompt = "The quick brown fox jumped over the lazy dog", 
            num_to_generate = 20
        )
)
```
Querying the network for representations.
```python
import bittensor
wallet = bittensor.wallet().create_if_non_existent()
graph = bittensor.metagraph().sync()
print ( bittensor.dendrite( wallet = wallet ).text_last_hidden_state
        (
            endpoints = graph.endpoints[graph.incentive.sort()[1][-1]],  // The highest ranked peer.
            inputs = "The quick brown fox jumped over the lazy dog"
        )
)
// Apply model. 
loss.backward() // Accumulate gradients on endpoints.
```
### 3.2. Server
Serving a custom model.
```python
import bittensor
import torch
from transformers import GPT2Model, GPT2Config
model = GPT2Model( GPT2Config(vocab_size = bittensor.__vocab_size__, n_embd = bittensor.__network_dim__ , n_head = 8))
optimizer = torch.optim.SGD( [ {"params": model.parameters()} ], lr = 0.01 )
def forward_text( pubkey, inputs_x ):
    return model( inputs_x )
def backward_text( pubkey, inputs_x, grads_dy ):
    with torch.enable_grad():
        outputs_y = model( inputs_x.to(device) ).last_hidden_state
        torch.autograd.backward (
            tensors = [ outputs_y.to(device) ],
            grad_tensors = [ grads_dy.to(device) ]
        )
        optimizer.step()
        optimizer.zero_grad() 
wallet = bittensor.wallet().create().register()
axon = bittensor.axon (
    wallet = wallet,
    forward_text = forward_text,
    backward_text = backward_text
).start().serve()
```
### 3.3. Validator 
Validating models by setting weights.
```python
import bittensor
import torch
graph = bittensor.metagraph().sync()
dataset = bittensor.dataset()
chain_weights = torch.ones( [graph.n.item()], dtype = torch.float32 )
for batch in dataset.dataloader( 10 ):
    // Train chain_weights.
bittensor.subtensor().set_weights (
    weights = chain_weights,
    uids = graph.uids,
    wait_for_inclusion = True,
    wallet = bittensor.wallet(),
)
```
## 4. Features
### 4.1. CLI
Creating a new wallet.
```bash
$ btcli new_coldkey
$ btcli new_hotkey
```
Listing your wallets
```bash
$ btcli list
```
Registering a wallet
```bash
$ btcli register
```
Running a miner
```bash
$ btcli run
```
Checking balances
```bash
$ btcli overview
```
Checking the incentive mechanism.
```bash
$ btcli metagraph
```
Transfering funds
```bash
$ btcli transfer
```
Staking/Unstaking from a hotkey
```bash
$ btcli stake
$ btcli unstake
```
### 4.2. Selecting the network to join 
There are two open Bittensor networks: staging (Nobunaga) and main (Nakamoto, Local).
- Nobunaga (staging)
- Nakamoto (main)
- Local (localhost, mirrors nakamoto)
```bash
$ export NETWORK=local 
$ python (..) --subtensor.network $NETWORK
or
>> btcli run --subtensor.network $NETWORK
```
### 4.3. Running a template miner
The following command will run Bittensor's template miner
```bash
$ cd bittensor
$ python ./bittensor/_neuron/text/template_miner/main.py
```
or 
```python3
>> import bittensor
>> bittensor.neurons.text.template_miner.neuron().run()
```
OR with customized settings
```bash
$ cd bittensor
$ python3 ./bittensor/_neuron/text/template_miner/main.py --wallet.name <WALLET NAME> --wallet.hotkey <HOTKEY NAME>
```
For the full list of settings, please run
```bash
$ python3 ~/.bittensor/bittensor/bittensor/_neuron/neurons/text/template_miner/main.py --help
```
### 4.4. Running a template server
The template server follows a similar structure as the template miner. 
```bash
$ cd bittensor
$ python3 ./bittensor/_neuron/text/core_server/main.py --wallet.name <WALLET NAME> --wallet.hotkey <HOTKEY NAME>
```
or 
```python3
>> import bittensor
>> bittensor.neurons.text.core_server.neuron().run()
```
For the full list of settings, please run
```bash
$ cd bittensor
$ python3 ./bittensor/_neuron/text/core_server/main.py --help
```
###  4.5. Serving an endpoint on the network
Endpoints are served to the bittensor network through the axon. The axon is instantiated via a wallet which holds an account on the Bittensor network.
```python
import bittensor
wallet = bittensor.wallet().create().register()
axon = bittensor.axon (
    wallet = wallet,
    forward_text = forward_text,
    backward_text = backward_text
).start().serve()
```
### 4.6. Syncing with the chain/ Finding the ranks/stake/uids of other nodes
Information from the chain is collected/formated by the metagraph.
```bash
btcli metagraph
```
and
```python
import bittensor
meta = bittensor.metagraph()
meta.sync()
# --- uid ---
print(meta.uids)
# --- hotkeys ---
print(meta.hotkeys)
# --- ranks ---
print(meta.R)
# --- stake ---
print(meta.S)
```
### 4.7. Finding and creating the endpoints for other nodes in the network
```python
import bittensor
meta = bittensor.metagraph()
meta.sync()
### Address for the node uid 0
endpoint_as_tensor = meta.endpoints[0]
endpoint_as_object = meta.endpoint_objs[0]
```
### 4.8. Querying others in the network
```python
import bittensor
meta = bittensor.metagraph()
meta.sync()
### Address for the node uid 0
endpoint_0 = meta.endpoints[0]
### Creating the wallet, and dendrite
wallet = bittensor.wallet().create().register()
den = bittensor.dendrite(wallet = wallet)
representations, _, _ = den.forward_text (
    endpoints = endpoint_0,
    inputs = "Hello World"
)
```
## 5. Release
The release manager should follow the instructions of the [RELEASE_GUIDELINES.md](./RELEASE_GUIDELINES.md) document.
## 6. License
The MIT License (MIT)
Copyright © 2021 Yuma Rao
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
## 7. Acknowledgments
**learning-at-home/hivemind**

%package help
Summary:	Development documents and examples for bittensor
Provides:	python3-bittensor-doc
%description help
### Internet-scale Neural Networks <!-- omit in toc -->
[Discord](https://discord.gg/bittensor) • [Docs](https://docs.bittensor.com/) • [Network](https://www.bittensor.com/network) • [Research](https://drive.google.com/file/d/1VnsobL6lIAAqcA1_Tbm8AYIQscfJV4KU) • [Code](https://github.com/opentensor/BitTensor)
</div>
This repository contains Bittensor's Python API, which can be used for the following purposes:
1. Querying the Bittensor network as a [client](https://github.com/opentensor/bittensor#31-client).
2. Running and building Bittensor miners and validators for [mining TAO](https://github.com/opentensor/bittensor#43-running-a-template-miner).
3. Pulling network [state information](https://github.com/opentensor/bittensor#3-using-bittensor).
4. Managing [TAO wallets](https://github.com/opentensor/bittensor#41-cli), balances, transfers, etc.
Bittensor is a mining network, similar to Bitcoin, that includes built-in incentives designed to encourage miners to provide value by hosting trained or training machine learning models. These models can be queried by clients seeking inference over inputs, such as token-based text generations or numerical embeddings from a large foundation model like GPT-NeoX-20B.
Token-based incentives are designed to drive the network's growth and distribute the value generated by the network directly to the individuals producing that value, without intermediaries. The network is open to all participants, and no individual or group has full control over what is learned, who can profit from it, or who can access it.
To learn more about Bittensor, please read our [paper](https://drive.google.com/file/d/1VnsobL6lIAAqcA1_Tbm8AYIQscfJV4KU/view).
- [1. Documentation](#1-documentation)
- [2. Install](#2-install)
- [3. Using Bittensor](#3-using-bittensor)
  - [3.1. Client](#31-client)
  - [3.2. Server](#32-server)
  - [3.3. Validator](#33-validator)
- [4. Features](#4-features)
  - [4.1. Using the CLI](#41-cli)
  - [4.2. Selecting the network to join](#42-selecting-the-network-to-join)
  - [4.3. Running a template miner](#43-running-a-template-miner)
  - [4.4. Running a template server](#44-running-a-template-server)
  - [4.5. Syncing with the chain/ Finding the ranks/stake/uids of other nodes](#46-syncing-with-the-chain-finding-the-ranksstakeuids-of-other-nodes)
  - [4.6. Finding and creating the endpoints for other nodes in the network](#47-finding-and-creating-the-endpoints-for-other-nodes-in-the-network)
  - [4.7. Querying others in the network](#48-querying-others-in-the-network)
- [5. Release](#5-release)
- [6. License](#6-license)
- [7. Acknowledgments](#7-acknowledgments)
## 1. Documentation
https://docs.bittensor.com/
## 2. Install
Three ways to install Bittensor
1. Through the installer:
```
$ /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/opentensor/bittensor/master/scripts/install.sh)"
```
2. With pip:
```bash
$ pip3 install bittensor
```
3. From source:
```
$ git clone https://github.com/opentensor/bittensor.git
$ python3 -m pip install -e bittensor/
```
## 3. Using Bittensor
The following examples showcase how to use the Bittensor API for 3 separate purposes.
### 3.1. Client 
Querying the network for generations.
```python
import bittensor
wallet = bittensor.wallet().create_if_non_existent()
graph = bittensor.metagraph().sync()
print ( bittensor.dendrite( wallet = wallet ).generate
        ( 
            endpoints = graph.endpoints[graph.incentive.sort()[1][-1]],  // The highest ranked peer.
            prompt = "The quick brown fox jumped over the lazy dog", 
            num_to_generate = 20
        )
)
```
Querying the network for representations.
```python
import bittensor
wallet = bittensor.wallet().create_if_non_existent()
graph = bittensor.metagraph().sync()
print ( bittensor.dendrite( wallet = wallet ).text_last_hidden_state
        (
            endpoints = graph.endpoints[graph.incentive.sort()[1][-1]],  // The highest ranked peer.
            inputs = "The quick brown fox jumped over the lazy dog"
        )
)
// Apply model. 
loss.backward() // Accumulate gradients on endpoints.
```
### 3.2. Server
Serving a custom model.
```python
import bittensor
import torch
from transformers import GPT2Model, GPT2Config
model = GPT2Model( GPT2Config(vocab_size = bittensor.__vocab_size__, n_embd = bittensor.__network_dim__ , n_head = 8))
optimizer = torch.optim.SGD( [ {"params": model.parameters()} ], lr = 0.01 )
def forward_text( pubkey, inputs_x ):
    return model( inputs_x )
def backward_text( pubkey, inputs_x, grads_dy ):
    with torch.enable_grad():
        outputs_y = model( inputs_x.to(device) ).last_hidden_state
        torch.autograd.backward (
            tensors = [ outputs_y.to(device) ],
            grad_tensors = [ grads_dy.to(device) ]
        )
        optimizer.step()
        optimizer.zero_grad() 
wallet = bittensor.wallet().create().register()
axon = bittensor.axon (
    wallet = wallet,
    forward_text = forward_text,
    backward_text = backward_text
).start().serve()
```
### 3.3. Validator 
Validating models by setting weights.
```python
import bittensor
import torch
graph = bittensor.metagraph().sync()
dataset = bittensor.dataset()
chain_weights = torch.ones( [graph.n.item()], dtype = torch.float32 )
for batch in dataset.dataloader( 10 ):
    // Train chain_weights.
bittensor.subtensor().set_weights (
    weights = chain_weights,
    uids = graph.uids,
    wait_for_inclusion = True,
    wallet = bittensor.wallet(),
)
```
## 4. Features
### 4.1. CLI
Creating a new wallet.
```bash
$ btcli new_coldkey
$ btcli new_hotkey
```
Listing your wallets
```bash
$ btcli list
```
Registering a wallet
```bash
$ btcli register
```
Running a miner
```bash
$ btcli run
```
Checking balances
```bash
$ btcli overview
```
Checking the incentive mechanism.
```bash
$ btcli metagraph
```
Transfering funds
```bash
$ btcli transfer
```
Staking/Unstaking from a hotkey
```bash
$ btcli stake
$ btcli unstake
```
### 4.2. Selecting the network to join 
There are two open Bittensor networks: staging (Nobunaga) and main (Nakamoto, Local).
- Nobunaga (staging)
- Nakamoto (main)
- Local (localhost, mirrors nakamoto)
```bash
$ export NETWORK=local 
$ python (..) --subtensor.network $NETWORK
or
>> btcli run --subtensor.network $NETWORK
```
### 4.3. Running a template miner
The following command will run Bittensor's template miner
```bash
$ cd bittensor
$ python ./bittensor/_neuron/text/template_miner/main.py
```
or 
```python3
>> import bittensor
>> bittensor.neurons.text.template_miner.neuron().run()
```
OR with customized settings
```bash
$ cd bittensor
$ python3 ./bittensor/_neuron/text/template_miner/main.py --wallet.name <WALLET NAME> --wallet.hotkey <HOTKEY NAME>
```
For the full list of settings, please run
```bash
$ python3 ~/.bittensor/bittensor/bittensor/_neuron/neurons/text/template_miner/main.py --help
```
### 4.4. Running a template server
The template server follows a similar structure as the template miner. 
```bash
$ cd bittensor
$ python3 ./bittensor/_neuron/text/core_server/main.py --wallet.name <WALLET NAME> --wallet.hotkey <HOTKEY NAME>
```
or 
```python3
>> import bittensor
>> bittensor.neurons.text.core_server.neuron().run()
```
For the full list of settings, please run
```bash
$ cd bittensor
$ python3 ./bittensor/_neuron/text/core_server/main.py --help
```
###  4.5. Serving an endpoint on the network
Endpoints are served to the bittensor network through the axon. The axon is instantiated via a wallet which holds an account on the Bittensor network.
```python
import bittensor
wallet = bittensor.wallet().create().register()
axon = bittensor.axon (
    wallet = wallet,
    forward_text = forward_text,
    backward_text = backward_text
).start().serve()
```
### 4.6. Syncing with the chain/ Finding the ranks/stake/uids of other nodes
Information from the chain is collected/formated by the metagraph.
```bash
btcli metagraph
```
and
```python
import bittensor
meta = bittensor.metagraph()
meta.sync()
# --- uid ---
print(meta.uids)
# --- hotkeys ---
print(meta.hotkeys)
# --- ranks ---
print(meta.R)
# --- stake ---
print(meta.S)
```
### 4.7. Finding and creating the endpoints for other nodes in the network
```python
import bittensor
meta = bittensor.metagraph()
meta.sync()
### Address for the node uid 0
endpoint_as_tensor = meta.endpoints[0]
endpoint_as_object = meta.endpoint_objs[0]
```
### 4.8. Querying others in the network
```python
import bittensor
meta = bittensor.metagraph()
meta.sync()
### Address for the node uid 0
endpoint_0 = meta.endpoints[0]
### Creating the wallet, and dendrite
wallet = bittensor.wallet().create().register()
den = bittensor.dendrite(wallet = wallet)
representations, _, _ = den.forward_text (
    endpoints = endpoint_0,
    inputs = "Hello World"
)
```
## 5. Release
The release manager should follow the instructions of the [RELEASE_GUIDELINES.md](./RELEASE_GUIDELINES.md) document.
## 6. License
The MIT License (MIT)
Copyright © 2021 Yuma Rao
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
## 7. Acknowledgments
**learning-at-home/hivemind**

%prep
%autosetup -n bittensor-4.0.1

%build
%py3_build

%install
%py3_install
install -d -m755 %{buildroot}/%{_pkgdocdir}
if [ -d doc ]; then cp -arf doc %{buildroot}/%{_pkgdocdir}; fi
if [ -d docs ]; then cp -arf docs %{buildroot}/%{_pkgdocdir}; fi
if [ -d example ]; then cp -arf example %{buildroot}/%{_pkgdocdir}; fi
if [ -d examples ]; then cp -arf examples %{buildroot}/%{_pkgdocdir}; fi
pushd %{buildroot}
if [ -d usr/lib ]; then
	find usr/lib -type f -printf "/%h/%f\n" >> filelist.lst
fi
if [ -d usr/lib64 ]; then
	find usr/lib64 -type f -printf "/%h/%f\n" >> filelist.lst
fi
if [ -d usr/bin ]; then
	find usr/bin -type f -printf "/%h/%f\n" >> filelist.lst
fi
if [ -d usr/sbin ]; then
	find usr/sbin -type f -printf "/%h/%f\n" >> filelist.lst
fi
touch doclist.lst
if [ -d usr/share/man ]; then
	find usr/share/man -type f -printf "/%h/%f.gz\n" >> doclist.lst
fi
popd
mv %{buildroot}/filelist.lst .
mv %{buildroot}/doclist.lst .

%files -n python3-bittensor -f filelist.lst
%dir %{python3_sitelib}/*

%files help -f doclist.lst
%{_docdir}/*

%changelog
* Fri May 05 2023 Python_Bot <Python_Bot@openeuler.org> - 4.0.1-1
- Package Spec generated