GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — JAX_llama/jax_example.py at main · openrightorg/JAX_llama

Expand file tree

/

jax_example.py

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
executable file
·
40 lines (33 loc) · 1.78 KB
/

jax_example.py

Copy path

File metadata and controls

executable file
·
40 lines (33 loc) · 1.78 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
import jax
import jax.numpy as jnp
from flax.core.frozen_dict import unfreeze, freeze
from jax_llama import convert_llama_weights, LLaMATokenizer, LLaMA, FlaxLLaMAForCausalLM, get_llama_param_partition_spec
from jax.sharding import Mesh
from jax.sharding import NamedSharding
from jax.experimental import mesh_utils
import fire
def load(ckpt_dir: str, tokenizer_path: str, **model_kwargs) -> LLaMA:
# setup jax mesh
devices = mesh_utils.create_device_mesh((1, len(jax.devices())))
mesh = Mesh(devices, axis_names=('dp', 'mp'))
print(f"Mesh: {mesh}")
# load jax model
tokenizer = LLaMATokenizer(tokenizer_path)
jax_params, jax_config = convert_llama_weights(ckpt_dir, tokenizer)
with jax.default_device(jax.devices('cpu')[0]):
jax_params = freeze(jax.tree_map(lambda x: jnp.asarray(x), jax_params))
# shard params
param_spec = freeze(get_llama_param_partition_spec(unfreeze(jax_params), fsdp=False))
jax_params = jax.tree_util.tree_map(lambda param, spec: jax.device_put(param, NamedSharding(mesh, spec)), jax_params, param_spec)
# build model
jax_model = FlaxLLaMAForCausalLM(jax_config, _do_init=False, **model_kwargs)
return LLaMA(jax_params, jax_model, tokenizer, mesh=mesh)
def main(ckpt_dir: str, tokenizer_path: str, max_gen_len: int=256, temperature: float = 0.8, top_p: float = 0.95):
generator = load(ckpt_dir, tokenizer_path)
prompts = ["The capital of Germany is the city of", "Here is my sonnet in the style of Shakespeare about an artificial intelligence:"]
results = generator.generate_from_str(prompts, max_gen_len=max_gen_len, temperature=temperature, top_p=top_p)
for result in results:
print(result)
print("\n==================================\n")
if __name__ == "__main__":
fire.Fire(main)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.