Skip to content

Commit f955ffb

Browse files
committed
Update to v0.0.8
1 parent 82b3571 commit f955ffb

File tree

9 files changed

+355
-2
lines changed

9 files changed

+355
-2
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,11 @@ query dates easier `5 days ago`.
6868

6969
### LazyOps API Usage
7070

71+
WIP often means documentation itself is WIP, while module is fully functional.
72+
Feel free to dig into the source code or try it out in examples.
73+
74+
Unfortunately documentation doesn't write itself... yet. And well. Lazyiness.
75+
7176
- [LazyOps Core](#lazyops-core)
7277
- [LazyClassses](#lazyclasses)
7378
- [LazyAPI](#lazyapi)

changelogs.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
v0.0.8 - July 28, 2021
2+
3+
- Add API for LazyHFModel

lazyops/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,15 @@
88
lazyops_root = os.path.abspath(os.path.dirname(__file__))
99
from .lazyclasses import lazyclass
1010

11+
1112
from .envs import LazyEnv, get_logger, lazywatcher, lazywatch
1213
from .models import LazyData, LazyTime, LazyDate, LazyFormatter, LazyTimer, LazyObject
1314
from .common import lazylibs, lazy_init, run_cmd, clone_repo, File
1415
from .utils import find_binary_in_path, timed_cache, latest_tf_ckpt, require_module
1516
from .utils import build_chunks, retryable, list_to_dict
1617
from .mp import lazy_parallelize, lazyproc, lazymultiproc, LazyProcs, LazyProc
1718
from .apis import LazyAPI, LazyAPIConfig
19+
from .lazyio import LazyHFModel
1820

1921
lazyenv = LazyEnv
2022
lazyitem = LazyData
@@ -39,6 +41,7 @@
3941
'LazyFormatter',
4042
'LazyTimer',
4143
'LazyObject',
44+
'LazyHFModel',
4245
'lazylibs',
4346
'lazy_init',
4447
'run_cmd',

lazyops/experimental/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# LazyOps Experimental
2+
3+
Experimental Modules built on top of LazyOps.

lazyops/experimental/gptj.py

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
from lazyops import get_logger, timer
2+
from lazyops.apis import LazyAPI
3+
4+
## Eleuther.ai API
5+
## Source: https://github.com/kingoflolz/mesh-transformer-jax/#gpt-j-6b
6+
7+
eai_config = {
8+
'url': 'https://api.eleuther.ai',
9+
'token': None,
10+
'default_params': {'top_p': 0.9, 'temp': 1},
11+
'params_key': None,
12+
'data_key': 'context',
13+
'default_fetch': 'complete',
14+
'default_async': 'complete_async',
15+
'route_config': {
16+
'complete': {
17+
'method': 'POST',
18+
'path': '/complete',
19+
'params': {'top_p': 0.9, 'temp': 1},
20+
'params_key': None,
21+
'data_key': 'context',
22+
'is_async': False,
23+
'prefix_payload': False,
24+
'decode_json': True,
25+
},
26+
'complete_async': {
27+
'method': 'POST',
28+
'path': '/complete',
29+
'params': {'top_p': 0.9, 'temp': 1},
30+
'params_key': None,
31+
'data_key': 'context',
32+
'is_async': True,
33+
'prefix_payload': False,
34+
'decode_json': True,
35+
},
36+
}
37+
}
38+
39+
# TextSynth API
40+
# Source: https://bellard.org/textsynth/
41+
42+
43+
textsynth_config = {
44+
'url': 'https://bellard.org/textsynth/api/v1/engines/gptj_6B',
45+
'token': None,
46+
'default_params': {'top_p': 0.9, 'temperature': 1, 'seed': 0, 'stream': False, 'top_k': 40},
47+
'params_key': None,
48+
'data_key': 'prompt',
49+
'default_fetch': 'complete',
50+
'default_async': 'complete_async',
51+
'route_config': {
52+
'complete': {
53+
'method': 'POST',
54+
'path': '/completions',
55+
'params': {'top_p': 0.9, 'temperature': 1, 'seed': 0, 'stream': False, 'top_k': 40},
56+
'params_key': None,
57+
'data_key': 'prompt',
58+
'is_async': False,
59+
'prefix_payload': False,
60+
'decode_json': True,
61+
},
62+
'complete_async': {
63+
'method': 'POST',
64+
'path': '/completions',
65+
'params': {'top_p': 0.9, 'temperature': 1, 'seed': 0, 'stream': False, 'top_k': 40},
66+
'params_key': None,
67+
'data_key': 'prompt',
68+
'is_async': True,
69+
'prefix_payload': False,
70+
'decode_json': True,
71+
},
72+
}
73+
}
74+
75+
logger = get_logger('LazyEXP', 'GPTJ')
76+
77+
class GPTJAPI:
78+
def __init__(self, fallback='eai'):
79+
self.eai = LazyAPI.build(eai_config)
80+
self._eai_config = {'top_p': 0.9, 'temp': 1}
81+
self.ts = LazyAPI.build(textsynth_config)
82+
self._ts_config = {'top_p': 0.9, 'temperature': 1, 'seed': 0, 'stream': False, 'top_k': 40}
83+
self._fallback = fallback
84+
85+
@property
86+
def primary(self):
87+
if self._fallback == 'eai':
88+
return self.ts
89+
return self.eai
90+
91+
@property
92+
def fallback(self):
93+
if self._fallback == 'eai':
94+
return self.eai
95+
return self.ts
96+
97+
def get_base_params(self, is_fallback=False):
98+
if is_fallback:
99+
if self._fallback == 'eai':
100+
return self._eai_config.copy()
101+
return self._ts_config.copy()
102+
if self._fallback == 'eai':
103+
return self._ts_config.copy()
104+
return self._eai_config.copy()
105+
106+
def build_params(self, text, is_fallback=False, **config):
107+
p = self.get_base_params(is_fallback)
108+
if config:
109+
for k,v in config.items():
110+
if k in p:
111+
config[k] = v
112+
return p
113+
114+
def predict(self, text, **config):
115+
p = self.build_params(text, **config)
116+
try:
117+
res = self.primary.complete(data=text, **p)
118+
#print(res)
119+
return res.get('completion', res.get('text','')), res
120+
121+
except Exception as e:
122+
logger.error(f'Error Calling API: {str(e)}')
123+
p = self.build_params(text, is_fallback=True, **config)
124+
try:
125+
res = self.fallback.complete(data=text, **p)
126+
return res.get('text', res.get('completion','')), res
127+
except Exception as e:
128+
logger.error(f'Error Calling API: {str(e)}')
129+
return None, None
130+
131+
async def as_predict(self, text, **config):
132+
p = self.build_params(text, **config)
133+
try:
134+
res = await self.primary.complete_async(**p)
135+
return res.get('completion', res.get('text','')), res
136+
137+
except Exception as e:
138+
logger.error(f'Error Calling API: {str(e)}')
139+
p = self.build_params(text, is_fallback=True, **config)
140+
try:
141+
res = await self.fallback.complete_async(**p)
142+
return res.get('text', res.get('completion','')), res
143+
except Exception as e:
144+
logger.error(f'Error Calling API: {str(e)}')
145+
return None, None
146+
147+
148+
149+
150+
if __name__ == '__main__':
151+
test_texts = [
152+
'In a shocking finding, scientist discovered a herd of unicorns living in a remote, previously unexplored valley, in the Andes Mountains. Even more surprising to the researchers was the fact that the unicorns spoke perfect English.',
153+
'Game of Thrones is',
154+
'For todays homework assignment, please describe the reasons for the US Civil War.'
155+
]
156+
api = GPTJAPI()
157+
for txt in test_texts:
158+
res, _ = api.predict(text=txt)
159+
logger.info(res)
160+
161+
162+
163+
164+
165+
166+

lazyops/lazyio/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
LazyIOJsonLines,
88
LazyIOPickle,
99
LazyIOType,
10+
LazyHFModel,
1011
)
1112

1213

@@ -32,6 +33,7 @@
3233
'LazyIOJsonLines',
3334
'LazyIOPickle',
3435
'LazyIOType',
36+
'LazyHFModel',
3537
'lazypickler',
3638
'lazypkler',
3739
'lazyjson',

lazyops/lazyio/_base.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33

44
import dill as pickler
55
import simdjson as json
6+
import os
67

78
from enum import Enum
89
from abc import ABCMeta
9-
from typing import Union, List, Any, TypeVar
10+
from typing import Union, List, Any, TypeVar, Optional, Dict
1011
from fileio import File, gfile
1112

1213
logger = get_logger(name='LazyIO')

0 commit comments

Comments
 (0)